mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-29 15:58:33 +01:00
Compare commits
No commits in common. "e5cfebef3b0d27592af11fa752a01b046f2e0f9c" and "9919ee4a97221f79f2a1627b7e8896ad1f624e5e" have entirely different histories.
e5cfebef3b
...
9919ee4a97
6 changed files with 41 additions and 64 deletions
|
@ -68,10 +68,6 @@ Fixes
|
|||
- Fixed an exception when trying to force-update the area of an element without a ui system
|
||||
- Fixed the scroll bar of an empty panel being positioned incorrectly
|
||||
- Fixed UiControls maintaining old input states when input types are toggled off
|
||||
- Fixed an occasional deadlock when a game is disposed with a scrolling Panel present
|
||||
|
||||
Removals
|
||||
- Marked Element.OnDisposed as obsolete in favor of the more predictable OnRemovedFromUi
|
||||
|
||||
### MLEM.Data
|
||||
Additions
|
||||
|
|
|
@ -31,7 +31,7 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public UiSystem System {
|
||||
get => this.system;
|
||||
private set {
|
||||
internal set {
|
||||
this.system = value;
|
||||
this.Controls = value?.Controls;
|
||||
this.Style = this.Style.OrStyle(value?.Style);
|
||||
|
@ -50,7 +50,7 @@ namespace MLEM.Ui.Elements {
|
|||
/// 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"/>.
|
||||
/// </summary>
|
||||
public RootElement Root { get; private set; }
|
||||
public RootElement Root { get; internal set; }
|
||||
/// <summary>
|
||||
/// The scale that this ui element renders with
|
||||
/// </summary>
|
||||
|
@ -425,10 +425,9 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public GenericCallback OnRemovedFromUi;
|
||||
/// <summary>
|
||||
/// Event that is called when this element's <see cref="Dispose"/> method is called.
|
||||
/// Event that is called when this element's <see cref="Dispose"/> method is called, which also happens in <see cref="Finalize"/>.
|
||||
/// This event is useful for unregistering global event handlers when this object should be destroyed.
|
||||
/// </summary>
|
||||
[Obsolete("OnDisposed will be removed in a future update. To unregister custom event handlers, use OnRemovedFromUi instead.")]
|
||||
public GenericCallback OnDisposed;
|
||||
|
||||
/// <summary>
|
||||
|
@ -481,15 +480,14 @@ namespace MLEM.Ui.Elements {
|
|||
this.size = size;
|
||||
|
||||
this.Children = new ReadOnlyCollection<Element>(this.children);
|
||||
this.GetTabNextElement = (backward, next) => next;
|
||||
this.GetGamepadNextElement = (dir, next) => next;
|
||||
this.GetTabNextElement += (backward, next) => next;
|
||||
this.GetGamepadNextElement += (dir, next) => next;
|
||||
|
||||
this.SetAreaDirty();
|
||||
this.SetSortedChildrenDirty();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[Obsolete("Dispose will be removed in a future update. To unregister custom event handlers, use OnRemovedFromUi instead.")]
|
||||
~Element() {
|
||||
this.Dispose();
|
||||
}
|
||||
|
@ -506,7 +504,12 @@ namespace MLEM.Ui.Elements {
|
|||
index = this.children.Count;
|
||||
this.children.Insert(index, element);
|
||||
element.Parent = this;
|
||||
element.AndChildren(e => e.AddedToUi(this.System, this.Root));
|
||||
element.AndChildren(e => {
|
||||
e.Root = this.Root;
|
||||
e.System = this.System;
|
||||
e.OnAddedToUi?.Invoke(e);
|
||||
this.Root?.InvokeOnElementAdded(e);
|
||||
});
|
||||
this.OnChildAdded?.Invoke(this, element);
|
||||
this.SetSortedChildrenDirty();
|
||||
element.SetAreaDirty();
|
||||
|
@ -525,7 +528,12 @@ namespace MLEM.Ui.Elements {
|
|||
// upwards to us if the element is auto-positioned
|
||||
element.SetAreaDirty();
|
||||
element.Parent = null;
|
||||
element.AndChildren(e => e.RemovedFromUi());
|
||||
element.AndChildren(e => {
|
||||
e.Root = null;
|
||||
e.System = null;
|
||||
e.OnRemovedFromUi?.Invoke(e);
|
||||
this.Root?.InvokeOnElementRemoved(e);
|
||||
});
|
||||
this.OnChildRemoved?.Invoke(this, element);
|
||||
this.SetSortedChildrenDirty();
|
||||
}
|
||||
|
@ -1090,7 +1098,6 @@ namespace MLEM.Ui.Elements {
|
|||
}
|
||||
|
||||
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
|
||||
[Obsolete("Dispose will be removed in a future update. To unregister custom event handlers, use OnRemovedFromUi instead.")]
|
||||
public virtual void Dispose() {
|
||||
this.OnDisposed?.Invoke(this);
|
||||
GC.SuppressFinalize(this);
|
||||
|
@ -1180,31 +1187,6 @@ namespace MLEM.Ui.Elements {
|
|||
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>
|
||||
/// A delegate used for the <see cref="Element.OnTextInput"/> event.
|
||||
/// </summary>
|
||||
|
|
|
@ -177,6 +177,15 @@ namespace MLEM.Ui.Elements {
|
|||
return base.GetElementUnderPos(position);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Dispose() {
|
||||
if (this.renderTarget != null) {
|
||||
this.renderTarget.Dispose();
|
||||
this.renderTarget = null;
|
||||
}
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scrolls this panel's <see cref="ScrollBar"/> to the given <see cref="Element"/> in such a way that its center is positioned in the center of this panel.
|
||||
/// </summary>
|
||||
|
@ -227,15 +236,6 @@ namespace MLEM.Ui.Elements {
|
|||
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>
|
||||
/// Prepares the panel for auto-scrolling, creating the render target and setting up the scroll bar's maximum value.
|
||||
/// </summary>
|
||||
|
@ -274,13 +274,11 @@ namespace MLEM.Ui.Elements {
|
|||
|
||||
// update the render target
|
||||
var targetArea = (Rectangle) this.GetRenderTargetArea();
|
||||
if (targetArea.Width <= 0 || targetArea.Height <= 0) {
|
||||
this.renderTarget?.Dispose();
|
||||
this.renderTarget = null;
|
||||
if (targetArea.Width <= 0 || targetArea.Height <= 0)
|
||||
return;
|
||||
}
|
||||
if (this.renderTarget == null || targetArea.Width != this.renderTarget.Width || targetArea.Height != this.renderTarget.Height) {
|
||||
this.renderTarget?.Dispose();
|
||||
if (this.renderTarget != null)
|
||||
this.renderTarget.Dispose();
|
||||
this.renderTarget = targetArea.IsEmpty ? null : new RenderTarget2D(this.System.Game.GraphicsDevice, targetArea.Width, targetArea.Height);
|
||||
this.relevantChildrenDirty = true;
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ namespace MLEM.Ui.Parsers {
|
|||
public static readonly ElementType[] ElementTypes =
|
||||
#if NET6_0_OR_GREATER
|
||||
Enum.GetValues<ElementType>();
|
||||
#else
|
||||
#else
|
||||
(ElementType[]) Enum.GetValues(typeof(ElementType));
|
||||
#endif
|
||||
|
||||
|
@ -145,15 +145,9 @@ namespace MLEM.Ui.Parsers {
|
|||
throw new NullReferenceException("A UI parser requires a GraphicsDevice for parsing images");
|
||||
|
||||
TextureRegion image = null;
|
||||
LoadImageAsync();
|
||||
return new Image(Anchor.AutoLeft, new Vector2(1, -1), _ => image) {
|
||||
OnAddedToUi = e => {
|
||||
if (image == null)
|
||||
LoadImageAsync();
|
||||
},
|
||||
OnRemovedFromUi = e => {
|
||||
image?.Texture.Dispose();
|
||||
image = null;
|
||||
}
|
||||
OnDisposed = e => image?.Texture.Dispose()
|
||||
};
|
||||
|
||||
async void LoadImageAsync() {
|
||||
|
|
|
@ -345,7 +345,10 @@ namespace MLEM.Ui {
|
|||
var root = new RootElement(name, element, this);
|
||||
this.rootElements.Add(root);
|
||||
root.Element.AndChildren(e => {
|
||||
e.AddedToUi(this, root);
|
||||
e.Root = root;
|
||||
e.System = this;
|
||||
e.OnAddedToUi?.Invoke(e);
|
||||
root.InvokeOnElementAdded(e);
|
||||
e.SetAreaDirty();
|
||||
});
|
||||
this.OnRootAdded?.Invoke(root);
|
||||
|
@ -365,7 +368,10 @@ namespace MLEM.Ui {
|
|||
this.rootElements.Remove(root);
|
||||
this.Controls.SelectElement(root, null);
|
||||
root.Element.AndChildren(e => {
|
||||
e.RemovedFromUi();
|
||||
e.Root = null;
|
||||
e.System = null;
|
||||
e.OnRemovedFromUi?.Invoke(e);
|
||||
root.InvokeOnElementRemoved(e);
|
||||
e.SetAreaDirty();
|
||||
});
|
||||
this.OnRootRemoved?.Invoke(root);
|
||||
|
|
|
@ -411,6 +411,7 @@ namespace MLEM.Graphics {
|
|||
this.indices?.Dispose();
|
||||
foreach (var buffer in this.vertexBuffers)
|
||||
buffer.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private Item Add(Texture2D texture, Vector2 pos, Vector2 offset, Vector2 size, float sin, float cos, Color color, Vector2 texTl, Vector2 texBr, float depth) {
|
||||
|
|
Loading…
Reference in a new issue