UIComponent.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using FL.FGUI;
  2. using System.Collections.Generic;
  3. using XGame.Framework;
  4. using XGame.Framework.Nodes;
  5. using XGame.Framework.UI;
  6. namespace FL.Nodes.GameMain
  7. {
  8. public class UIComponent : NodeComponent
  9. {
  10. private Dictionary<UILayer, HashSet<UIKey>> _openKeysMap = new();
  11. public override void OnEnable(object intent)
  12. {
  13. FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened);
  14. FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
  15. Context.UI.OpenAsync(UIKeys.MainHomePanel, "打开MainHomePanel");
  16. Context.UI.OpenAsync(UIKeys.MainHomeBottomPanel);
  17. Context.Tree.AddAsync(NodeKeys.Partners);
  18. Context.Tree.AddAsync(NodeKeys.DragonEgg);
  19. Context.Tree.AddAsync(NodeKeys.Mail);
  20. Context.Tree.AddAsync(NodeKeys.Summon);
  21. Context.Tree.AddAsync(NodeKeys.Shop);
  22. Context.Tree.AddAsync(NodeKeys.Item);
  23. Context.Tree.Remove(NodeKeys.Login, true);
  24. }
  25. public override void OnDisable()
  26. {
  27. // 先移除监听事件
  28. FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened);
  29. FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
  30. Context.UI.Close(UIKeys.MainHomePanel);
  31. Context.UI.Close(UIKeys.MainHomeBottomPanel);
  32. Context.UI.Close(UIKeys.CommonToastPanel);
  33. Context.Tree.Remove(NodeKeys.Partners);
  34. Context.Tree.Remove(NodeKeys.DragonEgg);
  35. Context.Tree.Remove(NodeKeys.Mail);
  36. Context.Tree.Remove(NodeKeys.Summon);
  37. Context.Tree.Remove(NodeKeys.Shop);
  38. Context.Tree.Remove(NodeKeys.Item);
  39. _openKeysMap.Clear();
  40. }
  41. #region UILayer管理
  42. private void OnUIOpened(int eventId, object args)
  43. {
  44. var uikey = args as UIKey;
  45. if (uikey == UIKeys.MainHomeBottomPanel)
  46. return; // 主界面底部菜单固定显示,忽略
  47. var layer = UIKeys.GetLayer(uikey);
  48. if (IsFiltered(layer))
  49. return;
  50. if (!_openKeysMap.TryGetValue(layer, out var openKeys))
  51. {
  52. openKeys = new HashSet<UIKey>();
  53. _openKeysMap[layer] = openKeys;
  54. }
  55. openKeys.Add(uikey);
  56. // 打开Normal的UI,关闭所有Middle和High
  57. // 打开Middle的UI,关闭所有High
  58. if (layer is UILayer.Normal or UILayer.Middle)
  59. {
  60. CloseUIByLayer(UILayer.High);
  61. }
  62. if (layer is UILayer.Normal)
  63. {
  64. CloseUIByLayer(UILayer.Middle);
  65. }
  66. }
  67. private void OnUIClosed(int eventId, object args)
  68. {
  69. var uikey = args as UIKey;
  70. var layer = UIKeys.GetLayer(uikey);
  71. if (_openKeysMap.TryGetValue(layer, out var openKeys))
  72. {
  73. openKeys.Remove(uikey);
  74. }
  75. }
  76. /// <summary>
  77. /// 关闭指定Layer的所有UI
  78. /// </summary>
  79. /// <param name="layer"></param>
  80. private void CloseUIByLayer(UILayer layer)
  81. {
  82. if (_openKeysMap.TryGetValue(layer, out var openKeys) && openKeys.Count > 0)
  83. {
  84. var list = ListPool.Acquire<UIKey>();
  85. list.AddRange(openKeys);
  86. foreach (var item in list)
  87. {
  88. Context.UI.Close(item);
  89. }
  90. ListPool.Recycle(list);
  91. }
  92. }
  93. private bool IsFiltered(UILayer layer)
  94. {
  95. return layer is UILayer.MapUI or UILayer.Toast or UILayer.Guide or UILayer.Effect;
  96. }
  97. #endregion
  98. }
  99. }