GFramework/GFramework.Godot/ui/GodotUiFactory.cs
GeWuYou c931cb7d66 feat(ui): 添加CanvasItem页面行为和Godot UI工厂
- 实现CanvasItemPageBehavior类支持所有继承自CanvasItem的节点
- 添加OnEnter、OnExit、OnPause、OnResume、OnHide、OnShow页面生命周期方法
- 创建GodotUiFactory工厂类用于创建UI页面实例
- 实现Create方法根据UI键创建页面行为实例
- 重命名IUiPageProvider为IUiPageBehaviorProvider接口
- 更新接口方法GetPage返回页面行为实例的描述
2026-01-15 12:33:19 +08:00

49 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using GFramework.Core.extensions;
using GFramework.Core.utility;
using GFramework.Game.Abstractions.ui;
using Godot;
namespace GFramework.Godot.ui;
/// <summary>
/// Godot UI工厂类用于创建UI页面实例
/// 继承自AbstractContextUtility并实现IUiFactory接口
/// </summary>
public class GodotUiFactory : AbstractContextUtility, IUiFactory
{
/// <summary>
/// UI注册表用于存储和获取PackedScene类型的UI资源
/// </summary>
private IWritableUiRegistry<PackedScene> _registry = null!;
/// <summary>
/// 根据指定的UI键创建UI页面实例
/// </summary>
/// <param name="uiKey">UI资源的唯一标识键</param>
/// <returns>实现IUiPage接口的UI页面实例</returns>
/// <exception cref="InvalidCastException">当UI场景没有继承IUiPage接口时抛出</exception>
public IPageBehavior Create(string uiKey)
{
// 从注册表中获取对应的场景资源
var scene = _registry.Get(uiKey);
// 实例化场景节点
var node = scene.Instantiate();
// 验证节点是否实现了IUiPageProvider接口
if (node is not IUiPageBehaviorProvider provider)
throw new InvalidCastException(
$"UI scene {uiKey} must implement IUiPageBehaviorProvider");
// 获取并返回页面行为实例
return provider.GetPage();
}
/// <summary>
/// 初始化方法获取UI注册表实例
/// </summary>
protected override void OnInit()
{
_registry = this.GetUtility<IWritableUiRegistry<PackedScene>>()!;
}
}