GSlider.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using System;
  2. using UnityEngine;
  3. using FairyGUI.Utils;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class GSlider : GComponent
  10. {
  11. double _min;
  12. double _max;
  13. double _value;
  14. ProgressTitleType _titleType;
  15. bool _reverse;
  16. bool _wholeNumbers;
  17. GObject _titleObject;
  18. GObject _barObjectH;
  19. GObject _barObjectV;
  20. float _barMaxWidth;
  21. float _barMaxHeight;
  22. float _barMaxWidthDelta;
  23. float _barMaxHeightDelta;
  24. GObject _gripObject;
  25. Vector2 _clickPos;
  26. float _clickPercent;
  27. float _barStartX;
  28. float _barStartY;
  29. EventListener _onChanged;
  30. EventListener _onGripTouchEnd;
  31. public bool changeOnClick;
  32. public bool canDrag;
  33. public GSlider()
  34. {
  35. _value = 50;
  36. _max = 100;
  37. changeOnClick = true;
  38. canDrag = true;
  39. }
  40. /// <summary>
  41. ///
  42. /// </summary>
  43. public EventListener onChanged
  44. {
  45. get { return _onChanged ?? (_onChanged = new EventListener(this, "onChanged")); }
  46. }
  47. /// <summary>
  48. ///
  49. /// </summary>
  50. public EventListener onGripTouchEnd
  51. {
  52. get { return _onGripTouchEnd ?? (_onGripTouchEnd = new EventListener(this, "onGripTouchEnd")); }
  53. }
  54. /// <summary>
  55. ///
  56. /// </summary>
  57. public ProgressTitleType titleType
  58. {
  59. get
  60. {
  61. return _titleType;
  62. }
  63. set
  64. {
  65. if (_titleType != value)
  66. {
  67. _titleType = value;
  68. Update();
  69. }
  70. }
  71. }
  72. /// <summary>
  73. ///
  74. /// </summary>
  75. public double min
  76. {
  77. get
  78. {
  79. return _min;
  80. }
  81. set
  82. {
  83. if (_min != value)
  84. {
  85. _min = value;
  86. Update();
  87. }
  88. }
  89. }
  90. /// <summary>
  91. ///
  92. /// </summary>
  93. public double max
  94. {
  95. get
  96. {
  97. return _max;
  98. }
  99. set
  100. {
  101. if (_max != value)
  102. {
  103. _max = value;
  104. Update();
  105. }
  106. }
  107. }
  108. /// <summary>
  109. ///
  110. /// </summary>
  111. public double value
  112. {
  113. get
  114. {
  115. return _value;
  116. }
  117. set
  118. {
  119. if (_value != value)
  120. {
  121. _value = value;
  122. Update();
  123. }
  124. }
  125. }
  126. /// <summary>
  127. ///
  128. /// </summary>
  129. public bool wholeNumbers
  130. {
  131. get
  132. {
  133. return _wholeNumbers;
  134. }
  135. set
  136. {
  137. if (_wholeNumbers != value)
  138. {
  139. _wholeNumbers = value;
  140. Update();
  141. }
  142. }
  143. }
  144. private void Update()
  145. {
  146. UpdateWithPercent((float)((_value - _min) / (_max - _min)), false);
  147. }
  148. private void UpdateWithPercent(float percent, bool manual)
  149. {
  150. percent = Mathf.Clamp01(percent);
  151. if (manual)
  152. {
  153. double newValue = _min + (_max - _min) * percent;
  154. if (newValue < _min)
  155. newValue = _min;
  156. if (newValue > _max)
  157. newValue = _max;
  158. if (_wholeNumbers)
  159. {
  160. newValue = Math.Round(newValue);
  161. percent = Mathf.Clamp01((float)((newValue - _min) / (_max - _min)));
  162. }
  163. if (newValue != _value)
  164. {
  165. _value = newValue;
  166. if (DispatchEvent("onChanged", null))
  167. return;
  168. }
  169. }
  170. if (_titleObject != null)
  171. {
  172. switch (_titleType)
  173. {
  174. case ProgressTitleType.Percent:
  175. _titleObject.text = Mathf.FloorToInt(percent * 100) + "%";
  176. break;
  177. case ProgressTitleType.ValueAndMax:
  178. _titleObject.text = Math.Round(_value) + "/" + Math.Round(max);
  179. break;
  180. case ProgressTitleType.Value:
  181. _titleObject.text = "" + Math.Round(_value);
  182. break;
  183. case ProgressTitleType.Max:
  184. _titleObject.text = "" + Math.Round(_max);
  185. break;
  186. }
  187. }
  188. float fullWidth = this.width - _barMaxWidthDelta;
  189. float fullHeight = this.height - _barMaxHeightDelta;
  190. if (!_reverse)
  191. {
  192. if (_barObjectH != null)
  193. {
  194. if (!SetFillAmount(_barObjectH, percent))
  195. _barObjectH.width = Mathf.RoundToInt(fullWidth * percent);
  196. }
  197. if (_barObjectV != null)
  198. {
  199. if (!SetFillAmount(_barObjectV, percent))
  200. _barObjectV.height = Mathf.RoundToInt(fullHeight * percent);
  201. }
  202. }
  203. else
  204. {
  205. if (_barObjectH != null)
  206. {
  207. if (!SetFillAmount(_barObjectH, 1 - percent))
  208. {
  209. _barObjectH.width = Mathf.RoundToInt(fullWidth * percent);
  210. _barObjectH.x = _barStartX + (fullWidth - _barObjectH.width);
  211. }
  212. }
  213. if (_barObjectV != null)
  214. {
  215. if (!SetFillAmount(_barObjectV, 1 - percent))
  216. {
  217. _barObjectV.height = Mathf.RoundToInt(fullHeight * percent);
  218. _barObjectV.y = _barStartY + (fullHeight - _barObjectV.height);
  219. }
  220. }
  221. }
  222. InvalidateBatchingState(true);
  223. }
  224. bool SetFillAmount(GObject bar, float amount)
  225. {
  226. if ((bar is GImage) && ((GImage)bar).fillMethod != FillMethod.None)
  227. ((GImage)bar).fillAmount = amount;
  228. else if ((bar is GLoader) && ((GLoader)bar).fillMethod != FillMethod.None)
  229. ((GLoader)bar).fillAmount = amount;
  230. else
  231. return false;
  232. return true;
  233. }
  234. override protected void ConstructExtension(ByteBuffer buffer)
  235. {
  236. buffer.Seek(0, 6);
  237. _titleType = (ProgressTitleType)buffer.ReadByte();
  238. _reverse = buffer.ReadBool();
  239. if (buffer.version >= 2)
  240. {
  241. _wholeNumbers = buffer.ReadBool();
  242. this.changeOnClick = buffer.ReadBool();
  243. }
  244. _titleObject = GetChild("title");
  245. _barObjectH = GetChild("bar");
  246. _barObjectV = GetChild("bar_v");
  247. _gripObject = GetChild("grip");
  248. if (_barObjectH != null)
  249. {
  250. _barMaxWidth = _barObjectH.width;
  251. _barMaxWidthDelta = this.width - _barMaxWidth;
  252. _barStartX = _barObjectH.x;
  253. }
  254. if (_barObjectV != null)
  255. {
  256. _barMaxHeight = _barObjectV.height;
  257. _barMaxHeightDelta = this.height - _barMaxHeight;
  258. _barStartY = _barObjectV.y;
  259. }
  260. if (_gripObject != null)
  261. {
  262. _gripObject.onTouchBegin.Add(__gripTouchBegin);
  263. _gripObject.onTouchMove.Add(__gripTouchMove);
  264. _gripObject.onTouchEnd.Add(__gripTouchEnd);
  265. }
  266. onTouchBegin.Add(__barTouchBegin);
  267. }
  268. override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
  269. {
  270. base.Setup_AfterAdd(buffer, beginPos);
  271. if (!buffer.Seek(beginPos, 6))
  272. {
  273. Update();
  274. return;
  275. }
  276. if ((ObjectType)buffer.ReadByte() != packageItem.objectType)
  277. {
  278. Update();
  279. return;
  280. }
  281. _value = buffer.ReadInt();
  282. _max = buffer.ReadInt();
  283. if (buffer.version >= 2)
  284. _min = buffer.ReadInt();
  285. Update();
  286. }
  287. override protected void HandleSizeChanged()
  288. {
  289. base.HandleSizeChanged();
  290. if (_barObjectH != null)
  291. _barMaxWidth = this.width - _barMaxWidthDelta;
  292. if (_barObjectV != null)
  293. _barMaxHeight = this.height - _barMaxHeightDelta;
  294. if (!this.underConstruct)
  295. Update();
  296. }
  297. private void __gripTouchBegin(EventContext context)
  298. {
  299. this.canDrag = true;
  300. context.StopPropagation();
  301. InputEvent evt = context.inputEvent;
  302. if (evt.button != 0)
  303. return;
  304. context.CaptureTouch();
  305. _clickPos = this.GlobalToLocal(new Vector2(evt.x, evt.y));
  306. _clickPercent = Mathf.Clamp01((float)((_value - _min) / (_max - _min)));
  307. }
  308. private void __gripTouchMove(EventContext context)
  309. {
  310. if (!this.canDrag)
  311. return;
  312. InputEvent evt = context.inputEvent;
  313. Vector2 pt = this.GlobalToLocal(new Vector2(evt.x, evt.y));
  314. if (float.IsNaN(pt.x))
  315. return;
  316. float deltaX = pt.x - _clickPos.x;
  317. float deltaY = pt.y - _clickPos.y;
  318. if (_reverse)
  319. {
  320. deltaX = -deltaX;
  321. deltaY = -deltaY;
  322. }
  323. float percent;
  324. if (_barObjectH != null)
  325. percent = _clickPercent + deltaX / _barMaxWidth;
  326. else
  327. percent = _clickPercent + deltaY / _barMaxHeight;
  328. UpdateWithPercent(percent, true);
  329. }
  330. private void __gripTouchEnd(EventContext context)
  331. {
  332. DispatchEvent("onGripTouchEnd", null);
  333. }
  334. private void __barTouchBegin(EventContext context)
  335. {
  336. if (!changeOnClick)
  337. return;
  338. InputEvent evt = context.inputEvent;
  339. Vector2 pt = _gripObject.GlobalToLocal(new Vector2(evt.x, evt.y));
  340. float percent = Mathf.Clamp01((float)((_value - _min) / (_max - _min)));
  341. float delta = 0;
  342. if (_barObjectH != null)
  343. delta = (pt.x - _gripObject.width / 2) / _barMaxWidth;
  344. if (_barObjectV != null)
  345. delta = (pt.y - _gripObject.height / 2) / _barMaxHeight;
  346. if (_reverse)
  347. percent -= delta;
  348. else
  349. percent += delta;
  350. UpdateWithPercent(percent, true);
  351. }
  352. }
  353. }