using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using MLEM.Font;
using MLEM.Formatting;
using MLEM.Formatting.Codes;
using MLEM.Misc;
using MLEM.Sound;
using MLEM.Textures;
using MLEM.Ui.Elements;
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.
/// Additional styles for built-in or custom element types can easily be added using .
///
public class UiStyle : GenericDataHolder {
///
/// The texture that is rendered on top of the
///
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;
///
/// The texture that the element uses when it is moused over ()
/// Note that, if you just want to change the button's color when hovered, use .
///
public NinePatch ButtonHoveredTexture;
///
/// The color that the element renders with when it is moused over ()
///
public Color ButtonHoveredColor = Color.LightGray;
///
/// The texture that the element uses when it
///
public NinePatch ButtonDisabledTexture;
///
/// The color that the element uses when it
///
public Color ButtonDisabledColor = Color.Gray;
///
/// The texture that the element uses
///
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);
///
/// The amount that a 's scrollable area is moved per single movement of the scroll wheel
///
public float PanelStepPerScroll = 10;
///
/// The size of the scroller of a 's scroll bar
///
public Vector2 PanelScrollerSize = new Vector2(5, 10);
///
/// The amount of pixels of room there should be between a 's scroll bar and the rest of its content
///
public float PanelScrollBarOffset = 1;
///
/// The texture that the element uses
///
public NinePatch TextFieldTexture;
///
/// The texture that the element uses when it is moused over ()
///
public NinePatch TextFieldHoveredTexture;
///
/// The color that the renders with when it is moused over ()
///
public Color TextFieldHoveredColor = Color.LightGray;
///
/// The x position that a 's text should start rendering at, based on the x position of the text field
///
public float TextFieldTextOffsetX = 4;
///
/// The width that a 's caret should render with
///
public float TextFieldCaretWidth = 0.5F;
///
/// The background texture that the element uses
///
public NinePatch ScrollBarBackground;
///
/// The texture that the scroll indicator of the element uses
///
public NinePatch ScrollBarScrollerTexture;
///
/// Whether or not a should use smooth scrolling
///
public bool ScrollBarSmoothScrolling;
///
/// The factor with which a 's smooth scrolling happens
///
public float ScrollBarSmoothScrollFactor = 0.75F;
///
/// The texture that the element uses
///
public NinePatch CheckboxTexture;
///
/// The texture that the element uses when it is moused over ()
///
public NinePatch CheckboxHoveredTexture;
///
/// The color that the element renders with when it is moused over ()
///
public Color CheckboxHoveredColor = Color.LightGray;
///
/// The texture that the element uses when it .
///
public NinePatch CheckboxDisabledTexture;
///
/// The color that the element uses when it .
///
public Color CheckboxDisabledColor = Color.Gray;
///
/// The texture that the element renders on top of its regular texture when it is
///
public TextureRegion CheckboxCheckmark;
///
/// The width of the space between a and its
///
public float CheckboxTextOffsetX = 2;
///
/// The texture that the element uses
///
public NinePatch RadioTexture;
///
/// The texture that the element uses when it is moused over ()
///
public NinePatch RadioHoveredTexture;
///
/// The color that the element renders with when it is moused over ()
///
public Color RadioHoveredColor = Color.LightGray;
///
/// The texture that the renders on top of its regular texture when it is
///
public TextureRegion RadioCheckmark;
///
/// The texture that the uses for its background
///
public NinePatch TooltipBackground;
///
/// The offset of the element's top left corner from the mouse position
///
public Vector2 TooltipOffset = new Vector2(8, 16);
///
/// The offset of the element's top center coordinate from the bottom center of the element snapped to when is true.
///
public Vector2 TooltipAutoNavOffset = new Vector2(0, 8);
///
/// The color that the text of a should have
///
public Color TooltipTextColor = Color.White;
///
/// The amount of time that the mouse has to be over an element with a for the tooltip to appear
///
public TimeSpan TooltipDelay = TimeSpan.Zero;
///
/// The width of a 's default text
///
public float TooltipTextWidth = 50;
///
/// The to apply to a by default
///
public Padding TooltipChildPadding = new Vector2(2);
///
/// The texture that the element uses for its background
///
public NinePatch ProgressBarTexture;
///
/// The color that the element renders with
///
public Color ProgressBarColor = Color.White;
///
/// The padding that the uses for its progress texture ()
///
public Vector2 ProgressBarProgressPadding = new Vector2(1);
///
/// The texture that the uses for displaying its progress
///
public NinePatch ProgressBarProgressTexture;
///
/// The color that the renders its progress texture with
///
public Color ProgressBarProgressColor = Color.Red;
///
/// The font that and other elements should use for rendering.
/// Note that, to specify a bold and italic font for , you should use and .
///
public GenericFont Font;
///
/// The scale that text should be rendered with in and other elements
///
public float TextScale = 1;
///
/// The color that the text of a should have
///
public Color TextColor = Color.White;
///
/// The that a should use by default.
///
public TextAlignment TextAlignment;
///
/// The that should be played when an element's and events are called.
/// Note that this sound is only played if the callbacks have any subscribers.
///
public SoundEffectInfo ActionSound;
///
/// The color that a 's codes should have.
/// This value is passed to .
///
public Color? LinkColor = Color.CornflowerBlue;
///
/// A set of additional fonts that can be used for the <f FontName> formatting code
///
public Dictionary AdditionalFonts = new Dictionary();
private readonly Dictionary> elementStyles = new Dictionary>();
///
/// Creates a new set of style settings with the default values.
///
public UiStyle() {}
///
/// Creates a new set of style settings with values inherited from the given style settings.
///
/// The original style settings, to copy into the new instance.
public UiStyle(UiStyle original) {
this.SelectionIndicator = original.SelectionIndicator;
this.MouseEnterAnimation = original.MouseEnterAnimation;
this.MouseExitAnimation = original.MouseExitAnimation;
this.ButtonTexture = original.ButtonTexture;
this.ButtonHoveredTexture = original.ButtonHoveredTexture;
this.ButtonHoveredColor = original.ButtonHoveredColor;
this.ButtonDisabledTexture = original.ButtonDisabledTexture;
this.ButtonDisabledColor = original.ButtonDisabledColor;
this.PanelTexture = original.PanelTexture;
this.PanelColor = original.PanelColor;
this.PanelChildPadding = original.PanelChildPadding;
this.PanelStepPerScroll = original.PanelStepPerScroll;
this.PanelScrollerSize = original.PanelScrollerSize;
this.PanelScrollBarOffset = original.PanelScrollBarOffset;
this.TextFieldTexture = original.TextFieldTexture;
this.TextFieldHoveredTexture = original.TextFieldHoveredTexture;
this.TextFieldHoveredColor = original.TextFieldHoveredColor;
this.TextFieldTextOffsetX = original.TextFieldTextOffsetX;
this.TextFieldCaretWidth = original.TextFieldCaretWidth;
this.ScrollBarBackground = original.ScrollBarBackground;
this.ScrollBarScrollerTexture = original.ScrollBarScrollerTexture;
this.ScrollBarSmoothScrolling = original.ScrollBarSmoothScrolling;
this.ScrollBarSmoothScrollFactor = original.ScrollBarSmoothScrollFactor;
this.CheckboxTexture = original.CheckboxTexture;
this.CheckboxHoveredTexture = original.CheckboxHoveredTexture;
this.CheckboxHoveredColor = original.CheckboxHoveredColor;
this.CheckboxDisabledTexture = original.CheckboxDisabledTexture;
this.CheckboxDisabledColor = original.CheckboxDisabledColor;
this.CheckboxCheckmark = original.CheckboxCheckmark;
this.CheckboxTextOffsetX = original.CheckboxTextOffsetX;
this.RadioTexture = original.RadioTexture;
this.RadioHoveredTexture = original.RadioHoveredTexture;
this.RadioHoveredColor = original.RadioHoveredColor;
this.RadioCheckmark = original.RadioCheckmark;
this.TooltipBackground = original.TooltipBackground;
this.TooltipOffset = original.TooltipOffset;
this.TooltipAutoNavOffset = original.TooltipAutoNavOffset;
this.TooltipTextColor = original.TooltipTextColor;
this.TooltipDelay = original.TooltipDelay;
this.TooltipTextWidth = original.TooltipTextWidth;
this.TooltipChildPadding = original.TooltipChildPadding;
this.ProgressBarTexture = original.ProgressBarTexture;
this.ProgressBarColor = original.ProgressBarColor;
this.ProgressBarProgressPadding = original.ProgressBarProgressPadding;
this.ProgressBarProgressTexture = original.ProgressBarProgressTexture;
this.ProgressBarProgressColor = original.ProgressBarProgressColor;
this.Font = original.Font;
this.TextScale = original.TextScale;
this.TextColor = original.TextColor;
this.TextAlignment = original.TextAlignment;
this.ActionSound = original.ActionSound;
this.LinkColor = original.LinkColor;
this.AdditionalFonts = new Dictionary(original.AdditionalFonts);
this.elementStyles = new Dictionary>(original.elementStyles);
}
///
/// 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;
}
}
}