2019-11-05 13:28:41 +01:00
|
|
|
using System.Collections.Generic;
|
2020-05-22 17:02:24 +02:00
|
|
|
using MLEM.Ui.Elements;
|
2019-11-05 13:28:41 +01:00
|
|
|
|
2019-10-14 21:28:12 +02:00
|
|
|
namespace MLEM.Ui.Style {
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// A struct used by <see cref="Element"/> to store style properties.
|
|
|
|
/// This is a helper struct that allows default style settings from <see cref="UiStyle"/> to be overridden by custom user settings easily.
|
|
|
|
/// Note that <c>T</c> implicitly converts to <c>StyleProp{T}</c> and vice versa.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type of style setting that this property stores</typeparam>
|
2019-10-14 21:28:12 +02:00
|
|
|
public struct StyleProp<T> {
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The currently applied style
|
|
|
|
/// </summary>
|
2019-10-14 21:28:12 +02:00
|
|
|
public T Value { get; private set; }
|
|
|
|
private bool isCustom;
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new style property with the given custom style.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="value">The custom style to apply</param>
|
2019-11-05 13:28:41 +01:00
|
|
|
public StyleProp(T value) {
|
|
|
|
this.isCustom = true;
|
|
|
|
this.Value = value;
|
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Sets this style property's value and marks it as being set by a <see cref="UiStyle"/>.
|
|
|
|
/// This allows this property to be overridden by custom style settings using <see cref="Set"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="value">The style to apply</param>
|
2019-10-14 21:28:12 +02:00
|
|
|
public void SetFromStyle(T value) {
|
2020-02-06 01:51:41 +01:00
|
|
|
if (!this.isCustom) {
|
2019-10-14 21:28:12 +02:00
|
|
|
this.Value = value;
|
2020-02-06 01:51:41 +01:00
|
|
|
}
|
2019-10-14 21:28:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Sets this style property's value and marks it as being custom.
|
|
|
|
/// This causes <see cref="SetFromStyle"/> not to override the style value through a <see cref="UiStyle"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="value"></param>
|
2019-10-14 21:28:12 +02:00
|
|
|
public void Set(T value) {
|
|
|
|
this.isCustom = true;
|
|
|
|
this.Value = value;
|
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the current style value or, if <see cref="HasValue"/> is false, the given default value.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="def">The default to return if this style property has no value</param>
|
|
|
|
/// <returns>The current value, or the default</returns>
|
2019-11-05 13:28:41 +01:00
|
|
|
public T OrDefault(T def) {
|
2020-02-06 17:36:51 +01:00
|
|
|
return this.HasValue() ? this.Value : def;
|
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns whether this style property has a value assigned to it using <see cref="SetFromStyle"/> or <see cref="Set"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Whether this style property has a value</returns>
|
2020-02-06 17:36:51 +01:00
|
|
|
public bool HasValue() {
|
|
|
|
return !EqualityComparer<T>.Default.Equals(this.Value, default);
|
2019-11-05 13:28:41 +01:00
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Implicitly converts a style property to its value.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="prop">The property to convert</param>
|
|
|
|
/// <returns>The style that the style property holds</returns>
|
2019-10-14 21:28:12 +02:00
|
|
|
public static implicit operator T(StyleProp<T> prop) {
|
|
|
|
return prop.Value;
|
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Implicitly converts a style to a style property.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="prop">The property to convert</param>
|
|
|
|
/// <returns>A style property with the given style value</returns>
|
2019-11-05 13:28:41 +01:00
|
|
|
public static implicit operator StyleProp<T>(T prop) {
|
|
|
|
return new StyleProp<T>(prop);
|
|
|
|
}
|
|
|
|
|
2019-10-14 21:28:12 +02:00
|
|
|
}
|
|
|
|
}
|