MainHomeBottomPanelCtrl.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /// #pkgName FGUI包名
  2. /// #panelName UIPanel名字
  3. /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
  4. /// 该脚本由模板创建
  5. /// created by cb 2024
  6. using FairyGUI;
  7. using XGame.Framework;
  8. using XGame.Framework.UI;
  9. namespace FL.FGUI
  10. {
  11. /// <summary>
  12. /// UI逻辑处理类
  13. /// </summary>
  14. /// <typeparam name=""></typeparam>
  15. public partial class MainHomeBottomPanelCtrl : UIController<MainHomeBottomPanelVM>
  16. {
  17. private UIKey _tempKey;
  18. protected override void OnEnable(object intent)
  19. {
  20. //AddUIListenres();
  21. VM.BtnCtrl.onChanged.Add(OnBtnCtrlChanged);
  22. FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened);
  23. FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
  24. }
  25. protected override void OnDisable()
  26. {
  27. //RemoveUIListenres();
  28. VM.BtnCtrl.onChanged.Remove(OnBtnCtrlChanged);
  29. FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened);
  30. FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
  31. _tempKey = null;
  32. }
  33. #region UI事件
  34. //private void AddUIListenres()
  35. //{
  36. // VM.RoleBtn.onClick.Add(OnClickRoleBtn);
  37. // VM.FubenBtn.onClick.Add(OnClickFubenBtn);
  38. // VM.ShopBtn.onClick.Add(OnClickShopBtn);
  39. // VM.HomeBtn.onClick.Add(OnClickHomeBtn);
  40. // VM.GonghuiBtn.onClick.Add(OnClickGonghuiBtn);
  41. // VM.JianlingBtn.onClick.Add(OnClickJianlingBtn);
  42. //}
  43. //private void RemoveUIListenres()
  44. //{
  45. // VM.RoleBtn.onClick.Remove(OnClickRoleBtn);
  46. // VM.FubenBtn.onClick.Remove(OnClickFubenBtn);
  47. // VM.ShopBtn.onClick.Remove(OnClickShopBtn);
  48. // VM.HomeBtn.onClick.Remove(OnClickHomeBtn);
  49. // VM.GonghuiBtn.onClick.Remove(OnClickGonghuiBtn);
  50. // VM.JianlingBtn.onClick.Remove(OnClickJianlingBtn);
  51. //}
  52. //private void OnClickRoleBtn(EventContext context)
  53. //{
  54. // XGame.Log.Debug($"OnClickRoleBtn");
  55. //}
  56. //private void OnClickFubenBtn(EventContext context)
  57. //{
  58. // XGame.Log.Debug($"OnClickFubenBtn");
  59. //}
  60. //private void OnClickShopBtn(EventContext context)
  61. //{
  62. // XGame.Log.Debug($"OnClickShopBtn");
  63. // Context.UI.OpenAsync(UIKeys.BattleMapBattleMapTest);
  64. //}
  65. //private void OnClickHomeBtn(EventContext context)
  66. //{
  67. // XGame.Log.Debug($"OnClickHomeBtn");
  68. //}
  69. //private void OnClickGonghuiBtn(EventContext context)
  70. //{
  71. // XGame.Log.Debug($"OnClickGonghuiBtn");
  72. //}
  73. //private void OnClickJianlingBtn(EventContext context)
  74. //{
  75. // XGame.Log.Debug($"OnClickJianlingBtn");
  76. //}
  77. #endregion
  78. #region UI开启/关闭监听事件
  79. private UIKey IndexToKey(int index)
  80. {
  81. return index switch
  82. {
  83. 1 => UIKeys.PlayerMainPanel,
  84. 2 => UIKeys.PartnerMainPanel,
  85. 3 => UIKeys.PartnerEpiMainPanel,
  86. 6 => UIKeys.DragonEggTreasurePanel,
  87. _ => null,
  88. };
  89. }
  90. private int KeyToIndex(UIKey uiKey)
  91. {
  92. if (uiKey == UIKeys.PlayerMainPanel) return 1;
  93. if (uiKey == UIKeys.PartnerMainPanel) return 2;
  94. if (uiKey == UIKeys.PartnerEpiMainPanel) return 3;
  95. if (uiKey == UIKeys.DragonEggTreasurePanel) return 6;
  96. return -1;
  97. }
  98. private void OnBtnCtrlChanged(EventContext context)
  99. {
  100. XGame.Log.Debug($"OnBtnCtrlChanged. previousIndex:{VM.BtnCtrl.previousIndex} selectedIndex:{VM.BtnCtrl.selectedIndex}");
  101. var lastUIKey = IndexToKey(VM.BtnCtrl.previousIndex);
  102. if (lastUIKey != null)
  103. {
  104. _tempKey = lastUIKey;
  105. Context.UI.Close(lastUIKey);
  106. }
  107. _tempKey = null;
  108. var nextUIKey = IndexToKey(VM.BtnCtrl.selectedIndex);
  109. if (nextUIKey != null)
  110. {
  111. _tempKey = nextUIKey;
  112. var async = Context.UI.OpenAsync(nextUIKey);
  113. async.On(_ =>
  114. {
  115. _tempKey = null;
  116. });
  117. }
  118. }
  119. private void OnUIOpened(int eventId, object args)
  120. {
  121. var uiKey = args as UIKey;
  122. XGame.Log.Debug($"收到UI开启事件 UIKey:{uiKey} selectedIndex:{VM.BtnCtrl.selectedIndex}");
  123. if (uiKey == _tempKey) return; // 避免重复监听
  124. var index = KeyToIndex(uiKey);
  125. if (index != -1)
  126. { // 是控制器关联的UI
  127. if (index != VM.BtnCtrl.selectedIndex)
  128. { // 控制器索引不一致
  129. VM.BtnCtrl.selectedIndex = index;
  130. }
  131. }
  132. }
  133. private void OnUIClosed(int eventId, object args)
  134. {
  135. var uiKey = args as UIKey;
  136. XGame.Log.Debug($"收到UI关闭事件 UIKey:{uiKey} selectedIndex:{VM.BtnCtrl.selectedIndex}");
  137. if (uiKey == _tempKey) return; // 避免重复监听
  138. var index = KeyToIndex(uiKey);
  139. if (index != -1)
  140. { // 是控制器关联的UI
  141. if (index == VM.BtnCtrl.selectedIndex)
  142. { // 控制器索引是当前关闭的UI
  143. VM.BtnCtrl.selectedIndex = 0;
  144. }
  145. }
  146. }
  147. #endregion
  148. }
  149. }