GFramework/GFramework.Godot/input/GodotInputBridge.cs
GwWuYou 339498e629 feat(GFramework.Godot): 引入 Godot 输入模块与架构锚点重构
新增 GodotInputModule 和相关输入事件类型,实现 Godot 输入系统与游戏框架的桥接。
重构架构锚点类名及其引用,统一使用 GFrameworkConstants 中定义的框架名称常量。
添加 AbstractGodotModule 基类以规范模块行为,并完善输入事件记录类定义。
2025-12-21 16:13:16 +08:00

80 lines
2.0 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输入桥接类用于将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;
}
}