GFramework/GFramework.Godot/input/AbstractGodotInputModule.cs
GeWuYou e39f32d9a8 fix(input): 修正输入系统事件处理逻辑
- 移除了输入处理中的提前返回语句,确保所有翻译器都能被遍历
- 修正了Godot输入桥接节点名称的格式字符串拼接错误
- 为输入上下文堆栈增加了带参数的Pop方法,支持安全弹出指定上下文
- 新增Peek方法用于获取堆栈顶部的输入上下文而不移除它
- 更新了输入处理方法的注释说明,明确处理流程是从堆栈顶部开始遍历
- 调整了堆栈遍历顺序,确保按照后进先出的原则处理输入事件
2025-12-22 13:18:12 +08:00

69 lines
2.4 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.architecture;
using GFramework.Core.constants;
using GFramework.Game.input;
using GFramework.Godot.architecture;
using GFramework.Godot.extensions;
using Godot;
namespace GFramework.Godot.input;
/// <summary>
/// Godot输入模块类用于管理Godot游戏引擎中的输入系统
/// </summary>
/// <typeparam name="T">架构类型必须继承自Architecture且具有无参构造函数</typeparam>
public abstract class AbstractGodotInputModule<T> : AbstractGodotModule<T>
where T : Architecture<T>, new()
{
private GodotInputBridge? _node;
/// <summary>
/// 启用默认的输入转换器
/// </summary>
protected virtual bool EnableDefaultTranslator => false;
/// <summary>
/// 架构锚点节点的唯一标识名称
/// 用于在Godot场景树中创建和查找架构锚点节点
/// </summary>
private const string GodotInputBridgeName = $"__{GFrameworkConstants.FrameworkName}__GodotInputBridge__";
/// <summary>
/// 获取模块对应的节点对象
/// </summary>
/// <exception cref="InvalidOperationException">当节点尚未创建时抛出异常</exception>
public override Node Node => _node
?? throw new InvalidOperationException("Node not created yet");
public override void OnDetach()
{
// 释放节点资源并清理引用
Node.QueueFreeX();
_node = null;
}
/// <summary>
/// 安装模块时调用此方法,用于获取所需的系统组件
/// </summary>
/// <param name="architecture">当前架构实例</param>
public override void Install(IArchitecture architecture)
{
// 从架构中获取输入系统实例
var inputSystem = architecture.GetSystem<InputSystem>()!;
if (EnableDefaultTranslator)
{
// 注册输入转换器
inputSystem.RegisterTranslator(new GodotInputTranslator(), true);
}
RegisterTranslator(inputSystem);
// 创建Godot输入桥接节点并绑定输入系统
_node = new GodotInputBridge { Name = GodotInputBridgeName };
_node.Bind(inputSystem);
}
/// <summary>
/// 注册翻译器的抽象方法,由子类实现具体的注册逻辑
/// </summary>
/// <param name="inputSystem">输入系统实例</param>
protected abstract void RegisterTranslator(InputSystem inputSystem);
}