using System; using System.Globalization; using UnityEngine; using UnityEngine.UIElements; namespace UnityEditor.UIElements { /// /// Makes a text field for entering an unsigned integer. /// class UIntField : TextValueField { /// /// USS class name of elements of this type. /// public new static readonly string ussClassName = "unity-uint-field"; /// /// USS class name of labels in elements of this type. /// public new static readonly string labelUssClassName = ussClassName + "__label"; /// /// USS class name of input elements in elements of this type. /// public new static readonly string inputUssClassName = ussClassName + "__input"; /// /// Constructor. /// public UIntField() : this(null) { } /// /// Constructor. /// /// Maximum number of characters the field can take. public UIntField(int maxLength) : this(null, maxLength) { } public UIntField(string label, int maxLength = -1) : base(label, maxLength, new UIntInput()) { AddToClassList(ussClassName); labelElement.AddToClassList(labelUssClassName); labelElement.AddToClassList("unity-property-field__label"); AddLabelDragger(); } UIntInput uIntInput => (UIntInput)textInputBase; /// /// Converts the given uint to a string. /// /// The uint to be converted to string. /// /// The uint as string. /// protected override string ValueToString(uint v) { return v.ToString(formatString, CultureInfo.InvariantCulture.NumberFormat); } /// /// Converts a string to an uint. /// /// The string to convert. /// /// The uint parsed from the string. /// protected override uint StringToValue(string str) { long.TryParse(str, out var result); return ClampInput(result); } /// /// Modify the value using a 3D delta and a speed, typically coming from an input device. /// /// A vector used to compute the value change. /// A multiplier for the value change. /// The start value. public override void ApplyInputDeviceDelta(Vector3 delta, DeltaSpeed speed, uint startValue) { uIntInput.ApplyInputDeviceDelta(delta, speed, startValue); } /// /// Instantiates an UIntField using the data read from a UXML file. /// public new class UxmlFactory : UxmlFactory { } /// /// Defines UxmlTraits for the UIntField. /// public new class UxmlTraits : TextValueFieldTraits { } public static uint ClampInput(long input) { input = input > uint.MaxValue ? uint.MaxValue : input; input = input < uint.MinValue ? uint.MinValue : input; return (uint)input; } class UIntInput : TextValueInput { internal UIntInput() { formatString = "#######0"; } UIntField parentUIntField => (UIntField)parent; protected override string allowedCharacters => "0123456789"; public override void ApplyInputDeviceDelta(Vector3 delta, DeltaSpeed speed, uint startValue) { var num = StringToValue(text) + (long)Math.Round(delta.x); var value = ClampInput(num); if (parentUIntField.isDelayed) text = ValueToString(value); else parentUIntField.value = value; } protected override string ValueToString(uint v) { return v.ToString(formatString); } protected override uint StringToValue(string str) { long.TryParse(str, out var result); return ClampInput(result); } } } }