// Copyright (c) 2026 GeWuYou // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. using GFramework.Game.Abstractions.enums; using GFramework.Game.Abstractions.ui; using GFramework.Godot.extensions; using Godot; namespace GFramework.Godot.ui; /// /// UI 页面行为基类,封装通用的生命周期管理逻辑。 /// 提供对 CanvasItem 类型视图节点的统一管理,包括显示、隐藏、进入、退出等操作。 /// /// CanvasItem 类型的视图节点。 public abstract class CanvasItemUiPageBehaviorBase : IUiPageBehavior where T : CanvasItem { /// /// UI 的唯一标识键。 /// private readonly string _key; /// /// IUiPage 接口引用(如果视图实现了该接口)。 /// private readonly IUiPage? _page; /// /// 视图节点的所有者实例。 /// protected readonly T Owner; /// /// 初始化 CanvasItemUiPageBehaviorBase 实例。 /// /// 视图节点的所有者实例。 /// UI 的唯一标识键。 protected CanvasItemUiPageBehaviorBase(T owner, string key) { Owner = owner; _key = key; _page = owner as IUiPage; } #region 抽象属性 - 子类必须实现 /// /// 获取或设置当前UI句柄。 /// /// /// 表示当前UI句柄的可空类型 。 /// /// /// 此属性允许获取或设置与当前上下文关联的UI句柄。若未设置,则其值为 null。不可重入的ui句柄通常为null /// public UiHandle? Handle { get; set; } /// /// 获取 UI 所属的层级。 /// 该属性由子类实现并指定具体的层级值。 /// 层级用于确定 UI 元素在界面中的显示顺序和逻辑分组。 /// public abstract UiLayer Layer { get; } /// /// 获取是否支持重入。 /// 由子类指定具体值。 /// public abstract bool IsReentrant { get; } /// /// 获取是否为模态窗口。 /// 由子类指定默认值。 /// public abstract bool IsModal { get; } /// /// 获取是否阻止下层输入。 /// 由子类指定默认值。 /// public abstract bool BlocksInput { get; } #endregion #region 基础属性 /// /// 获取视图节点实例。 /// public object View => Owner; /// /// 获取 UI 的唯一标识键。 /// public string Key => _key; /// /// 获取视图节点是否有效。 /// public bool IsAlive => Owner.IsValidNode(); /// /// 获取视图节点是否可见。 /// public bool IsVisible => Owner.Visible; #endregion #region 生命周期管理 /// /// 当 UI 进入时调用。 /// 默认调用 IUiPage 接口的 OnEnter 方法(如果存在)。 /// /// 进入参数。 public virtual void OnEnter(IUiPageEnterParam? param) { _page?.OnEnter(param); } /// /// 当 UI 退出时调用。 /// 默认调用 IUiPage 接口的 OnExit 方法(如果存在),并释放视图节点。 /// public virtual void OnExit() { _page?.OnExit(); Owner.QueueFreeX(); } /// /// 当 UI 暂停时调用。 /// 默认调用 IUiPage 接口的 OnPause 方法(如果存在),并根据 BlocksInput 决定是否暂停处理逻辑。 /// public virtual void OnPause() { _page?.OnPause(); // 只有阻止输入的 UI 才需要暂停处理 if (!BlocksInput) return; Owner.SetProcess(false); Owner.SetPhysicsProcess(false); Owner.SetProcessInput(false); } /// /// 当 UI 恢复时调用。 /// 默认调用 IUiPage 接口的 OnResume 方法(如果存在),并恢复处理逻辑。 /// public virtual void OnResume() { if (Owner.IsInvalidNode()) return; _page?.OnResume(); // 恢复处理 Owner.SetProcess(true); Owner.SetPhysicsProcess(true); Owner.SetProcessInput(true); } /// /// 当 UI 隐藏时调用。 /// 默认调用 IUiPage 接口的 OnHide 方法(如果存在),并隐藏视图节点。 /// public virtual void OnHide() { _page?.OnHide(); Owner.Hide(); } /// /// 当 UI 显示时调用。 /// 默认调用 IUiPage 接口的 OnShow 方法(如果存在),并显示视图节点,同时触发恢复逻辑。 /// public virtual void OnShow() { _page?.OnShow(); Owner.Show(); OnResume(); } #endregion }