NumberControl.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System.Globalization;
  2. namespace SRDebugger.UI.Controls.Data
  3. {
  4. using System;
  5. using System.Collections.Generic;
  6. using SRF;
  7. using SRF.UI;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. public class NumberControl : DataBoundControl
  11. {
  12. private static readonly Type[] IntegerTypes =
  13. {
  14. typeof (int), typeof (short), typeof (byte), typeof (sbyte), typeof (uint), typeof (ushort)
  15. };
  16. private static readonly Type[] DecimalTypes =
  17. {
  18. typeof (float), typeof (double)
  19. };
  20. public static readonly Dictionary<Type, ValueRange> ValueRanges = new Dictionary<Type, ValueRange>
  21. {
  22. {typeof (int), new ValueRange {MaxValue = int.MaxValue, MinValue = int.MinValue}},
  23. {typeof (short), new ValueRange {MaxValue = short.MaxValue, MinValue = short.MinValue}},
  24. {typeof (byte), new ValueRange {MaxValue = byte.MaxValue, MinValue = byte.MinValue}},
  25. {typeof (sbyte), new ValueRange {MaxValue = sbyte.MaxValue, MinValue = sbyte.MinValue}},
  26. {typeof (uint), new ValueRange {MaxValue = uint.MaxValue, MinValue = uint.MinValue}},
  27. {typeof (ushort), new ValueRange {MaxValue = ushort.MaxValue, MinValue = ushort.MinValue}},
  28. {typeof (float), new ValueRange {MaxValue = float.MaxValue, MinValue = float.MinValue}},
  29. {typeof (double), new ValueRange {MaxValue = double.MaxValue, MinValue = double.MinValue}}
  30. };
  31. private string _lastValue;
  32. private Type _type;
  33. public GameObject[] DisableOnReadOnly;
  34. public SRNumberButton DownNumberButton;
  35. [RequiredField] public SRNumberSpinner NumberSpinner;
  36. [RequiredField] public Text Title;
  37. public SRNumberButton UpNumberButton;
  38. protected override void Start()
  39. {
  40. base.Start();
  41. NumberSpinner.onEndEdit.AddListener(OnValueChanged);
  42. }
  43. private void OnValueChanged(string newValue)
  44. {
  45. try
  46. {
  47. var num = Convert.ChangeType(newValue, _type, CultureInfo.InvariantCulture);
  48. UpdateValue(num);
  49. }
  50. catch (Exception)
  51. {
  52. NumberSpinner.text = _lastValue;
  53. }
  54. LayoutRebuilder.MarkLayoutForRebuild(GetComponent<RectTransform>());
  55. }
  56. protected override void OnBind(string propertyName, Type t)
  57. {
  58. base.OnBind(propertyName, t);
  59. Title.text = propertyName;
  60. if (IsIntegerType(t))
  61. {
  62. NumberSpinner.contentType = InputField.ContentType.IntegerNumber;
  63. }
  64. else if (IsDecimalType(t))
  65. {
  66. NumberSpinner.contentType = InputField.ContentType.DecimalNumber;
  67. }
  68. else
  69. {
  70. throw new ArgumentException("Type must be one of expected types", "t");
  71. }
  72. var rangeAttrib = Property.GetAttribute<NumberRangeAttribute>();
  73. NumberSpinner.MaxValue = GetMaxValue(t);
  74. NumberSpinner.MinValue = GetMinValue(t);
  75. if (rangeAttrib != null)
  76. {
  77. NumberSpinner.MaxValue = Math.Min(rangeAttrib.Max, NumberSpinner.MaxValue);
  78. NumberSpinner.MinValue = Math.Max(rangeAttrib.Min, NumberSpinner.MinValue);
  79. }
  80. var incrementAttribute = Property.GetAttribute<IncrementAttribute>();
  81. if (incrementAttribute != null)
  82. {
  83. if (UpNumberButton != null)
  84. {
  85. UpNumberButton.Amount = incrementAttribute.Increment;
  86. }
  87. if (DownNumberButton != null)
  88. {
  89. DownNumberButton.Amount = -incrementAttribute.Increment;
  90. }
  91. }
  92. _type = t;
  93. NumberSpinner.interactable = !IsReadOnly;
  94. if (DisableOnReadOnly != null)
  95. {
  96. foreach (var childControl in DisableOnReadOnly)
  97. {
  98. childControl.SetActive(!IsReadOnly);
  99. }
  100. }
  101. }
  102. protected override void OnValueUpdated(object newValue)
  103. {
  104. var value = Convert.ToDecimal(newValue, CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture);
  105. if (value != _lastValue)
  106. {
  107. NumberSpinner.text = value;
  108. }
  109. _lastValue = value;
  110. }
  111. public override bool CanBind(Type type, bool isReadOnly)
  112. {
  113. return IsDecimalType(type) || IsIntegerType(type);
  114. }
  115. protected static bool IsIntegerType(Type t)
  116. {
  117. for (var i = 0; i < IntegerTypes.Length; i++)
  118. {
  119. if (IntegerTypes[i] == t)
  120. {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. protected static bool IsDecimalType(Type t)
  127. {
  128. for (var i = 0; i < DecimalTypes.Length; i++)
  129. {
  130. if (DecimalTypes[i] == t)
  131. {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. protected double GetMaxValue(Type t)
  138. {
  139. ValueRange value;
  140. if (ValueRanges.TryGetValue(t, out value))
  141. {
  142. return value.MaxValue;
  143. }
  144. Debug.LogWarning("[NumberControl] No MaxValue stored for type {0}".Fmt(t));
  145. return double.MaxValue;
  146. }
  147. protected double GetMinValue(Type t)
  148. {
  149. ValueRange value;
  150. if (ValueRanges.TryGetValue(t, out value))
  151. {
  152. return value.MinValue;
  153. }
  154. Debug.LogWarning("[NumberControl] No MinValue stored for type {0}".Fmt(t));
  155. return double.MinValue;
  156. }
  157. public struct ValueRange
  158. {
  159. public double MaxValue;
  160. public double MinValue;
  161. }
  162. }
  163. }