mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
cleaned up element addition/removal code
This commit is contained in:
parent
e21729de67
commit
e5cfebef3b
3 changed files with 42 additions and 31 deletions
|
@ -31,7 +31,7 @@ namespace MLEM.Ui.Elements {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public UiSystem System {
|
public UiSystem System {
|
||||||
get => this.system;
|
get => this.system;
|
||||||
internal set {
|
private set {
|
||||||
this.system = value;
|
this.system = value;
|
||||||
this.Controls = value?.Controls;
|
this.Controls = value?.Controls;
|
||||||
this.Style = this.Style.OrStyle(value?.Style);
|
this.Style = this.Style.OrStyle(value?.Style);
|
||||||
|
@ -50,7 +50,7 @@ namespace MLEM.Ui.Elements {
|
||||||
/// This element's <see cref="RootElement"/>.
|
/// This element's <see cref="RootElement"/>.
|
||||||
/// Note that this value is set even if this element has a <see cref="Parent"/>. To get the element that represents the root element, use <see cref="RootElement.Element"/>.
|
/// Note that this value is set even if this element has a <see cref="Parent"/>. To get the element that represents the root element, use <see cref="RootElement.Element"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public RootElement Root { get; internal set; }
|
public RootElement Root { get; private set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The scale that this ui element renders with
|
/// The scale that this ui element renders with
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -481,8 +481,8 @@ namespace MLEM.Ui.Elements {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
|
|
||||||
this.Children = new ReadOnlyCollection<Element>(this.children);
|
this.Children = new ReadOnlyCollection<Element>(this.children);
|
||||||
this.GetTabNextElement += (backward, next) => next;
|
this.GetTabNextElement = (backward, next) => next;
|
||||||
this.GetGamepadNextElement += (dir, next) => next;
|
this.GetGamepadNextElement = (dir, next) => next;
|
||||||
|
|
||||||
this.SetAreaDirty();
|
this.SetAreaDirty();
|
||||||
this.SetSortedChildrenDirty();
|
this.SetSortedChildrenDirty();
|
||||||
|
@ -506,12 +506,7 @@ namespace MLEM.Ui.Elements {
|
||||||
index = this.children.Count;
|
index = this.children.Count;
|
||||||
this.children.Insert(index, element);
|
this.children.Insert(index, element);
|
||||||
element.Parent = this;
|
element.Parent = this;
|
||||||
element.AndChildren(e => {
|
element.AndChildren(e => e.AddedToUi(this.System, this.Root));
|
||||||
e.Root = this.Root;
|
|
||||||
e.System = this.System;
|
|
||||||
e.OnAddedToUi?.Invoke(e);
|
|
||||||
this.Root?.InvokeOnElementAdded(e);
|
|
||||||
});
|
|
||||||
this.OnChildAdded?.Invoke(this, element);
|
this.OnChildAdded?.Invoke(this, element);
|
||||||
this.SetSortedChildrenDirty();
|
this.SetSortedChildrenDirty();
|
||||||
element.SetAreaDirty();
|
element.SetAreaDirty();
|
||||||
|
@ -530,12 +525,7 @@ namespace MLEM.Ui.Elements {
|
||||||
// upwards to us if the element is auto-positioned
|
// upwards to us if the element is auto-positioned
|
||||||
element.SetAreaDirty();
|
element.SetAreaDirty();
|
||||||
element.Parent = null;
|
element.Parent = null;
|
||||||
element.AndChildren(e => {
|
element.AndChildren(e => e.RemovedFromUi());
|
||||||
e.Root = null;
|
|
||||||
e.System = null;
|
|
||||||
e.OnRemovedFromUi?.Invoke(e);
|
|
||||||
this.Root?.InvokeOnElementRemoved(e);
|
|
||||||
});
|
|
||||||
this.OnChildRemoved?.Invoke(this, element);
|
this.OnChildRemoved?.Invoke(this, element);
|
||||||
this.SetSortedChildrenDirty();
|
this.SetSortedChildrenDirty();
|
||||||
}
|
}
|
||||||
|
@ -1190,6 +1180,31 @@ namespace MLEM.Ui.Elements {
|
||||||
return this.TransformInverse(position);
|
return this.TransformInverse(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called when this element is added to a <see cref="UiSystem"/> and, optionally, a given <see cref="RootElement"/>.
|
||||||
|
/// This method is called in <see cref="AddChild{T}"/> and <see cref="UiSystem.Add"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="system">The ui system to add to.</param>
|
||||||
|
/// <param name="root">The root element to add to.</param>
|
||||||
|
protected internal virtual void AddedToUi(UiSystem system, RootElement root) {
|
||||||
|
this.Root = root;
|
||||||
|
this.System = system;
|
||||||
|
this.OnAddedToUi?.Invoke(this);
|
||||||
|
root?.InvokeOnElementAdded(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called when this element is removed from a <see cref="UiSystem"/> and <see cref="RootElement"/>.
|
||||||
|
/// This method is called in <see cref="RemoveChild"/> and <see cref="UiSystem.Remove"/>.
|
||||||
|
/// </summary>
|
||||||
|
protected internal virtual void RemovedFromUi() {
|
||||||
|
var root = this.Root;
|
||||||
|
this.Root = null;
|
||||||
|
this.System = null;
|
||||||
|
this.OnRemovedFromUi?.Invoke(this);
|
||||||
|
root?.InvokeOnElementRemoved(this);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A delegate used for the <see cref="Element.OnTextInput"/> event.
|
/// A delegate used for the <see cref="Element.OnTextInput"/> event.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -70,13 +70,6 @@ namespace MLEM.Ui.Elements {
|
||||||
this.scrollOverflow = scrollOverflow;
|
this.scrollOverflow = scrollOverflow;
|
||||||
this.CanBeSelected = false;
|
this.CanBeSelected = false;
|
||||||
|
|
||||||
// we dispose our render target when removing so that it doesn't cause a memory leak
|
|
||||||
// if we're added back afterwards, it'll be recreated in ScrollSetup anyway
|
|
||||||
this.OnRemovedFromUi += _ => {
|
|
||||||
this.renderTarget?.Dispose();
|
|
||||||
this.renderTarget = null;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (scrollOverflow) {
|
if (scrollOverflow) {
|
||||||
this.ScrollBar = new ScrollBar(Anchor.TopRight, Vector2.Zero, 0, 0) {
|
this.ScrollBar = new ScrollBar(Anchor.TopRight, Vector2.Zero, 0, 0) {
|
||||||
OnValueChanged = (element, value) => this.ScrollChildren(),
|
OnValueChanged = (element, value) => this.ScrollChildren(),
|
||||||
|
@ -234,6 +227,15 @@ namespace MLEM.Ui.Elements {
|
||||||
this.ScrollChildren();
|
this.ScrollChildren();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected internal override void RemovedFromUi() {
|
||||||
|
base.RemovedFromUi();
|
||||||
|
// we dispose our render target when removing so that it doesn't cause a memory leak
|
||||||
|
// if we're added back afterwards, it'll be recreated in ScrollSetup anyway
|
||||||
|
this.renderTarget?.Dispose();
|
||||||
|
this.renderTarget = null;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Prepares the panel for auto-scrolling, creating the render target and setting up the scroll bar's maximum value.
|
/// Prepares the panel for auto-scrolling, creating the render target and setting up the scroll bar's maximum value.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -345,10 +345,7 @@ namespace MLEM.Ui {
|
||||||
var root = new RootElement(name, element, this);
|
var root = new RootElement(name, element, this);
|
||||||
this.rootElements.Add(root);
|
this.rootElements.Add(root);
|
||||||
root.Element.AndChildren(e => {
|
root.Element.AndChildren(e => {
|
||||||
e.Root = root;
|
e.AddedToUi(this, root);
|
||||||
e.System = this;
|
|
||||||
e.OnAddedToUi?.Invoke(e);
|
|
||||||
root.InvokeOnElementAdded(e);
|
|
||||||
e.SetAreaDirty();
|
e.SetAreaDirty();
|
||||||
});
|
});
|
||||||
this.OnRootAdded?.Invoke(root);
|
this.OnRootAdded?.Invoke(root);
|
||||||
|
@ -368,10 +365,7 @@ namespace MLEM.Ui {
|
||||||
this.rootElements.Remove(root);
|
this.rootElements.Remove(root);
|
||||||
this.Controls.SelectElement(root, null);
|
this.Controls.SelectElement(root, null);
|
||||||
root.Element.AndChildren(e => {
|
root.Element.AndChildren(e => {
|
||||||
e.Root = null;
|
e.RemovedFromUi();
|
||||||
e.System = null;
|
|
||||||
e.OnRemovedFromUi?.Invoke(e);
|
|
||||||
root.InvokeOnElementRemoved(e);
|
|
||||||
e.SetAreaDirty();
|
e.SetAreaDirty();
|
||||||
});
|
});
|
||||||
this.OnRootRemoved?.Invoke(root);
|
this.OnRootRemoved?.Invoke(root);
|
||||||
|
|
Loading…
Reference in a new issue