MainHomeBottomPanelCtrl.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. Mock();
  25. }
  26. protected override void OnDisable()
  27. {
  28. RemoveUIListenres();
  29. VM.BtnCtrl.onChanged.Remove(OnBtnCtrlChanged);
  30. FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened);
  31. FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
  32. _tempKey = null;
  33. }
  34. #region UI事件
  35. private void AddUIListenres()
  36. {
  37. VM.ClosePanelBtn.onClick.Add(OnClickClosePanelBtn);
  38. }
  39. private void RemoveUIListenres()
  40. {
  41. VM.ClosePanelBtn.onClick.Remove(OnClickClosePanelBtn);
  42. }
  43. private void OnClickClosePanelBtn(EventContext context)
  44. {
  45. VM.BtnCtrl.selectedIndex = 0;
  46. }
  47. #endregion
  48. private void Mock()
  49. { //TODO 临时代码
  50. VM.SetUnlockState(4, false, "主线4\r解锁");
  51. VM.SetUnlockState(5, false, "主线5\r解锁");
  52. }
  53. #region UI开启/关闭监听事件
  54. private bool IsUnlock(int index)
  55. { //TODO 临时代码
  56. return index switch
  57. {
  58. 4 => false,
  59. 5 => false,
  60. _ => true
  61. };
  62. }
  63. private UIKey IndexToKey(int index)
  64. {
  65. return index switch
  66. {
  67. 1 => UIKeys.PlayerMainPanel,
  68. 2 => UIKeys.PartnerMainPanel,
  69. 3 => UIKeys.PartnerEpiMainPanel,
  70. 6 => UIKeys.DragonEggTreasurePanel,
  71. _ => null,
  72. };
  73. }
  74. private int KeyToIndex(UIKey uiKey)
  75. {
  76. if (uiKey == UIKeys.PlayerMainPanel) return 1;
  77. if (uiKey == UIKeys.PartnerMainPanel) return 2;
  78. if (uiKey == UIKeys.PartnerEpiMainPanel) return 3;
  79. if (uiKey == UIKeys.DragonEggTreasurePanel) return 6;
  80. return -1;
  81. }
  82. private void OnBtnCtrlChanged(EventContext context)
  83. {
  84. XGame.Log.Debug($"OnBtnCtrlChanged. previousIndex:{VM.BtnCtrl.previousIndex} selectedIndex:{VM.BtnCtrl.selectedIndex}");
  85. var lastUIKey = IndexToKey(VM.BtnCtrl.previousIndex);
  86. if (lastUIKey != null)
  87. {
  88. _tempKey = lastUIKey;
  89. Context.UI.Close(lastUIKey);
  90. }
  91. _tempKey = null;
  92. if (!IsUnlock(VM.BtnCtrl.selectedIndex))
  93. {
  94. Context.ShowTips("功能未解锁!");
  95. VM.BtnCtrl.selectedIndex = 0;
  96. return;
  97. }
  98. var nextUIKey = IndexToKey(VM.BtnCtrl.selectedIndex);
  99. if (nextUIKey != null)
  100. {
  101. _tempKey = nextUIKey;
  102. var async = Context.UI.OpenAsync(nextUIKey);
  103. async.On(_ =>
  104. {
  105. _tempKey = null;
  106. });
  107. }
  108. }
  109. private void OnUIOpened(int eventId, object args)
  110. {
  111. var uiKey = args as UIKey;
  112. XGame.Log.Debug($"收到UI开启事件 UIKey:{uiKey} selectedIndex:{VM.BtnCtrl.selectedIndex}");
  113. if (uiKey == _tempKey) return; // 避免重复监听
  114. var index = KeyToIndex(uiKey);
  115. if (index != -1)
  116. { // 是控制器关联的UI
  117. if (index != VM.BtnCtrl.selectedIndex)
  118. { // 控制器索引不一致
  119. VM.BtnCtrl.selectedIndex = index;
  120. }
  121. }
  122. }
  123. private void OnUIClosed(int eventId, object args)
  124. {
  125. var uiKey = args as UIKey;
  126. XGame.Log.Debug($"收到UI关闭事件 UIKey:{uiKey} selectedIndex:{VM.BtnCtrl.selectedIndex}");
  127. if (uiKey == _tempKey) return; // 避免重复监听
  128. var index = KeyToIndex(uiKey);
  129. if (index != -1)
  130. { // 是控制器关联的UI
  131. if (index == VM.BtnCtrl.selectedIndex)
  132. { // 控制器索引是当前关闭的UI
  133. VM.BtnCtrl.selectedIndex = 0;
  134. }
  135. }
  136. }
  137. #endregion
  138. }
  139. }