GScrollBar.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using UnityEngine;
  2. using FairyGUI.Utils;
  3. namespace FairyGUI
  4. {
  5. /// <summary>
  6. /// GScrollBar class.
  7. /// </summary>
  8. public class GScrollBar : GComponent
  9. {
  10. GObject _grip;
  11. GObject _arrowButton1;
  12. GObject _arrowButton2;
  13. GObject _bar;
  14. ScrollPane _target;
  15. bool _vertical;
  16. float _scrollPerc;
  17. bool _fixedGripSize;
  18. bool _gripDragging;
  19. Vector2 _dragOffset;
  20. public GScrollBar()
  21. {
  22. _scrollPerc = 0;
  23. }
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. /// <param name="target"></param>
  28. /// <param name="vertical"></param>
  29. public void SetScrollPane(ScrollPane target, bool vertical)
  30. {
  31. _target = target;
  32. _vertical = vertical;
  33. }
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. public void SetDisplayPerc(float value)
  38. {
  39. if (_vertical)
  40. {
  41. if (!_fixedGripSize)
  42. _grip.height = Mathf.FloorToInt(value * _bar.height);
  43. _grip.y = Mathf.RoundToInt(_bar.y + (_bar.height - _grip.height) * _scrollPerc);
  44. }
  45. else
  46. {
  47. if (!_fixedGripSize)
  48. _grip.width = Mathf.FloorToInt(value * _bar.width);
  49. _grip.x = Mathf.RoundToInt(_bar.x + (_bar.width - _grip.width) * _scrollPerc);
  50. }
  51. _grip.visible = value != 0 && value != 1;
  52. }
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. public void setScrollPerc(float value)
  57. {
  58. _scrollPerc = value;
  59. if (_vertical)
  60. _grip.y = Mathf.RoundToInt(_bar.y + (_bar.height - _grip.height) * _scrollPerc);
  61. else
  62. _grip.x = Mathf.RoundToInt(_bar.x + (_bar.width - _grip.width) * _scrollPerc);
  63. }
  64. /// <summary>
  65. ///
  66. /// </summary>
  67. public float minSize
  68. {
  69. get
  70. {
  71. if (_vertical)
  72. return (_arrowButton1 != null ? _arrowButton1.height : 0) + (_arrowButton2 != null ? _arrowButton2.height : 0);
  73. else
  74. return (_arrowButton1 != null ? _arrowButton1.width : 0) + (_arrowButton2 != null ? _arrowButton2.width : 0);
  75. }
  76. }
  77. /// <summary>
  78. ///
  79. /// </summary>
  80. public bool gripDragging
  81. {
  82. get
  83. {
  84. return _gripDragging;
  85. }
  86. }
  87. override protected void ConstructExtension(ByteBuffer buffer)
  88. {
  89. buffer.Seek(0, 6);
  90. _fixedGripSize = buffer.ReadBool();
  91. _grip = GetChild("grip");
  92. if (_grip == null)
  93. {
  94. Debug.LogWarning("FairyGUI: " + this.resourceURL + " should define grip");
  95. return;
  96. }
  97. _bar = GetChild("bar");
  98. if (_bar == null)
  99. {
  100. Debug.LogWarning("FairyGUI: " + this.resourceURL + " should define bar");
  101. return;
  102. }
  103. _arrowButton1 = GetChild("arrow1");
  104. _arrowButton2 = GetChild("arrow2");
  105. _grip.onTouchBegin.Add(__gripTouchBegin);
  106. _grip.onTouchMove.Add(__gripTouchMove);
  107. _grip.onTouchEnd.Add(__gripTouchEnd);
  108. this.onTouchBegin.Add(__touchBegin);
  109. if (_arrowButton1 != null)
  110. _arrowButton1.onTouchBegin.Add(__arrowButton1Click);
  111. if (_arrowButton2 != null)
  112. _arrowButton2.onTouchBegin.Add(__arrowButton2Click);
  113. }
  114. void __gripTouchBegin(EventContext context)
  115. {
  116. if (_bar == null)
  117. return;
  118. context.StopPropagation();
  119. InputEvent evt = context.inputEvent;
  120. if (evt.button != 0)
  121. return;
  122. context.CaptureTouch();
  123. _gripDragging = true;
  124. _target.UpdateScrollBarVisible();
  125. _dragOffset = this.GlobalToLocal(new Vector2(evt.x, evt.y)) - _grip.xy;
  126. }
  127. void __gripTouchMove(EventContext context)
  128. {
  129. InputEvent evt = context.inputEvent;
  130. Vector2 pt = this.GlobalToLocal(new Vector2(evt.x, evt.y));
  131. if (float.IsNaN(pt.x))
  132. return;
  133. if (_vertical)
  134. {
  135. float curY = pt.y - _dragOffset.y;
  136. float diff = _bar.height - _grip.height;
  137. if (diff == 0)
  138. _target.percY = 0;
  139. else
  140. _target.percY = (curY - _bar.y) / diff;
  141. }
  142. else
  143. {
  144. float curX = pt.x - _dragOffset.x;
  145. float diff = _bar.width - _grip.width;
  146. if (diff == 0)
  147. _target.percX = 0;
  148. else
  149. _target.percX = (curX - _bar.x) / diff;
  150. }
  151. }
  152. void __gripTouchEnd(EventContext context)
  153. {
  154. _gripDragging = false;
  155. _target.UpdateScrollBarVisible();
  156. }
  157. void __arrowButton1Click(EventContext context)
  158. {
  159. context.StopPropagation();
  160. if (_vertical)
  161. _target.ScrollUp();
  162. else
  163. _target.ScrollLeft();
  164. }
  165. void __arrowButton2Click(EventContext context)
  166. {
  167. context.StopPropagation();
  168. if (_vertical)
  169. _target.ScrollDown();
  170. else
  171. _target.ScrollRight();
  172. }
  173. void __touchBegin(EventContext context)
  174. {
  175. context.StopPropagation();
  176. InputEvent evt = context.inputEvent;
  177. Vector2 pt = _grip.GlobalToLocal(new Vector2(evt.x, evt.y));
  178. if (_vertical)
  179. {
  180. if (pt.y < 0)
  181. _target.ScrollUp(4, false);
  182. else
  183. _target.ScrollDown(4, false);
  184. }
  185. else
  186. {
  187. if (pt.x < 0)
  188. _target.ScrollLeft(4, false);
  189. else
  190. _target.ScrollRight(4, false);
  191. }
  192. }
  193. }
  194. }