Better number ToString formatting, cleanups

This commit is contained in:
Sinai
2021-05-13 00:58:23 +10:00
parent b61020fe67
commit 275225a284
8 changed files with 92 additions and 93 deletions

View File

@ -374,15 +374,16 @@ namespace UnityExplorer
var sig = methodName; 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) if (cacheAmbiguous)
{ {
sig += "|"; sig += "|";
foreach (var arg in argumentTypes) foreach (var arg in argumentTypes)
sig += arg.FullName + ","; sig += arg.FullName + ",";
} }
else
{
sig += "|" + (argumentTypes?.Length.ToString() ?? "null");
}
try try
{ {

View File

@ -19,6 +19,41 @@ namespace UnityExplorer
typeof(DateTime), typeof(DateTime),
}; };
public const string NUMBER_FORMAT = "0.####";
private static readonly Dictionary<int, string> numSequenceStrings = new Dictionary<int, string>();
// 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) public static bool CanParse(Type type)
{ {
if (string.IsNullOrEmpty(type.FullName)) if (string.IsNullOrEmpty(type.FullName))
@ -77,10 +112,11 @@ namespace UnityExplorer
return false; return false;
} }
private static readonly HashSet<Type> nonFormattedTypes = new HashSet<Type> private static readonly HashSet<Type> formattedTypes = new HashSet<Type>
{ {
typeof(IntPtr), typeof(float),
typeof(UIntPtr), typeof(double),
typeof(decimal)
}; };
public static string ToStringForInput(object obj, Type type) public static string ToStringForInput(object obj, Type type)
@ -104,15 +140,15 @@ namespace UnityExplorer
{ {
return customTypesToString[type.FullName].Invoke(obj); return customTypesToString[type.FullName].Invoke(obj);
} }
else else if (formattedTypes.Contains(type))
{ {
if (nonFormattedTypes.Contains(type)) return ReflectionUtility.GetMethodInfo(type, "ToString", new Type[] { typeof(string), typeof(IFormatProvider) })
return obj.ToString(); .Invoke(obj, new object[] { NUMBER_FORMAT, en_US })
else
return ReflectionUtility.GetMethodInfo(type, "ToString", new Type[] { typeof(IFormatProvider) })
.Invoke(obj, new object[] { en_US })
as string; as string;
} }
else
return obj.ToString();
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -199,11 +235,7 @@ namespace UnityExplorer
if (!(obj is Vector2 vector)) if (!(obj is Vector2 vector))
return null; return null;
return string.Format(en_US, "{0}, {1}", new object[] return FormatDecimalSequence(vector.x, vector.y);
{
vector.x,
vector.y
});
} }
// Vector3 // Vector3
@ -226,12 +258,7 @@ namespace UnityExplorer
if (!(obj is Vector3 vector)) if (!(obj is Vector3 vector))
return null; return null;
return string.Format(en_US, "{0}, {1}, {2}", new object[] return FormatDecimalSequence(vector.x, vector.y, vector.z);
{
vector.x,
vector.y,
vector.z
});
} }
// Vector4 // Vector4
@ -255,13 +282,7 @@ namespace UnityExplorer
if (!(obj is Vector4 vector)) if (!(obj is Vector4 vector))
return null; return null;
return string.Format(en_US, "{0}, {1}, {2}, {3}", new object[] return FormatDecimalSequence(vector.x, vector.y, vector.z, vector.w);
{
vector.x,
vector.y,
vector.z,
vector.w
});
} }
// Quaternion // Quaternion
@ -297,12 +318,7 @@ namespace UnityExplorer
Vector3 vector = quaternion.eulerAngles; Vector3 vector = quaternion.eulerAngles;
return string.Format(en_US, "{0}, {1}, {2}", new object[] return FormatDecimalSequence(vector.x, vector.y, vector.z);
{
vector.x,
vector.y,
vector.z,
});
} }
// Rect // Rect
@ -326,13 +342,7 @@ namespace UnityExplorer
if (!(obj is Rect rect)) if (!(obj is Rect rect))
return null; return null;
return string.Format(en_US, "{0}, {1}, {2}, {3}", new object[] return FormatDecimalSequence(rect.x, rect.y, rect.width, rect.height);
{
rect.x,
rect.y,
rect.width,
rect.height
});
} }
// Color // Color
@ -359,13 +369,7 @@ namespace UnityExplorer
if (!(obj is Color color)) if (!(obj is Color color))
return null; return null;
return string.Format(en_US, "{0}, {1}, {2}, {3}", new object[] return FormatDecimalSequence(color.r, color.g, color.b, color.a);
{
color.r,
color.g,
color.b,
color.a
});
} }
// Color32 // Color32
@ -392,13 +396,8 @@ namespace UnityExplorer
if (!(obj is Color32 color)) if (!(obj is Color32 color))
return null; return null;
return string.Format(en_US, "{0}, {1}, {2}, {3}", new object[] // ints, this is fine
{ return $"{color.r}, {color.g}, {color.b}, {color.a}";
color.r,
color.g,
color.b,
color.a
});
} }
// Layermask (Int32) // Layermask (Int32)

