From f652854c1d5e215da520b487267fdd0a070213b5 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 14 Jun 2023 14:54:49 +0200 Subject: [PATCH] Added AddCustomStyle and ApplyCustomStyle to UiStyle to allow for easy custom styling of elements --- CHANGELOG.md | 1 + MLEM.Ui/Elements/Element.cs | 3 +++ MLEM.Ui/Elements/Panel.cs | 1 + MLEM.Ui/Parsers/UiParser.cs | 4 +-- MLEM.Ui/Style/UiStyle.cs | 49 ++++++++++++++++++++++++++++++++++++- 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdd8d35..37d3e6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ Removals Additions - Added AutoInlineCenter and AutoInlineBottom anchors - Added UiAnimation system +- Added AddCustomStyle and ApplyCustomStyle to UiStyle to allow for easy custom styling of elements Improvements - Increased Element area calculation recursion limit to 64 diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index e97b1c7..3daa80d 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -1290,8 +1290,11 @@ namespace MLEM.Ui.Elements { this.SelectionIndicator = this.SelectionIndicator.OrStyle(style.SelectionIndicator); this.ActionSound = this.ActionSound.OrStyle(style.ActionSound); this.SecondActionSound = this.SecondActionSound.OrStyle(style.ActionSound); + this.MouseEnterAnimation = this.MouseEnterAnimation.OrStyle(style.MouseEnterAnimation); + this.MouseExitAnimation = this.MouseExitAnimation.OrStyle(style.MouseExitAnimation); this.System?.InvokeOnElementStyleInit(this); + style.ApplyCustomStyle(this); } /// diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index d523b17..7ae328c 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -212,6 +212,7 @@ namespace MLEM.Ui.Elements { protected override void InitStyle(UiStyle style) { base.InitStyle(style); this.Texture = this.Texture.OrStyle(style.PanelTexture); + this.DrawColor = this.DrawColor.OrStyle(style.PanelColor); this.StepPerScroll = this.StepPerScroll.OrStyle(style.PanelStepPerScroll); this.ScrollerSize = this.ScrollerSize.OrStyle(style.PanelScrollerSize); this.ScrollBarOffset = this.ScrollBarOffset.OrStyle(style.PanelScrollBarOffset); diff --git a/MLEM.Ui/Parsers/UiParser.cs b/MLEM.Ui/Parsers/UiParser.cs index e787fed..b1c3b50 100644 --- a/MLEM.Ui/Parsers/UiParser.cs +++ b/MLEM.Ui/Parsers/UiParser.cs @@ -109,11 +109,11 @@ namespace MLEM.Ui.Parsers { /// /// Specifies an action to be invoked when a new element with the given is parsed in or . - /// These actions can be used to modify the style properties of the created elements. + /// These actions can be used to modify the style properties of the created elements similarly to . /// /// The element types that should be styled. Can be a combined flag. /// The action that styles the elements with the given element type. - /// Whether the function should be added to the existing style settings, or replace them. + /// Whether the function should be added to the existing style settings rather than replacing them. /// The type of elements that the given flags are expected to be. /// This parser, for chaining. public UiParser Style(ElementType types, Action style, bool add = false) where T : Element { diff --git a/MLEM.Ui/Style/UiStyle.cs b/MLEM.Ui/Style/UiStyle.cs index e5c3092..3b2ee25 100644 --- a/MLEM.Ui/Style/UiStyle.cs +++ b/MLEM.Ui/Style/UiStyle.cs @@ -13,7 +13,7 @@ namespace MLEM.Ui.Style { /// /// The style settings for a . /// Each uses these style settings by default, however you can also change these settings per element using the elements' individual style settings. - /// Note that this class is a , meaning additional styles for custom components can easily be added using + /// Additional styles for built-in or custom element types can easily be added using . /// public class UiStyle : GenericDataHolder { @@ -22,6 +22,14 @@ namespace MLEM.Ui.Style { /// public NinePatch SelectionIndicator; /// + /// A that is played when the mouse enters an element. + /// + public UiAnimation MouseEnterAnimation; + /// + /// A that is played when the mouse exists an element. + /// + public UiAnimation MouseExitAnimation; + /// /// The texture that the element uses /// public NinePatch ButtonTexture; @@ -47,6 +55,10 @@ namespace MLEM.Ui.Style { /// public NinePatch PanelTexture; /// + /// The color that the element draws with. + /// + public Color PanelColor = Color.White; + /// /// The to apply to a by default /// public Padding PanelChildPadding = new Vector2(5); @@ -222,5 +234,40 @@ namespace MLEM.Ui.Style { /// public Dictionary AdditionalFonts = new Dictionary(); + private readonly Dictionary> elementStyles = new Dictionary>(); + + /// + /// Adds an action to the given type that allows applying any kind of custom styling or behavior to it. + /// Custom styles added in this manner can be applied to an element using . + /// + /// The style action to add. + /// Whether the function should be added to the existing style settings rather than replacing them. + /// The type that the should apply to. + public void AddCustomStyle(Action style, bool add = false) where T : Element { + if (add && this.elementStyles.ContainsKey(typeof(T))) { + this.elementStyles[typeof(T)] += Action; + } else { + this.elementStyles[typeof(T)] = Action; + } + + void Action(Element e) { + style.Invoke((T) e); + } + } + + /// + /// Applies a set of custom styling actions to the given which were added through . + /// This method is automatically invoked in . + /// + /// The element to apply custom styling to. + /// Whether any custom styling exists for the given . + public bool ApplyCustomStyle(Element element) { + if (this.elementStyles.TryGetValue(element.GetType(), out var style)) { + style?.Invoke(element); + return true; + } + return false; + } + } }