From 0a924725a5e1dcc1fab902da457183d62c27cbba Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Mon, 19 Jan 2026 22:23:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(ui):=20=E6=9B=B4=E6=96=B0UiRouterBase?= =?UTF-8?q?=E4=BB=A5=E6=94=AF=E6=8C=81=E5=9F=BA=E4=BA=8E=E9=94=AE=E5=80=BC?= =?UTF-8?q?=E7=9A=84=E9=A1=B5=E9=9D=A2=E5=AF=BC=E8=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加PeekKey方法获取页面栈顶元素的键值 - 修改Peek方法返回IUiPageBehavior对象而不是类型名称 - 更新IsTop和Contains方法使用键值比较替代类型名比较 - 在UiTransitionEvent中使用PeekKey获取来源UI键值 - 为CanvasItemUiPageBehavior添加key参数和Key属性实现 - 在IUiPageBehavior接口中定义Key属性 - 更新IUiRouter接口定义新的PeekKey和Peek方法 - 添加必要的using引用和异步任务配置 --- .../ui/IUiPageBehavior.cs | 7 ++++ GFramework.Game.Abstractions/ui/IUiRouter.cs | 9 ++++- GFramework.Game/ui/UiRouterBase.cs | 34 +++++++++++++------ .../ui/CanvasItemUiPageBehavior.cs | 12 +++++-- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/GFramework.Game.Abstractions/ui/IUiPageBehavior.cs b/GFramework.Game.Abstractions/ui/IUiPageBehavior.cs index 76c9c2a..6309438 100644 --- a/GFramework.Game.Abstractions/ui/IUiPageBehavior.cs +++ b/GFramework.Game.Abstractions/ui/IUiPageBehavior.cs @@ -11,6 +11,13 @@ public interface IUiPageBehavior /// 页面视图实例 object View { get; } + /// + /// 获取键值 + /// + /// 返回当前对象的键标识符 + string Key { get; } + + /// /// 获取页面是否处于活动状态 /// diff --git a/GFramework.Game.Abstractions/ui/IUiRouter.cs b/GFramework.Game.Abstractions/ui/IUiRouter.cs index c942344..aeefefc 100644 --- a/GFramework.Game.Abstractions/ui/IUiRouter.cs +++ b/GFramework.Game.Abstractions/ui/IUiRouter.cs @@ -80,7 +80,14 @@ public interface IUiRouter : ISystem /// 获取当前栈顶UI的Key /// /// 当前UI Key,如果栈为空返回空字符串 - string Peek(); + string PeekKey(); + + /// + /// 获取当前栈顶的UI页面行为对象 + /// + /// 栈顶的IUiPageBehavior对象,如果栈为空则返回null + IUiPageBehavior Peek(); + /// /// 判断指定UI是否为当前栈顶UI diff --git a/GFramework.Game/ui/UiRouterBase.cs b/GFramework.Game/ui/UiRouterBase.cs index 6733713..aa9738e 100644 --- a/GFramework.Game/ui/UiRouterBase.cs +++ b/GFramework.Game/ui/UiRouterBase.cs @@ -1,3 +1,7 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; using GFramework.Core.Abstractions.logging; using GFramework.Core.extensions; using GFramework.Core.logging; @@ -201,15 +205,26 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter AfterChange(@event); } + /// - /// 获取栈顶元素的视图类型名称 + /// 获取页面栈顶元素的键值,但不移除该元素 /// - /// 如果栈为空则返回空字符串,否则返回栈顶元素视图类型的名称 - public string Peek() + /// 如果页面栈为空则返回空字符串,否则返回栈顶元素的键值 + public string PeekKey() { - return _stack.Count == 0 ? string.Empty : _stack.Peek().View.GetType().Name; + return _stack.Count == 0 ? string.Empty : _stack.Peek().Key; } + /// + /// 获取页面栈顶元素,但不移除该元素 + /// + /// 返回栈顶的IUiPageBehavior元素 + public IUiPageBehavior Peek() + { + return _stack.Peek(); + } + + /// /// 判断栈顶元素是否指定的UI类型 /// @@ -217,10 +232,7 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter /// 如果栈为空或栈顶元素类型不匹配则返回false,否则返回true public bool IsTop(string uiKey) { - if (_stack.Count == 0) - return false; - - return _stack.Peek().View.GetType().Name == uiKey; + return _stack.Count != 0 && _stack.Peek().Key.Equals(uiKey); } /// @@ -230,7 +242,7 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter /// 如果栈中存在指定类型的UI元素则返回true,否则返回false public bool Contains(string uiKey) { - return _stack.Any(p => p.View.GetType().Name == uiKey); + return _stack.Any(p => p.Key.Equals(uiKey)); } /// @@ -265,7 +277,7 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter { return new UiTransitionEvent { - FromUiKey = Peek(), + FromUiKey = PeekKey(), ToUiKey = toUiKey, TransitionType = type, Policy = policy ?? UiTransitionPolicy.Exclusive, @@ -289,7 +301,7 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter private void AfterChange(UiTransitionEvent @event) { Log.Debug("AfterChange phases started: {0}", @event.TransitionType); - _ = Task.Run(async () => + _ = Task.Run(async () => { try { diff --git a/GFramework.Godot/ui/CanvasItemUiPageBehavior.cs b/GFramework.Godot/ui/CanvasItemUiPageBehavior.cs index 94da6db..4bd7673 100644 --- a/GFramework.Godot/ui/CanvasItemUiPageBehavior.cs +++ b/GFramework.Godot/ui/CanvasItemUiPageBehavior.cs @@ -9,16 +9,24 @@ namespace GFramework.Godot.ui; /// 支持所有继承自 CanvasItem 的节点 /// /// CanvasItem 类型的视图节点 -public class CanvasItemUiPageBehavior(T owner) : IUiPageBehavior +public class CanvasItemUiPageBehavior(T owner, string key) : IUiPageBehavior where T : CanvasItem { private readonly IUiPage? _page = owner as IUiPage; /// - /// 页面视图对象 + /// 获取页面视图对象 /// + /// 返回与当前实例关联的视图对象 public object View => owner; + /// + /// 获取当前实例的键值标识符 + /// + /// 返回用于标识当前实例的键字符串 + public string Key => key; + + /// /// 获取页面是否存活状态 ///