From b46975391b65083313c50fb066bf2192101bcd7f Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 6 Jan 2022 23:26:14 +0100 Subject: [PATCH] Only set a paragraph's area dirty when a text change would cause it to change size --- CHANGELOG.md | 1 + MLEM.Ui/Elements/Element.cs | 7 ++++++- MLEM.Ui/Elements/Paragraph.cs | 21 +++++++++++++++------ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6374973..d836072 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ Fixes ### MLEM.Ui Improvements - Allow for checkboxes and radio buttons to be disabled +- Only set a paragraph's area dirty when a text change would cause it to change size ### MLEM.Data Improvements diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index ef289a7..f386e39 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -398,6 +398,11 @@ namespace MLEM.Ui.Elements { /// The input handler that this element's use /// protected InputHandler Input => this.Controls.Input; + /// + /// The of this element's , or the if this element has no parent. + /// This value is the one that is passed to during . + /// + protected RectangleF ParentArea => this.Parent?.ChildPaddedArea ?? (RectangleF) this.system.Viewport; private readonly List children = new List(); private bool sortedChildrenDirty; @@ -554,7 +559,7 @@ namespace MLEM.Ui.Elements { return; this.System.Stopwatch.Restart(); - var parentArea = this.Parent != null ? this.Parent.ChildPaddedArea : (RectangleF) this.system.Viewport; + var parentArea = this.ParentArea; var parentCenterX = parentArea.X + parentArea.Width / 2; var parentCenterY = parentArea.Y + parentArea.Height / 2; var actualSize = this.CalcActualSize(parentArea); diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index 9511c3b..48ff87d 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -2,6 +2,7 @@ using System; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MLEM.Extensions; using MLEM.Font; using MLEM.Formatting; using MLEM.Formatting.Codes; @@ -24,8 +25,7 @@ namespace MLEM.Ui.Elements { get => this.regularFont; set { this.regularFont = value; - this.SetAreaDirty(); - this.TokenizedText = null; + this.SetTextDirty(); } } /// @@ -59,8 +59,7 @@ namespace MLEM.Ui.Elements { if (this.text != value) { this.text = value; this.IsHidden = string.IsNullOrWhiteSpace(this.text); - this.SetAreaDirty(); - this.TokenizedText = null; + this.SetTextDirty(); } } } @@ -95,8 +94,7 @@ namespace MLEM.Ui.Elements { get => this.alignment; set { this.alignment = value; - this.SetAreaDirty(); - this.TokenizedText = null; + this.SetTextDirty(); } } @@ -187,6 +185,17 @@ namespace MLEM.Ui.Elements { } } + /// + /// A helper method that causes the to be reset. + /// Additionally, if this paragraph's area has changed enough to warrant it, or if it has any children. + /// + protected void SetTextDirty() { + this.TokenizedText = null; + // only set our area dirty if our size changed as a result of this action or if we have link children we need to update + if (!this.AreaDirty && (this.Children.Count > 0 || !this.CalcActualSize(this.ParentArea).Equals(this.DisplayArea.Size, Epsilon))) + this.SetAreaDirty(); + } + private void QueryTextCallback() { if (this.GetTextCallback != null) this.Text = this.GetTextCallback(this);