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 bool isCustom;
///
/// Creates a new style property with the given custom style.
///
/// The custom style to apply
public StyleProp(T value) {
this.isCustom = true;
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
public void SetFromStyle(T value) {
if (!this.isCustom) {
this.Value = value;
}
}
///
/// 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.isCustom = true;
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);
}
///
/// 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);
}
}
}