From 3541b8d3e1ea4c75957815a38f355b52095bd32d Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 21 Dec 2021 00:01:57 +0100 Subject: [PATCH] Automatically set area dirty when changing child padding or paragraph fonts --- CHANGELOG.md | 1 + MLEM.Ui/Elements/Element.cs | 10 ++++++++-- MLEM.Ui/Elements/Panel.cs | 2 +- MLEM.Ui/Elements/Paragraph.cs | 13 ++++++++++--- MLEM.Ui/Elements/TextField.cs | 2 +- MLEM.Ui/Elements/Tooltip.cs | 2 +- MLEM.Ui/Style/StyleProp.cs | 23 ++++++++++++++--------- Tests/UiTests.cs | 8 +++++++- 8 files changed, 43 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af8a4a4..efeb506 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ Improvements - Skip unnecessary area updates for elements with dirty parents - Calculate panel scroll bar height based on content height - Remember the location that a scroll bar scroller was grabbed in when scrolling +- Automatically set area dirty when changing child padding or paragraph fonts Fixes - Fixed VerticalSpace height parameter being an integer diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index d27db6e..dcbd9cc 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -281,9 +281,14 @@ namespace MLEM.Ui.Elements { /// /// The child padding that this element has. /// The child padding moves any added to this element inwards by the given amount in each direction. - /// When setting this style after this element has already been added to a ui, should be called. /// - public StyleProp ChildPadding; + public StyleProp ChildPadding { + get => this.childPadding; + set { + this.childPadding = value; + this.SetAreaDirty(); + } + } /// /// Event that is called after this element is drawn, but before its children are drawn @@ -405,6 +410,7 @@ namespace MLEM.Ui.Elements { private bool isHidden; private int priority; private UiStyle style; + private StyleProp childPadding; /// /// Creates a new element with the given anchor and size and sets up some default event reactions. diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 8d8d663..4a1d085 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -211,8 +211,8 @@ namespace MLEM.Ui.Elements { this.Texture.SetFromStyle(style.PanelTexture); this.StepPerScroll.SetFromStyle(style.PanelStepPerScroll); this.ScrollerSize.SetFromStyle(style.PanelScrollerSize); - this.ChildPadding.SetFromStyle(style.PanelChildPadding); this.ScrollBarOffset.SetFromStyle(style.PanelScrollBarOffset); + this.ChildPadding = this.ChildPadding.CopyFromStyle(style.PanelChildPadding); this.SetScrollBarStyle(); } diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index 8d04ded..465cd04 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -20,7 +20,14 @@ namespace MLEM.Ui.Elements { /// The font that this paragraph draws text with. /// To set its bold and italic font, use and . /// - public StyleProp RegularFont; + public StyleProp RegularFont { + get => this.regularFont; + set { + this.regularFont = value; + this.SetAreaDirty(); + this.TokenizedText = null; + } + } /// /// The tokenized version of the /// @@ -53,7 +60,6 @@ namespace MLEM.Ui.Elements { this.text = value; this.IsHidden = string.IsNullOrWhiteSpace(this.text); this.SetAreaDirty(); - // force text to be re-tokenized this.TokenizedText = null; } } @@ -96,6 +102,7 @@ namespace MLEM.Ui.Elements { private string text; private TextAlignment alignment; + private StyleProp regularFont; /// /// Creates a new paragraph with the given settings. @@ -150,7 +157,7 @@ namespace MLEM.Ui.Elements { /// protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.RegularFont.SetFromStyle(style.Font ?? throw new NotSupportedException("Paragraphs cannot use ui styles that don't have a font. Please supply a custom font by setting UiStyle.Font.")); + this.RegularFont = this.RegularFont.CopyFromStyle(style.Font ?? throw new NotSupportedException("Paragraphs cannot use ui styles that don't have a font. Please supply a custom font by setting UiStyle.Font.")); this.TextScale.SetFromStyle(style.TextScale); this.TextColor.SetFromStyle(style.TextColor); } diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index 3a41679..9b4a618 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -211,7 +211,7 @@ namespace MLEM.Ui.Elements { this.InputRule = rule ?? DefaultRule; this.Multiline = multiline; if (font != null) - this.Font.Set(font); + this.Font = font; if (text != null) this.SetText(text, true); diff --git a/MLEM.Ui/Elements/Tooltip.cs b/MLEM.Ui/Elements/Tooltip.cs index 86ea485..f72a27d 100644 --- a/MLEM.Ui/Elements/Tooltip.cs +++ b/MLEM.Ui/Elements/Tooltip.cs @@ -79,7 +79,7 @@ namespace MLEM.Ui.Elements { this.Texture.SetFromStyle(style.TooltipBackground); this.MouseOffset.SetFromStyle(style.TooltipOffset); this.Delay.SetFromStyle(style.TooltipDelay); - this.ChildPadding.SetFromStyle(style.TooltipChildPadding); + this.ChildPadding = this.ChildPadding.CopyFromStyle(style.TooltipChildPadding); if (this.Paragraph != null) { this.Paragraph.TextColor.SetFromStyle(style.TooltipTextColor, 1); this.Paragraph.Size = new Vector2(style.TooltipTextWidth, 0); diff --git a/MLEM.Ui/Style/StyleProp.cs b/MLEM.Ui/Style/StyleProp.cs index d41e0ea..946ef6d 100644 --- a/MLEM.Ui/Style/StyleProp.cs +++ b/MLEM.Ui/Style/StyleProp.cs @@ -26,11 +26,12 @@ namespace MLEM.Ui.Style { } /// - /// Sets this style property's value and marks it as being set by a . - /// This allows this property to be overridden by custom style settings using . + /// Sets this style property's value and marks it as being set by a if it doesn't have a custom value yet. + /// This allows this property to be overridden by custom style settings using or a higher . /// /// The style to apply /// The priority that this style value has. Higher priority style values will override lower priority style values. + /// public void SetFromStyle(T value, byte priority = 0) { if (priority >= this.lastSetPriority) { this.Value = value; @@ -39,13 +40,17 @@ namespace MLEM.Ui.Style { } /// - /// Sets this style property's value and marks it as being custom. - /// This causes not to override the style value through a . + /// Creates a copy of this style property and sets its value and marks it as being set by a if it doesn't have a custom value yet. + /// This allows this property to be overridden by custom style settings using or a higher . /// - /// - public void Set(T value) { - this.lastSetPriority = byte.MaxValue; - this.Value = value; + /// The style to apply + /// The priority that the style value has. Higher priority style values will override lower priority style values. + /// The new style + /// + public StyleProp CopyFromStyle(T value, byte priority = 0) { + var ret = this; + ret.SetFromStyle(value, priority); + return ret; } /// @@ -58,7 +63,7 @@ namespace MLEM.Ui.Style { } /// - /// Returns whether this style property has a value assigned to it using or . + /// Returns whether this style property has a value assigned to it using or . /// /// Whether this style property has a value public bool HasValue() { diff --git a/Tests/UiTests.cs b/Tests/UiTests.cs index 8be6774..a55e76e 100644 --- a/Tests/UiTests.cs +++ b/Tests/UiTests.cs @@ -79,10 +79,16 @@ namespace Tests { Assert.AreEqual(null, style.Value); style.SetFromStyle("from style"); Assert.AreEqual("from style", style.Value); - style.Set("custom"); + style = "custom"; Assert.AreEqual("custom", style.Value); style.SetFromStyle("from style again"); Assert.AreEqual("custom", style.Value); + + var copy = style.CopyFromStyle("copy from style", byte.MaxValue); + var weakCopy = style.CopyFromStyle("weak copy"); + Assert.AreEqual("copy from style", copy.Value); + Assert.AreEqual("custom", weakCopy.Value); + Assert.AreEqual("custom", style.Value); } [Test]