diff --git a/MLEM.Ui/Elements/Button.cs b/MLEM.Ui/Elements/Button.cs index 3bb46ed..0c42523 100644 --- a/MLEM.Ui/Elements/Button.cs +++ b/MLEM.Ui/Elements/Button.cs @@ -52,14 +52,10 @@ 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. + /// If this value changes often, consider using . /// public virtual bool IsDisabled { - get { - if (this.AutoDisableCondition != null) - this.IsDisabled = this.AutoDisableCondition(this); - return this.isDisabled; - } + get => this.isDisabled || this.AutoDisableCondition?.Invoke(this) == true; set => this.isDisabled = value; } /// @@ -79,7 +75,8 @@ namespace MLEM.Ui.Elements { /// 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. + /// An optional function that can be used to modify the result of automatically based on a user-defined condition. This removes the need to disable a button based on a condition in or manually. + /// Note that, if 's underlying value is set to using , this function's result will be ignored. /// public Func AutoDisableCondition; diff --git a/MLEM.Ui/Elements/Image.cs b/MLEM.Ui/Elements/Image.cs index 976eb7e..c4a499d 100644 --- a/MLEM.Ui/Elements/Image.cs +++ b/MLEM.Ui/Elements/Image.cs @@ -30,17 +30,13 @@ namespace MLEM.Ui.Elements { /// public TextureRegion Texture { get { - if (this.GetTextureCallback != null) - this.Texture = this.GetTextureCallback(this); - return this.texture; + var ret = this.GetTextureCallback?.Invoke(this) ?? this.texture; + this.CheckTextureChange(ret); + return ret; } set { - if (this.texture != value) { - this.texture = value; - this.IsHidden = this.texture == null; - if (this.scaleToImage) - this.SetAreaDirty(); - } + this.texture = value; + this.CheckTextureChange(value); } } /// @@ -75,8 +71,12 @@ namespace MLEM.Ui.Elements { /// public float ImageRotation; + /// + public override bool IsHidden => base.IsHidden || this.Texture == null; + private bool scaleToImage; private TextureRegion texture; + private TextureRegion lastTexture; /// /// Creates a new image with the given settings @@ -123,6 +123,15 @@ namespace MLEM.Ui.Elements { base.Draw(time, batch, alpha, context); } + private void CheckTextureChange(TextureRegion newTexture) { + if (this.lastTexture == newTexture) + return; + var nullChanged = this.lastTexture == null != (newTexture == null); + this.lastTexture = newTexture; + if (nullChanged || this.scaleToImage) + this.SetAreaDirty(); + } + /// /// A delegate method used for /// diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index 9b47c02..c73fc33 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -53,20 +53,13 @@ namespace MLEM.Ui.Elements { /// public string Text { get { - this.QueryTextCallback(); - return this.text; + var ret = this.GetTextCallback?.Invoke(this) ?? this.text; + this.CheckTextChange(ret); + return ret; } set { - if (this.text != value) { - this.text = value; - this.SetTextDirty(); - - var force = string.IsNullOrWhiteSpace(this.text); - if (this.forceHide != force) { - this.forceHide = force; - this.SetAreaDirty(); - } - } + this.text = value; + this.CheckTextChange(value); } } /// @@ -105,12 +98,12 @@ namespace MLEM.Ui.Elements { } /// - public override bool IsHidden => base.IsHidden || this.forceHide; + public override bool IsHidden => base.IsHidden || string.IsNullOrWhiteSpace(this.Text); private string text; + private string lastText; private StyleProp alignment; private StyleProp regularFont; - private bool forceHide = true; /// /// Creates a new paragraph with the given settings. @@ -141,7 +134,6 @@ namespace MLEM.Ui.Elements { /// public override void Update(GameTime time) { - this.QueryTextCallback(); base.Update(time); if (this.TokenizedText != null) this.TokenizedText.Update(time); @@ -149,7 +141,6 @@ namespace MLEM.Ui.Elements { /// public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteBatchContext context) { - this.QueryTextCallback(); var pos = this.DisplayArea.Location + new Vector2(this.GetAlignmentOffset(), 0); var sc = this.TextScale * this.TextScaleMultiplier * this.Scale; var color = this.TextColor.OrDefault(Color.White) * alpha; @@ -202,9 +193,14 @@ namespace MLEM.Ui.Elements { this.SetAreaDirty(); } - private void QueryTextCallback() { - if (this.GetTextCallback != null) - this.Text = this.GetTextCallback(this); + private void CheckTextChange(string newText) { + if (this.lastText == newText) + return; + var emptyChanged = string.IsNullOrWhiteSpace(this.lastText) != string.IsNullOrWhiteSpace(newText); + this.lastText = newText; + if (emptyChanged) + this.SetAreaDirty(); + this.SetTextDirty(); } private float GetAlignmentOffset() {