From 6be4143331bd718ce6b61797324bd370e9f5eca3 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 3 Feb 2023 11:31:18 +0100 Subject: [PATCH] Fixed images not updating their hidden state properly when the displayed texture changes --- CHANGELOG.md | 4 ++++ MLEM.Ui/Elements/Image.cs | 28 +++++++++++++++++----------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 999122a..47d10fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ Jump to version: ## 6.2.0 (In Development) +### MLEM.Ui +Fixes +- Fixed images not updating their hidden state properly when the displayed texture changes + ## 6.1.0 ### MLEM diff --git a/MLEM.Ui/Elements/Image.cs b/MLEM.Ui/Elements/Image.cs index c4a499d..00be592 100644 --- a/MLEM.Ui/Elements/Image.cs +++ b/MLEM.Ui/Elements/Image.cs @@ -30,13 +30,12 @@ namespace MLEM.Ui.Elements { /// public TextureRegion Texture { get { - var ret = this.GetTextureCallback?.Invoke(this) ?? this.texture; - this.CheckTextureChange(ret); - return ret; + this.CheckTextureChange(); + return this.displayedTexture; } set { - this.texture = value; - this.CheckTextureChange(value); + this.explicitlySetTexture = value; + this.CheckTextureChange(); } } /// @@ -75,8 +74,8 @@ namespace MLEM.Ui.Elements { public override bool IsHidden => base.IsHidden || this.Texture == null; private bool scaleToImage; - private TextureRegion texture; - private TextureRegion lastTexture; + private TextureRegion explicitlySetTexture; + private TextureRegion displayedTexture; /// /// Creates a new image with the given settings @@ -106,6 +105,12 @@ namespace MLEM.Ui.Elements { return this.Texture != null && this.scaleToImage ? this.Texture.Size.ToVector2() * this.Scale : base.CalcActualSize(parentArea); } + /// + public override void Update(GameTime time) { + this.CheckTextureChange(); + base.Update(time); + } + /// public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteBatchContext context) { if (this.Texture == null) @@ -123,11 +128,12 @@ namespace MLEM.Ui.Elements { base.Draw(time, batch, alpha, context); } - private void CheckTextureChange(TextureRegion newTexture) { - if (this.lastTexture == newTexture) + private void CheckTextureChange() { + var newTexture = this.GetTextureCallback?.Invoke(this) ?? this.explicitlySetTexture; + if (this.displayedTexture == newTexture) return; - var nullChanged = this.lastTexture == null != (newTexture == null); - this.lastTexture = newTexture; + var nullChanged = this.displayedTexture == null != (newTexture == null); + this.displayedTexture = newTexture; if (nullChanged || this.scaleToImage) this.SetAreaDirty(); }