diff --git a/CHANGELOG.md b/CHANGELOG.md index efeb506..988eb2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ Improvements - **Made Image ScaleToImage take ui scale into account** - **Added style properties for a lot of hardcoded default element styles** - **Allow setting a custom effect and depth stencil state for ui drawing** +- **Made StyleProp immutable** - Exposed the epsilon value used by Element calculations - Allow style properties to set style values with a higher priority, which allows elements to style their default children - Allow changing the entire ui style for a single element diff --git a/MLEM.Ui/Elements/Button.cs b/MLEM.Ui/Elements/Button.cs index 730c0b7..c44e404 100644 --- a/MLEM.Ui/Elements/Button.cs +++ b/MLEM.Ui/Elements/Button.cs @@ -83,7 +83,7 @@ namespace MLEM.Ui.Elements { public Button(Anchor anchor, Vector2 size, string text = null, string tooltipText = null) : base(anchor, size) { if (text != null) { this.Text = new Paragraph(Anchor.Center, 1, text, true); - this.Text.Padding.SetFromStyle(new Padding(1), 1); + this.Text.Padding = this.Text.Padding.OrStyle(new Padding(1), 1); this.AddChild(this.Text); } if (tooltipText != null) @@ -108,11 +108,11 @@ namespace MLEM.Ui.Elements { /// protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Texture.SetFromStyle(style.ButtonTexture); - this.HoveredTexture.SetFromStyle(style.ButtonHoveredTexture); - this.HoveredColor.SetFromStyle(style.ButtonHoveredColor); - this.DisabledTexture.SetFromStyle(style.ButtonDisabledTexture); - this.DisabledColor.SetFromStyle(style.ButtonDisabledColor); + this.Texture = this.Texture.OrStyle(style.ButtonTexture); + this.HoveredTexture = this.HoveredTexture.OrStyle(style.ButtonHoveredTexture); + this.HoveredColor = this.HoveredColor.OrStyle(style.ButtonHoveredColor); + this.DisabledTexture = this.DisabledTexture.OrStyle(style.ButtonDisabledTexture); + this.DisabledColor = this.DisabledColor.OrStyle(style.ButtonDisabledColor); } } diff --git a/MLEM.Ui/Elements/Checkbox.cs b/MLEM.Ui/Elements/Checkbox.cs index ea03cc6..2fdff3a 100644 --- a/MLEM.Ui/Elements/Checkbox.cs +++ b/MLEM.Ui/Elements/Checkbox.cs @@ -102,11 +102,11 @@ namespace MLEM.Ui.Elements { /// protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Texture.SetFromStyle(style.CheckboxTexture); - this.HoveredTexture.SetFromStyle(style.CheckboxHoveredTexture); - this.HoveredColor.SetFromStyle(style.CheckboxHoveredColor); - this.Checkmark.SetFromStyle(style.CheckboxCheckmark); - this.TextOffsetX.SetFromStyle(style.CheckboxTextOffsetX); + this.Texture = this.Texture.OrStyle(style.CheckboxTexture); + this.HoveredTexture = this.HoveredTexture.OrStyle(style.CheckboxHoveredTexture); + this.HoveredColor = this.HoveredColor.OrStyle(style.CheckboxHoveredColor); + this.Checkmark = this.Checkmark.OrStyle(style.CheckboxCheckmark); + this.TextOffsetX = this.TextOffsetX.OrStyle(style.CheckboxTextOffsetX); } /// diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index dcbd9cc..ef289a7 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -1050,9 +1050,9 @@ namespace MLEM.Ui.Elements { /// /// The new style protected virtual void InitStyle(UiStyle style) { - this.SelectionIndicator.SetFromStyle(style.SelectionIndicator); - this.ActionSound.SetFromStyle(style.ActionSound); - this.SecondActionSound.SetFromStyle(style.ActionSound); + this.SelectionIndicator = this.SelectionIndicator.OrStyle(style.SelectionIndicator); + this.ActionSound = this.ActionSound.OrStyle(style.ActionSound); + this.SecondActionSound = this.SecondActionSound.OrStyle(style.ActionSound); } /// diff --git a/MLEM.Ui/Elements/ElementHelper.cs b/MLEM.Ui/Elements/ElementHelper.cs index b7755f1..918acc8 100644 --- a/MLEM.Ui/Elements/ElementHelper.cs +++ b/MLEM.Ui/Elements/ElementHelper.cs @@ -24,7 +24,7 @@ namespace MLEM.Ui.Elements { public static Button ImageButton(Anchor anchor, Vector2 size, TextureRegion texture, string text = null, string tooltipText = null, float imagePadding = 2) { var button = new Button(anchor, size, text, tooltipText); var image = new Image(Anchor.CenterLeft, Vector2.One, texture); - image.Padding.SetFromStyle(new Padding(imagePadding), 1); + image.Padding = image.Padding.OrStyle(new Padding(imagePadding), 1); button.OnAreaUpdated += e => image.Size = new Vector2(e.Area.Height, e.Area.Height) / e.Scale; button.AddChild(image, 0); return button; diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 4a1d085..b84e9cf 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -208,11 +208,11 @@ namespace MLEM.Ui.Elements { /// protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Texture.SetFromStyle(style.PanelTexture); - this.StepPerScroll.SetFromStyle(style.PanelStepPerScroll); - this.ScrollerSize.SetFromStyle(style.PanelScrollerSize); - this.ScrollBarOffset.SetFromStyle(style.PanelScrollBarOffset); - this.ChildPadding = this.ChildPadding.CopyFromStyle(style.PanelChildPadding); + this.Texture = this.Texture.OrStyle(style.PanelTexture); + this.StepPerScroll = this.StepPerScroll.OrStyle(style.PanelStepPerScroll); + this.ScrollerSize = this.ScrollerSize.OrStyle(style.PanelScrollerSize); + this.ScrollBarOffset = this.ScrollBarOffset.OrStyle(style.PanelScrollBarOffset); + this.ChildPadding = this.ChildPadding.OrStyle(style.PanelChildPadding); this.SetScrollBarStyle(); } diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index 465cd04..9511c3b 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -157,9 +157,9 @@ namespace MLEM.Ui.Elements { /// protected override void InitStyle(UiStyle style) { base.InitStyle(style); - 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); + this.RegularFont = this.RegularFont.OrStyle(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 = this.TextScale.OrStyle(style.TextScale); + this.TextColor = this.TextColor.OrStyle(style.TextColor); } /// diff --git a/MLEM.Ui/Elements/ProgressBar.cs b/MLEM.Ui/Elements/ProgressBar.cs index 08edff0..ece0d4f 100644 --- a/MLEM.Ui/Elements/ProgressBar.cs +++ b/MLEM.Ui/Elements/ProgressBar.cs @@ -112,11 +112,11 @@ namespace MLEM.Ui.Elements { /// protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Texture.SetFromStyle(style.ProgressBarTexture); - this.Color.SetFromStyle(style.ProgressBarColor); - this.ProgressPadding.SetFromStyle(style.ProgressBarProgressPadding); - this.ProgressTexture.SetFromStyle(style.ProgressBarProgressTexture); - this.ProgressColor.SetFromStyle(style.ProgressBarProgressColor); + this.Texture = this.Texture.OrStyle(style.ProgressBarTexture); + this.Color = this.Color.OrStyle(style.ProgressBarColor); + this.ProgressPadding = this.ProgressPadding.OrStyle(style.ProgressBarProgressPadding); + this.ProgressTexture = this.ProgressTexture.OrStyle(style.ProgressBarProgressTexture); + this.ProgressColor = this.ProgressColor.OrStyle(style.ProgressBarProgressColor); } } diff --git a/MLEM.Ui/Elements/RadioButton.cs b/MLEM.Ui/Elements/RadioButton.cs index 71f5412..3e1e869 100644 --- a/MLEM.Ui/Elements/RadioButton.cs +++ b/MLEM.Ui/Elements/RadioButton.cs @@ -39,10 +39,10 @@ namespace MLEM.Ui.Elements { /// protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Texture.SetFromStyle(style.RadioTexture); - this.HoveredTexture.SetFromStyle(style.RadioHoveredTexture); - this.HoveredColor.SetFromStyle(style.RadioHoveredColor); - this.Checkmark.SetFromStyle(style.RadioCheckmark); + this.Texture = this.Texture.OrStyle(style.RadioTexture); + this.HoveredTexture = this.HoveredTexture.OrStyle(style.RadioHoveredTexture); + this.HoveredColor = this.HoveredColor.OrStyle(style.RadioHoveredColor); + this.Checkmark = this.Checkmark.OrStyle(style.RadioCheckmark); } } diff --git a/MLEM.Ui/Elements/ScrollBar.cs b/MLEM.Ui/Elements/ScrollBar.cs index a12f2c5..4b356e6 100644 --- a/MLEM.Ui/Elements/ScrollBar.cs +++ b/MLEM.Ui/Elements/ScrollBar.cs @@ -217,10 +217,10 @@ namespace MLEM.Ui.Elements { /// protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Background.SetFromStyle(style.ScrollBarBackground); - this.ScrollerTexture.SetFromStyle(style.ScrollBarScrollerTexture); - this.SmoothScrolling.SetFromStyle(style.ScrollBarSmoothScrolling); - this.SmoothScrollFactor.SetFromStyle(style.ScrollBarSmoothScrollFactor); + this.Background = this.Background.OrStyle(style.ScrollBarBackground); + this.ScrollerTexture = this.ScrollerTexture.OrStyle(style.ScrollBarScrollerTexture); + this.SmoothScrolling = this.SmoothScrolling.OrStyle(style.ScrollBarSmoothScrolling); + this.SmoothScrollFactor = this.SmoothScrollFactor.OrStyle(style.ScrollBarSmoothScrollFactor); } /// diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index 9b4a618..14ce61a 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -394,13 +394,13 @@ namespace MLEM.Ui.Elements { /// protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.TextScale.SetFromStyle(style.TextScale); - this.Font.SetFromStyle(style.Font); - this.Texture.SetFromStyle(style.TextFieldTexture); - this.HoveredTexture.SetFromStyle(style.TextFieldHoveredTexture); - this.HoveredColor.SetFromStyle(style.TextFieldHoveredColor); - this.TextOffsetX.SetFromStyle(style.TextFieldTextOffsetX); - this.CaretWidth.SetFromStyle(style.TextFieldCaretWidth); + this.TextScale = this.TextScale.OrStyle(style.TextScale); + this.Font = this.Font.OrStyle(style.Font); + this.Texture = this.Texture.OrStyle(style.TextFieldTexture); + this.HoveredTexture = this.HoveredTexture.OrStyle(style.TextFieldHoveredTexture); + this.HoveredColor = this.HoveredColor.OrStyle(style.TextFieldHoveredColor); + this.TextOffsetX = this.TextOffsetX.OrStyle(style.TextFieldTextOffsetX); + this.CaretWidth = this.CaretWidth.OrStyle(style.TextFieldCaretWidth); } private bool FilterText(ref string text, bool removeMismatching) { diff --git a/MLEM.Ui/Elements/Tooltip.cs b/MLEM.Ui/Elements/Tooltip.cs index f72a27d..b2b276b 100644 --- a/MLEM.Ui/Elements/Tooltip.cs +++ b/MLEM.Ui/Elements/Tooltip.cs @@ -76,12 +76,12 @@ namespace MLEM.Ui.Elements { /// protected override void InitStyle(UiStyle style) { base.InitStyle(style); - this.Texture.SetFromStyle(style.TooltipBackground); - this.MouseOffset.SetFromStyle(style.TooltipOffset); - this.Delay.SetFromStyle(style.TooltipDelay); - this.ChildPadding = this.ChildPadding.CopyFromStyle(style.TooltipChildPadding); + this.Texture = this.Texture.OrStyle(style.TooltipBackground); + this.MouseOffset = this.MouseOffset.OrStyle(style.TooltipOffset); + this.Delay = this.Delay.OrStyle(style.TooltipDelay); + this.ChildPadding = this.ChildPadding.OrStyle(style.TooltipChildPadding); if (this.Paragraph != null) { - this.Paragraph.TextColor.SetFromStyle(style.TooltipTextColor, 1); + this.Paragraph.TextColor = this.Paragraph.TextColor.OrStyle(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 87163a9..8106160 100644 --- a/MLEM.Ui/Style/StyleProp.cs +++ b/MLEM.Ui/Style/StyleProp.cs @@ -9,35 +9,30 @@ namespace MLEM.Ui.Style { /// Note that T implicitly converts to StyleProp{T} and vice versa. /// /// The type of style setting that this property stores - public struct StyleProp : IEquatable> { + public readonly struct StyleProp : IEquatable> { + + /// + /// The empty style property, with no and a priority of 0. + /// + public static StyleProp None => default; /// /// The currently applied style /// - public T Value { get; private set; } - private byte priority; + public readonly T Value; + private readonly byte priority; /// /// Creates a new style property with the given custom style and a priority of . + /// To create a style property with a lower priority, use on an existing priority, or use . /// /// The custom style to apply - public StyleProp(T value) { - this.priority = byte.MaxValue; - this.Value = value; + public StyleProp(T value) : this(value, byte.MaxValue) { } - /// - /// 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.priority) { - this.Value = value; - this.priority = priority; - } + private StyleProp(T value, byte priority) { + this.Value = value; + this.priority = priority; } /// @@ -46,12 +41,19 @@ namespace MLEM.Ui.Style { /// /// 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; + /// The style with the higher priority + public StyleProp OrStyle(T value, byte priority = 0) { + return this.OrStyle(new StyleProp(value, priority)); + } + + /// + /// Chooses and returns the style property with the higher priority, out of this value and . + /// This allows this property to be overridden by custom style settings using or a higher priority. + /// + /// The style property to compare with + /// The style property with the higher priority + public StyleProp OrStyle(StyleProp other) { + return other.priority >= this.priority ? other : this; } /// @@ -64,7 +66,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 a55e76e..528bb71 100644 --- a/Tests/UiTests.cs +++ b/Tests/UiTests.cs @@ -77,15 +77,15 @@ namespace Tests { public void TestStyle() { var style = new StyleProp(); Assert.AreEqual(null, style.Value); - style.SetFromStyle("from style"); + style = style.OrStyle("from style"); Assert.AreEqual("from style", style.Value); style = "custom"; Assert.AreEqual("custom", style.Value); - style.SetFromStyle("from style again"); + style = style.OrStyle("from style again"); Assert.AreEqual("custom", style.Value); - var copy = style.CopyFromStyle("copy from style", byte.MaxValue); - var weakCopy = style.CopyFromStyle("weak copy"); + var copy = style.OrStyle("copy from style", byte.MaxValue); + var weakCopy = style.OrStyle("weak copy"); Assert.AreEqual("copy from style", copy.Value); Assert.AreEqual("custom", weakCopy.Value); Assert.AreEqual("custom", style.Value);