1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-16 02:53:11 +01:00
MLEM/MLEM.Ui/Style/StyleProp.cs

85 lines
3.3 KiB
C#
Raw Normal View History

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
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>
public struct StyleProp<T> {
2020-05-22 17:02:24 +02:00
/// <summary>
/// The currently applied style
/// </summary>
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>
public void SetFromStyle(T value) {
if (!this.isCustom) {
this.Value = value;
}
}
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>
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>
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);
}
}
}