MainHomeBottomPanelCtrl.cs 6.5 KB

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