View File

@ -15,7 +15,6 @@ namespace UnityExplorer
internal static Dictionary<string, MethodInfo> toStringMethods = new Dictionary<string, MethodInfo>(); internal static Dictionary<string, MethodInfo> toStringMethods = new Dictionary<string, MethodInfo>();
internal static Dictionary<string, MethodInfo> toStringFormattedMethods = new Dictionary<string, MethodInfo>(); internal static Dictionary<string, MethodInfo> toStringFormattedMethods = new Dictionary<string, MethodInfo>();
// string allocs
private const string nullString = "<color=grey>null</color>"; private const string nullString = "<color=grey>null</color>";
private const string nullUnknown = nullString + " (?)"; private const string nullUnknown = nullString + " (?)";
private const string destroyedString = "<color=red>Destroyed</color>"; private const string destroyedString = "<color=red>Destroyed</color>";
@ -140,7 +139,7 @@ namespace UnityExplorer
try try
{ {
var formatMethod = type.GetMethod("ToString", ArgumentUtility.ParseArgs); 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); toStringFormattedMethods.Add(type.AssemblyQualifiedName, formatMethod);
toStringMethods.Add(type.AssemblyQualifiedName, null); toStringMethods.Add(type.AssemblyQualifiedName, null);
} }
@ -158,8 +157,8 @@ namespace UnityExplorer
string toString; string toString;
try try
{ {
if (toStringFormattedMethods.TryGetValue(type.AssemblyQualifiedName, out MethodInfo f3method)) if (toStringFormattedMethods.TryGetValue(type.AssemblyQualifiedName, out MethodInfo formatMethod))
toString = (string)f3method.Invoke(value, new object[] { "F3" }); toString = (string)formatMethod.Invoke(value, new object[] { ParseUtility.NUMBER_FORMAT });
else else
toString = (string)toStringMethods[type.AssemblyQualifiedName].Invoke(value, ArgumentUtility.EmptyArgs); toString = (string)toStringMethods[type.AssemblyQualifiedName].Invoke(value, ArgumentUtility.EmptyArgs);
} }

View File

@ -51,14 +51,6 @@ namespace UnityExplorer
return false; return false;
} }
/// <summary>
/// Print a nice {X, Y, Z} output of the Vector3, formatted to 3 decimal places.
/// </summary>
public static string ToStringPretty(this Vector3 vec)
{
return $"{vec.x:F3}, {vec.y:F3}, {vec.z:F3}";
}
/// <summary> /// <summary>
/// Get the full Transform heirarchy path for this provided Transform. /// Get the full Transform heirarchy path for this provided Transform.
/// </summary> /// </summary>

View File

@ -185,7 +185,7 @@ namespace UnityExplorer.UI.CacheObject
return ValueState.ValueStruct; return ValueState.ValueStruct;
else if (typeof(IDictionary).IsAssignableFrom(type)) else if (typeof(IDictionary).IsAssignableFrom(type))
return ValueState.Dictionary; return ValueState.Dictionary;
else if (typeof(IEnumerable).IsAssignableFrom(type)) else if (!typeof(Transform).IsAssignableFrom(type) && typeof(IEnumerable).IsAssignableFrom(type))
return ValueState.Collection; return ValueState.Collection;
else else
return ValueState.Unsupported; return ValueState.Unsupported;

View File

