1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-10 03:28:43 +02:00

Added AddCustomStyle and ApplyCustomStyle to UiStyle to allow for easy custom styling of elements

This commit is contained in:
Ell 2023-06-14 14:54:49 +02:00
parent d48b7e2e71
commit f652854c1d
5 changed files with 55 additions and 3 deletions

View file

@ -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

View file

@ -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);
}
/// <summary>

View file

@ -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);

View file

@ -109,11 +109,11 @@ namespace MLEM.Ui.Parsers {
/// <summary>
/// Specifies an action to be invoked when a new element with the given <see cref="ElementType"/> is parsed in <see cref="Parse"/> or <see cref="ParseInto"/>.
/// 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 <see cref="UiStyle.AddCustomStyle{T}"/>.
/// </summary>
/// <param name="types">The element types that should be styled. Can be a combined flag.</param>
/// <param name="style">The action that styles the elements with the given element type.</param>
/// <param name="add">Whether the <paramref name="style"/> function should be added to the existing style settings, or replace them.</param>
/// <param name="add">Whether the <paramref name="style"/> function should be added to the existing style settings rather than replacing them.</param>
/// <typeparam name="T">The type of elements that the given <see cref="ElementType"/> flags are expected to be.</typeparam>
/// <returns>This parser, for chaining.</returns>
public UiParser Style<T>(ElementType types, Action<T> style, bool add = false) where T : Element {

View file

@ -13,7 +13,7 @@ namespace MLEM.Ui.Style {
/// <summary>
/// The style settings for a <see cref="UiSystem"/>.
/// Each <see cref="Element"/> 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 <see cref="GenericDataHolder"/>, meaning additional styles for custom components can easily be added using <see cref="GenericDataHolder.SetData"/>
/// Additional styles for built-in or custom element types can easily be added using <see cref="AddCustomStyle{T}"/>.
/// </summary>
public class UiStyle : GenericDataHolder {
@ -22,6 +22,14 @@ namespace MLEM.Ui.Style {
/// </summary>
public NinePatch SelectionIndicator;
/// <summary>
/// A <see cref="UiAnimation"/> that is played when the mouse enters an element.
/// </summary>
public UiAnimation MouseEnterAnimation;
/// <summary>
/// A <see cref="UiAnimation"/> that is played when the mouse exists an element.
/// </summary>
public UiAnimation MouseExitAnimation;
/// <summary>
/// The texture that the <see cref="Button"/> element uses
/// </summary>
public NinePatch ButtonTexture;
@ -47,6 +55,10 @@ namespace MLEM.Ui.Style {
/// </summary>
public NinePatch PanelTexture;
/// <summary>
/// The color that the <see cref="Panel"/> element draws with.
/// </summary>
public Color PanelColor = Color.White;
/// <summary>
/// The <see cref="Element.ChildPadding"/> to apply to a <see cref="Panel"/> by default
/// </summary>
public Padding PanelChildPadding = new Vector2(5);
@ -222,5 +234,40 @@ namespace MLEM.Ui.Style {
/// </summary>
public Dictionary<string, GenericFont> AdditionalFonts = new Dictionary<string, GenericFont>();
private readonly Dictionary<Type, Action<Element>> elementStyles = new Dictionary<Type, Action<Element>>();
/// <summary>
/// Adds an action to the given <see cref="Element"/> type <typeparamref name="T"/> 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 <see cref="ApplyCustomStyle"/>.
/// </summary>
/// <param name="style">The style action to add.</param>
/// <param name="add">Whether the <paramref name="style"/> function should be added to the existing style settings rather than replacing them.</param>
/// <typeparam name="T">The <see cref="Element"/> type that the <paramref name="style"/> should apply to.</typeparam>
public void AddCustomStyle<T>(Action<T> 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);
}
}
/// <summary>
/// Applies a set of custom styling actions to the given <paramref name="element"/> which were added through <see cref="AddCustomStyle{T}"/>.
/// This method is automatically invoked in <see cref="Element.InitStyle"/>.
/// </summary>
/// <param name="element">The element to apply custom styling to.</param>
/// <returns>Whether any custom styling exists for the given <paramref name="element"/>.</returns>
public bool ApplyCustomStyle(Element element) {
if (this.elementStyles.TryGetValue(element.GetType(), out var style)) {
style?.Invoke(element);
return true;
}
return false;
}
}
}