UIProgressBar.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [ExecuteInEditMode]
  5. [AddComponentMenu("NGUI/Interaction/NGUI Progress Bar")]
  6. public class UIProgressBar : UIWidgetContainer
  7. {
  8. public Transform cachedTransform
  9. {
  10. get
  11. {
  12. if (this.mTrans == null)
  13. {
  14. this.mTrans = base.transform;
  15. }
  16. return this.mTrans;
  17. }
  18. }
  19. public Camera cachedCamera
  20. {
  21. get
  22. {
  23. if (this.mCam == null)
  24. {
  25. this.mCam = NGUITools.FindCameraForLayer(base.gameObject.layer);
  26. }
  27. return this.mCam;
  28. }
  29. }
  30. public UIWidget foregroundWidget
  31. {
  32. get
  33. {
  34. return this.mFG;
  35. }
  36. set
  37. {
  38. if (this.mFG != value)
  39. {
  40. this.mFG = value;
  41. this.mIsDirty = true;
  42. }
  43. }
  44. }
  45. public UIWidget backgroundWidget
  46. {
  47. get
  48. {
  49. return this.mBG;
  50. }
  51. set
  52. {
  53. if (this.mBG != value)
  54. {
  55. this.mBG = value;
  56. this.mIsDirty = true;
  57. }
  58. }
  59. }
  60. public UIProgressBar.FillDirection fillDirection
  61. {
  62. get
  63. {
  64. return this.mFill;
  65. }
  66. set
  67. {
  68. if (this.mFill != value)
  69. {
  70. this.mFill = value;
  71. this.ForceUpdate();
  72. }
  73. }
  74. }
  75. public float value
  76. {
  77. get
  78. {
  79. if (this.numberOfSteps > 1)
  80. {
  81. return Mathf.Round(this.mValue * (float)(this.numberOfSteps - 1)) / (float)(this.numberOfSteps - 1);
  82. }
  83. return this.mValue;
  84. }
  85. set
  86. {
  87. float num = Mathf.Clamp01(value);
  88. if (this.mValue != num)
  89. {
  90. float value2 = this.value;
  91. this.mValue = num;
  92. if (value2 != this.value)
  93. {
  94. this.ForceUpdate();
  95. if (UIProgressBar.current == null && NGUITools.GetActive(this) && EventDelegate.IsValid(this.onChange))
  96. {
  97. UIProgressBar.current = this;
  98. EventDelegate.Execute(this.onChange);
  99. UIProgressBar.current = null;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. public float alpha
  106. {
  107. get
  108. {
  109. if (this.mFG != null)
  110. {
  111. return this.mFG.alpha;
  112. }
  113. if (this.mBG != null)
  114. {
  115. return this.mBG.alpha;
  116. }
  117. return 1f;
  118. }
  119. set
  120. {
  121. if (this.mFG != null)
  122. {
  123. this.mFG.alpha = value;
  124. if (this.mFG.GetComponent<Collider>() != null)
  125. {
  126. this.mFG.GetComponent<Collider>().enabled = (this.mFG.alpha > 0.001f);
  127. }
  128. else if (this.mFG.GetComponent<Collider2D>() != null)
  129. {
  130. this.mFG.GetComponent<Collider2D>().enabled = (this.mFG.alpha > 0.001f);
  131. }
  132. }
  133. if (this.mBG != null)
  134. {
  135. this.mBG.alpha = value;
  136. if (this.mBG.GetComponent<Collider>() != null)
  137. {
  138. this.mBG.GetComponent<Collider>().enabled = (this.mBG.alpha > 0.001f);
  139. }
  140. else if (this.mBG.GetComponent<Collider2D>() != null)
  141. {
  142. this.mBG.GetComponent<Collider2D>().enabled = (this.mBG.alpha > 0.001f);
  143. }
  144. }
  145. if (this.thumb != null)
  146. {
  147. UIWidget component = this.thumb.GetComponent<UIWidget>();
  148. if (component != null)
  149. {
  150. component.alpha = value;
  151. if (component.GetComponent<Collider>() != null)
  152. {
  153. component.GetComponent<Collider>().enabled = (component.alpha > 0.001f);
  154. }
  155. else if (component.GetComponent<Collider2D>() != null)
  156. {
  157. component.GetComponent<Collider2D>().enabled = (component.alpha > 0.001f);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. protected bool isHorizontal
  164. {
  165. get
  166. {
  167. return this.mFill == UIProgressBar.FillDirection.LeftToRight || this.mFill == UIProgressBar.FillDirection.RightToLeft;
  168. }
  169. }
  170. protected bool isInverted
  171. {
  172. get
  173. {
  174. return this.mFill == UIProgressBar.FillDirection.RightToLeft || this.mFill == UIProgressBar.FillDirection.TopToBottom;
  175. }
  176. }
  177. protected void Start()
  178. {
  179. this.Upgrade();
  180. if (Application.isPlaying)
  181. {
  182. if (this.mBG != null)
  183. {
  184. this.mBG.autoResizeBoxCollider = true;
  185. }
  186. this.OnStart();
  187. if (UIProgressBar.current == null && this.onChange != null)
  188. {
  189. UIProgressBar.current = this;
  190. EventDelegate.Execute(this.onChange);
  191. UIProgressBar.current = null;
  192. }
  193. }
  194. this.ForceUpdate();
  195. }
  196. protected virtual void Upgrade()
  197. {
  198. }
  199. protected virtual void OnStart()
  200. {
  201. }
  202. protected void Update()
  203. {
  204. if (this.mIsDirty)
  205. {
  206. this.ForceUpdate();
  207. }
  208. }
  209. protected void OnValidate()
  210. {
  211. if (NGUITools.GetActive(this))
  212. {
  213. this.Upgrade();
  214. this.mIsDirty = true;
  215. float num = Mathf.Clamp01(this.mValue);
  216. if (this.mValue != num)
  217. {
  218. this.mValue = num;
  219. }
  220. if (this.numberOfSteps < 0)
  221. {
  222. this.numberOfSteps = 0;
  223. }
  224. else if (this.numberOfSteps > 20)
  225. {
  226. this.numberOfSteps = 20;
  227. }
  228. this.ForceUpdate();
  229. }
  230. else
  231. {
  232. float num2 = Mathf.Clamp01(this.mValue);
  233. if (this.mValue != num2)
  234. {
  235. this.mValue = num2;
  236. }
  237. if (this.numberOfSteps < 0)
  238. {
  239. this.numberOfSteps = 0;
  240. }
  241. else if (this.numberOfSteps > 20)
  242. {
  243. this.numberOfSteps = 20;
  244. }
  245. }
  246. }
  247. protected float ScreenToValue(Vector2 screenPos)
  248. {
  249. Transform cachedTransform = this.cachedTransform;
  250. Plane plane = new Plane(cachedTransform.rotation * Vector3.back, cachedTransform.position);
  251. Ray ray = this.cachedCamera.ScreenPointToRay(screenPos);
  252. float distance;
  253. if (!plane.Raycast(ray, out distance))
  254. {
  255. return this.value;
  256. }
  257. return this.LocalToValue(cachedTransform.InverseTransformPoint(ray.GetPoint(distance)));
  258. }
  259. protected virtual float LocalToValue(Vector2 localPos)
  260. {
  261. if (!(this.mFG != null))
  262. {
  263. return this.value;
  264. }
  265. Vector3[] localCorners = this.mFG.localCorners;
  266. Vector3 vector = localCorners[2] - localCorners[0];
  267. if (this.isHorizontal)
  268. {
  269. float num = (localPos.x - localCorners[0].x) / vector.x;
  270. return (!this.isInverted) ? num : (1f - num);
  271. }
  272. float num2 = (localPos.y - localCorners[0].y) / vector.y;
  273. return (!this.isInverted) ? num2 : (1f - num2);
  274. }
  275. public virtual void ForceUpdate()
  276. {
  277. this.mIsDirty = false;
  278. if (this.mFG != null)
  279. {
  280. UIBasicSprite uibasicSprite = this.mFG as UIBasicSprite;
  281. if (this.isHorizontal)
  282. {
  283. if (uibasicSprite != null && uibasicSprite.type == UIBasicSprite.Type.Filled)
  284. {
  285. if (uibasicSprite.fillDirection == UIBasicSprite.FillDirection.Horizontal || uibasicSprite.fillDirection == UIBasicSprite.FillDirection.Vertical)
  286. {
  287. uibasicSprite.fillDirection = UIBasicSprite.FillDirection.Horizontal;
  288. uibasicSprite.invert = this.isInverted;
  289. }
  290. uibasicSprite.fillAmount = this.value;
  291. }
  292. else
  293. {
  294. this.mFG.drawRegion = ((!this.isInverted) ? new Vector4(0f, 0f, this.value, 1f) : new Vector4(1f - this.value, 0f, 1f, 1f));
  295. this.mFG.enabled = (this.value > 0.001f);
  296. }
  297. }
  298. else if (uibasicSprite != null && uibasicSprite.type == UIBasicSprite.Type.Filled)
  299. {
  300. if (uibasicSprite.fillDirection == UIBasicSprite.FillDirection.Horizontal || uibasicSprite.fillDirection == UIBasicSprite.FillDirection.Vertical)
  301. {
  302. uibasicSprite.fillDirection = UIBasicSprite.FillDirection.Vertical;
  303. uibasicSprite.invert = this.isInverted;
  304. }
  305. uibasicSprite.fillAmount = this.value;
  306. }
  307. else
  308. {
  309. this.mFG.drawRegion = ((!this.isInverted) ? new Vector4(0f, 0f, 1f, this.value) : new Vector4(0f, 1f - this.value, 1f, 1f));
  310. this.mFG.enabled = (this.value > 0.001f);
  311. }
  312. }
  313. if (this.thumb != null && (this.mFG != null || this.mBG != null))
  314. {
  315. Vector3[] array = (!(this.mFG != null)) ? this.mBG.localCorners : this.mFG.localCorners;
  316. Vector4 vector = (!(this.mFG != null)) ? this.mBG.border : this.mFG.border;
  317. Vector3[] array2 = array;
  318. int num = 0;
  319. array2[num].x = array2[num].x + vector.x;
  320. Vector3[] array3 = array;
  321. int num2 = 1;
  322. array3[num2].x = array3[num2].x + vector.x;
  323. Vector3[] array4 = array;
  324. int num3 = 2;
  325. array4[num3].x = array4[num3].x - vector.z;
  326. Vector3[] array5 = array;
  327. int num4 = 3;
  328. array5[num4].x = array5[num4].x - vector.z;
  329. Vector3[] array6 = array;
  330. int num5 = 0;
  331. array6[num5].y = array6[num5].y + vector.y;
  332. Vector3[] array7 = array;
  333. int num6 = 1;
  334. array7[num6].y = array7[num6].y - vector.w;
  335. Vector3[] array8 = array;
  336. int num7 = 2;
  337. array8[num7].y = array8[num7].y - vector.w;
  338. Vector3[] array9 = array;
  339. int num8 = 3;
  340. array9[num8].y = array9[num8].y + vector.y;
  341. Transform transform = (!(this.mFG != null)) ? this.mBG.cachedTransform : this.mFG.cachedTransform;
  342. for (int i = 0; i < 4; i++)
  343. {
  344. array[i] = transform.TransformPoint(array[i]);
  345. }
  346. if (this.isHorizontal)
  347. {
  348. Vector3 a = Vector3.Lerp(array[0], array[1], 0.5f);
  349. Vector3 b = Vector3.Lerp(array[2], array[3], 0.5f);
  350. this.SetThumbPosition(Vector3.Lerp(a, b, (!this.isInverted) ? this.value : (1f - this.value)));
  351. }
  352. else
  353. {
  354. Vector3 a2 = Vector3.Lerp(array[0], array[3], 0.5f);
  355. Vector3 b2 = Vector3.Lerp(array[1], array[2], 0.5f);
  356. this.SetThumbPosition(Vector3.Lerp(a2, b2, (!this.isInverted) ? this.value : (1f - this.value)));
  357. }
  358. }
  359. }
  360. protected void SetThumbPosition(Vector3 worldPos)
  361. {
  362. Transform parent = this.thumb.parent;
  363. if (parent != null)
  364. {
  365. worldPos = parent.InverseTransformPoint(worldPos);
  366. worldPos.x = Mathf.Round(worldPos.x);
  367. worldPos.y = Mathf.Round(worldPos.y);
  368. worldPos.z = 0f;
  369. if (Vector3.Distance(this.thumb.localPosition, worldPos) > 0.001f)
  370. {
  371. this.thumb.localPosition = worldPos;
  372. }
  373. }
  374. else if (Vector3.Distance(this.thumb.position, worldPos) > 1E-05f)
  375. {
  376. this.thumb.position = worldPos;
  377. }
  378. }
  379. public static UIProgressBar current;
  380. public UIProgressBar.OnDragFinished onDragFinished;
  381. public Transform thumb;
  382. [HideInInspector]
  383. [SerializeField]
  384. protected UIWidget mBG;
  385. [HideInInspector]
  386. [SerializeField]
  387. protected UIWidget mFG;
  388. [HideInInspector]
  389. [SerializeField]
  390. protected float mValue = 1f;
  391. [HideInInspector]
  392. [SerializeField]
  393. protected UIProgressBar.FillDirection mFill;
  394. protected Transform mTrans;
  395. protected bool mIsDirty;
  396. protected Camera mCam;
  397. protected float mOffset;
  398. public int numberOfSteps;
  399. public List<EventDelegate> onChange = new List<EventDelegate>();
  400. public enum FillDirection
  401. {
  402. LeftToRight,
  403. RightToLeft,
  404. BottomToTop,
  405. TopToBottom
  406. }
  407. public delegate void OnDragFinished();
  408. }