@ -14,19 +14,17 @@ namespace UnityExplorer.UI.Inspectors
{ {
public class GameObjectInspector : InspectorBase public class GameObjectInspector : InspectorBase
{ {
//public GameObject Target;
public GameObject GOTarget => Target as GameObject; public GameObject GOTarget => Target as GameObject;
private Text NameText; private Text NameText;
public TransformTree TransformTree; public TransformTree TransformTree;
private ScrollPool<TransformCell> transformScroll; private ScrollPool<TransformCell> transformScroll;
private readonly List<GameObject> cachedChildren = new List<GameObject>();
public ButtonListSource<Component> ComponentList; public ButtonListSource<Component> ComponentList;
private ScrollPool<ButtonCell> componentScroll; private ScrollPool<ButtonCell> componentScroll;
private readonly List<GameObject> _rootEntries = new List<GameObject>();
public override void OnBorrowedFromPool(object target) public override void OnBorrowedFromPool(object target)
{ {
base.OnBorrowedFromPool(target); base.OnBorrowedFromPool(target);
@ -54,13 +52,10 @@ namespace UnityExplorer.UI.Inspectors
public override void OnReturnToPool() public override void OnReturnToPool()
{ {
base.OnReturnToPool(); base.OnReturnToPool();
}
//// release component and transform lists protected override void OnCloseClicked()
//this.TransformTree.ScrollPool.ReleaseCells(); {
//this.TransformTree.ScrollPool.SetUninitialized(); InspectorManager.ReleaseInspector(this);
//
//this.ComponentList.ScrollPool.ReleaseCells();
//this.ComponentList.ScrollPool.SetUninitialized();
} }
private float timeOfLastUpdate; private float timeOfLastUpdate;
@ -89,12 +84,20 @@ namespace UnityExplorer.UI.Inspectors
} }
} }
private void RefreshTopInfo()
{
}
#region Transform and Component Lists
private IEnumerable<GameObject> GetTransformEntries() private IEnumerable<GameObject> GetTransformEntries()
{ {
_rootEntries.Clear(); cachedChildren.Clear();
for (int i = 0; i < GOTarget.transform.childCount; i++) for (int i = 0; i < GOTarget.transform.childCount; i++)
_rootEntries.Add(GOTarget.transform.GetChild(i).gameObject); cachedChildren.Add(GOTarget.transform.GetChild(i).gameObject);
return _rootEntries; return cachedChildren;
} }
private readonly List<Component> _componentEntries = new List<Component>(); private readonly List<Component> _componentEntries = new List<Component>();
@ -178,10 +181,10 @@ namespace UnityExplorer.UI.Inspectors
InspectorManager.Inspect(comp); InspectorManager.Inspect(comp);
} }
protected override void OnCloseClicked() #endregion
{
InspectorManager.ReleaseInspector(this);
} #region UI Construction
public override GameObject CreateContent(GameObject parent) public override GameObject CreateContent(GameObject parent)
{ {
@ -212,5 +215,7 @@ namespace UnityExplorer.UI.Inspectors
return UIRoot; return UIRoot;
} }
#endregion
} }
} }

View File

@ -10,12 +10,12 @@ namespace UnityExplorer.UI
{ {
public class InputFieldRef : UIBehaviourModel public class InputFieldRef : UIBehaviourModel
{ {
public InputFieldRef(InputField InputField) public InputFieldRef(InputField component)
{ {
this.Component = InputField; this.Component = component;
Rect = InputField.GetComponent<RectTransform>(); Rect = component.GetComponent<RectTransform>();
PlaceholderText = InputField.placeholder.TryCast<Text>(); PlaceholderText = component.placeholder.TryCast<Text>();
InputField.onValueChanged.AddListener(OnInputChanged); component.onValueChanged.AddListener(OnInputChanged);
} }
public event Action<string> OnValueChanged; public event Action<string> OnValueChanged;

View File

@ -120,6 +120,9 @@ namespace UnityExplorer.UI.Panels
else else
RuntimeProvider.Instance.SetColorBlock(NavButton.Component, UIManager.disabledButtonColor, UIManager.disabledButtonColor * 1.2f); RuntimeProvider.Instance.SetColorBlock(NavButton.Component, UIManager.disabledButtonColor, UIManager.disabledButtonColor * 1.2f);
} }
if (!active)
this.Dragger.WasDragging = false;
} }
public override void Destroy() public override void Destroy()