diff --git a/CHANGELOG.md b/CHANGELOG.md index 56b8ff6..8f00791 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ Additions - Added ScrollBar.MouseDragScrolling - Added Panel.ScrollToElement - Added ElementHelper.MakeGrid +- Added Element.AutoHideCondition and Button.AutoDisableCondition Improvements - Allow elements to auto-adjust their size even when their children are aligned oddly diff --git a/MLEM.Ui/Elements/Button.cs b/MLEM.Ui/Elements/Button.cs index 5293b2d..3bb46ed 100644 --- a/MLEM.Ui/Elements/Button.cs +++ b/MLEM.Ui/Elements/Button.cs @@ -1,3 +1,4 @@ +using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MLEM.Graphics; @@ -51,8 +52,16 @@ namespace MLEM.Ui.Elements { /// /// Set this property to true to mark the button as disabled. /// A disabled button cannot be moused over, selected or pressed. + /// If this value changes often, consider using to set it automatically. /// - public virtual bool IsDisabled { get; set; } + public virtual bool IsDisabled { + get { + if (this.AutoDisableCondition != null) + this.IsDisabled = this.AutoDisableCondition(this); + return this.isDisabled; + } + set => this.isDisabled = value; + } /// /// Whether this button's should be truncated if it exceeds this button's width. /// Defaults to false. @@ -69,12 +78,18 @@ namespace MLEM.Ui.Elements { /// If this is true, will be able to return true even if is true. /// public bool CanSelectDisabled; + /// + /// An optional function that can be used to set automatically based on a user-defined condition. This removes the need to disable a button based on a condition in or manually. + /// + public Func AutoDisableCondition; /// public override bool CanBeSelected => base.CanBeSelected && (this.CanSelectDisabled || !this.IsDisabled); /// public override bool CanBePressed => base.CanBePressed && !this.IsDisabled; + private bool isDisabled; + /// /// Creates a new button with the given settings /// diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 22a6550..c460ddd 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -172,9 +172,15 @@ namespace MLEM.Ui.Elements { /// /// Set this property to true to cause this element to be hidden. /// Hidden elements don't receive input events, aren't rendered and don't factor into auto-anchoring. + /// If this value changes often, consider using to set it automatically. /// public virtual bool IsHidden { - get => this.isHidden; + get { + // instead of just returning, we set IsHidden here because we might have to set our area dirty + if (this.AutoHideCondition != null) + this.IsHidden = this.AutoHideCondition(this); + return this.isHidden; + } set { if (this.isHidden == value) return; @@ -430,6 +436,10 @@ namespace MLEM.Ui.Elements { /// [Obsolete("OnDisposed will be removed in a future update. To unregister custom event handlers, use OnRemovedFromUi instead.")] public GenericCallback OnDisposed; + /// + /// An optional function that can be used to set automatically based on a user-defined condition. This removes the need to hide an element based on a condition in or manually. + /// + public Func AutoHideCondition; /// /// A list of all of this element's direct children.