From 67acf2cfeb11123aa6fb65722723c1ae256c12fd Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Thu, 29 Feb 2024 02:37:04 +0800 Subject: [PATCH] update debug/meta --- src/Debug/EcsDebugUtility.cs | 75 ++++-- .../MetaAttributes/EcsMetaAttribute.cs.meta | 11 + .../MetaAttributes/MetaColorAttribute.cs | 250 ++++++++++++++++-- 3 files changed, 288 insertions(+), 48 deletions(-) create mode 100644 src/Debug/MetaAttributes/EcsMetaAttribute.cs.meta diff --git a/src/Debug/EcsDebugUtility.cs b/src/Debug/EcsDebugUtility.cs index a06623a..6cd3f19 100644 --- a/src/Debug/EcsDebugUtility.cs +++ b/src/Debug/EcsDebugUtility.cs @@ -1,5 +1,4 @@ -using DCFApixels.DragonECS.Internal; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -173,19 +172,21 @@ namespace DCFApixels.DragonECS public List colors = new List(); public NameColor(IEnumerable nameWords) { + int i = 0; foreach (var word in nameWords) { - if (!_words.TryGetValue(word, out WordColor color)) + if (_words.TryGetValue(word, out WordColor color) == false) { - color = new WordColor(); - _words.Add(word, color); - - MetaColor newColor = new MetaColor(BitsUtility.NextXorShiftState(word.GetHashCode())); - newColor = new MetaColor(newColor.r, newColor.g, newColor.b); - color.color = newColor.UpContrastColor() / 2; + unchecked + { + color = new WordColor(); + color.color = new MetaColor(word).UpContrastColor(); + _words.Add(word, color); + } } color.wordsCount++; colors.Add(color); + i++; } } private int CalcTotalWordsColor() @@ -199,17 +200,31 @@ namespace DCFApixels.DragonECS } public MetaColor CalcColor() { - float r = 0, g = 0, b = 0; - int totalWordsCount = CalcTotalWordsColor(); + float r = 0; + float g = 0; + float b = 0; + float totalWordsCount = CalcTotalWordsColor(); for (int i = 0, iMax = colors.Count; i < iMax; i++) { var color = colors[i]; - float m = (float)color.wordsCount / totalWordsCount; - r += m * color.color.r; - g += m * color.color.g; - b += m * color.color.b; + float m = color.wordsCount / totalWordsCount; + r = m * color.color.r; + g = m * color.color.g; + b = m * color.color.b; } - return new MetaColor((byte)r, (byte)g, (byte)b); + return UpContrastColor(r, g, b); + } + + public MetaColor UpContrastColor(float r, float g, float b) + { + float minChannel = Math.Min(Math.Min(r, g), b); + float maxChannel = Math.Max(Math.Max(r, g), b); + if (maxChannel == minChannel) + { + return default; + } + float factor = 255f / (maxChannel - minChannel); + return new MetaColor((byte)((r - minChannel) * factor), (byte)((g - minChannel) * factor), (byte)((b - minChannel) * factor)); } } private static Dictionary _names = new Dictionary(); @@ -226,6 +241,7 @@ namespace DCFApixels.DragonECS private static List SplitString(string s) { string subs; + List words = new List(); int start = 0; for (int i = 1; i < s.Length; i++) @@ -384,10 +400,6 @@ namespace DCFApixels.DragonECS #endregion #region GetCachedTypeMeta - public static TypeMetaDataCached GetMeta(this object self) - { - return GetCachedTypeMeta(self); - } private static readonly Dictionary _metaCache = new Dictionary(); public static TypeMetaDataCached GetCachedTypeMeta(object obj) { @@ -446,13 +458,20 @@ namespace DCFApixels.DragonECS } } + public static class TypeMetaDataCachedExtensions + { + public static TypeMetaDataCached GetMeta(this object self) + { + return EcsDebugUtility.GetCachedTypeMeta(self); + } + } public class TypeMetaDataCached { internal readonly Type _type; internal string _name; internal MetaGroup _group; - internal MetaColor? _color; + internal MetaColor _color; internal string _description; internal IReadOnlyCollection _tags; @@ -472,7 +491,7 @@ namespace DCFApixels.DragonECS { get { - if (string.IsNullOrEmpty(_name)) + if (_initFlags.HasFlag(InitFlag.Name) == false) { _name = EcsDebugUtility.GetName(_type); _initFlags |= InitFlag.Name; @@ -484,7 +503,7 @@ namespace DCFApixels.DragonECS { get { - if (_group.IsNull) + if (_initFlags.HasFlag(InitFlag.Group) == false) { _group = EcsDebugUtility.GetGroup(_type); _initFlags |= InitFlag.Group; @@ -496,19 +515,19 @@ namespace DCFApixels.DragonECS { get { - if (_color.HasValue == false) + if (_initFlags.HasFlag(InitFlag.Color) == false) { _color = EcsDebugUtility.GetColor(_type); - _initFlags |= InitFlag.Color; + //_initFlags |= InitFlag.Color; } - return _color.Value; + return _color; } } public string Description { get { - if (_description == null) + if (_initFlags.HasFlag(InitFlag.Description) == false) { _description = EcsDebugUtility.GetDescription(_type); _initFlags |= InitFlag.Description; @@ -520,7 +539,7 @@ namespace DCFApixels.DragonECS { get { - if (_tags == null) + if (_initFlags.HasFlag(InitFlag.Tags) == false) { _tags = EcsDebugUtility.GetTags(_type); _initFlags |= InitFlag.Tags; diff --git a/src/Debug/MetaAttributes/EcsMetaAttribute.cs.meta b/src/Debug/MetaAttributes/EcsMetaAttribute.cs.meta new file mode 100644 index 0000000..94d5ad9 --- /dev/null +++ b/src/Debug/MetaAttributes/EcsMetaAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eb8cc656a6e80f843b8794af9f63faa8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Debug/MetaAttributes/MetaColorAttribute.cs b/src/Debug/MetaAttributes/MetaColorAttribute.cs index 248942d..9801d69 100644 --- a/src/Debug/MetaAttributes/MetaColorAttribute.cs +++ b/src/Debug/MetaAttributes/MetaColorAttribute.cs @@ -1,24 +1,78 @@ -using System; +using DCFApixels.DragonECS.Internal; +using System; using System.Runtime.InteropServices; namespace DCFApixels.DragonECS { + public interface IMetaColor + { + #region Properties + public byte R { get; } + public byte G { get; } + public byte B { get; } + public byte A { get; } + public float FloatR { get; } + public float FloatG { get; } + public float FloatB { get; } + public float FloatA { get; } + #endregion + } [AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Interface, Inherited = false, AllowMultiple = false)] - public sealed class MetaColorAttribute : EcsMetaAttribute + public sealed class MetaColorAttribute : EcsMetaAttribute, IMetaColor { public readonly MetaColor color; - public byte R => color.r; - public byte G => color.g; - public byte B => color.b; - public float FloatR => R / (float)byte.MaxValue; - public float FloatG => G / (float)byte.MaxValue; - public float FloatB => B / (float)byte.MaxValue; - public MetaColorAttribute(byte r, byte g, byte b) => color = new MetaColor(r, g, b, 255); - public MetaColorAttribute(int colorCode) => color = new MetaColor(colorCode, true); + + #region Properties + public byte R + { + get { return color.r; } + } + public byte G + { + get { return color.g; } + } + public byte B + { + get { return color.b; } + } + public byte A + { + get { return color.a; } + } + public float FloatR + { + get { return R / (float)byte.MaxValue; } + } + public float FloatG + { + get { return G / (float)byte.MaxValue; } + } + public float FloatB + { + get { return B / (float)byte.MaxValue; } + } + public float FloatA + { + get { return A / (float)byte.MaxValue; } + } + #endregion + + #region Constructors + public MetaColorAttribute(byte r, byte g, byte b) + { + color = new MetaColor(r, g, b, 255); + } + public MetaColorAttribute(int colorCode) + { + color = new MetaColor(colorCode, true); + } + #endregion } + [StructLayout(LayoutKind.Explicit, Pack = 1, Size = 4)] - public readonly struct MetaColor + public readonly struct MetaColor : IMetaColor { + #region Consts public static readonly MetaColor BlackColor = new MetaColor(Black); /// color code Red. RGB is (255, 0, 0) public const int Red = (255 << 24) | (000 << 16) | (000 << 8) | 255; @@ -63,16 +117,50 @@ namespace DCFApixels.DragonECS public const int White = -1; /// color code Black. RGB is (0, 0, 0) public const int Black = 0; + #endregion [FieldOffset(0)] public readonly int colorCode; [FieldOffset(3)] public readonly byte r; [FieldOffset(2)] public readonly byte g; [FieldOffset(1)] public readonly byte b; [FieldOffset(0)] public readonly byte a; - public float FloatR => r / (float)byte.MaxValue; - public float FloatG => g / (float)byte.MaxValue; - public float FloatB => b / (float)byte.MaxValue; - public float FloatA => a / (float)byte.MaxValue; + + #region Properties + byte IMetaColor.R + { + get { return r; } + } + byte IMetaColor.G + { + get { return g; } + } + byte IMetaColor.B + { + get { return b; } + } + byte IMetaColor.A + { + get { return a; } + } + public float FloatR + { + get { return r / (float)byte.MaxValue; } + } + public float FloatG + { + get { return g / (float)byte.MaxValue; } + } + public float FloatB + { + get { return b / (float)byte.MaxValue; } + } + public float FloatA + { + get { return a / (float)byte.MaxValue; } + } + #endregion + + #region Constructors public MetaColor(byte r, byte g, byte b) : this() { this.r = r; @@ -87,11 +175,45 @@ namespace DCFApixels.DragonECS this.b = b; this.a = a; } - public MetaColor(int colorCode) : this() => this.colorCode = colorCode; - public MetaColor(int colorCode, bool withoutAlpha) : this() => this.colorCode = withoutAlpha ? colorCode | 255 : colorCode; - public (byte, byte, byte) ToTupleRGB() => (r, g, b); - public (byte, byte, byte, byte) ToTupleRGBA() => (r, g, b, a); + public MetaColor(int colorCode) : this() + { + this.colorCode = colorCode; + } + public MetaColor(int colorCode, bool withoutAlpha) : this() + { + this.colorCode = withoutAlpha ? colorCode | 255 : colorCode; + } + public MetaColor(string stringCode) : this() + { + unchecked + { + const uint MAGIC_CONST = 0xA638_783E; + uint colorCode = (uint)stringCode.GetHashCode(); + colorCode ^= MAGIC_CONST; + colorCode = BitsUtility.NextXorShiftState(colorCode); + this.colorCode = (int)colorCode | 255; + this.colorCode = UpContrastColor().colorCode; + } + } + #endregion + #region Deconstructs + public void Deconstruct(out byte r, out byte g, out byte b) + { + r = this.r; + g = this.g; + b = this.b; + } + public void Deconstruct(out byte r, out byte g, out byte b, out byte a) + { + r = this.r; + g = this.g; + b = this.b; + a = this.a; + } + #endregion + + #region Methods public MetaColor UpContrastColor() { byte minChannel = Math.Min(Math.Min(r, g), b); @@ -103,9 +225,97 @@ namespace DCFApixels.DragonECS float factor = 255f / (maxChannel - minChannel); return new MetaColor((byte)((r - minChannel) * factor), (byte)((g - minChannel) * factor), (byte)((b - minChannel) * factor)); } + #endregion + + #region Operators public static MetaColor operator /(MetaColor a, float b) { - return new MetaColor((byte)(a.r / b), (byte)(a.g / b), (byte)(a.b / b)); + return new MetaColor( + (byte)(a.r / b), + (byte)(a.g / b), + (byte)(a.b / b)); } + public static MetaColor operator /(float b, MetaColor a) + { + return new MetaColor( + (byte)(a.r / b), + (byte)(a.g / b), + (byte)(a.b / b)); + } + public static MetaColor operator /(MetaColor a, MetaColor b) + { + return new MetaColor( + (byte)(a.r / b.r), + (byte)(a.g / b.g), + (byte)(a.b / b.b)); + } + + public static MetaColor operator *(MetaColor a, float b) + { + return new MetaColor( + (byte)(a.r * b), + (byte)(a.g * b), + (byte)(a.b * b)); + } + + public static MetaColor operator *(float b, MetaColor a) + { + return new MetaColor( + (byte)(a.r * b), + (byte)(a.g * b), + (byte)(a.b * b)); + } + public static MetaColor operator *(MetaColor a, MetaColor b) + { + return new MetaColor( + (byte)(a.r * b.r), + (byte)(a.g * b.g), + (byte)(a.b * b.b)); + } + + public static MetaColor operator +(MetaColor a, byte b) + { + return new MetaColor( + (byte)(a.r + b), + (byte)(a.g + b), + (byte)(a.b + b)); + } + public static MetaColor operator +(byte b, MetaColor a) + { + return new MetaColor( + (byte)(a.r + b), + (byte)(a.g + b), + (byte)(a.b + b)); + } + public static MetaColor operator +(MetaColor a, MetaColor b) + { + return new MetaColor( + (byte)(a.r + b.r), + (byte)(a.g + b.g), + (byte)(a.b + b.b)); + } + + public static MetaColor operator -(MetaColor a, byte b) + { + return new MetaColor( + (byte)(a.r - b), + (byte)(a.g - b), + (byte)(a.b - b)); + } + public static MetaColor operator -(byte b, MetaColor a) + { + return new MetaColor( + (byte)(a.r - b), + (byte)(a.g - b), + (byte)(a.b - b)); + } + public static MetaColor operator -(MetaColor a, MetaColor b) + { + return new MetaColor( + (byte)(a.r - b.r), + (byte)(a.g - b.g), + (byte)(a.b - b.b)); + } + #endregion } } \ No newline at end of file