123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- using System;
- using System.Collections.Generic;
- using AnimationOrTween;
- using UnityEngine;
- [ExecuteInEditMode]
- [AddComponentMenu("NGUI/Interaction/Toggle")]
- public class UIToggle : UIWidgetContainer
- {
- public bool value
- {
- get
- {
- return (!this.mStarted) ? this.startsActive : this.mIsActive;
- }
- set
- {
- if (!this.mStarted)
- {
- this.startsActive = value;
- }
- else if (this.group == 0 || value || this.optionCanBeNone || !this.mStarted)
- {
- this.Set(value);
- }
- }
- }
- [Obsolete("Use 'value' instead")]
- public bool isChecked
- {
- get
- {
- return this.value;
- }
- set
- {
- this.value = value;
- }
- }
- public static UIToggle GetActiveToggle(int group)
- {
- for (int i = 0; i < UIToggle.list.size; i++)
- {
- UIToggle uitoggle = UIToggle.list[i];
- if (uitoggle != null && uitoggle.group == group && uitoggle.mIsActive)
- {
- return uitoggle;
- }
- }
- return null;
- }
- private void OnEnable()
- {
- UIToggle.list.Add(this);
- }
- private void OnDisable()
- {
- UIToggle.list.Remove(this);
- }
- private void Start()
- {
- if (this.startsChecked)
- {
- this.startsChecked = false;
- this.startsActive = true;
- }
- if (!Application.isPlaying)
- {
- if (this.checkSprite != null && this.activeSprite == null)
- {
- this.activeSprite = this.checkSprite;
- this.checkSprite = null;
- }
- if (this.checkAnimation != null && this.activeAnimation == null)
- {
- this.activeAnimation = this.checkAnimation;
- this.checkAnimation = null;
- }
- if (Application.isPlaying && this.activeSprite != null)
- {
- this.activeSprite.alpha = ((!this.startsActive) ? 0f : 1f);
- }
- if (EventDelegate.IsValid(this.onChange))
- {
- this.eventReceiver = null;
- this.functionName = null;
- }
- }
- else
- {
- this.mIsActive = !this.startsActive;
- this.mStarted = true;
- bool flag = this.instantTween;
- this.instantTween = true;
- this.Set(this.startsActive);
- this.instantTween = flag;
- }
- }
- private void OnClick()
- {
- if (base.enabled)
- {
- this.value = !this.value;
- }
- }
- private void Set(bool state)
- {
- if (!this.mStarted)
- {
- this.mIsActive = state;
- this.startsActive = state;
- if (this.activeSprite != null)
- {
- this.activeSprite.alpha = ((!state) ? 0f : 1f);
- }
- }
- else if (this.mIsActive != state)
- {
- if (this.group != 0 && state)
- {
- int i = 0;
- int size = UIToggle.list.size;
- while (i < size)
- {
- UIToggle uitoggle = UIToggle.list[i];
- if (uitoggle != this && uitoggle.group == this.group)
- {
- uitoggle.Set(false);
- }
- if (UIToggle.list.size != size)
- {
- size = UIToggle.list.size;
- i = 0;
- }
- else
- {
- i++;
- }
- }
- }
- this.mIsActive = state;
- if (this.activeSprite != null)
- {
- if (this.instantTween || !NGUITools.GetActive(this))
- {
- this.activeSprite.alpha = ((!this.mIsActive) ? 0f : 1f);
- }
- else
- {
- TweenAlpha.Begin(this.activeSprite.gameObject, 0.15f, (!this.mIsActive) ? 0f : 1f);
- }
- }
- if (UIToggle.current == null)
- {
- UIToggle uitoggle2 = UIToggle.current;
- UIToggle.current = this;
- if (EventDelegate.IsValid(this.onChange))
- {
- EventDelegate.Execute(this.onChange);
- }
- else if (this.eventReceiver != null && !string.IsNullOrEmpty(this.functionName))
- {
- this.eventReceiver.SendMessage(this.functionName, this.mIsActive, SendMessageOptions.DontRequireReceiver);
- }
- UIToggle.current = uitoggle2;
- }
- if (this.activeAnimation != null)
- {
- ActiveAnimation activeAnimation = ActiveAnimation.Play(this.activeAnimation, null, (!state) ? Direction.Reverse : Direction.Forward, EnableCondition.IgnoreDisabledState, DisableCondition.DoNotDisable);
- if (activeAnimation != null && (this.instantTween || !NGUITools.GetActive(this)))
- {
- activeAnimation.Finish();
- }
- }
- }
- }
- public static BetterList<UIToggle> list = new BetterList<UIToggle>();
- public static UIToggle current;
- public int group;
- public UIWidget activeSprite;
- public Animation activeAnimation;
- public bool startsActive;
- public bool instantTween;
- public bool optionCanBeNone;
- public List<EventDelegate> onChange = new List<EventDelegate>();
- [HideInInspector]
- [SerializeField]
- private UISprite checkSprite;
- [HideInInspector]
- [SerializeField]
- private Animation checkAnimation;
- [HideInInspector]
- [SerializeField]
- private GameObject eventReceiver;
- [HideInInspector]
- [SerializeField]
- private string functionName = "OnActivate";
- [HideInInspector]
- [SerializeField]
- private bool startsChecked;
- private bool mIsActive = true;
- private bool mStarted;
- }
|