From 275225a284187a8515d656a611cb609f55a89e48 Mon Sep 17 00:00:00 2001 From: Sinai Date: Thu, 13 May 2021 00:58:23 +1000 Subject: [PATCH] Better number ToString formatting, cleanups --- src/Core/Reflection/ReflectionUtility.cs | 7 +- src/Core/Utility/ParseUtility.cs | 109 +++++++++++------------ src/Core/Utility/ToStringUtility.cs | 7 +- src/Core/Utility/UnityHelpers.cs | 8 -- src/UI/CacheObject/CacheObjectBase.cs | 2 +- src/UI/Inspectors/GameObjectInspector.cs | 39 ++++---- src/UI/Models/InputFieldRef.cs | 10 +-- src/UI/Panels/UIPanel.cs | 3 + 8 files changed, 92 insertions(+), 93 deletions(-) diff --git a/src/Core/Reflection/ReflectionUtility.cs b/src/Core/Reflection/ReflectionUtility.cs index dfb7f8d..c057d96 100644 --- a/src/Core/Reflection/ReflectionUtility.cs +++ b/src/Core/Reflection/ReflectionUtility.cs @@ -374,15 +374,16 @@ namespace UnityExplorer var sig = methodName; - // If the signature could be ambiguous (internally, within UnityExplorer's own use) - // then append the arguments to the key. - // Currently not needed and not used, but just in case I need it one day. if (cacheAmbiguous) { sig += "|"; foreach (var arg in argumentTypes) sig += arg.FullName + ","; } + else + { + sig += "|" + (argumentTypes?.Length.ToString() ?? "null"); + } try { diff --git a/src/Core/Utility/ParseUtility.cs b/src/Core/Utility/ParseUtility.cs index 089f81d..5897eb6 100644 --- a/src/Core/Utility/ParseUtility.cs +++ b/src/Core/Utility/ParseUtility.cs @@ -19,6 +19,41 @@ namespace UnityExplorer typeof(DateTime), }; + public const string NUMBER_FORMAT = "0.####"; + + private static readonly Dictionary numSequenceStrings = new Dictionary(); + + // Helper for formatting float/double/decimal numbers to maximum of 4 decimal points. + public static string FormatDecimalSequence(params object[] numbers) + { + if (numbers.Length <= 0) + return null; + + int count = numbers.Length; + var formatString = GetSequenceFormatString(count); + + return string.Format(en_US, formatString, numbers); + } + + public static string GetSequenceFormatString(int count) + { + if (count <= 0) + return null; + + if (numSequenceStrings.ContainsKey(count)) + return numSequenceStrings[count]; + + string[] strings = new string[count]; + + for (int i = 0; i < count; i++) + strings[i] = $"{{{i}:{NUMBER_FORMAT}}}"; + + string s = string.Join(", ", strings); + + numSequenceStrings.Add(count, s); + return s; + } + public static bool CanParse(Type type) { if (string.IsNullOrEmpty(type.FullName)) @@ -77,10 +112,11 @@ namespace UnityExplorer return false; } - private static readonly HashSet nonFormattedTypes = new HashSet + private static readonly HashSet formattedTypes = new HashSet { - typeof(IntPtr), - typeof(UIntPtr), + typeof(float), + typeof(double), + typeof(decimal) }; public static string ToStringForInput(object obj, Type type) @@ -104,15 +140,15 @@ namespace UnityExplorer { return customTypesToString[type.FullName].Invoke(obj); } - else + else if (formattedTypes.Contains(type)) { - if (nonFormattedTypes.Contains(type)) - return obj.ToString(); - else - return ReflectionUtility.GetMethodInfo(type, "ToString", new Type[] { typeof(IFormatProvider) }) - .Invoke(obj, new object[] { en_US }) - as string; + return ReflectionUtility.GetMethodInfo(type, "ToString", new Type[] { typeof(string), typeof(IFormatProvider) }) + .Invoke(obj, new object[] { NUMBER_FORMAT, en_US }) + as string; } + else + return obj.ToString(); + } catch (Exception ex) { @@ -199,11 +235,7 @@ namespace UnityExplorer if (!(obj is Vector2 vector)) return null; - return string.Format(en_US, "{0}, {1}", new object[] - { - vector.x, - vector.y - }); + return FormatDecimalSequence(vector.x, vector.y); } // Vector3 @@ -226,12 +258,7 @@ namespace UnityExplorer if (!(obj is Vector3 vector)) return null; - return string.Format(en_US, "{0}, {1}, {2}", new object[] - { - vector.x, - vector.y, - vector.z - }); + return FormatDecimalSequence(vector.x, vector.y, vector.z); } // Vector4 @@ -255,13 +282,7 @@ namespace UnityExplorer if (!(obj is Vector4 vector)) return null; - return string.Format(en_US, "{0}, {1}, {2}, {3}", new object[] - { - vector.x, - vector.y, - vector.z, - vector.w - }); + return FormatDecimalSequence(vector.x, vector.y, vector.z, vector.w); } // Quaternion @@ -297,12 +318,7 @@ namespace UnityExplorer Vector3 vector = quaternion.eulerAngles; - return string.Format(en_US, "{0}, {1}, {2}", new object[] - { - vector.x, - vector.y, - vector.z, - }); + return FormatDecimalSequence(vector.x, vector.y, vector.z); } // Rect @@ -326,13 +342,7 @@ namespace UnityExplorer if (!(obj is Rect rect)) return null; - return string.Format(en_US, "{0}, {1}, {2}, {3}", new object[] - { - rect.x, - rect.y, - rect.width, - rect.height - }); + return FormatDecimalSequence(rect.x, rect.y, rect.width, rect.height); } // Color @@ -359,13 +369,7 @@ namespace UnityExplorer if (!(obj is Color color)) return null; - return string.Format(en_US, "{0}, {1}, {2}, {3}", new object[] - { - color.r, - color.g, - color.b, - color.a - }); + return FormatDecimalSequence(color.r, color.g, color.b, color.a); } // Color32 @@ -392,13 +396,8 @@ namespace UnityExplorer if (!(obj is Color32 color)) return null; - return string.Format(en_US, "{0}, {1}, {2}, {3}", new object[] - { - color.r, - color.g, - color.b, - color.a - }); + // ints, this is fine + return $"{color.r}, {color.g}, {color.b}, {color.a}"; } // Layermask (Int32) diff --git a/src/Core/Utility/ToStringUtility.cs b/src/Core/Utility/ToStringUtility.cs index 1559093..971504d 100644 --- a/src/Core/Utility/ToStringUtility.cs +++ b/src/Core/Utility/ToStringUtility.cs @@ -15,7 +15,6 @@ namespace UnityExplorer internal static Dictionary toStringMethods = new Dictionary(); internal static Dictionary toStringFormattedMethods = new Dictionary(); - // string allocs private const string nullString = "null"; private const string nullUnknown = nullString + " (?)"; private const string destroyedString = "Destroyed"; @@ -140,7 +139,7 @@ namespace UnityExplorer try { var formatMethod = type.GetMethod("ToString", ArgumentUtility.ParseArgs); - formatMethod.Invoke(value, new object[] { "F3" }); + formatMethod.Invoke(value, new object[] { ParseUtility.NUMBER_FORMAT }); toStringFormattedMethods.Add(type.AssemblyQualifiedName, formatMethod); toStringMethods.Add(type.AssemblyQualifiedName, null); } @@ -158,8 +157,8 @@ namespace UnityExplorer string toString; try { - if (toStringFormattedMethods.TryGetValue(type.AssemblyQualifiedName, out MethodInfo f3method)) - toString = (string)f3method.Invoke(value, new object[] { "F3" }); + if (toStringFormattedMethods.TryGetValue(type.AssemblyQualifiedName, out MethodInfo formatMethod)) + toString = (string)formatMethod.Invoke(value, new object[] { ParseUtility.NUMBER_FORMAT }); else toString = (string)toStringMethods[type.AssemblyQualifiedName].Invoke(value, ArgumentUtility.EmptyArgs); } diff --git a/src/Core/Utility/UnityHelpers.cs b/src/Core/Utility/UnityHelpers.cs index 2247bef..0fcf41e 100644 --- a/src/Core/Utility/UnityHelpers.cs +++ b/src/Core/Utility/UnityHelpers.cs @@ -51,14 +51,6 @@ namespace UnityExplorer return false; } - /// - /// Print a nice {X, Y, Z} output of the Vector3, formatted to 3 decimal places. - /// - public static string ToStringPretty(this Vector3 vec) - { - return $"{vec.x:F3}, {vec.y:F3}, {vec.z:F3}"; - } - /// /// Get the full Transform heirarchy path for this provided Transform. /// diff --git a/src/UI/CacheObject/CacheObjectBase.cs b/src/UI/CacheObject/CacheObjectBase.cs index 149d86a..fd1d526 100644 --- a/src/UI/CacheObject/CacheObjectBase.cs +++ b/src/UI/CacheObject/CacheObjectBase.cs @@ -185,7 +185,7 @@ namespace UnityExplorer.UI.CacheObject return ValueState.ValueStruct; else if (typeof(IDictionary).IsAssignableFrom(type)) return ValueState.Dictionary; - else if (typeof(IEnumerable).IsAssignableFrom(type)) + else if (!typeof(Transform).IsAssignableFrom(type) && typeof(IEnumerable).IsAssignableFrom(type)) return ValueState.Collection; else return ValueState.Unsupported; diff --git a/src/UI/Inspectors/GameObjectInspector.cs b/src/UI/Inspectors/GameObjectInspector.cs index 754a686..13ff6d5 100644 --- a/src/UI/Inspectors/GameObjectInspector.cs +++ b/src/UI/Inspectors/GameObjectInspector.cs @@ -14,19 +14,17 @@ namespace UnityExplorer.UI.Inspectors { public class GameObjectInspector : InspectorBase { - //public GameObject Target; public GameObject GOTarget => Target as GameObject; private Text NameText; public TransformTree TransformTree; private ScrollPool transformScroll; + private readonly List cachedChildren = new List(); public ButtonListSource ComponentList; private ScrollPool componentScroll; - private readonly List _rootEntries = new List(); - public override void OnBorrowedFromPool(object target) { base.OnBorrowedFromPool(target); @@ -54,13 +52,10 @@ namespace UnityExplorer.UI.Inspectors public override void OnReturnToPool() { base.OnReturnToPool(); - - //// release component and transform lists - //this.TransformTree.ScrollPool.ReleaseCells(); - //this.TransformTree.ScrollPool.SetUninitialized(); - // - //this.ComponentList.ScrollPool.ReleaseCells(); - //this.ComponentList.ScrollPool.SetUninitialized(); + } + protected override void OnCloseClicked() + { + InspectorManager.ReleaseInspector(this); } private float timeOfLastUpdate; @@ -89,12 +84,20 @@ namespace UnityExplorer.UI.Inspectors } } + private void RefreshTopInfo() + { + + } + + + #region Transform and Component Lists + private IEnumerable GetTransformEntries() { - _rootEntries.Clear(); + cachedChildren.Clear(); for (int i = 0; i < GOTarget.transform.childCount; i++) - _rootEntries.Add(GOTarget.transform.GetChild(i).gameObject); - return _rootEntries; + cachedChildren.Add(GOTarget.transform.GetChild(i).gameObject); + return cachedChildren; } private readonly List _componentEntries = new List(); @@ -178,10 +181,10 @@ namespace UnityExplorer.UI.Inspectors InspectorManager.Inspect(comp); } - protected override void OnCloseClicked() - { - InspectorManager.ReleaseInspector(this); - } + #endregion + + + #region UI Construction public override GameObject CreateContent(GameObject parent) { @@ -212,5 +215,7 @@ namespace UnityExplorer.UI.Inspectors return UIRoot; } + + #endregion } } diff --git a/src/UI/Models/InputFieldRef.cs b/src/UI/Models/InputFieldRef.cs index f0569ec..0ddba3d 100644 --- a/src/UI/Models/InputFieldRef.cs +++ b/src/UI/Models/InputFieldRef.cs @@ -10,12 +10,12 @@ namespace UnityExplorer.UI { public class InputFieldRef : UIBehaviourModel { - public InputFieldRef(InputField InputField) + public InputFieldRef(InputField component) { - this.Component = InputField; - Rect = InputField.GetComponent(); - PlaceholderText = InputField.placeholder.TryCast(); - InputField.onValueChanged.AddListener(OnInputChanged); + this.Component = component; + Rect = component.GetComponent(); + PlaceholderText = component.placeholder.TryCast(); + component.onValueChanged.AddListener(OnInputChanged); } public event Action OnValueChanged; diff --git a/src/UI/Panels/UIPanel.cs b/src/UI/Panels/UIPanel.cs index c1edc63..95fd4e5 100644 --- a/src/UI/Panels/UIPanel.cs +++ b/src/UI/Panels/UIPanel.cs @@ -120,6 +120,9 @@ namespace UnityExplorer.UI.Panels else RuntimeProvider.Instance.SetColorBlock(NavButton.Component, UIManager.disabledButtonColor, UIManager.disabledButtonColor * 1.2f); } + + if (!active) + this.Dragger.WasDragging = false; } public override void Destroy()