diff --git a/GFramework.Game/GFramework.Game.csproj b/GFramework.Game/GFramework.Game.csproj index 6faca1d..bca044e 100644 --- a/GFramework.Game/GFramework.Game.csproj +++ b/GFramework.Game/GFramework.Game.csproj @@ -9,6 +9,7 @@ + diff --git a/GFramework.Game/input/IGameInputEvent.cs b/GFramework.Game/input/IGameInputEvent.cs deleted file mode 100644 index 2a4d79e..0000000 --- a/GFramework.Game/input/IGameInputEvent.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace GFramework.Game.input; - -/// -/// 游戏输入事件接口 -/// -public interface IGameInputEvent; diff --git a/GFramework.Game/input/IInputContext.cs b/GFramework.Game/input/IInputContext.cs deleted file mode 100644 index 259a475..0000000 --- a/GFramework.Game/input/IInputContext.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace GFramework.Game.input; - -/// -/// 输入上下文接口,用于处理游戏中的输入事件 -/// -public interface IInputContext -{ - /// - /// 处理游戏输入事件 - /// - /// 要处理的游戏输入事件 - /// 返回 true 表示输入被吃掉,不再向下传播;返回 false 表示输入未被处理,可以继续传播给其他处理器 - bool Handle(IGameInputEvent input); -} diff --git a/GFramework.Game/input/IInputTranslator.cs b/GFramework.Game/input/IInputTranslator.cs deleted file mode 100644 index 2936b28..0000000 --- a/GFramework.Game/input/IInputTranslator.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace GFramework.Game.input; - -/// -/// 输入转换器接口 -/// -public interface IInputTranslator -{ - /// - /// 尝试将引擎输入翻译为游戏输入 - /// - /// 原始的引擎输入对象 - /// 输出参数,如果翻译成功则包含对应的游戏输入事件 - /// 如果翻译成功返回true,否则返回false - bool TryTranslate(object rawInput, out IGameInputEvent gameEvent); -} diff --git a/GFramework.Game/input/InputContextStack.cs b/GFramework.Game/input/InputContextStack.cs deleted file mode 100644 index 5c6663a..0000000 --- a/GFramework.Game/input/InputContextStack.cs +++ /dev/null @@ -1,54 +0,0 @@ -namespace GFramework.Game.input; - -/// -/// 输入上下文堆栈管理器,用于管理多个输入上下文的堆栈结构 -/// -public class InputContextStack -{ - private readonly Stack _stack = new(); - - /// - /// 将指定的输入上下文压入堆栈顶部 - /// - /// 要压入堆栈的输入上下文对象 - public void Push(IInputContext context) => _stack.Push(context); - - /// - /// 弹出堆栈顶部的元素 - /// - public void Pop() => _stack.Pop(); - - /// - /// 弹出堆栈顶部的输入上下文 - /// - /// 要弹出的输入上下文对象 - /// 如果成功弹出返回true,否则返回false - public bool Pop(IInputContext ctx) - { - // 检查堆栈顶部元素是否与指定上下文匹配,如果不匹配则返回false - if (!_stack.TryPeek(out var top) || top != ctx) return false; - _stack.Pop(); - return true; - } - - - /// - /// 获取堆栈顶部的输入上下文但不移除它 - /// - /// 堆栈顶部的输入上下文 - public IInputContext Peek() => _stack.Peek(); - - - /// - /// 处理游戏输入事件 - /// - /// 要处理的游戏输入事件 - /// 如果任何一个输入上下文成功处理了输入事件则返回true,否则返回false - public bool Handle(IGameInputEvent input) - { - // 从堆栈顶部开始遍历输入上下文,尝试处理输入事件 - return _stack.Any(ctx => ctx.Handle(input)); - } - -} - diff --git a/GFramework.Game/input/InputEvents.cs b/GFramework.Game/input/InputEvents.cs deleted file mode 100644 index b6c30b0..0000000 --- a/GFramework.Game/input/InputEvents.cs +++ /dev/null @@ -1,31 +0,0 @@ -namespace GFramework.Game.input; - -public static class InputEvents -{ - /// - /// 按键输入事件 - /// - /// 按键操作名称 - /// 按键是否被按下,true表示按下,false表示释放 - /// 是否为回显事件,用于处理按键重复触发 - public sealed record KeyInputEvent( - string Action, - bool Pressed, - bool Echo - ) : IGameInputEvent; - - /// - /// 指针/鼠标输入事件 - /// - /// 二维向量类型 - /// 指针当前位置坐标 - /// 指针位置变化量 - /// 鼠标按键编号,0表示左键,1表示右键,2表示中键 - /// 按键是否被按下,true表示按下,false表示释放 - public sealed record PointerInputEvent( - TVector2 Position, - TVector2 Delta, - int Button, - bool Pressed - ) : IGameInputEvent where TVector2 : struct; -} \ No newline at end of file diff --git a/GFramework.Game/input/InputSystem.cs b/GFramework.Game/input/InputSystem.cs deleted file mode 100644 index 38bbf6a..0000000 --- a/GFramework.Game/input/InputSystem.cs +++ /dev/null @@ -1,86 +0,0 @@ -using GFramework.Core.extensions; -using GFramework.Core.system; - -namespace GFramework.Game.input; - -/// -/// 输入系统类,负责管理输入上下文堆栈并处理游戏输入事件 -/// -public class InputSystem : AbstractSystem -{ - private readonly List _translators = []; - private readonly InputContextStack _contextStack = new(); - - /// - /// 将输入上下文推入上下文堆栈 - /// - /// 要推入的输入上下文对象 - public void PushContext(IInputContext ctx) - => _contextStack.Push(ctx); - - /// - /// 从上下文堆栈中弹出顶层输入上下文 - /// - public void PopContext() - => _contextStack.Pop(); - - /// - /// 处理游戏输入事件,首先尝试通过上下文堆栈处理,如果未被处理则发送事件 - /// - /// 要处理的游戏输入事件 - public void Handle(IGameInputEvent input) - { - // 尝试通过上下文堆栈处理输入事件 - if (_contextStack.Handle(input)) - return; - // 如果上下文堆栈未能处理,则发送该事件 - this.SendEvent(input); - } - - /// - /// 系统初始化方法,在系统启动时调用 - /// - protected override void OnInit() - { - - } - - /// - /// 注销输入转换器 - /// - /// 要注销的输入转换器接口实例 - public void UnregisterTranslator(IInputTranslator translator) - => _translators.Remove(translator); - - /// - /// 注册输入转换器 - /// - /// 输入转换器接口实例 - /// 是否为高优先级,true时插入到转换器列表开头,false时添加到列表末尾 - public void RegisterTranslator(IInputTranslator translator, bool highPriority = false) - { - if (_translators.Contains(translator)) - return; - // 根据优先级设置决定插入位置 - if (highPriority) - _translators.Insert(0, translator); - else - _translators.Add(translator); - } - - - /// - /// 处理原始输入数据 - /// - /// 原始输入对象 - public void HandleRaw(object rawInput) - { - // 遍历所有注册的转换器,尝试将原始输入转换为游戏事件 - foreach (var t in _translators) - { - if (!t.TryTranslate(rawInput, out var gameEvent)) continue; - Handle(gameEvent); - } - } - -} diff --git a/GFramework.Godot/GFramework.Godot.csproj b/GFramework.Godot/GFramework.Godot.csproj index fc85a79..45bb153 100644 --- a/GFramework.Godot/GFramework.Godot.csproj +++ b/GFramework.Godot/GFramework.Godot.csproj @@ -21,4 +21,8 @@ + + + + diff --git a/GFramework.Godot/input/AbstractGodotInputModule.cs b/GFramework.Godot/input/AbstractGodotInputModule.cs deleted file mode 100644 index ac7ae7c..0000000 --- a/GFramework.Godot/input/AbstractGodotInputModule.cs +++ /dev/null @@ -1,69 +0,0 @@ -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; - -/// -/// Godot输入模块类,用于管理Godot游戏引擎中的输入系统 -/// -/// 架构类型,必须继承自Architecture且具有无参构造函数 -public abstract class AbstractGodotInputModule : AbstractGodotModule - where T : Architecture, new() -{ - private GodotInputBridge? _node; - - /// - /// 启用默认的输入转换器 - /// - protected virtual bool EnableDefaultTranslator => false; - - /// - /// 架构锚点节点的唯一标识名称 - /// 用于在Godot场景树中创建和查找架构锚点节点 - /// - private const string GodotInputBridgeName = $"__{GFrameworkConstants.FrameworkName}__GodotInputBridge__"; - - /// - /// 获取模块对应的节点对象 - /// - /// 当节点尚未创建时抛出异常 - public override Node Node => _node - ?? throw new InvalidOperationException("Node not created yet"); - - public override void OnDetach() - { - // 释放节点资源并清理引用 - Node.QueueFreeX(); - _node = null; - } - - /// - /// 安装模块时调用此方法,用于获取所需的系统组件 - /// - /// 当前架构实例 - public override void Install(IArchitecture architecture) - { - // 从架构中获取输入系统实例 - var inputSystem = architecture.GetSystem()!; - if (EnableDefaultTranslator) - { - // 注册输入转换器 - inputSystem.RegisterTranslator(new GodotInputTranslator(), true); - } - - RegisterTranslator(inputSystem); - // 创建Godot输入桥接节点并绑定输入系统 - _node = new GodotInputBridge { Name = GodotInputBridgeName }; - _node.Bind(inputSystem); - } - - /// - /// 注册翻译器的抽象方法,由子类实现具体的注册逻辑 - /// - /// 输入系统实例 - protected abstract void RegisterTranslator(InputSystem inputSystem); -} \ No newline at end of file diff --git a/GFramework.Godot/input/GodotInputBridge.cs b/GFramework.Godot/input/GodotInputBridge.cs deleted file mode 100644 index cfb6b71..0000000 --- a/GFramework.Godot/input/GodotInputBridge.cs +++ /dev/null @@ -1,41 +0,0 @@ -using GFramework.Game.input; -using Godot; - -namespace GFramework.Godot.input; - -/// -/// Godot输入桥接类,用于将Godot的输入事件转换为游戏框架的输入事件 -/// -public partial class GodotInputBridge : Node -{ - private InputSystem _inputSystem = null!; - - /// - /// 绑定输入系统 - /// - /// 要绑定的输入系统实例 - public void Bind(InputSystem inputSystem) - { - _inputSystem = inputSystem; - } - - /// - /// 捕获阶段:最早 - /// - public override void _Input(InputEvent @event) - { - _inputSystem.HandleRaw( - new GodotRawInput(@event, GodotInputPhase.Capture) - ); - } - - /// - /// 冒泡阶段:UI 未处理 - /// - public override void _UnhandledInput(InputEvent @event) - { - _inputSystem.HandleRaw( - new GodotRawInput(@event, GodotInputPhase.Bubble) - ); - } -} diff --git a/GFramework.Godot/input/GodotInputPhase.cs b/GFramework.Godot/input/GodotInputPhase.cs deleted file mode 100644 index e250a8d..0000000 --- a/GFramework.Godot/input/GodotInputPhase.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace GFramework.Godot.input; - -/// -/// 输入处理阶段枚举,用于区分Godot引擎中不同的输入处理阶段 -/// -public enum GodotInputPhase -{ - /// - /// 捕获阶段,在_Input方法中处理输入事件 - /// 这是输入事件的第一个处理阶段,事件会沿着节点树向下传递 - /// - Capture, // _Input - - /// - /// 冒泡阶段,在_UnhandledInput方法中处理输入事件 - /// 这是输入事件的第二个处理阶段,未被处理的事件会向上冒泡传递 - /// - Bubble // _UnhandledInput -} diff --git a/GFramework.Godot/input/GodotInputTranslator.cs b/GFramework.Godot/input/GodotInputTranslator.cs deleted file mode 100644 index 3144bbe..0000000 --- a/GFramework.Godot/input/GodotInputTranslator.cs +++ /dev/null @@ -1,68 +0,0 @@ -using GFramework.Game.input; -using Godot; - -namespace GFramework.Godot.input; - -/// -/// 将Godot引擎的输入事件转换为游戏通用输入事件的翻译器 -/// -public sealed class GodotInputTranslator : IInputTranslator -{ - /// - /// 尝试将原始输入对象转换为游戏输入事件 - /// - /// 原始输入对象,应为Godot的InputEvent类型 - /// 输出参数,转换成功时返回对应的游戏输入事件,失败时返回null - /// 转换成功返回true,否则返回false - public bool TryTranslate(object rawInput, out IGameInputEvent gameEvent) - { - gameEvent = null!; - - if (rawInput is not GodotRawInput raw) - return false; - - var evt = raw.Event; - - // 支持多个输入阶段:Capture, Bubble, 和其他阶段 - // 在拖拽过程中,可能需要在Capture阶段也处理输入 - if (raw.Phase != GodotInputPhase.Bubble && raw.Phase != GodotInputPhase.Capture) - 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( - mb.Position, - Vector2.Zero, - (int)mb.ButtonIndex, - mb.Pressed - ); - return true; - } - - // Mouse motion - if (evt is InputEventMouseMotion mm) - { - gameEvent = new InputEvents.PointerInputEvent( - mm.Position, - mm.Relative, - 0, - false - ); - return true; - } - - return false; - } -} diff --git a/GFramework.Godot/input/GodotRawInput.cs b/GFramework.Godot/input/GodotRawInput.cs deleted file mode 100644 index 1888c7f..0000000 --- a/GFramework.Godot/input/GodotRawInput.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Godot; - -namespace GFramework.Godot.input; - -/// -/// 表示Godot原始输入数据的只读结构体 -/// -/// 输入事件对象 -/// 输入阶段 -public readonly struct GodotRawInput(InputEvent evt, GodotInputPhase phase) -{ - /// - /// 获取输入事件对象 - /// - public readonly InputEvent Event = evt; - - /// - /// 获取输入阶段 - /// - public readonly GodotInputPhase Phase = phase; -}