NumberControl.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Collections.Generic;
  3. using SRF;
  4. using SRF.UI;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. using UnityEngine.UI;
  8. namespace SRDebugger.UI.Controls.Data
  9. {
  10. public class NumberControl : DataBoundControl
  11. {
  12. protected override void Start()
  13. {
  14. base.Start();
  15. this.NumberSpinner.onEndEdit.AddListener(new UnityAction<string>(this.OnValueChanged));
  16. }
  17. private void OnValueChanged(string newValue)
  18. {
  19. try
  20. {
  21. object newValue2 = Convert.ChangeType(newValue, this._type);
  22. base.UpdateValue(newValue2);
  23. }
  24. catch (Exception)
  25. {
  26. this.NumberSpinner.text = this._lastValue;
  27. }
  28. }
  29. protected override void OnBind(string propertyName, Type t)
  30. {
  31. base.OnBind(propertyName, t);
  32. this.Title.text = propertyName;
  33. if (NumberControl.IsIntegerType(t))
  34. {
  35. this.NumberSpinner.contentType = InputField.ContentType.IntegerNumber;
  36. }
  37. else
  38. {
  39. if (!NumberControl.IsDecimalType(t))
  40. {
  41. throw new ArgumentException("Type must be one of expected types", "t");
  42. }
  43. this.NumberSpinner.contentType = InputField.ContentType.DecimalNumber;
  44. }
  45. SROptions.NumberRangeAttribute attribute = base.Property.GetAttribute<SROptions.NumberRangeAttribute>();
  46. this.NumberSpinner.MaxValue = this.GetMaxValue(t);
  47. this.NumberSpinner.MinValue = this.GetMinValue(t);
  48. if (attribute != null)
  49. {
  50. this.NumberSpinner.MaxValue = Math.Min(attribute.Max, this.NumberSpinner.MaxValue);
  51. this.NumberSpinner.MinValue = Math.Max(attribute.Min, this.NumberSpinner.MinValue);
  52. }
  53. SROptions.IncrementAttribute attribute2 = base.Property.GetAttribute<SROptions.IncrementAttribute>();
  54. if (attribute2 != null)
  55. {
  56. if (this.UpNumberButton != null)
  57. {
  58. this.UpNumberButton.Amount = attribute2.Increment;
  59. }
  60. if (this.DownNumberButton != null)
  61. {
  62. this.DownNumberButton.Amount = -attribute2.Increment;
  63. }
  64. }
  65. this._type = t;
  66. this.NumberSpinner.interactable = !base.IsReadOnly;
  67. if (this.DisableOnReadOnly != null)
  68. {
  69. foreach (GameObject gameObject in this.DisableOnReadOnly)
  70. {
  71. gameObject.SetActive(!base.IsReadOnly);
  72. }
  73. }
  74. }
  75. protected override void OnValueUpdated(object newValue)
  76. {
  77. string text = Convert.ToString(newValue);
  78. if (text != this._lastValue)
  79. {
  80. this.NumberSpinner.text = text;
  81. }
  82. this._lastValue = text;
  83. }
  84. public override bool CanBind(Type type, bool isReadOnly)
  85. {
  86. return NumberControl.IsDecimalType(type) || NumberControl.IsIntegerType(type);
  87. }
  88. protected static bool IsIntegerType(Type t)
  89. {
  90. for (int i = 0; i < NumberControl.IntegerTypes.Length; i++)
  91. {
  92. if (NumberControl.IntegerTypes[i] == t)
  93. {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. protected static bool IsDecimalType(Type t)
  100. {
  101. for (int i = 0; i < NumberControl.DecimalTypes.Length; i++)
  102. {
  103. if (NumberControl.DecimalTypes[i] == t)
  104. {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. protected double GetMaxValue(Type t)
  111. {
  112. NumberControl.ValueRange valueRange;
  113. if (NumberControl.ValueRanges.TryGetValue(t, out valueRange))
  114. {
  115. return valueRange.MaxValue;
  116. }
  117. UnityEngine.Debug.LogWarning("[NumberControl] No MaxValue stored for type {0}".Fmt(new object[]
  118. {
  119. t
  120. }));
  121. return double.MaxValue;
  122. }
  123. protected double GetMinValue(Type t)
  124. {
  125. NumberControl.ValueRange valueRange;
  126. if (NumberControl.ValueRanges.TryGetValue(t, out valueRange))
  127. {
  128. return valueRange.MinValue;
  129. }
  130. UnityEngine.Debug.LogWarning("[NumberControl] No MinValue stored for type {0}".Fmt(new object[]
  131. {
  132. t
  133. }));
  134. return double.MinValue;
  135. }
  136. private static readonly Type[] IntegerTypes = new Type[]
  137. {
  138. typeof(int),
  139. typeof(short),
  140. typeof(byte),
  141. typeof(sbyte),
  142. typeof(uint),
  143. typeof(ushort)
  144. };
  145. private static readonly Type[] DecimalTypes = new Type[]
  146. {
  147. typeof(float),
  148. typeof(double)
  149. };
  150. public static readonly Dictionary<Type, NumberControl.ValueRange> ValueRanges = new Dictionary<Type, NumberControl.ValueRange>
  151. {
  152. {
  153. typeof(int),
  154. new NumberControl.ValueRange
  155. {
  156. MaxValue = 2147483647.0,
  157. MinValue = -2147483648.0
  158. }
  159. },
  160. {
  161. typeof(short),
  162. new NumberControl.ValueRange
  163. {
  164. MaxValue = 32767.0,
  165. MinValue = -32768.0
  166. }
  167. },
  168. {
  169. typeof(byte),
  170. new NumberControl.ValueRange
  171. {
  172. MaxValue = 255.0,
  173. MinValue = 0.0
  174. }
  175. },
  176. {
  177. typeof(sbyte),
  178. new NumberControl.ValueRange
  179. {
  180. MaxValue = 127.0,
  181. MinValue = -128.0
  182. }
  183. },
  184. {
  185. typeof(uint),
  186. new NumberControl.ValueRange
  187. {
  188. MaxValue = 4294967295.0,
  189. MinValue = 0.0
  190. }
  191. },
  192. {
  193. typeof(ushort),
  194. new NumberControl.ValueRange
  195. {
  196. MaxValue = 65535.0,
  197. MinValue = 0.0
  198. }
  199. },
  200. {
  201. typeof(float),
  202. new NumberControl.ValueRange
  203. {
  204. MaxValue = 3.4028234663852886E+38,
  205. MinValue = -3.4028234663852886E+38
  206. }
  207. },
  208. {
  209. typeof(double),
  210. new NumberControl.ValueRange
  211. {
  212. MaxValue = double.MaxValue,
  213. MinValue = double.MinValue
  214. }
  215. }
  216. };
  217. private string _lastValue;
  218. private Type _type;
  219. public GameObject[] DisableOnReadOnly;
  220. public SRNumberButton DownNumberButton;
  221. [RequiredField]
  222. public SRNumberSpinner NumberSpinner;
  223. [RequiredField]
  224. public Text Title;
  225. public SRNumberButton UpNumberButton;
  226. public struct ValueRange
  227. {
  228. public double MaxValue;
  229. public double MinValue;
  230. }
  231. }
  232. }