UIScrollBar.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [AddComponentMenu("NGUI/Interaction/NGUI Scroll Bar")]
  5. public class UIScrollBar : UISlider
  6. {
  7. [Obsolete("Use 'value' instead")]
  8. public float scrollValue
  9. {
  10. get
  11. {
  12. return base.value;
  13. }
  14. set
  15. {
  16. base.value = value;
  17. }
  18. }
  19. public float barSize
  20. {
  21. get
  22. {
  23. return this.mSize;
  24. }
  25. set
  26. {
  27. float num = Mathf.Clamp01(value);
  28. if (this.mEnableFixSize)
  29. {
  30. float num2;
  31. if (base.isHorizontal)
  32. {
  33. num2 = this.mFG.localSize.x;
  34. }
  35. else
  36. {
  37. num2 = this.mFG.localSize.y;
  38. }
  39. num = this.mFixSizePixcel / num2;
  40. }
  41. if (this.mSize != num)
  42. {
  43. this.mSize = num;
  44. this.mIsDirty = true;
  45. if (NGUITools.GetActive(this))
  46. {
  47. if (UIProgressBar.current == null && this.onChange != null)
  48. {
  49. UIProgressBar.current = this;
  50. EventDelegate.Execute(this.onChange);
  51. UIProgressBar.current = null;
  52. }
  53. this.ForceUpdate();
  54. }
  55. }
  56. }
  57. }
  58. protected override void Upgrade()
  59. {
  60. if (this.mDir != UIScrollBar.Direction.Upgraded)
  61. {
  62. this.mValue = this.mScroll;
  63. if (this.mDir == UIScrollBar.Direction.Horizontal)
  64. {
  65. this.mFill = ((!this.mInverted) ? UIProgressBar.FillDirection.LeftToRight : UIProgressBar.FillDirection.RightToLeft);
  66. }
  67. else
  68. {
  69. this.mFill = ((!this.mInverted) ? UIProgressBar.FillDirection.TopToBottom : UIProgressBar.FillDirection.BottomToTop);
  70. }
  71. this.mDir = UIScrollBar.Direction.Upgraded;
  72. }
  73. }
  74. protected override void OnStart()
  75. {
  76. base.OnStart();
  77. if (this.mFG != null && this.mFG.gameObject != base.gameObject)
  78. {
  79. if (!(this.mFG.GetComponent<Collider>() != null) && !(this.mFG.GetComponent<Collider2D>() != null))
  80. {
  81. return;
  82. }
  83. UIEventListener uieventListener = UIEventListener.Get(this.mFG.gameObject);
  84. UIEventListener uieventListener2 = uieventListener;
  85. uieventListener2.onPress = (UIEventListener.BoolDelegate)Delegate.Combine(uieventListener2.onPress, new UIEventListener.BoolDelegate(base.OnPressForeground));
  86. UIEventListener uieventListener3 = uieventListener;
  87. uieventListener3.onDrag = (UIEventListener.VectorDelegate)Delegate.Combine(uieventListener3.onDrag, new UIEventListener.VectorDelegate(base.OnDragForeground));
  88. this.mFG.autoResizeBoxCollider = true;
  89. }
  90. }
  91. protected override float LocalToValue(Vector2 localPos)
  92. {
  93. if (!(this.mFG != null))
  94. {
  95. return base.LocalToValue(localPos);
  96. }
  97. float num = Mathf.Clamp01(this.mSize) * 0.5f;
  98. float num2 = num;
  99. float num3 = 1f - num;
  100. Vector3[] localCorners = this.mFG.localCorners;
  101. if (base.isHorizontal)
  102. {
  103. num2 = Mathf.Lerp(localCorners[0].x, localCorners[2].x, num2);
  104. num3 = Mathf.Lerp(localCorners[0].x, localCorners[2].x, num3);
  105. float num4 = num3 - num2;
  106. if (num4 == 0f)
  107. {
  108. return base.value;
  109. }
  110. return (!base.isInverted) ? ((localPos.x - num2) / num4) : ((num3 - localPos.x) / num4);
  111. }
  112. else
  113. {
  114. num2 = Mathf.Lerp(localCorners[0].y, localCorners[1].y, num2);
  115. num3 = Mathf.Lerp(localCorners[3].y, localCorners[2].y, num3);
  116. float num5 = num3 - num2;
  117. if (num5 == 0f)
  118. {
  119. return base.value;
  120. }
  121. return (!base.isInverted) ? ((localPos.y - num2) / num5) : ((num3 - localPos.y) / num5);
  122. }
  123. }
  124. public override void ForceUpdate()
  125. {
  126. if (this.mFG != null)
  127. {
  128. this.mIsDirty = false;
  129. float num = Mathf.Clamp01(this.mSize) * 0.5f;
  130. float num2 = Mathf.Lerp(num, 1f - num, base.value);
  131. float num3 = num2 - num;
  132. float num4 = num2 + num;
  133. if (base.isHorizontal)
  134. {
  135. this.mFG.drawRegion = ((!base.isInverted) ? new Vector4(num3, 0f, num4, 1f) : new Vector4(1f - num4, 0f, 1f - num3, 1f));
  136. }
  137. else
  138. {
  139. this.mFG.drawRegion = ((!base.isInverted) ? new Vector4(0f, num3, 1f, num4) : new Vector4(0f, 1f - num4, 1f, 1f - num3));
  140. }
  141. if (this.thumb != null)
  142. {
  143. Vector4 drawingDimensions = this.mFG.drawingDimensions;
  144. Vector3 position = new Vector3(Mathf.Lerp(drawingDimensions.x, drawingDimensions.z, 0.5f), Mathf.Lerp(drawingDimensions.y, drawingDimensions.w, 0.5f));
  145. base.SetThumbPosition(this.mFG.cachedTransform.TransformPoint(position));
  146. }
  147. }
  148. else
  149. {
  150. base.ForceUpdate();
  151. }
  152. }
  153. [HideInInspector]
  154. [SerializeField]
  155. protected float mSize = 1f;
  156. [HideInInspector]
  157. [SerializeField]
  158. private float mScroll;
  159. [HideInInspector]
  160. [SerializeField]
  161. private UIScrollBar.Direction mDir = UIScrollBar.Direction.Upgraded;
  162. public bool mEnableFixSize;
  163. public float mFixSizePixcel = 0.1f;
  164. private enum Direction
  165. {
  166. Horizontal,
  167. Vertical,
  168. Upgraded
  169. }
  170. }