using System.Collections.Generic; using MLEM.Ui.Elements; namespace MLEM.Ui.Style { /// /// A struct used by to store style properties. /// This is a helper struct that allows default style settings from to be overridden by custom user settings easily. /// Note that T implicitly converts to StyleProp{T} and vice versa. /// /// The type of style setting that this property stores public struct StyleProp { /// /// The currently applied style /// public T Value { get; private set; } private byte lastSetPriority; /// /// Creates a new style property with the given custom style. /// /// The custom style to apply public StyleProp(T value) { this.lastSetPriority = byte.MaxValue; this.Value = value; } /// /// 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 . /// /// 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; this.lastSetPriority = priority; } } /// /// Sets this style property's value and marks it as being custom. /// This causes not to override the style value through a . /// /// public void Set(T value) { this.lastSetPriority = byte.MaxValue; this.Value = value; } /// /// Returns the current style value or, if is false, the given default value. /// /// The default to return if this style property has no value /// The current value, or the default public T OrDefault(T def) { return this.HasValue() ? this.Value : def; } /// /// Returns whether this style property has a value assigned to it using or . /// /// Whether this style property has a value public bool HasValue() { return !EqualityComparer.Default.Equals(this.Value, default); } /// Returns the fully qualified type name of this instance. /// The fully qualified type name. public override string ToString() { return this.Value?.ToString(); } /// /// Implicitly converts a style property to its value. /// /// The property to convert /// The style that the style property holds public static implicit operator T(StyleProp prop) { return prop.Value; } /// /// Implicitly converts a style to a style property. /// /// The property to convert /// A style property with the given style value public static implicit operator StyleProp(T prop) { return new StyleProp(prop); } } }