GFramework/GFramework.Godot/UI/GodotUiFactory.cs
gewuyou ff553977e3 chore(license): 补齐 Apache-2.0 文件头治理
- 新增许可证文件头检查与修复脚本

- 补充维护者手动修复 PR 工作流和 CI 校验

- 更新贡献指南中的文件头说明

- 补齐仓库维护源码和配置文件的许可证声明
2026-05-03 19:39:49 +08:00

66 lines
2.1 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.

// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using GFramework.Core.Abstractions.Logging;
using GFramework.Core.Extensions;
using GFramework.Core.Logging;
using GFramework.Core.Utility;
using GFramework.Game.Abstractions.UI;
namespace GFramework.Godot.UI;
/// <summary>
/// Godot UI工厂类用于创建UI页面实例。
/// 继承自AbstractContextUtility并实现IUiFactory接口。
/// </summary>
public class GodotUiFactory : AbstractContextUtility, IUiFactory
{
/// <summary>
/// 日志记录器,用于记录调试信息。
/// </summary>
private static readonly ILogger Log =
LoggerFactoryResolver.Provider.CreateLogger(nameof(GodotUiFactory));
/// <summary>
/// UI注册表用于管理UI场景资源。
/// </summary>
private IGodotUiRegistry _registry = null!;
/// <summary>
/// 根据指定的UI键创建UI页面实例。
/// </summary>
/// <param name="uiKey">UI页面的唯一标识符。</param>
/// <returns>返回创建的UI页面行为实例。</returns>
/// <exception cref="InvalidCastException">
/// 当UI场景未实现IUiPageBehaviorProvider接口时抛出异常。
/// </exception>
public IUiPageBehavior Create(string uiKey)
{
// 从注册表中获取指定UI键对应的场景
var scene = _registry.Get(uiKey);
// 实例化场景节点
var node = scene.Instantiate();
// 检查节点是否实现了IUiPageBehaviorProvider接口
if (node is not IUiPageBehaviorProvider provider)
throw new InvalidCastException(
$"UI scene {uiKey} must implement IUiPageBehaviorProvider");
// 获取页面行为实例
var page = provider.GetPage();
// 记录调试日志
Log.Debug("Created UI instance: {0}", uiKey);
return page;
}
/// <summary>
/// 初始化方法,在对象初始化时调用。
/// 获取并设置UI注册表实例。
/// </summary>
protected override void OnInit()
{
_registry = this.GetUtility<IGodotUiRegistry>()!;
}
}