MainHomeBottomPanelCtrl.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. 6 => UIKeys.DragonEggBox,
  86. _ => null,
  87. };
  88. }
  89. private int KeyToIndex(UIKey uiKey)
  90. {
  91. if (uiKey == UIKeys.PlayerMainPanel) return 1;
  92. if (uiKey == UIKeys.PartnerMainPanel) return 2;
  93. if (uiKey == UIKeys.DragonEggBox) return 6;
  94. return -1;
  95. }
  96. private void OnBtnCtrlChanged(EventContext context)
  97. {
  98. XGame.Log.Debug($"OnBtnCtrlChanged. previousIndex:{VM.BtnCtrl.previousIndex} selectedIndex:{VM.BtnCtrl.selectedIndex}");
  99. var lastUIKey = IndexToKey(VM.BtnCtrl.previousIndex);
  100. if (lastUIKey != null)
  101. {
  102. _tempKey = lastUIKey;
  103. Context.UI.Close(lastUIKey);
  104. }
  105. _tempKey = null;
  106. var nextUIKey = IndexToKey(VM.BtnCtrl.selectedIndex);
  107. if (nextUIKey != null)
  108. {
  109. _tempKey = nextUIKey;
  110. var async = Context.UI.OpenAsync(nextUIKey);
  111. async.On(_ =>
  112. {
  113. _tempKey = null;
  114. });
  115. }
  116. }
  117. private void OnUIOpened(int eventId, object args)
  118. {
  119. var uiKey = args as UIKey;
  120. XGame.Log.Debug($"收到UI开启事件 UIKey:{uiKey} selectedIndex:{VM.BtnCtrl.selectedIndex}");
  121. if (uiKey == _tempKey) return; // 避免重复监听
  122. var index = KeyToIndex(uiKey);
  123. if (index != -1)
  124. { // 是控制器关联的UI
  125. if (index != VM.BtnCtrl.selectedIndex)
  126. { // 控制器索引不一致
  127. VM.BtnCtrl.selectedIndex = index;
  128. }
  129. }
  130. }
  131. private void OnUIClosed(int eventId, object args)
  132. {
  133. var uiKey = args as UIKey;
  134. XGame.Log.Debug($"收到UI关闭事件 UIKey:{uiKey} selectedIndex:{VM.BtnCtrl.selectedIndex}");
  135. if (uiKey == _tempKey) return; // 避免重复监听
  136. var index = KeyToIndex(uiKey);
  137. if (index != -1)
  138. { // 是控制器关联的UI
  139. if (index == VM.BtnCtrl.selectedIndex)
  140. { // 控制器索引是当前关闭的UI
  141. VM.BtnCtrl.selectedIndex = 0;
  142. }
  143. }
  144. }
  145. #endregion
  146. }
  147. }