diff --git a/MiraAPI.Example/ExampleColors.cs b/MiraAPI.Example/ExampleColors.cs index 297a370e..0b9c4b6e 100644 --- a/MiraAPI.Example/ExampleColors.cs +++ b/MiraAPI.Example/ExampleColors.cs @@ -5,20 +5,14 @@ using UnityEngine; namespace MiraAPI.Example; -[RegisterCustomColors] +[RegisterCustomColors("color.")] public static class ExampleColors { - public static CustomColor Cerulean { get; } = new("color.cerulean", new Color(0.0f, 0.48f, 0.65f)) - { - ColorBrightness = CustomColorBrightness.Lighter, - }; + public static CustomColor Cerulean { get; } = new("cerulean", new Color(0.0f, 0.48f, 0.65f), CustomColorBrightness.Lighter); - public static CustomColor Rose { get; } = new("color.rose", new Color(0.98f, 0.26f, 0.62f)); + public static CustomColor Rose { get; } = new("rose", new Color(0.98f, 0.26f, 0.62f)); - public static CustomColor Gold { get; } = new("color.gold", new Color(1.0f, 0.84f, 0.0f)); + public static CustomColor Gold { get; } = new("gold", new Color(1.0f, 0.84f, 0.0f)); - public static VariableColor Rainbow { get; } = new("color.rainbow", t => new HsbColor(MathUtilities.PingPong(t, 1f, 0.3f), 1f, 1f)) - { - ColorBrightness = CustomColorBrightness.Lighter, - }; + public static VariableColor Rainbow { get; } = new("rainbow", t => new HsbColor(MathUtilities.PingPong(t, 1f, 0.3f), 1f, 1f), CustomColorBrightness.Lighter); } diff --git a/MiraAPI/Colors/CustomColor.cs b/MiraAPI/Colors/CustomColor.cs index f3c38395..836c1fec 100644 --- a/MiraAPI/Colors/CustomColor.cs +++ b/MiraAPI/Colors/CustomColor.cs @@ -1,4 +1,5 @@ -using System.Diagnostics.CodeAnalysis; +using System; +using System.Diagnostics.CodeAnalysis; using MiraAPI.Translation; using MiraAPI.Utilities; using UnityEngine; @@ -8,45 +9,41 @@ namespace MiraAPI.Colors; /// /// Represents a custom color with a main color and a shadow color. /// -/// The name of the option. +/// The name of the option as a value. +/// The name of the option as a value. /// The main color. /// The shadow color. +/// The brightness type of the color. [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "Internal code.")] -[SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1513:Closing brace should be followed by blank line", Justification = "Looks weird.")] -public class CustomColor(StringNames name, Color32 mainColor, Color32 shadowColor) +public class CustomColor(StringNames? nameId, string? rawId, Color32 mainColor, Color32 shadowColor, CustomColorBrightness brightness) { - /// - /// Gets or sets the main color. - /// - public Color32 MainColor - { - get; - set - { - field = value; - InternalMainColor = value; - } - } - = mainColor; + private StringNames? name = nameId; + private CustomColorBrightness colorBrightness = brightness; /// - /// Gets or sets the shadow color. + /// Gets the main color. /// - public Color32 ShadowColor - { - get; - set - { - field = value; - InternalShadowColor = value; - } - } - = shadowColor; + public Color32 MainColor { get; } = mainColor; /// - /// Gets or sets the name of the color. + /// Gets the shadow color. /// - public StringNames Name { get; set; } = name; + public Color32 ShadowColor { get; } = shadowColor; + + /// + /// Gets the name of the color. + /// + public StringNames Name + { + get + { + if (!name.HasValue && !string.IsNullOrEmpty(RawId)) + name = MiraLocaleManager.GetOrCreateLocaleString(RawId); + + return name.GetValueOrDefault(); + } + internal set => name = value; + } /// /// Gets the id of the color. @@ -54,28 +51,104 @@ public class CustomColor(StringNames name, Color32 mainColor, Color32 shadowColo public int ColorId { get; internal set; } = -1; /// - /// Gets or sets whether the color is lighter or darker. Can be used to help with colorblind stuff. + /// Gets or sets whether the color is lighter or darker. /// - public CustomColorBrightness ColorBrightness { get; set; } = CustomColorBrightness.Darker; + public CustomColorBrightness ColorBrightness + { + get => colorBrightness; + [Obsolete("Use the constructor to set ColorBrightness instead. This will be removed in a future version.")] + set => colorBrightness = value; + } + internal string? RawId = rawId; internal Color InternalMainColor = mainColor; internal Color InternalShadowColor = shadowColor; - /// - public CustomColor(StringNames name, Color32 mainColor) - : this(name, mainColor, mainColor.DarkenAbsolute(60)) + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The main color. + /// The shadow color. + /// The brightness type of the color. + public CustomColor(StringNames nameId, Color32 mainColor, Color32 shadowColor, CustomColorBrightness brightness) + : this(nameId, null, mainColor, shadowColor, brightness) { } - /// - public CustomColor(string name, Color32 mainColor) - : this(name, mainColor, mainColor.DarkenAbsolute(60)) + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The main color. + /// The brightness type of the color. + public CustomColor(StringNames nameId, Color32 mainColor, CustomColorBrightness brightness) + : this(nameId, null, mainColor, mainColor.DarkenAbsolute(60), brightness) { } - /// - public CustomColor(string name, Color32 mainColor, Color32 shadowColor) - : this(MiraLocaleManager.GetOrCreateLocaleString(name), mainColor, shadowColor) + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The main color. + /// The shadow color. + /// The brightness type of the color. + public CustomColor(string rawId, Color32 mainColor, Color32 shadowColor, CustomColorBrightness brightness) + : this(null, rawId, mainColor, shadowColor, brightness) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The main color. + /// The brightness type of the color. + public CustomColor(string rawId, Color32 mainColor, CustomColorBrightness brightness) + : this(null, rawId, mainColor, mainColor.DarkenAbsolute(60), brightness) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The main color. + /// The shadow color. + public CustomColor(StringNames nameId, Color32 mainColor, Color32 shadowColor) + : this(nameId, null, mainColor, shadowColor, CustomColorBrightness.Darker) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The main color. + public CustomColor(StringNames nameId, Color32 mainColor) + : this(nameId, null, mainColor, mainColor.DarkenAbsolute(60), CustomColorBrightness.Darker) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The main color. + /// The shadow color. + public CustomColor(string rawId, Color32 mainColor, Color32 shadowColor) + : this(null, rawId, mainColor, shadowColor, CustomColorBrightness.Darker) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The main color. + public CustomColor(string rawId, Color32 mainColor) + : this(null, rawId, mainColor, mainColor.DarkenAbsolute(60), CustomColorBrightness.Darker) { } } diff --git a/MiraAPI/Colors/PaletteManager.cs b/MiraAPI/Colors/PaletteManager.cs index 760822fc..b05c30fd 100644 --- a/MiraAPI/Colors/PaletteManager.cs +++ b/MiraAPI/Colors/PaletteManager.cs @@ -50,7 +50,9 @@ public static class PaletteManager var colors = CustomColors.Select(x => x.MainColor).ToArray(); var shadowColors = CustomColors.Select(x => x.ShadowColor).ToArray(); var stringNames = CustomColors.Select(x => x.Name).ToArray(); + var newColorId = originalLength; + foreach (var color in CustomColors) { color.ColorId = newColorId++; diff --git a/MiraAPI/Colors/RegisterCustomColorsAttribute.cs b/MiraAPI/Colors/RegisterCustomColorsAttribute.cs index 63565829..899957c9 100644 --- a/MiraAPI/Colors/RegisterCustomColorsAttribute.cs +++ b/MiraAPI/Colors/RegisterCustomColorsAttribute.cs @@ -6,4 +6,18 @@ namespace MiraAPI.Colors; /// Used to mark a class for registration. /// [AttributeUsage(AttributeTargets.Class)] -public class RegisterCustomColorsAttribute : Attribute; +public class RegisterCustomColorsAttribute(string idPrefix) : Attribute +{ + /// + /// Gets the translation ID prefix for all of the colors within the container type. + /// + public string IdPrefix { get; } = idPrefix; + + /// + /// Initializes a new instance of the class. + /// + public RegisterCustomColorsAttribute() + : this(string.Empty) + { + } +} diff --git a/MiraAPI/Colors/VariableColor.cs b/MiraAPI/Colors/VariableColor.cs index 84a38358..7c61e1d5 100644 --- a/MiraAPI/Colors/VariableColor.cs +++ b/MiraAPI/Colors/VariableColor.cs @@ -1,6 +1,5 @@ using System; using MiraAPI.LocalSettings; -using MiraAPI.Translation; using MiraAPI.Utilities; using UnityEngine; @@ -9,40 +8,189 @@ namespace MiraAPI.Colors; /// /// Represents a dynamic custom color that changes over time. /// -/// The name of the color. +/// The name of the option as a value. +/// The name of the option as a value. /// The function to evaluate the dynamic main color. /// The function to evaluate the dynamic shadow color. /// The static main color used when dynamic colors are disabled. /// The static shadow color used when dynamic colors are disabled. /// The default time that is used to automatically fill in the static colors when the latter is not provided. -public sealed class VariableColor(StringNames name, Func mainEvaluator, Func? shadowEvaluator, Color32? mainColor = null, Color32? shadowColor = null, float defaultTime = 0f) - : CustomColor(name, mainColor ?? mainEvaluator(defaultTime), shadowColor ?? shadowEvaluator?.Invoke(defaultTime) ?? (mainColor ?? mainEvaluator(defaultTime)).DarkenAbsolute(60)) +/// The brightness type of the color. +public sealed class VariableColor( + StringNames? nameId, + string? rawId, + Func mainEvaluator, + Func? shadowEvaluator, + Color32? mainColor, + Color32? shadowColor, + float defaultTime, + CustomColorBrightness brightness) + : CustomColor( + nameId, + rawId, + mainColor ?? mainEvaluator(defaultTime), + shadowColor ?? shadowEvaluator?.Invoke(defaultTime) ?? (mainColor ?? mainEvaluator(defaultTime)).DarkenAbsolute(60), + brightness) { /// - /// Gets or sets a delegate that takes in a time value and returns the dynamic main color. + /// Gets a delegate that takes in a time value and returns the dynamic main color. /// - public Func MainEvaluator { get; set; } = mainEvaluator; + public Func MainEvaluator { get; } = mainEvaluator; /// - /// Gets or sets a delegate that takes in a time value and returns the dynamic shadow color. + /// Gets a delegate that takes in a time value and returns the dynamic shadow color. /// - public Func ShadowEvaluator { get; set; } = shadowEvaluator ?? (t => mainEvaluator(t).DarkenAbsolute(0.24f)); + public Func ShadowEvaluator { get; } = shadowEvaluator ?? (t => mainEvaluator(t).DarkenAbsolute(0.24f)); - /// - public VariableColor(StringNames name, Func mainEvaluator, Color32? mainColor = null, float defaultTime = 0f) - : this(name, mainEvaluator, null, mainColor, null, defaultTime) + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The function to evaluate the dynamic main color. + /// The function to evaluate the dynamic shadow color. + /// The static main color used when dynamic colors are disabled. + /// The static shadow color used when dynamic colors are disabled. + /// The default time that is used to automatically fill in the static colors when the latter is not provided. + /// The brightness type of the color. + public VariableColor( + StringNames nameId, + Func mainEvaluator, + Func? shadowEvaluator, + CustomColorBrightness brightness, + Color32? mainColor = null, + Color32? shadowColor = null, + float defaultTime = 0f) + : this(nameId, null, mainEvaluator, shadowEvaluator, mainColor, shadowColor, defaultTime, brightness) { } - /// - public VariableColor(string name, Func mainEvaluator, Color32? mainColor = null, float defaultTime = 0f) - : this(name, mainEvaluator, null, mainColor, null, defaultTime) + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The function to evaluate the dynamic main color. + /// The static main color used when dynamic colors are disabled. + /// The default time that is used to automatically fill in the static colors when the latter is not provided. + /// The brightness type of the color. + public VariableColor( + StringNames nameId, + Func mainEvaluator, + CustomColorBrightness brightness, + Color32? mainColor = null, + float defaultTime = 0f) + : this(nameId, null, mainEvaluator, null, mainColor, null, defaultTime, brightness) { } - /// - public VariableColor(string name, Func mainEvaluator, Func? shadowEvaluator, Color32? mainColor = null, Color32? shadowColor = null, float defaultTime = 0f) - : this(MiraLocaleManager.GetOrCreateLocaleString(name), mainEvaluator, shadowEvaluator, mainColor, shadowColor, defaultTime) + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The function to evaluate the dynamic main color. + /// The function to evaluate the dynamic shadow color. + /// The static main color used when dynamic colors are disabled. + /// The static shadow color used when dynamic colors are disabled. + /// The default time that is used to automatically fill in the static colors when the latter is not provided. + /// The brightness type of the color. + public VariableColor( + string rawId, + Func mainEvaluator, + Func? shadowEvaluator, + CustomColorBrightness brightness, + Color32? mainColor = null, + Color32? shadowColor = null, + float defaultTime = 0f) + : this(null, rawId, mainEvaluator, shadowEvaluator, mainColor, shadowColor, defaultTime, brightness) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The function to evaluate the dynamic main color. + /// The static main color used when dynamic colors are disabled. + /// The default time that is used to automatically fill in the static colors when the latter is not provided. + /// The brightness type of the color. + public VariableColor( + string rawId, + Func mainEvaluator, + CustomColorBrightness brightness, + Color32? mainColor = null, + float defaultTime = 0f) + : this(null, rawId, mainEvaluator, null, mainColor, null, defaultTime, brightness) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The function to evaluate the dynamic main color. + /// The function to evaluate the dynamic shadow color. + /// The static main color used when dynamic colors are disabled. + /// The static shadow color used when dynamic colors are disabled. + /// The default time that is used to automatically fill in the static colors when the latter is not provided. + public VariableColor( + StringNames nameId, + Func mainEvaluator, + Func? shadowEvaluator, + Color32? mainColor = null, + Color32? shadowColor = null, + float defaultTime = 0f) + : this(nameId, null, mainEvaluator, shadowEvaluator, mainColor, shadowColor, defaultTime, CustomColorBrightness.Darker) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The function to evaluate the dynamic main color. + /// The static main color used when dynamic colors are disabled. + /// The default time that is used to automatically fill in the static colors when the latter is not provided. + public VariableColor( + StringNames nameId, + Func mainEvaluator, + Color32? mainColor = null, + float defaultTime = 0f) + : this(nameId, null, mainEvaluator, null, mainColor, null, defaultTime, CustomColorBrightness.Darker) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The function to evaluate the dynamic main color. + /// The function to evaluate the dynamic shadow color. + /// The static main color used when dynamic colors are disabled. + /// The static shadow color used when dynamic colors are disabled. + /// The default time that is used to automatically fill in the static colors when the latter is not provided. + public VariableColor( + string rawId, + Func mainEvaluator, + Func? shadowEvaluator, + Color32? mainColor = null, + Color32? shadowColor = null, + float defaultTime = 0f) + : this(null, rawId, mainEvaluator, shadowEvaluator, mainColor, shadowColor, defaultTime, CustomColorBrightness.Darker) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The name of the option as a value. + /// The function to evaluate the dynamic main color. + /// The static main color used when dynamic colors are disabled. + /// The default time that is used to automatically fill in the static colors when the latter is not provided. + public VariableColor( + string rawId, + Func mainEvaluator, + Color32? mainColor = null, + float defaultTime = 0f) + : this(null, rawId, mainEvaluator, null, mainColor, null, defaultTime, CustomColorBrightness.Darker) { } diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs index 3214b099..9d4b47dd 100644 --- a/MiraAPI/PluginLoading/MiraPluginManager.cs +++ b/MiraAPI/PluginLoading/MiraPluginManager.cs @@ -305,7 +305,9 @@ public sealed class MiraPluginManager { try { - if (type.GetCustomAttribute() == null) + var attribute = type.GetCustomAttribute(); + + if (attribute == null) { return; } @@ -329,6 +331,11 @@ public sealed class MiraPluginManager continue; } + if (!string.IsNullOrEmpty(color.RawId)) + { + color.Name = MiraLocaleManager.GetOrCreateLocaleString(attribute.IdPrefix + color.RawId); + } + PaletteManager.CustomColors.Add(color); if (color is VariableColor)