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

100 lines
4.5 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 readonly struct StyleProp<T> {
2021-12-21 11:54:32 +01:00
/// <summary>
/// The empty style property, with no <see cref="Value"/> and a priority of 0.
/// </summary>
public static StyleProp<T> None => default;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The currently applied style
/// </summary>
2021-12-21 11:54:32 +01:00
public readonly T Value;
private readonly byte priority;
2020-05-22 17:02:24 +02:00
/// <summary>
/// Creates a new style property with the given custom style and a priority of <see cref="byte.MaxValue"/>.
2021-12-21 11:54:32 +01:00
/// To create a style property with a lower priority, use <see cref="OrStyle(T,byte)"/> on an existing priority, or use <see cref="None"/>.
2020-05-22 17:02:24 +02:00
/// </summary>
/// <param name="value">The custom style to apply</param>
2021-12-28 14:56:11 +01:00
public StyleProp(T value) : this(value, byte.MaxValue) {}
2021-12-21 11:54:32 +01:00
private StyleProp(T value, byte priority) {
2019-11-05 13:28:41 +01:00
this.Value = value;
2021-12-21 11:54:32 +01:00
this.priority = priority;
2019-11-05 13:28:41 +01:00
}
2020-05-22 17:02:24 +02:00
/// <summary>
2021-12-21 11:54:32 +01:00
/// Creates a copy of this style property and sets its value and marks it as being set by a <see cref="UiStyle"/> if it doesn't have a custom value yet.
/// This allows this property to be overridden by custom style settings using <see cref="StyleProp{T}(T)"/> or a higher <paramref name="priority"/>.
2020-05-22 17:02:24 +02:00
/// </summary>
/// <param name="value">The style to apply</param>
2021-12-21 11:54:32 +01:00
/// <param name="priority">The priority that the style value has. Higher priority style values will override lower priority style values.</param>
/// <returns>The style with the higher priority</returns>
public StyleProp<T> OrStyle(T value, byte priority = 0) {
return this.OrStyle(new StyleProp<T>(value, priority));
}
2020-05-22 17:02:24 +02:00
/// <summary>
2021-12-21 11:54:32 +01:00
/// Chooses and returns the style property with the higher priority, out of this value and <paramref name="other"/>.
/// This allows this property to be overridden by custom style settings using <see cref="StyleProp{T}(T)"/> or a higher priority.
2020-05-22 17:02:24 +02:00
/// </summary>
2021-12-21 11:54:32 +01:00
/// <param name="other">The style property to compare with</param>
/// <returns>The style property with the higher priority</returns>
public StyleProp<T> OrStyle(StyleProp<T> other) {
return other.priority >= this.priority ? other : this;
}
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>
2021-12-21 11:54:32 +01:00
/// Returns whether this style property has a value assigned to it using <see cref="OrStyle(StyleProp{T})"/> or <see cref="StyleProp{T}(T)"/>.
2020-05-22 17:02:24 +02:00
/// </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
}
2021-11-30 11:46:06 +01:00
/// <summary>Returns the fully qualified type name of this instance.</summary>
/// <returns>The fully qualified type name.</returns>
public override string ToString() {
2021-10-30 15:33:38 +02:00
return this.Value?.ToString();
}
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);
}
}
2022-06-17 18:23:47 +02:00
}