UIManager.UIGroup.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System.Collections.Generic;
  8. namespace GameFramework.UI
  9. {
  10. internal sealed partial class UIManager : GameFrameworkModule, IUIManager
  11. {
  12. /// <summary>
  13. /// 界面组。
  14. /// </summary>
  15. private sealed partial class UIGroup : IUIGroup
  16. {
  17. private readonly string m_Name;
  18. private int m_Depth;
  19. private bool m_Pause;
  20. private readonly IUIGroupHelper m_UIGroupHelper;
  21. private readonly GameFrameworkLinkedList<UIFormInfo> m_UIFormInfos;
  22. private LinkedListNode<UIFormInfo> m_CachedNode;
  23. /// <summary>
  24. /// 初始化界面组的新实例。
  25. /// </summary>
  26. /// <param name="name">界面组名称。</param>
  27. /// <param name="depth">界面组深度。</param>
  28. /// <param name="uiGroupHelper">界面组辅助器。</param>
  29. public UIGroup(string name, int depth, IUIGroupHelper uiGroupHelper)
  30. {
  31. if (string.IsNullOrEmpty(name))
  32. {
  33. throw new GameFrameworkException("UI group name is invalid.");
  34. }
  35. if (uiGroupHelper == null)
  36. {
  37. throw new GameFrameworkException("UI group helper is invalid.");
  38. }
  39. m_Name = name;
  40. m_Pause = false;
  41. m_UIGroupHelper = uiGroupHelper;
  42. m_UIFormInfos = new GameFrameworkLinkedList<UIFormInfo>();
  43. m_CachedNode = null;
  44. Depth = depth;
  45. }
  46. /// <summary>
  47. /// 获取界面组名称。
  48. /// </summary>
  49. public string Name
  50. {
  51. get
  52. {
  53. return m_Name;
  54. }
  55. }
  56. /// <summary>
  57. /// 获取或设置界面组深度。
  58. /// </summary>
  59. public int Depth
  60. {
  61. get
  62. {
  63. return m_Depth;
  64. }
  65. set
  66. {
  67. if (m_Depth == value)
  68. {
  69. return;
  70. }
  71. m_Depth = value;
  72. m_UIGroupHelper.SetDepth(m_Depth);
  73. Refresh();
  74. }
  75. }
  76. /// <summary>
  77. /// 获取或设置界面组是否暂停。
  78. /// </summary>
  79. public bool Pause
  80. {
  81. get
  82. {
  83. return m_Pause;
  84. }
  85. set
  86. {
  87. if (m_Pause == value)
  88. {
  89. return;
  90. }
  91. m_Pause = value;
  92. Refresh();
  93. }
  94. }
  95. /// <summary>
  96. /// 获取界面组中界面数量。
  97. /// </summary>
  98. public int UIFormCount
  99. {
  100. get
  101. {
  102. return m_UIFormInfos.Count;
  103. }
  104. }
  105. /// <summary>
  106. /// 获取当前界面。
  107. /// </summary>
  108. public IUIForm CurrentUIForm
  109. {
  110. get
  111. {
  112. return m_UIFormInfos.First != null ? m_UIFormInfos.First.Value.UIForm : null;
  113. }
  114. }
  115. /// <summary>
  116. /// 获取界面组辅助器。
  117. /// </summary>
  118. public IUIGroupHelper Helper
  119. {
  120. get
  121. {
  122. return m_UIGroupHelper;
  123. }
  124. }
  125. /// <summary>
  126. /// 界面组轮询。
  127. /// </summary>
  128. /// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
  129. /// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
  130. public void Update(float elapseSeconds, float realElapseSeconds)
  131. {
  132. LinkedListNode<UIFormInfo> current = m_UIFormInfos.First;
  133. while (current != null)
  134. {
  135. if (current.Value.Paused)
  136. {
  137. break;
  138. }
  139. m_CachedNode = current.Next;
  140. current.Value.UIForm.OnUpdate(elapseSeconds, realElapseSeconds);
  141. current = m_CachedNode;
  142. m_CachedNode = null;
  143. }
  144. }
  145. /// <summary>
  146. /// 界面组中是否存在界面。
  147. /// </summary>
  148. /// <param name="serialId">界面序列编号。</param>
  149. /// <returns>界面组中是否存在界面。</returns>
  150. public bool HasUIForm(int serialId)
  151. {
  152. foreach (UIFormInfo uiFormInfo in m_UIFormInfos)
  153. {
  154. if (uiFormInfo.UIForm.SerialId == serialId)
  155. {
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161. /// <summary>
  162. /// 界面组中是否存在界面。
  163. /// </summary>
  164. /// <param name="uiFormAssetName">界面资源名称。</param>
  165. /// <returns>界面组中是否存在界面。</returns>
  166. public bool HasUIForm(string uiFormAssetName)
  167. {
  168. if (string.IsNullOrEmpty(uiFormAssetName))
  169. {
  170. throw new GameFrameworkException("UI form asset name is invalid.");
  171. }
  172. foreach (UIFormInfo uiFormInfo in m_UIFormInfos)
  173. {
  174. if (uiFormInfo.UIForm.UIFormAssetName == uiFormAssetName)
  175. {
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. /// <summary>
  182. /// 从界面组中获取界面。
  183. /// </summary>
  184. /// <param name="serialId">界面序列编号。</param>
  185. /// <returns>要获取的界面。</returns>
  186. public IUIForm GetUIForm(int serialId)
  187. {
  188. foreach (UIFormInfo uiFormInfo in m_UIFormInfos)
  189. {
  190. if (uiFormInfo.UIForm.SerialId == serialId)
  191. {
  192. return uiFormInfo.UIForm;
  193. }
  194. }
  195. return null;
  196. }
  197. /// <summary>
  198. /// 从界面组中获取界面。
  199. /// </summary>
  200. /// <param name="uiFormAssetName">界面资源名称。</param>
  201. /// <returns>要获取的界面。</returns>
  202. public IUIForm GetUIForm(string uiFormAssetName)
  203. {
  204. if (string.IsNullOrEmpty(uiFormAssetName))
  205. {
  206. throw new GameFrameworkException("UI form asset name is invalid.");
  207. }
  208. foreach (UIFormInfo uiFormInfo in m_UIFormInfos)
  209. {
  210. if (uiFormInfo.UIForm.UIFormAssetName == uiFormAssetName)
  211. {
  212. return uiFormInfo.UIForm;
  213. }
  214. }
  215. return null;
  216. }
  217. /// <summary>
  218. /// 从界面组中获取界面。
  219. /// </summary>
  220. /// <param name="uiFormAssetName">界面资源名称。</param>
  221. /// <returns>要获取的界面。</returns>
  222. public IUIForm[] GetUIForms(string uiFormAssetName)
  223. {
  224. if (string.IsNullOrEmpty(uiFormAssetName))
  225. {
  226. throw new GameFrameworkException("UI form asset name is invalid.");
  227. }
  228. List<IUIForm> results = new List<IUIForm>();
  229. foreach (UIFormInfo uiFormInfo in m_UIFormInfos)
  230. {
  231. if (uiFormInfo.UIForm.UIFormAssetName == uiFormAssetName)
  232. {
  233. results.Add(uiFormInfo.UIForm);
  234. }
  235. }
  236. return results.ToArray();
  237. }
  238. /// <summary>
  239. /// 从界面组中获取界面。
  240. /// </summary>
  241. /// <param name="uiFormAssetName">界面资源名称。</param>
  242. /// <param name="results">要获取的界面。</param>
  243. public void GetUIForms(string uiFormAssetName, List<IUIForm> results)
  244. {
  245. if (string.IsNullOrEmpty(uiFormAssetName))
  246. {
  247. throw new GameFrameworkException("UI form asset name is invalid.");
  248. }
  249. if (results == null)
  250. {
  251. throw new GameFrameworkException("Results is invalid.");
  252. }
  253. results.Clear();
  254. foreach (UIFormInfo uiFormInfo in m_UIFormInfos)
  255. {
  256. if (uiFormInfo.UIForm.UIFormAssetName == uiFormAssetName)
  257. {
  258. results.Add(uiFormInfo.UIForm);
  259. }
  260. }
  261. }
  262. /// <summary>
  263. /// 从界面组中获取所有界面。
  264. /// </summary>
  265. /// <returns>界面组中的所有界面。</returns>
  266. public IUIForm[] GetAllUIForms()
  267. {
  268. List<IUIForm> results = new List<IUIForm>();
  269. foreach (UIFormInfo uiFormInfo in m_UIFormInfos)
  270. {
  271. results.Add(uiFormInfo.UIForm);
  272. }
  273. return results.ToArray();
  274. }
  275. /// <summary>
  276. /// 从界面组中获取所有界面。
  277. /// </summary>
  278. /// <param name="results">界面组中的所有界面。</param>
  279. public void GetAllUIForms(List<IUIForm> results)
  280. {
  281. if (results == null)
  282. {
  283. throw new GameFrameworkException("Results is invalid.");
  284. }
  285. results.Clear();
  286. foreach (UIFormInfo uiFormInfo in m_UIFormInfos)
  287. {
  288. results.Add(uiFormInfo.UIForm);
  289. }
  290. }
  291. /// <summary>
  292. /// 往界面组增加界面。
  293. /// </summary>
  294. /// <param name="uiForm">要增加的界面。</param>
  295. public void AddUIForm(IUIForm uiForm)
  296. {
  297. m_UIFormInfos.AddFirst(UIFormInfo.Create(uiForm));
  298. }
  299. /// <summary>
  300. /// 从界面组移除界面。
  301. /// </summary>
  302. /// <param name="uiForm">要移除的界面。</param>
  303. public void RemoveUIForm(IUIForm uiForm)
  304. {
  305. UIFormInfo uiFormInfo = GetUIFormInfo(uiForm);
  306. if (uiFormInfo == null)
  307. {
  308. throw new GameFrameworkException(Utility.Text.Format("Can not find UI form info for serial id '{0}', UI form asset name is '{1}'.", uiForm.SerialId, uiForm.UIFormAssetName));
  309. }
  310. if (!uiFormInfo.Covered)
  311. {
  312. uiFormInfo.Covered = true;
  313. uiForm.OnCover();
  314. }
  315. if (!uiFormInfo.Paused)
  316. {
  317. uiFormInfo.Paused = true;
  318. uiForm.OnPause();
  319. }
  320. if (m_CachedNode != null && m_CachedNode.Value.UIForm == uiForm)
  321. {
  322. m_CachedNode = m_CachedNode.Next;
  323. }
  324. if (!m_UIFormInfos.Remove(uiFormInfo))
  325. {
  326. throw new GameFrameworkException(Utility.Text.Format("UI group '{0}' not exists specified UI form '[{1}]{2}'.", m_Name, uiForm.SerialId, uiForm.UIFormAssetName));
  327. }
  328. ReferencePool.Release(uiFormInfo);
  329. }
  330. /// <summary>
  331. /// 激活界面。
  332. /// </summary>
  333. /// <param name="uiForm">要激活的界面。</param>
  334. /// <param name="userData">用户自定义数据。</param>
  335. public void RefocusUIForm(IUIForm uiForm, object userData)
  336. {
  337. UIFormInfo uiFormInfo = GetUIFormInfo(uiForm);
  338. if (uiFormInfo == null)
  339. {
  340. throw new GameFrameworkException("Can not find UI form info.");
  341. }
  342. m_UIFormInfos.Remove(uiFormInfo);
  343. m_UIFormInfos.AddFirst(uiFormInfo);
  344. }
  345. /// <summary>
  346. /// 刷新界面组。
  347. /// </summary>
  348. public void Refresh()
  349. {
  350. LinkedListNode<UIFormInfo> current = m_UIFormInfos.First;
  351. bool pause = m_Pause;
  352. bool cover = false;
  353. int depth = UIFormCount;
  354. while (current != null && current.Value != null)
  355. {
  356. LinkedListNode<UIFormInfo> next = current.Next;
  357. current.Value.UIForm.OnDepthChanged(Depth, depth--);
  358. if (current.Value == null)
  359. {
  360. return;
  361. }
  362. if (pause)
  363. {
  364. if (!current.Value.Covered)
  365. {
  366. current.Value.Covered = true;
  367. current.Value.UIForm.OnCover();
  368. if (current.Value == null)
  369. {
  370. return;
  371. }
  372. }
  373. if (!current.Value.Paused)
  374. {
  375. current.Value.Paused = true;
  376. current.Value.UIForm.OnPause();
  377. if (current.Value == null)
  378. {
  379. return;
  380. }
  381. }
  382. }
  383. else
  384. {
  385. if (current.Value.Paused)
  386. {
  387. current.Value.Paused = false;
  388. current.Value.UIForm.OnResume();
  389. if (current.Value == null)
  390. {
  391. return;
  392. }
  393. }
  394. if (current.Value.UIForm.PauseCoveredUIForm)
  395. {
  396. pause = true;
  397. }
  398. if (cover)
  399. {
  400. if (!current.Value.Covered)
  401. {
  402. current.Value.Covered = true;
  403. current.Value.UIForm.OnCover();
  404. if (current.Value == null)
  405. {
  406. return;
  407. }
  408. }
  409. }
  410. else
  411. {
  412. if (current.Value.Covered)
  413. {
  414. current.Value.Covered = false;
  415. current.Value.UIForm.OnReveal();
  416. if (current.Value == null)
  417. {
  418. return;
  419. }
  420. }
  421. cover = true;
  422. }
  423. }
  424. current = next;
  425. }
  426. }
  427. internal void InternalGetUIForms(string uiFormAssetName, List<IUIForm> results)
  428. {
  429. foreach (UIFormInfo uiFormInfo in m_UIFormInfos)
  430. {
  431. if (uiFormInfo.UIForm.UIFormAssetName == uiFormAssetName)
  432. {
  433. results.Add(uiFormInfo.UIForm);
  434. }
  435. }
  436. }
  437. internal void InternalGetAllUIForms(List<IUIForm> results)
  438. {
  439. foreach (UIFormInfo uiFormInfo in m_UIFormInfos)
  440. {
  441. results.Add(uiFormInfo.UIForm);
  442. }
  443. }
  444. private UIFormInfo GetUIFormInfo(IUIForm uiForm)
  445. {
  446. if (uiForm == null)
  447. {
  448. throw new GameFrameworkException("UI form is invalid.");
  449. }
  450. foreach (UIFormInfo uiFormInfo in m_UIFormInfos)
  451. {
  452. if (uiFormInfo.UIForm == uiForm)
  453. {
  454. return uiFormInfo;
  455. }
  456. }
  457. return null;
  458. }
  459. }
  460. }
  461. }