UIComponent.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.Remove(NodeKeys.Login, true);
  21. }
  22. public override void OnDisable()
  23. {
  24. // 先移除监听事件
  25. FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened);
  26. FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
  27. Context.UI.Close(UIKeys.MainHomePanel);
  28. Context.UI.Close(UIKeys.MainHomeBottomPanel);
  29. Context.UI.Close(UIKeys.CommonToastPanel);
  30. Context.Tree.Remove(NodeKeys.Partners);
  31. Context.Tree.Remove(NodeKeys.DragonEgg);
  32. Context.Tree.Remove(NodeKeys.Mail);
  33. _openKeysMap.Clear();
  34. }
  35. #region UILayer管理
  36. private void OnUIOpened(int eventId, object args)
  37. {
  38. var uikey = args as UIKey;
  39. if (uikey == UIKeys.MainHomeBottomPanel)
  40. return; // 主界面底部菜单固定显示,忽略
  41. var layer = UIKeys.GetLayer(uikey);
  42. if (IsFiltered(layer))
  43. return;
  44. if (!_openKeysMap.TryGetValue(layer, out var openKeys))
  45. {
  46. openKeys = new HashSet<UIKey>();
  47. _openKeysMap[layer] = openKeys;
  48. }
  49. openKeys.Add(uikey);
  50. // 打开Normal的UI,关闭所有Middle和High
  51. // 打开Middle的UI,关闭所有High
  52. if (layer is UILayer.Normal or UILayer.Middle)
  53. {
  54. CloseUIByLayer(UILayer.High);
  55. }
  56. if (layer is UILayer.Normal)
  57. {
  58. CloseUIByLayer(UILayer.Middle);
  59. }
  60. }
  61. private void OnUIClosed(int eventId, object args)
  62. {
  63. var uikey = args as UIKey;
  64. var layer = UIKeys.GetLayer(uikey);
  65. if (_openKeysMap.TryGetValue(layer, out var openKeys))
  66. {
  67. openKeys.Remove(uikey);
  68. }
  69. }
  70. /// <summary>
  71. /// 关闭指定Layer的所有UI
  72. /// </summary>
  73. /// <param name="layer"></param>
  74. private void CloseUIByLayer(UILayer layer)
  75. {
  76. if (_openKeysMap.TryGetValue(layer, out var openKeys) && openKeys.Count > 0)
  77. {
  78. var list = ListPool.Acquire<UIKey>();
  79. list.AddRange(openKeys);
  80. foreach (var item in list)
  81. {
  82. Context.UI.Close(item);
  83. }
  84. ListPool.Recycle(list);
  85. }
  86. }
  87. private bool IsFiltered(UILayer layer)
  88. {
  89. return layer is UILayer.MapUI or UILayer.Toast or UILayer.Guide or UILayer.Effect;
  90. }
  91. #endregion
  92. }
  93. }