From 01e7e4e35efb918430ad591bf56c5b5b005ec276 Mon Sep 17 00:00:00 2001 From: CortexCore <2630229280@qq.com> Date: Thu, 9 Jan 2025 17:49:38 +0800 Subject: [PATCH] 1 --- .../Extensions/DisplayAttributeExtensions.cs | 32 ++--- Src/Core/Models/ContextModel.cs | 2 +- Src/Core/Wrapper/IWrapper.cs | 28 +++++ Src/NetSupport/MathematicsSupport.cs | 112 ++++++++++++++++++ Src/Unity/Scripts/UX/Library/TabBar.cs | 1 + 5 files changed, 159 insertions(+), 16 deletions(-) diff --git a/Src/Core/Extensions/DisplayAttributeExtensions.cs b/Src/Core/Extensions/DisplayAttributeExtensions.cs index 0c70cff..0f62fe3 100644 --- a/Src/Core/Extensions/DisplayAttributeExtensions.cs +++ b/Src/Core/Extensions/DisplayAttributeExtensions.cs @@ -23,33 +23,35 @@ namespace BITKit public static string GetDisplayName(this object self) { { - if (self.GetType().GetCustomAttribute() is { } displayNameAttribute) + var type = self is Type t? t: self.GetType(); + if (type.GetCustomAttribute() is { } displayNameAttribute) { return displayNameAttribute.DisplayName; } #if NET5_0_OR_GREATER - if (self.GetType().GetCustomAttribute() is { } displayAttribute) + if (type.GetCustomAttribute() is { } displayAttribute) { return displayAttribute.Name; } #endif } { - if (self is Enum) - { - var field = self.GetType().GetField(self.ToString()!); + if (self is not Enum) return self.ToString(); + + var field = self.GetType().GetField(self.ToString()!); + + if(field is null) return self.ToString(); - if (field.GetCustomAttribute() is DisplayNameAttribute displayNameAttribute) - { - return displayNameAttribute.DisplayName; - } -#if NET5_0_OR_GREATER - if (field.GetCustomAttribute() is DisplayAttribute displayAttribute) - { - return displayAttribute.Name; - } -#endif + if (field.GetCustomAttribute() is { } displayNameAttribute) + { + return displayNameAttribute.DisplayName; } +#if NET5_0_OR_GREATER + if (field.GetCustomAttribute() is { } displayAttribute) + { + return displayAttribute.Name; + } +#endif } return self.ToString(); } diff --git a/Src/Core/Models/ContextModel.cs b/Src/Core/Models/ContextModel.cs index 136a02f..db828e6 100644 --- a/Src/Core/Models/ContextModel.cs +++ b/Src/Core/Models/ContextModel.cs @@ -66,7 +66,7 @@ namespace BITKit public override string ToString() { - return JsonConvert.SerializeObject(this); + return JsonConvert.SerializeObject(this,Formatting.Indented); } [JsonProperty("status_code")] public int StatusCode = 200; diff --git a/Src/Core/Wrapper/IWrapper.cs b/Src/Core/Wrapper/IWrapper.cs index d046406..7c38bfe 100644 --- a/Src/Core/Wrapper/IWrapper.cs +++ b/Src/Core/Wrapper/IWrapper.cs @@ -16,6 +16,34 @@ namespace BITKit public T Value { get; set; }=default; public object Obj { get => Value; set => Value = (T)value; } } + + public class PropertyWrapper : IWrapper + { + private readonly PropertyInfo _field; + private readonly object _target; + public event Action OnValueChanged; + public PropertyWrapper(PropertyInfo field, object target) + { + _field = field; + _target = target ?? throw new ArgumentNullException(nameof(target)); + } + public T Value + { + get => (T)_field.GetValue(_target); + set + { + var prev = Value; + _field.SetValue(_target, value); + OnValueChanged?.Invoke(prev, value); + } + } + + public object Obj + { + get => _field.GetValue(_target); + set => _field.SetValue(_target, value); + } + } public class FieldWrapper : IWrapper { private readonly FieldInfo _field; diff --git a/Src/NetSupport/MathematicsSupport.cs b/Src/NetSupport/MathematicsSupport.cs index 8bd0afb..b311f53 100644 --- a/Src/NetSupport/MathematicsSupport.cs +++ b/Src/NetSupport/MathematicsSupport.cs @@ -5,6 +5,82 @@ using Unity.Mathematics; #if NET5_0_OR_GREATER namespace BITKit { + public class Int3JsonConvertJsonConverter : JsonConverter + { + public override void WriteJson(JsonWriter writer, int3 value, JsonSerializer serializer) + { + writer.WriteStartObject(); + + writer.WritePropertyName(nameof(value.x)); + writer.WriteValue(value.x); + + writer.WritePropertyName(nameof(value.y)); + writer.WriteValue(value.y); + + writer.WritePropertyName(nameof(value.z)); + writer.WriteValue(value.z); + + writer.WriteEndObject(); + } + + public override int3 ReadJson(JsonReader reader, Type objectType, int3 existingValue, bool hasExistingValue, JsonSerializer serializer) + { + var result = new int3(0, 0, 0); // 默认值为 (0, 0, 0) + + while (reader.TokenType != JsonToken.EndObject) + { + reader.Read(); + if (reader.TokenType != JsonToken.PropertyName) continue; + + var property = reader.Value.ToString(); + if (property == nameof(int3.x)) result.x = (int)reader.ReadAsInt32()!; + if (property == nameof(int3.y)) result.y = (int)reader.ReadAsInt32()!; + if (property == nameof(int3.z)) result.z = (int)reader.ReadAsInt32()!; + } + + return result; + } + } + public class Int4JsonConvertJsonConverter : JsonConverter + { + public override void WriteJson(JsonWriter writer, int4 value, JsonSerializer serializer) + { + writer.WriteStartObject(); + + writer.WritePropertyName(nameof(value.x)); + writer.WriteValue(value.x); + + writer.WritePropertyName(nameof(value.y)); + writer.WriteValue(value.y); + + writer.WritePropertyName(nameof(value.z)); + writer.WriteValue(value.z); + + writer.WritePropertyName(nameof(value.w)); + writer.WriteValue(value.w); + + writer.WriteEndObject(); + } + + public override int4 ReadJson(JsonReader reader, Type objectType, int4 existingValue, bool hasExistingValue, JsonSerializer serializer) + { + var result = new int4(0, 0, 0, 0); // 默认值为 (0, 0, 0, 0) + + while (reader.TokenType != JsonToken.EndObject) + { + reader.Read(); + if (reader.TokenType != JsonToken.PropertyName) continue; + + var property = reader.Value.ToString(); + if (property == nameof(int4.x)) result.x = (int)reader.ReadAsInt32()!; + if (property == nameof(int4.y)) result.y = (int)reader.ReadAsInt32()!; + if (property == nameof(int4.z)) result.z = (int)reader.ReadAsInt32()!; + if (property == nameof(int4.w)) result.w = (int)reader.ReadAsInt32()!; + } + + return result; + } + } public class Float2JsonConvertJsonConverter:JsonConverter { public override void WriteJson(JsonWriter writer, float2 value, JsonSerializer serializer) @@ -37,6 +113,42 @@ namespace BITKit return result; } } + public class Float3JsonConvertJsonConverter : JsonConverter + { + public override void WriteJson(JsonWriter writer, float3 value, JsonSerializer serializer) + { + writer.WriteStartObject(); + + writer.WritePropertyName(nameof(value.x)); + writer.WriteValue((double)value.x); // 将 float 转为 double,避免精度问题 + + writer.WritePropertyName(nameof(value.y)); + writer.WriteValue((double)value.y); // 将 float 转为 double + + writer.WritePropertyName(nameof(value.z)); + writer.WriteValue((double)value.z); // 将 float 转为 double + + writer.WriteEndObject(); + } + + public override float3 ReadJson(JsonReader reader, Type objectType, float3 existingValue, bool hasExistingValue, JsonSerializer serializer) + { + var result = float3.zero; // 默认值为 (0, 0, 0) + + while (reader.TokenType != JsonToken.EndObject) + { + reader.Read(); + if (reader.TokenType != JsonToken.PropertyName) continue; + + var property = reader.Value.ToString(); + if (property == nameof(float3.x)) result.x = (float)reader.ReadAsDouble()!; + if (property == nameof(float3.y)) result.y = (float)reader.ReadAsDouble()!; + if (property == nameof(float3.z)) result.z = (float)reader.ReadAsDouble()!; + } + + return result; + } + } public class Float4JsonConvertJsonConverter:JsonConverter { public override void WriteJson(JsonWriter writer, float4 value, JsonSerializer serializer) diff --git a/Src/Unity/Scripts/UX/Library/TabBar.cs b/Src/Unity/Scripts/UX/Library/TabBar.cs index 78f42c8..e00a7f6 100644 --- a/Src/Unity/Scripts/UX/Library/TabBar.cs +++ b/Src/Unity/Scripts/UX/Library/TabBar.cs @@ -61,6 +61,7 @@ namespace BITKit.UX get=>_currentTab; set { + if(_currentTab == value)return; _currentTab = value; SetTabs(value); }