using System; using System.Collections; using CIG; using CIGEnums; using SUISS.Core; using UnityEngine; public class PopupBaseState : State { public bool Closed { get { return this._closed; } } public override void Enter(State oldState) { if (this._previousState == null) { this._closed = false; this._previousState = oldState; PopupBaseState._popupCount++; if (this._skipNextAnimation) { this.PopupView.SkipAnimation(); this._skipNextAnimation = false; } else { this.PopupView.AnimateIn(); } SingletonMonobehaviour.Instance.PlayClip(Clip.ButtonClick); base.Enter(oldState); } } public override void Leave(State newState) { if (this._nextState == null) { this._nextState = newState; } if (newState == this._previousState) { this.Clear(newState); } } public void ClosePopup() { this.ClosePopup(null, true, false); } public void ClosePopup(Action callback) { this.ClosePopup(callback, true, false); } public void ClosePopup(Action callback, bool animate) { this.ClosePopup(callback, animate, false); } public virtual void ClosePopup(Action callback, bool animate, bool synchronous) { if (this.Closed) { return; } this._closed = true; if (animate) { this.PopupView.AnimateOut(); } else { this.PopupView.Close(); } SingletonMonobehaviour.Instance.PlayClip(Clip.ButtonClick, true); if (synchronous) { this.SwitchStateSynchronously(this._previousState, callback); } else { base.StartCoroutine(this.WaitAndSwitchState((!animate) ? 0f : this.PopupView.TweenTime, this._previousState, callback)); } } protected PopupBaseView PopupView { get { return this.View as PopupBaseView; } } protected T SafeSwitchState() where T : State { if (this.SafeSwitchState(typeof(T).Name)) { return this._fsm.CurrentState as T; } return (T)((object)null); } protected bool SafeSwitchState(State state) { return this.SafeSwitchState(state.GetType().Name); } protected bool SafeSwitchState(string newStateName) { if (this._fsm.CurrentState == this) { this._fsm.SwitchState(newStateName); return true; } return false; } protected virtual void DismissPopup() { this.ClosePopup(); } protected override void OnUpdate() { base.OnUpdate(); if (UnityEngine.Input.GetKeyDown(KeyCode.Escape)) { this.DismissPopup(); } } private void SwitchStateSynchronously(State state, Action callback) { this._fsm.SwitchState(state); if (callback != null) { callback(); } } private IEnumerator WaitAndSwitchState(float waitTime, State state, Action callback) { if (waitTime > 0f) { yield return new WaitForSeconds(waitTime); } State currentState = this._fsm.CurrentState; if (currentState == this) { this._fsm.SwitchState(state); } else { if (currentState is PopupBaseState) { ((PopupBaseState)currentState).FixStates(this, state); } this.Clear(this._previousState); } if (callback != null) { callback(); } yield break; } private void FixStates(State brokenState, State newState) { if (this._previousState == brokenState) { this._previousState = newState; } if (this._nextState == brokenState) { this._nextState = newState; } } private void Clear(State newState) { this._previousState = (this._nextState = null); PopupBaseState._popupCount--; base.Leave(newState); if (PopupBaseState._popupCount == 0) { } } private State _previousState; private State _nextState; private bool _closed = true; private static int _popupCount; protected bool _skipNextAnimation; }