123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- 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<UILayer, HashSet<UIKey>> _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<UIKey>();
- _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);
- }
- }
- /// <summary>
- /// 关闭指定Layer的所有UI
- /// </summary>
- /// <param name="layer"></param>
- private void CloseUIByLayer(UILayer layer)
- {
- if (_openKeysMap.TryGetValue(layer, out var openKeys) && openKeys.Count > 0)
- {
- var list = ListPool.Acquire<UIKey>();
- 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
- }
- }
|