mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-23 03:04:29 +08:00
refactor(input): 重构Godot输入模块为抽象基类并优化输入处理流程 将 `GodotInputModule` 重命名为 `AbstractGodotInputModule` 并改为抽象类, 以便支持更灵活的输入翻译器注册机制。引入 `GodotInputPhase` 枚举和 `GodotRawInput` 结构体以区分输入处理的不同阶段(捕获与冒泡)。 同时修改 `GodotInputTranslator` 仅在Bubble阶段生成游戏事件,提升输入处理精度。 ```
68 lines
1.9 KiB
C#
68 lines
1.9 KiB
C#
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;
|
||
}
|
||
}
|