Kaynağa Gözat

fix:打开Normal层的UI自动关闭Middle和High的UI

chenbin 1 hafta önce
ebeveyn
işleme
8b05a1bebc

+ 2 - 2
Assets/Scripts/Business/Domain/Framework/FGUI/FguiModule.cs

@@ -90,8 +90,8 @@ namespace XGame.Framework.FGUI
                 {
                     _closedMap.Remove(key);
                     _uitree.SetAsLastSibling(closedView.Panel);
-                    closedView.Enable(intent);
                     _openedMap.Add(key, closedView);
+                    closedView.Enable(intent);
                     FrameworkEvent.Instance.Notify(EventDefine.UI_OPENED, uikey);
                 });
             }
@@ -115,8 +115,8 @@ namespace XGame.Framework.FGUI
                     (context as IUIViewAdapter).Key = uikey;
                     (view as UIView).Init(context, panel);
                     _uitree.AddPanel(panel);
-                    view.Enable(intent);
                     _openedMap.Add(key, view);
+                    view.Enable(intent);
                     FrameworkEvent.Instance.Notify(EventDefine.UI_OPENED, uikey);
                 });
                 var pkgLoadAsync = LoadPackageAsync(uikey);

+ 1 - 1
Assets/Scripts/Business/Runtime/FGUI/Bag/JumpListItem/BagJumpListItemCtrl.cs

@@ -42,7 +42,7 @@ namespace FL.FGUI
             {
                 Context.UI.OpenAsync(UIKeys.NameToKey(_jumpInfo.UI), _jumpInfo.Param);
                 Context.ClosePanel();
-                Context.UI.Close(UIKeys.BagBackpackPanel);
+                //Context.UI.Close(UIKeys.BagBackpackPanel);
             }
         }
 

+ 22 - 0
Assets/Scripts/Business/Runtime/FGUI/UIKeys.cs

@@ -6,6 +6,7 @@ namespace FL.FGUI
     public static partial class UIKeys
     {
         private static Dictionary<string, UIKey> _nameToKeys;
+        private static Dictionary<UIKey, UILayer> _uikeyToLayerMap;
         public static UIKey NameToKey(string uiName)
         {
             if (_nameToKeys == null)
@@ -26,10 +27,31 @@ namespace FL.FGUI
             XGame.Log.Error($"找不到UIKey. uiName:{uiName}");
             return default;
         }
+        public static UILayer GetLayer(UIKey uikey)
+        {
+            if (_uikeyToLayerMap == null)
+            {
+                _uikeyToLayerMap = new Dictionary<UIKey, UILayer>();
+            }
+            if (_uikeyToLayerMap.TryGetValue(uikey, out var layer))
+            {
+                return layer;
+            }
+            layer = UILayer.Normal;
+            var property = uikey.UIViewType.GetProperty("Layer", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.IgnoreCase);
+            if (property != null)
+            {
+                layer = (UILayer)property.GetValue(null);
+            }
+            _uikeyToLayerMap.Add(uikey, layer);
+            return layer;
+        }
         public static void Dispose()
         {
             _nameToKeys?.Clear();
             _nameToKeys = null;
+            _uikeyToLayerMap?.Clear();
+            _uikeyToLayerMap = null;
         }
     }
 }

+ 71 - 7
Assets/Scripts/Business/Runtime/Nodes/GameMain/UIComponent.cs

@@ -1,15 +1,20 @@
 using FL.FGUI;
-using XGame.Framework.Asyncs;
-using XGame.Framework.FGUI;
-using XGame.Framework.Loadable;
+using System.Collections.Generic;
+using XGame.Framework;
 using XGame.Framework.Nodes;
+using XGame.Framework.UI;
 
 namespace FL.Nodes.GameMain
 {
-    public class UIComponent : NodeComponent, ILoadAsyncHandle
+    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);
 
@@ -20,20 +25,79 @@ namespace FL.Nodes.GameMain
         }
         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);
+
+            _openKeysMap.Clear();
         }
 
-        void ILoadAsyncHandle.OnLoadAsync(IAsyncGroup group)
+        #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)
         {
-            Context.UI.Preload(UINestedKeys.CommonItemBase).Join(group);
+            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);
+            }
         }
-        void ILoadAsyncHandle.OnUnloadAsync(IAsyncGroup group)
+        private bool IsFiltered(UILayer layer)
         {
+            return layer is UILayer.MapUI or UILayer.Toast or UILayer.Guide or UILayer.Effect;
         }
+        #endregion
     }
 }