Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

350 строки
12 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using CIG;
  5. using CIG.Translation;
  6. using CIGEnums;
  7. using SUISS.Core;
  8. using SUISSEngine;
  9. using UnityEngine;
  10. public sealed class PopupManager : SingletonMonobehaviour<PopupManager>
  11. {
  12. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  13. public event PopupManager.StatesInitializedEventHandler StatesInitializedEvent;
  14. private void FireStatesInitializedEvent()
  15. {
  16. if (this.StatesInitializedEvent != null)
  17. {
  18. this.StatesInitializedEvent();
  19. }
  20. }
  21. public bool PopupsMayOpen
  22. {
  23. get
  24. {
  25. return this._popupsMayOpen;
  26. }
  27. set
  28. {
  29. this._popupsMayOpen = value;
  30. if (value)
  31. {
  32. this.OpenNextPopup();
  33. }
  34. }
  35. }
  36. public bool SuppressQueue
  37. {
  38. get
  39. {
  40. return this._popupsAreSuppressed;
  41. }
  42. set
  43. {
  44. this._popupsAreSuppressed = value;
  45. if (value)
  46. {
  47. this.OpenNextPopup();
  48. }
  49. }
  50. }
  51. private int SuppressedPopups { get; set; }
  52. protected override void Awake()
  53. {
  54. base.Awake();
  55. if (!this._isValidNewInstance)
  56. {
  57. return;
  58. }
  59. this._popupsMayOpen = true;
  60. this.fsm.StatesInitializedEvent += this.FireStatesInitializedEvent;
  61. }
  62. protected override void OnDestroy()
  63. {
  64. this.fsm.StatesInitializedEvent -= this.FireStatesInitializedEvent;
  65. base.OnDestroy();
  66. }
  67. public void ShowOverlay()
  68. {
  69. this.overlay.SetActive(true);
  70. }
  71. public void HideOverlay()
  72. {
  73. this.overlay.SetActive(false);
  74. }
  75. public PopupRequest? RequestPopup<T>(PopupManager.PopupOpened callback) where T : State
  76. {
  77. PopupRequest popupRequest = new PopupRequest(this.fsm.GetState<T>(), callback);
  78. if (this.PopupsMayOpen)
  79. {
  80. this.OpenPopupNow(popupRequest);
  81. return null;
  82. }
  83. this.scheduledRequests.AddLast(popupRequest);
  84. return new PopupRequest?(popupRequest);
  85. }
  86. public PopupRequest? RequestFirstPopup<T>(PopupManager.PopupOpened callback) where T : State
  87. {
  88. PopupRequest popupRequest = new PopupRequest(this.fsm.GetState<T>(), callback);
  89. if (this.PopupsMayOpen)
  90. {
  91. this.OpenPopupNow(popupRequest);
  92. return null;
  93. }
  94. this.scheduledRequests.AddFirst(popupRequest);
  95. return new PopupRequest?(popupRequest);
  96. }
  97. public void RemovePopupRequest(PopupRequest request)
  98. {
  99. this.scheduledRequests.Remove(request);
  100. }
  101. private void OpenPopupNow(PopupRequest request)
  102. {
  103. State requestedState = request.requestedState;
  104. if (this.fsm.CurrentState == requestedState)
  105. {
  106. return;
  107. }
  108. this.fsm.SwitchState(requestedState);
  109. if (this.fsm.CurrentState == requestedState)
  110. {
  111. if (request.callback != null)
  112. {
  113. request.callback(requestedState);
  114. }
  115. }
  116. else
  117. {
  118. UnityEngine.Debug.LogWarning("State change unsuccessfull! PopupManager may have become corrupted");
  119. }
  120. }
  121. private void OpenNextPopup()
  122. {
  123. if (this.scheduledRequests.Count == 0)
  124. {
  125. return;
  126. }
  127. if (this.SuppressQueue)
  128. {
  129. if (++this.SuppressedPopups > 2)
  130. {
  131. UnityEngine.Debug.LogWarning("Are you quite sure you intended to suppress the popup queue for so long? Maybe you forgot to revert the suppress functionality?");
  132. }
  133. return;
  134. }
  135. this.SuppressedPopups = 0;
  136. PopupRequest value = this.scheduledRequests.First.Value;
  137. this.scheduledRequests.RemoveFirst();
  138. this.OpenPopupNow(value);
  139. }
  140. public void OpenShopMenu(ShopMenuTabs shopMenuTab)
  141. {
  142. this.RequestFirstPopup<ShopPopupState>(delegate(State state)
  143. {
  144. ((ShopPopupState)state).OpenPopup(shopMenuTab, false);
  145. });
  146. }
  147. [Obsolete("Use CloseRecursive instead", false)]
  148. public void CloseAll()
  149. {
  150. GUIView[] array = UnityEngine.Object.FindObjectsOfType<GUIView>();
  151. foreach (GUIView guiview in array)
  152. {
  153. if (guiview is PopupBaseView)
  154. {
  155. ((PopupBaseState)guiview.State).ClosePopup();
  156. }
  157. }
  158. }
  159. public void CloseRecursive(bool animated = true)
  160. {
  161. while (this.PopupIsOpen())
  162. {
  163. ((PopupBaseState)this.fsm.CurrentState).ClosePopup(null, animated, true);
  164. }
  165. }
  166. public bool PopupIsOpen()
  167. {
  168. return !(this.fsm.CurrentState is HUDState) && this.fsm.CurrentState is PopupBaseState;
  169. }
  170. public void OpenRoadMenu()
  171. {
  172. this.RequestFirstPopup<RoadSelectionPopupState>(null);
  173. }
  174. public void OpenYouNeedMore(Currencies purchasePrice, bool enableGoldAsAlternativeForCash, Action<bool, Currencies> purchaseCallback)
  175. {
  176. if (enableGoldAsAlternativeForCash)
  177. {
  178. }
  179. else
  180. {
  181. }
  182. SingletonMonobehaviour<CIGGameStats>.Instance.AddScreenViewed();
  183. this.RequestFirstPopup<MoreCashGoldPopupState>(delegate(State State)
  184. {
  185. ((MoreCashGoldPopupState)State).UpdateInfo(purchasePrice, enableGoldAsAlternativeForCash, purchaseCallback);
  186. });
  187. }
  188. public void ShowOkPopup(ILocalizedString title, ILocalizedString body, Action okButtonAction, bool dismissable = true)
  189. {
  190. this.RequestFirstGenericPopup(null, title, body, Localization.Key("ok"), null, okButtonAction, null, null, dismissable);
  191. }
  192. public void ShowOkCancelPopup(ILocalizedString title, ILocalizedString body, Action okButtonAction, Action cancelButtonAction)
  193. {
  194. this.RequestFirstGenericPopup(null, title, body, Localization.Key("ok"), Localization.Key("cancel"), okButtonAction, cancelButtonAction, cancelButtonAction, true);
  195. }
  196. public void ShowOkCancelPopup(UISpriteType spriteType, ILocalizedString title, ILocalizedString body, Action okButtonAction, Action cancelButtonAction)
  197. {
  198. this.RequestFirstGenericPopup(spriteType, title, body, Localization.Key("ok"), Localization.Key("cancel"), okButtonAction, cancelButtonAction, cancelButtonAction, true);
  199. }
  200. public void ShowInfoPopup(ILocalizedString title, ILocalizedString body)
  201. {
  202. this.RequestFirstGenericPopup(null, title, body, Localization.Key("ok"), null, null, null, null, true);
  203. }
  204. public void RequestGenericPopup(Sprite sprite, ILocalizedString title, ILocalizedString body, ILocalizedString greenText, ILocalizedString redText, Action greenButtonAction, Action redButtonAction, Action closeAction, bool dismissable = true)
  205. {
  206. this.RequestPopup<GenericPopupState>(delegate(State state)
  207. {
  208. ((GenericPopupState)state).UpdateInfo(sprite, title, body, greenText, redText, greenButtonAction, redButtonAction, closeAction, dismissable);
  209. });
  210. }
  211. public void RequestGenericPopup(UISpriteType spriteType, ILocalizedString title, ILocalizedString body, ILocalizedString greenText, ILocalizedString redText, Action greenButtonAction, Action redButtonAction, Action closeAction, bool dismissable = true)
  212. {
  213. this.RequestGenericPopup(SingletonMonobehaviour<UISpriteAssetCollection>.Instance.GetAsset(spriteType), title, body, greenText, redText, greenButtonAction, redButtonAction, closeAction, dismissable);
  214. }
  215. public void RequestFirstGenericPopup(Sprite sprite, ILocalizedString title, ILocalizedString body, ILocalizedString greenText, ILocalizedString redText, Action greenButtonAction, Action redButtonAction, Action closeAction, bool dismissable = true)
  216. {
  217. this.RequestFirstPopup<GenericPopupState>(delegate(State state)
  218. {
  219. ((GenericPopupState)state).UpdateInfo(sprite, title, body, greenText, redText, greenButtonAction, redButtonAction, closeAction, dismissable);
  220. });
  221. }
  222. public void RequestFirstGenericPopup(UISpriteType spriteType, ILocalizedString title, ILocalizedString body, ILocalizedString greenText, ILocalizedString redText, Action greenButtonAction, Action redButtonAction, Action closeAction, bool dismissable = true)
  223. {
  224. this.RequestFirstGenericPopup(SingletonMonobehaviour<UISpriteAssetCollection>.Instance.GetAsset(spriteType), title, body, greenText, redText, greenButtonAction, redButtonAction, closeAction, dismissable);
  225. }
  226. public void RequestFirstAdvicePopup(Sprite sprite, ILocalizedString title, ILocalizedString body, ILocalizedString greenText, ILocalizedString redText, Action greenButtonAction, Action redButtonAction, Action closeAction, Building building, bool dismissable = true)
  227. {
  228. this.RequestFirstPopup<GenericPopupState>(delegate(State state)
  229. {
  230. ((GenericPopupState)state).UpdateInfo(sprite, title, body, greenText, redText, greenButtonAction, redButtonAction, closeAction, dismissable);
  231. if (building != null)
  232. {
  233. SpriteRenderer bottomRenderer = building.GetComponent<GridTile>().bottomRenderer;
  234. if (bottomRenderer != null)
  235. {
  236. ((GenericPopupState)state).SetBottomSprite(bottomRenderer);
  237. }
  238. }
  239. });
  240. }
  241. public void RequestFirstAdvicePopup(UISpriteType spriteType, ILocalizedString title, ILocalizedString body, ILocalizedString greenText, ILocalizedString redText, Action greenButtonAction, Action redButtonAction, Action closeAction, Building building, bool dismissable = true)
  242. {
  243. this.RequestFirstAdvicePopup(SingletonMonobehaviour<UISpriteAssetCollection>.Instance.GetAsset(spriteType), title, body, greenText, redText, greenButtonAction, redButtonAction, closeAction, building, dismissable);
  244. }
  245. public GenericPopupState ShowOrUpdateGenericPopup(Sprite sprite, ILocalizedString title, ILocalizedString body, ILocalizedString greenText, ILocalizedString redText, Action greenButtonAction, Action redButtonAction, Action closeAction, bool dismissable = true)
  246. {
  247. GenericPopupState genericPopupState;
  248. if (this.fsm.CurrentState is GenericPopupState)
  249. {
  250. genericPopupState = (GenericPopupState)this.fsm.CurrentState;
  251. genericPopupState.UpdateInfo(sprite, title, body, greenText, redText, greenButtonAction, redButtonAction, closeAction, dismissable);
  252. }
  253. else
  254. {
  255. genericPopupState = this.fsm.SwitchState<GenericPopupState>();
  256. genericPopupState.UpdateInfo(sprite, title, body, greenText, redText, greenButtonAction, redButtonAction, closeAction, dismissable);
  257. }
  258. return genericPopupState;
  259. }
  260. public GenericPopupState ShowOrUpdateGenericPopup(UISpriteType spriteType, ILocalizedString title, ILocalizedString body, ILocalizedString greenText, ILocalizedString redText, Action greenButtonAction, Action redButtonAction, Action closeAction, bool dismissable = true)
  261. {
  262. return this.ShowOrUpdateGenericPopup(SingletonMonobehaviour<UISpriteAssetCollection>.Instance.GetAsset(spriteType), title, body, greenText, redText, greenButtonAction, redButtonAction, closeAction, dismissable);
  263. }
  264. public void ShowReceiveRewardPopup(Currencies reward)
  265. {
  266. this.fsm.SwitchState<ReceiveRewardPopupState>().SetCurrencyReward(reward);
  267. SingletonMonobehaviour<CIGGameStats>.Instance.AddScreenViewed("receive_currencies");
  268. SingletonMonobehaviour<CIGAudioManager>.Instance.PlayClip(Clip.Applause);
  269. }
  270. public void ShowTooMuchParallelBuildsPopup(Action succesAction, Action cancelAction, Currencies extraCost)
  271. {
  272. this.RequestPopup<TooMuchParallelBuildsPopupState>(delegate(State state)
  273. {
  274. ((TooMuchParallelBuildsPopupState)state).UpdateInfo(succesAction, cancelAction, extraCost);
  275. });
  276. }
  277. public void ShowDailyQuestPopup()
  278. {
  279. this.RequestFirstPopup<QuestsPopupState>(delegate(State state)
  280. {
  281. ((QuestsPopupState)state).Init(QuestsMenuTabs.DailyQuests);
  282. });
  283. }
  284. public void ShowBuildDemolishPopup(BuildDemolishTabs buildDemolishTab = BuildDemolishTabs.Build)
  285. {
  286. this.RequestFirstPopup<BuildDemolishPopupState>(delegate(State state)
  287. {
  288. ((BuildDemolishPopupState)state).SwitchTab(buildDemolishTab);
  289. });
  290. }
  291. public bool AreStatesInitialized
  292. {
  293. get
  294. {
  295. return this.fsm.AreStatesInitialized;
  296. }
  297. }
  298. public FiniteStateMachine fsm;
  299. public GameObject overlay;
  300. private LinkedList<PopupRequest> scheduledRequests = new LinkedList<PopupRequest>();
  301. private bool _popupsMayOpen;
  302. private bool _popupsAreSuppressed;
  303. public delegate void PopupOpened(State state);
  304. public delegate void StatesInitializedEventHandler();
  305. }