mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-23 11:14:30 +08:00
新增 GodotInputModule 和相关输入事件类型,实现 Godot 输入系统与游戏框架的桥接。 重构架构锚点类名及其引用,统一使用 GFrameworkConstants 中定义的框架名称常量。 添加 AbstractGodotModule 基类以规范模块行为,并完善输入事件记录类定义。
80 lines
2.0 KiB
C#
80 lines
2.0 KiB
C#
using GFramework.Game.input;
|
||
using Godot;
|
||
|
||
namespace GFramework.Godot.input;
|
||
|
||
/// <summary>
|
||
/// Godot输入桥接类,用于将Godot的输入事件转换为游戏框架的输入事件
|
||
/// </summary>
|
||
public partial class GodotInputBridge : Node
|
||
{
|
||
private InputSystem _inputSystem = null!;
|
||
|
||
/// <summary>
|
||
/// 绑定输入系统
|
||
/// </summary>
|
||
/// <param name="inputSystem">要绑定的输入系统实例</param>
|
||
public void Bind(InputSystem inputSystem)
|
||
{
|
||
_inputSystem = inputSystem;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 处理输入事件的回调方法
|
||
/// </summary>
|
||
/// <param name="event">Godot输入事件</param>
|
||
public override void _Input(InputEvent @event)
|
||
{
|
||
var gameEvent = Translate(@event);
|
||
if (gameEvent == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
_inputSystem.Handle(gameEvent);
|
||
GetViewport().SetInputAsHandled();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将Godot输入事件翻译为游戏框架输入事件
|
||
/// </summary>
|
||
/// <param name="evt">Godot输入事件</param>
|
||
/// <returns>翻译后的游戏输入事件,如果无法翻译则返回null</returns>
|
||
private static IGameInputEvent? Translate(InputEvent evt)
|
||
{
|
||
// 处理动作输入事件
|
||
if (evt is InputEventAction action)
|
||
{
|
||
return new InputEvents.KeyInputEvent(
|
||
action.Action,
|
||
action.Pressed,
|
||
false
|
||
);
|
||
}
|
||
|
||
// 鼠标按钮
|
||
if (evt is InputEventMouseButton mb)
|
||
{
|
||
return new InputEvents.PointerInputEvent<Vector2>(
|
||
mb.Position,
|
||
Vector2.Zero,
|
||
(int)mb.ButtonIndex,
|
||
mb.Pressed
|
||
);
|
||
}
|
||
|
||
// 鼠标移动
|
||
if (evt is InputEventMouseMotion mm)
|
||
{
|
||
return new InputEvents.PointerInputEvent<Vector2>(
|
||
mm.Position,
|
||
mm.Relative,
|
||
0,
|
||
false
|
||
);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|