UIToggle.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections.Generic;
  3. using AnimationOrTween;
  4. using UnityEngine;
  5. [ExecuteInEditMode]
  6. [AddComponentMenu("NGUI/Interaction/Toggle")]
  7. public class UIToggle : UIWidgetContainer
  8. {
  9. public bool value
  10. {
  11. get
  12. {
  13. return (!this.mStarted) ? this.startsActive : this.mIsActive;
  14. }
  15. set
  16. {
  17. if (!this.mStarted)
  18. {
  19. this.startsActive = value;
  20. }
  21. else if (this.group == 0 || value || this.optionCanBeNone || !this.mStarted)
  22. {
  23. this.Set(value);
  24. }
  25. }
  26. }
  27. [Obsolete("Use 'value' instead")]
  28. public bool isChecked
  29. {
  30. get
  31. {
  32. return this.value;
  33. }
  34. set
  35. {
  36. this.value = value;
  37. }
  38. }
  39. public static UIToggle GetActiveToggle(int group)
  40. {
  41. for (int i = 0; i < UIToggle.list.size; i++)
  42. {
  43. UIToggle uitoggle = UIToggle.list[i];
  44. if (uitoggle != null && uitoggle.group == group && uitoggle.mIsActive)
  45. {
  46. return uitoggle;
  47. }
  48. }
  49. return null;
  50. }
  51. private void OnEnable()
  52. {
  53. UIToggle.list.Add(this);
  54. }
  55. private void OnDisable()
  56. {
  57. UIToggle.list.Remove(this);
  58. }
  59. private void Start()
  60. {
  61. if (this.startsChecked)
  62. {
  63. this.startsChecked = false;
  64. this.startsActive = true;
  65. }
  66. if (!Application.isPlaying)
  67. {
  68. if (this.checkSprite != null && this.activeSprite == null)
  69. {
  70. this.activeSprite = this.checkSprite;
  71. this.checkSprite = null;
  72. }
  73. if (this.checkAnimation != null && this.activeAnimation == null)
  74. {
  75. this.activeAnimation = this.checkAnimation;
  76. this.checkAnimation = null;
  77. }
  78. if (Application.isPlaying && this.activeSprite != null)
  79. {
  80. this.activeSprite.alpha = ((!this.startsActive) ? 0f : 1f);
  81. }
  82. if (EventDelegate.IsValid(this.onChange))
  83. {
  84. this.eventReceiver = null;
  85. this.functionName = null;
  86. }
  87. }
  88. else
  89. {
  90. this.mIsActive = !this.startsActive;
  91. this.mStarted = true;
  92. bool flag = this.instantTween;
  93. this.instantTween = true;
  94. this.Set(this.startsActive);
  95. this.instantTween = flag;
  96. }
  97. }
  98. private void OnClick()
  99. {
  100. if (base.enabled)
  101. {
  102. this.value = !this.value;
  103. }
  104. }
  105. private void Set(bool state)
  106. {
  107. if (!this.mStarted)
  108. {
  109. this.mIsActive = state;
  110. this.startsActive = state;
  111. if (this.activeSprite != null)
  112. {
  113. this.activeSprite.alpha = ((!state) ? 0f : 1f);
  114. }
  115. }
  116. else if (this.mIsActive != state)
  117. {
  118. if (this.group != 0 && state)
  119. {
  120. int i = 0;
  121. int size = UIToggle.list.size;
  122. while (i < size)
  123. {
  124. UIToggle uitoggle = UIToggle.list[i];
  125. if (uitoggle != this && uitoggle.group == this.group)
  126. {
  127. uitoggle.Set(false);
  128. }
  129. if (UIToggle.list.size != size)
  130. {
  131. size = UIToggle.list.size;
  132. i = 0;
  133. }
  134. else
  135. {
  136. i++;
  137. }
  138. }
  139. }
  140. this.mIsActive = state;
  141. if (this.activeSprite != null)
  142. {
  143. if (this.instantTween || !NGUITools.GetActive(this))
  144. {
  145. this.activeSprite.alpha = ((!this.mIsActive) ? 0f : 1f);
  146. }
  147. else
  148. {
  149. TweenAlpha.Begin(this.activeSprite.gameObject, 0.15f, (!this.mIsActive) ? 0f : 1f);
  150. }
  151. }
  152. if (UIToggle.current == null)
  153. {
  154. UIToggle uitoggle2 = UIToggle.current;
  155. UIToggle.current = this;
  156. if (EventDelegate.IsValid(this.onChange))
  157. {
  158. EventDelegate.Execute(this.onChange);
  159. }
  160. else if (this.eventReceiver != null && !string.IsNullOrEmpty(this.functionName))
  161. {
  162. this.eventReceiver.SendMessage(this.functionName, this.mIsActive, SendMessageOptions.DontRequireReceiver);
  163. }
  164. UIToggle.current = uitoggle2;
  165. }
  166. if (this.activeAnimation != null)
  167. {
  168. ActiveAnimation activeAnimation = ActiveAnimation.Play(this.activeAnimation, null, (!state) ? Direction.Reverse : Direction.Forward, EnableCondition.IgnoreDisabledState, DisableCondition.DoNotDisable);
  169. if (activeAnimation != null && (this.instantTween || !NGUITools.GetActive(this)))
  170. {
  171. activeAnimation.Finish();
  172. }
  173. }
  174. }
  175. }
  176. public static BetterList<UIToggle> list = new BetterList<UIToggle>();
  177. public static UIToggle current;
  178. public int group;
  179. public UIWidget activeSprite;
  180. public Animation activeAnimation;
  181. public bool startsActive;
  182. public bool instantTween;
  183. public bool optionCanBeNone;
  184. public List<EventDelegate> onChange = new List<EventDelegate>();
  185. [HideInInspector]
  186. [SerializeField]
  187. private UISprite checkSprite;
  188. [HideInInspector]
  189. [SerializeField]
  190. private Animation checkAnimation;
  191. [HideInInspector]
  192. [SerializeField]
  193. private GameObject eventReceiver;
  194. [HideInInspector]
  195. [SerializeField]
  196. private string functionName = "OnActivate";
  197. [HideInInspector]
  198. [SerializeField]
  199. private bool startsChecked;
  200. private bool mIsActive = true;
  201. private bool mStarted;
  202. }