UISlider.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [AddComponentMenu("NGUI/Interaction/NGUI Slider")]
  5. public class UISlider : UIProgressBar
  6. {
  7. [Obsolete("Use 'value' instead")]
  8. public float sliderValue
  9. {
  10. get
  11. {
  12. return base.value;
  13. }
  14. set
  15. {
  16. base.value = value;
  17. }
  18. }
  19. [Obsolete("Use 'fillDirection' instead")]
  20. public bool inverted
  21. {
  22. get
  23. {
  24. return base.isInverted;
  25. }
  26. set
  27. {
  28. }
  29. }
  30. protected override void Upgrade()
  31. {
  32. if (this.direction != UISlider.Direction.Upgraded)
  33. {
  34. this.mValue = this.rawValue;
  35. if (this.foreground != null)
  36. {
  37. this.mFG = this.foreground.GetComponent<UIWidget>();
  38. }
  39. if (this.direction == UISlider.Direction.Horizontal)
  40. {
  41. this.mFill = ((!this.mInverted) ? UIProgressBar.FillDirection.LeftToRight : UIProgressBar.FillDirection.RightToLeft);
  42. }
  43. else
  44. {
  45. this.mFill = ((!this.mInverted) ? UIProgressBar.FillDirection.BottomToTop : UIProgressBar.FillDirection.TopToBottom);
  46. }
  47. this.direction = UISlider.Direction.Upgraded;
  48. }
  49. }
  50. protected override void OnStart()
  51. {
  52. GameObject go = (!(this.mBG != null) || (!(this.mBG.GetComponent<Collider>() != null) && !(this.mBG.GetComponent<Collider2D>() != null))) ? base.gameObject : this.mBG.gameObject;
  53. UIEventListener uieventListener = UIEventListener.Get(go);
  54. UIEventListener uieventListener2 = uieventListener;
  55. uieventListener2.onPress = (UIEventListener.BoolDelegate)Delegate.Combine(uieventListener2.onPress, new UIEventListener.BoolDelegate(this.OnPressBackground));
  56. UIEventListener uieventListener3 = uieventListener;
  57. uieventListener3.onDrag = (UIEventListener.VectorDelegate)Delegate.Combine(uieventListener3.onDrag, new UIEventListener.VectorDelegate(this.OnDragBackground));
  58. if (this.thumb != null && (this.thumb.GetComponent<Collider>() != null || this.thumb.GetComponent<Collider2D>() != null) && (this.mFG == null || this.thumb != this.mFG.cachedTransform))
  59. {
  60. UIEventListener uieventListener4 = UIEventListener.Get(this.thumb.gameObject);
  61. UIEventListener uieventListener5 = uieventListener4;
  62. uieventListener5.onPress = (UIEventListener.BoolDelegate)Delegate.Combine(uieventListener5.onPress, new UIEventListener.BoolDelegate(this.OnPressForeground));
  63. UIEventListener uieventListener6 = uieventListener4;
  64. uieventListener6.onDrag = (UIEventListener.VectorDelegate)Delegate.Combine(uieventListener6.onDrag, new UIEventListener.VectorDelegate(this.OnDragForeground));
  65. }
  66. }
  67. protected void OnPressBackground(GameObject go, bool isPressed)
  68. {
  69. if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
  70. {
  71. return;
  72. }
  73. this.mCam = UICamera.currentCamera;
  74. base.value = base.ScreenToValue(UICamera.lastTouchPosition);
  75. if (!isPressed && this.onDragFinished != null)
  76. {
  77. this.onDragFinished();
  78. }
  79. }
  80. protected void OnDragBackground(GameObject go, Vector2 delta)
  81. {
  82. if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
  83. {
  84. return;
  85. }
  86. this.mCam = UICamera.currentCamera;
  87. base.value = base.ScreenToValue(UICamera.lastTouchPosition);
  88. }
  89. protected void OnPressForeground(GameObject go, bool isPressed)
  90. {
  91. if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
  92. {
  93. return;
  94. }
  95. this.mCam = UICamera.currentCamera;
  96. if (isPressed)
  97. {
  98. this.mOffset = ((!(this.mFG == null)) ? (base.value - base.ScreenToValue(UICamera.lastTouchPosition)) : 0f);
  99. }
  100. else if (this.onDragFinished != null)
  101. {
  102. this.onDragFinished();
  103. }
  104. }
  105. protected void OnDragForeground(GameObject go, Vector2 delta)
  106. {
  107. if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
  108. {
  109. return;
  110. }
  111. this.mCam = UICamera.currentCamera;
  112. base.value = this.mOffset + base.ScreenToValue(UICamera.lastTouchPosition);
  113. }
  114. protected void OnKey(KeyCode key)
  115. {
  116. if (base.enabled)
  117. {
  118. float num = ((float)this.numberOfSteps <= 1f) ? 0.125f : (1f / (float)(this.numberOfSteps - 1));
  119. switch (this.mFill)
  120. {
  121. case UIProgressBar.FillDirection.LeftToRight:
  122. if (key == KeyCode.LeftArrow)
  123. {
  124. base.value = this.mValue - num;
  125. }
  126. else if (key == KeyCode.RightArrow)
  127. {
  128. base.value = this.mValue + num;
  129. }
  130. break;
  131. case UIProgressBar.FillDirection.RightToLeft:
  132. if (key == KeyCode.LeftArrow)
  133. {
  134. base.value = this.mValue + num;
  135. }
  136. else if (key == KeyCode.RightArrow)
  137. {
  138. base.value = this.mValue - num;
  139. }
  140. break;
  141. case UIProgressBar.FillDirection.BottomToTop:
  142. if (key == KeyCode.DownArrow)
  143. {
  144. base.value = this.mValue - num;
  145. }
  146. else if (key == KeyCode.UpArrow)
  147. {
  148. base.value = this.mValue + num;
  149. }
  150. break;
  151. case UIProgressBar.FillDirection.TopToBottom:
  152. if (key == KeyCode.DownArrow)
  153. {
  154. base.value = this.mValue + num;
  155. }
  156. else if (key == KeyCode.UpArrow)
  157. {
  158. base.value = this.mValue - num;
  159. }
  160. break;
  161. }
  162. }
  163. }
  164. [HideInInspector]
  165. [SerializeField]
  166. private Transform foreground;
  167. [HideInInspector]
  168. [SerializeField]
  169. private float rawValue = 1f;
  170. [HideInInspector]
  171. [SerializeField]
  172. private UISlider.Direction direction = UISlider.Direction.Upgraded;
  173. [HideInInspector]
  174. [SerializeField]
  175. protected bool mInverted;
  176. private enum Direction
  177. {
  178. Horizontal,
  179. Vertical,
  180. Upgraded
  181. }
  182. }