custom colors API stuff

This commit is contained in:
XtraCube committed 2024-08-26 09:57:46 -04:00
1 parent 0e39612325
commit 6de53fb761
6 files changed
+67 -24

No files matched your search

+25 -8
View File
@@ -11,6 +11,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MiraAPI.Utilities;
namespace MiraAPI.PluginLoading;
@@ -123,19 +124,35 @@ internal class MiraPluginManager
{
foreach (var type in assembly.GetTypes())
{
foreach (FieldInfo field in type.GetFields())
if (type.GetCustomAttribute<RegisterColorsAttribute>() == null)
{
foreach (var attribute in field.GetCustomAttributes())
continue;
}
if (!type.IsStatic())
{
Logger<MiraApiPlugin>.Error($"Color class {type.Name} must be static.");
continue;
}
foreach (var property in type.GetProperties())
{
if (property.PropertyType != typeof(CustomColor))
{
if (attribute.GetType().BaseType == typeof(RegisterColorAttribute))
{
/* Palette.PlayerColors = Palette.PlayerColors.ToArray().AddToArray();
Palette.ShadowColors = Palette.ShadowColors.ToArray().AddToArray();
Palette.ColorNames = Palette.ColorNames.ToArray().AddToArray();*/
}
continue;
}
var color = (CustomColor)property.GetValue(null);
if (color == null)
{
Logger<MiraApiPlugin>.Error($"Color property {property.Name} in {type.Name} is null.");
continue;
}
PaletteManager.CustomColors.Add(color);
}
}
PaletteManager.RegisterAllColors();
}
private static void RegisterButtonAttribute(Assembly assembly)
+3 -10
View File
@@ -1,12 +1,12 @@
using UnityEngine;
namespace MiraAPI.Utilities;
public class CustomColor
namespace MiraAPI.Utilities.Colors;
public sealed class CustomColor
{
public CustomColor(Color32 mainColor, StringNames name)
{
MainColor = mainColor;
ShadowColor = GetShadowColor(mainColor, 60);
ShadowColor = mainColor.GetShadowColor(60);
Name = name;
}
@@ -20,11 +20,4 @@ public class CustomColor
public Color32 MainColor { get; }
public Color32 ShadowColor { get; }
public StringNames Name { get; }
public static Color32 GetShadowColor(Color32 c, byte darknessAmount)
{
return
new Color32((byte)Mathf.Clamp(c.r - darknessAmount, 0, 255), (byte)Mathf.Clamp(c.g - darknessAmount, 0, 255),
(byte)Mathf.Clamp(c.b - darknessAmount, 0, 255), byte.MaxValue);
}
}
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
namespace MiraAPI.Utilities.Colors;
internal static class PaletteManager
{
public static readonly List<CustomColor> CustomColors = [];
public static void RegisterAllColors()
{
var colors = CustomColors.Select(x => x.MainColor);
var shadowColors = CustomColors.Select(x => x.ShadowColor);
var stringNames = CustomColors.Select(x => x.Name);
Palette.PlayerColors = Palette.PlayerColors.ToArray().AddRangeToArray(colors.ToArray());
Palette.ShadowColors = Palette.ShadowColors.ToArray().AddRangeToArray(shadowColors.ToArray());
Palette.ColorNames = Palette.ColorNames.ToArray().AddRangeToArray(stringNames.ToArray());
}
}
@@ -1,6 +0,0 @@
using System;
namespace MiraAPI.Utilities.Colors;
[AttributeUsage(AttributeTargets.Field)]
public class RegisterColorAttribute : Attribute;
@@ -0,0 +1,6 @@
using System;
namespace MiraAPI.Utilities.Colors;
[AttributeUsage(AttributeTargets.Class)]
public class RegisterColorsAttribute : Attribute;
+12
View File
@@ -11,6 +11,18 @@ namespace MiraAPI.Utilities;
public static class Extensions
{
public static bool IsStatic(this Type type)
{
return type is { IsClass: true, IsAbstract: true, IsSealed: true };
}
public static Color32 GetShadowColor(this Color32 color, byte darknessAmount)
{
return
new Color32((byte)Mathf.Clamp(color.r - darknessAmount, 0, 255), (byte)Mathf.Clamp(color.g - darknessAmount, 0, 255),
(byte)Mathf.Clamp(color.b - darknessAmount, 0, 255), byte.MaxValue);
}
public static void SetOutline(this SpriteRenderer spriteRenderer, bool active, Color color)
{
spriteRenderer.material.SetFloat(ShaderID.Outline, active ? 1f : 0f);