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;
// 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
{

View File

@ -19,6 +19,41 @@ namespace UnityExplorer
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)
{
if (string.IsNullOrEmpty(type.FullName))
@ -77,10 +112,11 @@ namespace UnityExplorer
return false;
}
private static readonly HashSet<Type> nonFormattedTypes = new HashSet<Type>
private static readonly HashSet<Type> formattedTypes = new HashSet<Type>
{
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)

View File

@ -15,7 +15,6 @@ namespace UnityExplorer
internal static Dictionary<string, MethodInfo> toStringMethods = 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 nullUnknown = nullString + " (?)";
private const string destroyedString = "<color=red>Destroyed</color>";
@ -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);
}

View File

@ -51,14 +51,6 @@ namespace UnityExplorer
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>
/// Get the full Transform heirarchy path for this provided Transform.
/// </summary>

View File

@ -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;

View File

@ -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<TransformCell> transformScroll;
private readonly List<GameObject> cachedChildren = new List<GameObject>();
public ButtonListSource<Component> ComponentList;
private ScrollPool<ButtonCell> componentScroll;
private readonly List<GameObject> _rootEntries = new List<GameObject>();
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<GameObject> 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<Component> _componentEntries = new List<Component>();
@ -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
}
}

View File

@ -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<RectTransform>();
PlaceholderText = InputField.placeholder.TryCast<Text>();
InputField.onValueChanged.AddListener(OnInputChanged);
this.Component = component;
Rect = component.GetComponent<RectTransform>();
PlaceholderText = component.placeholder.TryCast<Text>();
component.onValueChanged.AddListener(OnInputChanged);
}
public event Action<string> OnValueChanged;

View File

@ -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()