You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

205 regels
3.8 KiB

  1. using System;
  2. using System.Collections;
  3. using CIG;
  4. using CIGEnums;
  5. using SUISS.Core;
  6. using UnityEngine;
  7. public class PopupBaseState : State
  8. {
  9. public bool Closed
  10. {
  11. get
  12. {
  13. return this._closed;
  14. }
  15. }
  16. public override void Enter(State oldState)
  17. {
  18. if (this._previousState == null)
  19. {
  20. this._closed = false;
  21. this._previousState = oldState;
  22. PopupBaseState._popupCount++;
  23. if (this._skipNextAnimation)
  24. {
  25. this.PopupView.SkipAnimation();
  26. this._skipNextAnimation = false;
  27. }
  28. else
  29. {
  30. this.PopupView.AnimateIn();
  31. }
  32. SingletonMonobehaviour<CIGAudioManager>.Instance.PlayClip(Clip.ButtonClick);
  33. base.Enter(oldState);
  34. }
  35. }
  36. public override void Leave(State newState)
  37. {
  38. if (this._nextState == null)
  39. {
  40. this._nextState = newState;
  41. }
  42. if (newState == this._previousState)
  43. {
  44. this.Clear(newState);
  45. }
  46. }
  47. public void ClosePopup()
  48. {
  49. this.ClosePopup(null, true, false);
  50. }
  51. public void ClosePopup(Action callback)
  52. {
  53. this.ClosePopup(callback, true, false);
  54. }
  55. public void ClosePopup(Action callback, bool animate)
  56. {
  57. this.ClosePopup(callback, animate, false);
  58. }
  59. public virtual void ClosePopup(Action callback, bool animate, bool synchronous)
  60. {
  61. if (this.Closed)
  62. {
  63. return;
  64. }
  65. this._closed = true;
  66. if (animate)
  67. {
  68. this.PopupView.AnimateOut();
  69. }
  70. else
  71. {
  72. this.PopupView.Close();
  73. }
  74. SingletonMonobehaviour<CIGAudioManager>.Instance.PlayClip(Clip.ButtonClick, true);
  75. if (synchronous)
  76. {
  77. this.SwitchStateSynchronously(this._previousState, callback);
  78. }
  79. else
  80. {
  81. base.StartCoroutine(this.WaitAndSwitchState((!animate) ? 0f : this.PopupView.TweenTime, this._previousState, callback));
  82. }
  83. }
  84. protected PopupBaseView PopupView
  85. {
  86. get
  87. {
  88. return this.View as PopupBaseView;
  89. }
  90. }
  91. protected T SafeSwitchState<T>() where T : State
  92. {
  93. if (this.SafeSwitchState(typeof(T).Name))
  94. {
  95. return this._fsm.CurrentState as T;
  96. }
  97. return (T)((object)null);
  98. }
  99. protected bool SafeSwitchState(State state)
  100. {
  101. return this.SafeSwitchState(state.GetType().Name);
  102. }
  103. protected bool SafeSwitchState(string newStateName)
  104. {
  105. if (this._fsm.CurrentState == this)
  106. {
  107. this._fsm.SwitchState(newStateName);
  108. return true;
  109. }
  110. return false;
  111. }
  112. protected virtual void DismissPopup()
  113. {
  114. this.ClosePopup();
  115. }
  116. protected override void OnUpdate()
  117. {
  118. base.OnUpdate();
  119. if (UnityEngine.Input.GetKeyDown(KeyCode.Escape))
  120. {
  121. this.DismissPopup();
  122. }
  123. }
  124. private void SwitchStateSynchronously(State state, Action callback)
  125. {
  126. this._fsm.SwitchState(state);
  127. if (callback != null)
  128. {
  129. callback();
  130. }
  131. }
  132. private IEnumerator WaitAndSwitchState(float waitTime, State state, Action callback)
  133. {
  134. if (waitTime > 0f)
  135. {
  136. yield return new WaitForSeconds(waitTime);
  137. }
  138. State currentState = this._fsm.CurrentState;
  139. if (currentState == this)
  140. {
  141. this._fsm.SwitchState(state);
  142. }
  143. else
  144. {
  145. if (currentState is PopupBaseState)
  146. {
  147. ((PopupBaseState)currentState).FixStates(this, state);
  148. }
  149. this.Clear(this._previousState);
  150. }
  151. if (callback != null)
  152. {
  153. callback();
  154. }
  155. yield break;
  156. }
  157. private void FixStates(State brokenState, State newState)
  158. {
  159. if (this._previousState == brokenState)
  160. {
  161. this._previousState = newState;
  162. }
  163. if (this._nextState == brokenState)
  164. {
  165. this._nextState = newState;
  166. }
  167. }
  168. private void Clear(State newState)
  169. {
  170. this._previousState = (this._nextState = null);
  171. PopupBaseState._popupCount--;
  172. base.Leave(newState);
  173. if (PopupBaseState._popupCount == 0)
  174. {
  175. }
  176. }
  177. private State _previousState;
  178. private State _nextState;
  179. private bool _closed = true;
  180. private static int _popupCount;
  181. protected bool _skipNextAnimation;
  182. }