using FL.FGUI; using System.Collections.Generic; using XGame.Framework; using XGame.Framework.Nodes; using XGame.Framework.UI; namespace FL.Nodes.GameMain { public class UIComponent : NodeComponent { private Dictionary> _openKeysMap = new(); public override void OnEnable(object intent) { FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened); FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed); Context.UI.OpenAsync(UIKeys.MainHomePanel, "打开MainHomePanel"); Context.UI.OpenAsync(UIKeys.MainHomeBottomPanel); Context.Tree.AddAsync(NodeKeys.Partners); Context.Tree.AddAsync(NodeKeys.DragonEgg); Context.Tree.AddAsync(NodeKeys.Mail); Context.Tree.AddAsync(NodeKeys.Summon); Context.Tree.AddAsync(NodeKeys.Shop); Context.Tree.AddAsync(NodeKeys.Item); Context.Tree.Remove(NodeKeys.Login, true); } public override void OnDisable() { // 先移除监听事件 FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened); FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed); Context.UI.Close(UIKeys.MainHomePanel); Context.UI.Close(UIKeys.MainHomeBottomPanel); Context.UI.Close(UIKeys.CommonToastPanel); Context.Tree.Remove(NodeKeys.Partners); Context.Tree.Remove(NodeKeys.DragonEgg); Context.Tree.Remove(NodeKeys.Mail); Context.Tree.Remove(NodeKeys.Summon); Context.Tree.Remove(NodeKeys.Shop); Context.Tree.Remove(NodeKeys.Item); _openKeysMap.Clear(); } #region UILayer管理 private void OnUIOpened(int eventId, object args) { var uikey = args as UIKey; if (uikey == UIKeys.MainHomeBottomPanel) return; // 主界面底部菜单固定显示,忽略 var layer = UIKeys.GetLayer(uikey); if (IsFiltered(layer)) return; if (!_openKeysMap.TryGetValue(layer, out var openKeys)) { openKeys = new HashSet(); _openKeysMap[layer] = openKeys; } openKeys.Add(uikey); // 打开Normal的UI,关闭所有Middle和High // 打开Middle的UI,关闭所有High if (layer is UILayer.Normal or UILayer.Middle) { CloseUIByLayer(UILayer.High); } if (layer is UILayer.Normal) { CloseUIByLayer(UILayer.Middle); } } private void OnUIClosed(int eventId, object args) { var uikey = args as UIKey; var layer = UIKeys.GetLayer(uikey); if (_openKeysMap.TryGetValue(layer, out var openKeys)) { openKeys.Remove(uikey); } } /// /// 关闭指定Layer的所有UI /// /// private void CloseUIByLayer(UILayer layer) { if (_openKeysMap.TryGetValue(layer, out var openKeys) && openKeys.Count > 0) { var list = ListPool.Acquire(); list.AddRange(openKeys); foreach (var item in list) { Context.UI.Close(item); } ListPool.Recycle(list); } } private bool IsFiltered(UILayer layer) { return layer is UILayer.MapUI or UILayer.Toast or UILayer.Guide or UILayer.Effect; } #endregion } }