using GFramework.Game.Abstractions.ui;
using Godot;
namespace GFramework.Godot.ui;
///
/// Godot UI注册表实现类,用于管理PackedScene类型的UI资源注册和获取
///
public class GodotUiRegistry : IWritableUiRegistry
{
///
/// 存储UI键值对的字典,键为UI标识符,值为对应的PackedScene对象
///
private readonly Dictionary _map = new(StringComparer.Ordinal);
///
/// 注册UI场景到注册表中
///
/// UI的唯一标识符
/// 要注册的PackedScene对象
/// 返回当前UI注册表实例,支持链式调用
public IWritableUiRegistry Register(string key, PackedScene scene)
{
_map[key] = scene;
return this;
}
///
/// 根据键获取已注册的UI场景
///
/// UI的唯一标识符
/// 当指定的键未找到对应的UI场景时抛出异常
/// 对应的PackedScene对象
public PackedScene Get(string uiKey)
{
return !_map.TryGetValue(uiKey, out var scene)
? throw new KeyNotFoundException($"UI not registered: {uiKey}")
: scene;
}
}