using System; using System.Collections.Generic; using AnimationOrTween; using UnityEngine; [ExecuteInEditMode] [AddComponentMenu("NGUI/Interaction/Toggle")] public class UIToggle : UIWidgetContainer { public delegate bool Validate(bool choice); public static BetterList list = new BetterList(); public static UIToggle current; public int group; public UIWidget activeSprite; public Animation activeAnimation; public Animator animator; public UITweener tween; public bool startsActive; public bool instantTween; public bool optionCanBeNone; public List onChange = new List(); public Validate validator; [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; public bool value { get { if (!mStarted) { return startsActive; } return mIsActive; } set { if (!mStarted) { startsActive = value; } else if (group == 0 || value || optionCanBeNone || !mStarted) { Set(value); } } } public bool isColliderEnabled { get { Collider component = GetComponent(); if (component != null) { return component.enabled; } Collider2D component2 = GetComponent(); if (component2 != null) { return component2.enabled; } return false; } } [Obsolete("Use 'value' instead")] public bool isChecked { get { return value; } set { this.value = value; } } public static UIToggle GetActiveToggle(int group) { for (int i = 0; i < list.size; i++) { UIToggle uIToggle = list[i]; if (uIToggle != null && uIToggle.group == group && uIToggle.mIsActive) { return uIToggle; } } return null; } private void OnEnable() { list.Add(this); } private void OnDisable() { list.Remove(this); } private void Start() { if (startsChecked) { startsChecked = false; startsActive = true; } if (!Application.isPlaying) { if (checkSprite != null && activeSprite == null) { activeSprite = checkSprite; checkSprite = null; } if (checkAnimation != null && activeAnimation == null) { activeAnimation = checkAnimation; checkAnimation = null; } if (Application.isPlaying && activeSprite != null) { activeSprite.alpha = (startsActive ? 1f : 0f); } if (EventDelegate.IsValid(onChange)) { eventReceiver = null; functionName = null; } } else { mIsActive = !startsActive; mStarted = true; bool flag = instantTween; instantTween = true; Set(startsActive); instantTween = flag; } } private void OnClick() { if (base.enabled && isColliderEnabled && UICamera.currentTouchID != -2) { value = !value; } } public void Set(bool state) { if (validator != null && !validator(state)) { return; } if (!mStarted) { mIsActive = state; startsActive = state; if (activeSprite != null) { activeSprite.alpha = (state ? 1f : 0f); } } else { if (mIsActive == state) { return; } if (group != 0 && state) { int num = 0; int size = list.size; while (num < size) { UIToggle uIToggle = list[num]; if (uIToggle != this && uIToggle.group == group) { uIToggle.Set(state: false); } if (list.size != size) { size = list.size; num = 0; } else { num++; } } } mIsActive = state; if (activeSprite != null) { if (instantTween || !NGUITools.GetActive(this)) { activeSprite.alpha = (mIsActive ? 1f : 0f); } else { TweenAlpha.Begin(activeSprite.gameObject, 0.15f, mIsActive ? 1f : 0f); } } if (current == null) { UIToggle uIToggle2 = current; current = this; if (EventDelegate.IsValid(onChange)) { EventDelegate.Execute(onChange); } else if (eventReceiver != null && !string.IsNullOrEmpty(functionName)) { eventReceiver.SendMessage(functionName, mIsActive, SendMessageOptions.DontRequireReceiver); } current = uIToggle2; } if (animator != null) { ActiveAnimation activeAnimation = ActiveAnimation.Play(animator, null, state ? Direction.Forward : Direction.Reverse, EnableCondition.IgnoreDisabledState, DisableCondition.DoNotDisable); if (activeAnimation != null && (instantTween || !NGUITools.GetActive(this))) { activeAnimation.Finish(); } } else if (this.activeAnimation != null) { ActiveAnimation activeAnimation2 = ActiveAnimation.Play(this.activeAnimation, null, state ? Direction.Forward : Direction.Reverse, EnableCondition.IgnoreDisabledState, DisableCondition.DoNotDisable); if (activeAnimation2 != null && (instantTween || !NGUITools.GetActive(this))) { activeAnimation2.Finish(); } } else { if (!(tween != null)) { return; } bool active = NGUITools.GetActive(this); if (tween.tweenGroup != 0) { UITweener[] componentsInChildren = tween.GetComponentsInChildren(); int i = 0; for (int num2 = componentsInChildren.Length; i < num2; i++) { UITweener uITweener = componentsInChildren[i]; if (uITweener.tweenGroup == tween.tweenGroup) { uITweener.Play(state); if (instantTween || !active) { uITweener.tweenFactor = (state ? 1f : 0f); } } } } else { tween.Play(state); if (instantTween || !active) { tween.tweenFactor = (state ? 1f : 0f); } } } } } }