GFramework/GFramework.Game/input/InputContextStack.cs
GwWuYou 564a7e3f24 feat(input): 添加游戏输入事件处理系统
- 新增 IGameInputEvent 接口定义游戏输入事件
- 新增 IInputContext 接口用于处理输入事件的上下文
- 新增 InputContextStack 类管理输入上下文堆栈
- 新增 InputSystem 类负责整体输入事件分发与处理
- 移除旧版 Godot 输入系统相关实现代码
- 定义输入事件处理流程:上下文堆栈 -> 事件发送机制
2025-12-20 22:35:07 +08:00

34 lines
1.2 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.

namespace GFramework.Game.input;
/// <summary>
/// 输入上下文堆栈管理器,用于管理多个输入上下文的堆栈结构
/// </summary>
public class InputContextStack
{
private readonly Stack<IInputContext> _stack = new();
/// <summary>
/// 将指定的输入上下文压入堆栈顶部
/// </summary>
/// <param name="context">要压入堆栈的输入上下文对象</param>
public void Push(IInputContext context) => _stack.Push(context);
/// <summary>
/// 弹出堆栈顶部的输入上下文
/// </summary>
public void Pop() => _stack.Pop();
/// <summary>
/// 处理游戏输入事件,遍历堆栈中的所有上下文直到找到能够处理该事件的上下文
/// </summary>
/// <param name="input">要处理的游戏输入事件</param>
/// <returns>如果堆栈中任意一个上下文成功处理了输入事件则返回true否则返回false</returns>
public bool Handle(IGameInputEvent input)
{
// 遍历堆栈中的所有上下文调用其Handle方法处理输入事件
// Any方法会在第一个返回true的上下文处停止遍历
return _stack.Any(ctx => ctx.Handle(input));
}
}