GFramework/GFramework.Godot/input/GodotInputTranslator.cs
GwWuYou 028ece27db ```
refactor(input): 重构Godot输入模块为抽象基类并优化输入处理流程

将 `GodotInputModule` 重命名为 `AbstractGodotInputModule` 并改为抽象类,
以便支持更灵活的输入翻译器注册机制。引入 `GodotInputPhase` 枚举和
`GodotRawInput` 结构体以区分输入处理的不同阶段(捕获与冒泡)。
同时修改 `GodotInputTranslator` 仅在Bubble阶段生成游戏事件,提升输入处理精度。
```
2025-12-21 16:52:36 +08:00

68 lines
1.9 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.Game.input;
using Godot;
namespace GFramework.Godot.input;
/// <summary>
/// 将Godot引擎的输入事件转换为游戏通用输入事件的翻译器
/// </summary>
public sealed class GodotInputTranslator : IInputTranslator
{
/// <summary>
/// 尝试将原始输入对象转换为游戏输入事件
/// </summary>
/// <param name="rawInput">原始输入对象应为Godot的InputEvent类型</param>
/// <param name="gameEvent">输出参数转换成功时返回对应的游戏输入事件失败时返回null</param>
/// <returns>转换成功返回true否则返回false</returns>
public bool TryTranslate(object rawInput, out IGameInputEvent gameEvent)
{
gameEvent = null!;
if (rawInput is not GodotRawInput raw)
return false;
var evt = raw.Event;
// 示例规则:只在 Bubble 阶段生成游戏输入
if (raw.Phase != GodotInputPhase.Bubble)
return false;
// Action
if (evt is InputEventAction action)
{
gameEvent = new InputEvents.KeyInputEvent(
action.Action,
action.Pressed,
false
);
return true;
}
// Mouse button
if (evt is InputEventMouseButton mb)
{
gameEvent = new InputEvents.PointerInputEvent<Vector2>(
mb.Position,
Vector2.Zero,
(int)mb.ButtonIndex,
mb.Pressed
);
return true;
}
// Mouse motion
if (evt is InputEventMouseMotion mm)
{
gameEvent = new InputEvents.PointerInputEvent<Vector2>(
mm.Position,
mm.Relative,
0,
false
);
return true;
}
return false;
}
}