GeWuYou fb14d7122c docs(style): 更新文档中的命名空间导入格式
- 将所有小写的命名空间导入更正为首字母大写格式
- 统一 GFramework 框架的命名空间引用规范
- 修复 core、ecs、godot 等模块的命名空间导入错误
- 标准化文档示例代码中的 using 语句格式
- 确保所有文档中的命名空间引用保持一致性
- 更新 global using 语句以匹配正确的命名空间格式
2026-03-10 07:18:49 +08:00

115 lines
4.0 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) 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;
namespace GFramework.Game.Abstractions.Scene;
/// <summary>
/// 场景过渡事件,封装了场景切换过程中的上下文信息。
/// 提供了场景切换的元数据和自定义上下文数据的存储能力。
/// </summary>
public sealed class SceneTransitionEvent
{
private readonly Dictionary<string, object> _context = new(StringComparer.Ordinal);
/// <summary>
/// 获取或初始化源场景的唯一标识符。
/// 表示切换前的场景,如果是首次加载场景则为 null。
/// </summary>
public string? FromSceneKey { get; init; }
/// <summary>
/// 获取或初始化目标场景的唯一标识符。
/// 表示切换后的场景,如果是清空操作则为 null。
/// </summary>
public string? ToSceneKey { get; init; }
/// <summary>
/// 获取或初始化场景过渡类型。
/// 指示当前执行的是哪种场景切换操作Push/Pop/Replace/Clear
/// </summary>
public SceneTransitionType TransitionType { get; init; }
/// <summary>
/// 获取或初始化场景进入参数。
/// 包含传递给新场景的初始化数据或上下文信息。
/// </summary>
public ISceneEnterParam? EnterParam { get; init; }
/// <summary>
/// 从上下文中获取指定键的值。
/// </summary>
/// <typeparam name="T">值的类型。</typeparam>
/// <param name="key">上下文键。</param>
/// <param name="defaultValue">如果键不存在时返回的默认值。</param>
/// <returns>上下文中存储的值,如果键不存在则返回默认值。</returns>
public T Get<T>(string key, T defaultValue = default!)
{
if (_context.TryGetValue(key, out var value) && value is T typedValue)
return typedValue;
return defaultValue;
}
/// <summary>
/// 设置上下文中指定键的值。
/// </summary>
/// <typeparam name="T">值的类型。</typeparam>
/// <param name="key">上下文键。</param>
/// <param name="value">要存储的值。</param>
public void Set<T>(string key, T value)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
_context[key] = value!;
}
/// <summary>
/// 尝试从上下文中获取指定键的值。
/// </summary>
/// <typeparam name="T">值的类型。</typeparam>
/// <param name="key">上下文键。</param>
/// <param name="value">输出参数,如果键存在则包含对应的值。</param>
/// <returns>如果键存在且类型匹配则返回 true否则返回 false。</returns>
public bool TryGet<T>(string key, out T value)
{
if (_context.TryGetValue(key, out var obj) && obj is T typedValue)
{
value = typedValue;
return true;
}
value = default!;
return false;
}
/// <summary>
/// 检查上下文中是否存在指定的键。
/// </summary>
/// <param name="key">上下文键。</param>
/// <returns>如果键存在则返回 true否则返回 false。</returns>
public bool Has(string key)
{
return _context.ContainsKey(key);
}
/// <summary>
/// 从上下文中移除指定的键。
/// </summary>
/// <param name="key">上下文键。</param>
/// <returns>如果键存在并成功移除则返回 true否则返回 false。</returns>
public bool Remove(string key)
{
return _context.Remove(key);
}
}