MainHomeBottomPanelCtrl.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /// #pkgName FGUI包名
  2. /// #panelName UIPanel名字
  3. /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
  4. /// 该脚本由模板创建
  5. /// created by cb 2024
  6. using FairyGUI;
  7. using System.Collections.Generic;
  8. using XGame;
  9. using XGame.Framework;
  10. using XGame.Framework.UI;
  11. namespace FL.FGUI
  12. {
  13. /// <summary>
  14. /// UI逻辑处理类
  15. /// </summary>
  16. /// <typeparam name=""></typeparam>
  17. public partial class MainHomeBottomPanelCtrl : UIController<MainHomeBottomPanelVM>
  18. {
  19. private UIKey _tempKey;
  20. /// <summary>
  21. /// 已开启的Normal层UI队列
  22. /// </summary>
  23. private List<UIKey> _normalUIs = new();
  24. protected override void OnEnable(object intent)
  25. {
  26. AddUIListenres();
  27. VM.BtnCtrl.onChanged.Add(OnBtnCtrlChanged);
  28. FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened);
  29. FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
  30. Mock();
  31. }
  32. protected override void OnDisable()
  33. {
  34. RemoveUIListenres();
  35. VM.BtnCtrl.onChanged.Remove(OnBtnCtrlChanged);
  36. FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened);
  37. FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
  38. _tempKey = null;
  39. _normalUIs.Clear();
  40. }
  41. #region UI事件
  42. private void AddUIListenres()
  43. {
  44. VM.ClosePanelBtn.onClick.Add(OnClickClosePanelBtn);
  45. }
  46. private void RemoveUIListenres()
  47. {
  48. VM.ClosePanelBtn.onClick.Remove(OnClickClosePanelBtn);
  49. }
  50. private void OnClickClosePanelBtn(EventContext context)
  51. {
  52. if (_normalUIs.Count > 0)
  53. {
  54. var uikey = _normalUIs[0];
  55. _normalUIs.RemoveAt(0);
  56. Context.UI.Close(uikey);
  57. return;
  58. }
  59. VM.BtnCtrl.selectedIndex = 0;
  60. }
  61. #endregion
  62. private void Mock()
  63. { //TODO 临时代码
  64. VM.SetUnlockState(4, false, "主线4\r解锁");
  65. VM.SetUnlockState(5, false, "主线5\r解锁");
  66. }
  67. #region UI开启/关闭监听事件
  68. private bool IsUnlock(int index)
  69. { //TODO 临时代码
  70. return index switch
  71. {
  72. 4 => false,
  73. 5 => false,
  74. _ => true
  75. };
  76. }
  77. private UIKey IndexToKey(int index)
  78. {
  79. return index switch
  80. {
  81. 1 => UIKeys.PlayerMainPanel,
  82. 2 => UIKeys.PartnerMainPanel,
  83. 3 => UIKeys.PartnerEpiMainPanel,
  84. 6 => UIKeys.DragonEggTreasurePanel,
  85. _ => null,
  86. };
  87. }
  88. private int KeyToIndex(UIKey uiKey)
  89. {
  90. if (uiKey == UIKeys.PlayerMainPanel) return 1;
  91. if (uiKey == UIKeys.PartnerMainPanel) return 2;
  92. if (uiKey == UIKeys.PartnerEpiMainPanel) return 3;
  93. if (uiKey == UIKeys.DragonEggTreasurePanel) return 6;
  94. return -1;
  95. }
  96. private void OnBtnCtrlChanged(EventContext context)
  97. {
  98. Log.Debug($"OnBtnCtrlChanged. previousIndex:{VM.BtnCtrl.previousIndex} selectedIndex:{VM.BtnCtrl.selectedIndex}");
  99. if (VM.BtnCtrl.selectedIndex == 7)
  100. { // 7为单独显示返回按钮
  101. return;
  102. }
  103. CloseAllNormalUI();
  104. var lastUIKey = IndexToKey(VM.BtnCtrl.previousIndex);
  105. if (lastUIKey != null)
  106. {
  107. _tempKey = lastUIKey;
  108. Context.UI.Close(lastUIKey);
  109. }
  110. _tempKey = null;
  111. if (!IsUnlock(VM.BtnCtrl.selectedIndex))
  112. {
  113. Context.ShowTips("功能未解锁!");
  114. VM.BtnCtrl.selectedIndex = 0;
  115. return;
  116. }
  117. var nextUIKey = IndexToKey(VM.BtnCtrl.selectedIndex);
  118. if (nextUIKey != null)
  119. {
  120. _tempKey = nextUIKey;
  121. var async = Context.UI.OpenAsync(nextUIKey);
  122. async.On(_ =>
  123. {
  124. _tempKey = null;
  125. });
  126. }
  127. }
  128. private void OnUIOpened(int eventId, object args)
  129. {
  130. var uikey = args as UIKey;
  131. Log.Debug($"收到UI开启事件 UIKey:{uikey} selectedIndex:{VM.BtnCtrl.selectedIndex}");
  132. if (uikey == _tempKey) return; // 避免重复监听
  133. var index = KeyToIndex(uikey);
  134. if (index != -1)
  135. { // 是控制器关联的UI
  136. if (index != VM.BtnCtrl.selectedIndex)
  137. { // 控制器索引不一致
  138. VM.BtnCtrl.selectedIndex = index;
  139. }
  140. return;
  141. }
  142. if (_normalUIs.Contains(uikey))
  143. {
  144. Log.Error($"重复记录UI开启事件. UIKey:{uikey}");
  145. return;
  146. }
  147. var layer = UIKeys.GetLayer(uikey);
  148. if (layer != UILayer.Normal)
  149. return;
  150. if (VM.BtnCtrl.selectedIndex == 0)
  151. {// 7为单独显示返回按钮
  152. VM.BtnCtrl.selectedIndex = 7;
  153. }
  154. _normalUIs.Insert(0, uikey);
  155. }
  156. private void OnUIClosed(int eventId, object args)
  157. {
  158. var uikey = args as UIKey;
  159. Log.Debug($"收到UI关闭事件 UIKey:{uikey} selectedIndex:{VM.BtnCtrl.selectedIndex}");
  160. if (uikey == _tempKey) return; // 避免重复监听
  161. if (UIKeys.GetLayer(uikey) == UILayer.Normal)
  162. {
  163. _normalUIs.Remove(uikey);
  164. }
  165. var index = KeyToIndex(uikey);
  166. if (index != -1)
  167. { // 是控制器关联的UI
  168. if (index == VM.BtnCtrl.selectedIndex)
  169. { // 控制器索引是当前关闭的UI
  170. VM.BtnCtrl.selectedIndex = 0;
  171. }
  172. }
  173. }
  174. /// <summary>
  175. /// 关闭所有的Normal层UI
  176. /// </summary>
  177. private void CloseAllNormalUI()
  178. {
  179. if (_normalUIs.Count == 0)
  180. return;
  181. for(var i = _normalUIs.Count - 1; i >= 0; i--)
  182. {
  183. var uikey = _normalUIs[i];
  184. _normalUIs.RemoveAt(i);
  185. _tempKey = uikey;
  186. Context.UI.Close(uikey);
  187. }
  188. _tempKey = null;
  189. }
  190. #endregion
  191. }
  192. }