Files
HelloVulkan/Engine/include/Engine/Core/Input.h
T
atdunbg 26e222a9cf 修复编辑器相机与 game 形态黑屏;中文字体与 Q 选择热键
FPS 式视角合成(yaw 世界轴/pitch 本地轴,永不 roll);
RMB 自由视角后环绕 pivot 失效待重建(修 Focus 后 MMB/滚轮
瞬移锁回);SceneRenderer 默认 kDisplayDirect(game 形态
Blit 空转+清屏黑屏根因);swapchain 优先 Opaque 合成;
ImGui 加载思源黑体(CJK 常用集);Q=纯选择不出杆;
新增 InputMap 动作映射
2026-10-08 17:02:39 +08:00

141 lines
5.4 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.
//
// Input.h:统一输入门面(脚本/游戏逻辑唯一入口)。
//
// 目的:
// 1. 上层代码(脚本、SandboxApp、编辑器相机)不再直接依赖 GLFW——
// 只依赖本枚举与门面,Window 后端将来可换(SDL/平台原生)
// 2. 嵌入脚本时暴露的也是这一套(Lua/Python 绑定包一层即可)
//
// 键码值 = GLFW 物理键码(glf3.h 标准值),强转 int 可直接传给
// Window::IsKeyPressed;但调用方不得依赖这一点。
//
// 语义:轮询接口沿用 Window 的门控规则——UI 捕获键盘(编辑器文本框
// 输入中)时 IsKeyDown 返回 false,游戏侧静默;鼠标捕获同理。
//
#pragma once
#include <glm/glm.hpp>
#include "Engine/Core/Types.h"
#include "Engine/Input/InputAction.h"
namespace Engine {
class Window;
namespace Input {
enum class Key : int {
// ---- 可打印区(值 = ASCII)----
Space = 32,
Apostrophe = 39,
Comma = 44,
Minus = 45,
Period = 46,
Slash = 47,
Num0 = 48, Num1 = 49, Num2 = 50, Num3 = 51, Num4 = 52,
Num5 = 53, Num6 = 54, Num7 = 55, Num8 = 56, Num9 = 57,
Semicolon = 59,
Equal = 61,
A = 65, B = 66, C = 67, D = 68, E = 69, F = 70, G = 71,
H = 72, I = 73, J = 74, K = 75, L = 76, M = 77, N = 78,
O = 79, P = 80, Q = 81, R = 82, S = 83, T = 84, U = 85,
V = 86, W = 87, X = 88, Y = 89, Z = 90,
BracketLeft = 91,
Backslash = 92,
BracketRight = 93,
GraveAccent = 96,
// ---- 控制键(值 = GLFW 键码)----
Escape = 256,
Enter = 257,
Tab = 258,
Backspace = 259,
Insert = 260,
Delete = 261,
Right = 262,
Left = 263,
Down = 264,
Up = 265,
PageUp = 266,
PageDown = 267,
Home = 268,
End = 269,
F1 = 290, F2 = 291, F3 = 292, F4 = 293, F5 = 294, F6 = 295,
F7 = 296, F8 = 297, F9 = 298, F10 = 299, F11 = 300,
F12 = 301,
// ---- 修饰键 ----
LeftShift = 340,
LeftControl = 341,
LeftAlt = 342,
LeftSuper = 343,
RightShift = 344,
RightControl = 345,
RightAlt = 346,
RightSuper = 347,
};
enum class MouseButton : int {
Left = 0, // GLFW_MOUSE_BUTTON_1
Right = 1,
Middle = 2,
};
// 装配:Application 构造时自动调用(后端 Window 就绪后)。
// 非 const:门面含状态设置(光标捕获),不只是查询
void Init(Window& window);
// 轮询查询(UI 捕获对应输入时返回 false——游戏侧静默)
bool IsKeyDown(Key key);
bool IsMouseButtonDown(MouseButton button);
// ---- 光标捕获(自由视角三件套:隐藏光标 + 无界鼠标增量 +
// 滚轮增量)。语义沿 Window 后端:捕获时光标隐藏、位置锁定
// 窗口中心,GetMouseDelta 出无界增量;重复设置同值无操作 ----
// 隐藏并锁定光标(自由视角/环绕中);false 恢复正常光标
void SetCursorCaptured(bool captured);
bool IsCursorCaptured();
// 帧间光标增量(无界,捕获态下才有意义;窗口尺寸/模式变化
// 由后端参考点重置防跳变)
void GetMouseDelta(f32& dx, f32& dy);
// 滚轮增量(上行 = +1,一档 ≈1):读后清零。UI 捕获鼠标
//(面板悬停)时不累积——滚轮归 ImGui
f32 ConsumeScrollDelta();
// ---- 输入动作层(具名动作,键鼠+手柄混绑;InputMap 藏在 cpp)----
// 采样:主循环单点调用 Poll(物理/脚本/OnUpdate 之前),整帧
// 一致。active=false(GameFlow 非 Running)时全部动作静默清零。
// 默认绑定表在 Init 时内建(Move=Axis2D、Jump=Button)
// 装配期(编辑器/游戏配置阶段调用;运行中 Define 也可,索引重建)
void DefineAction(const std::string& name, ActionKind kind);
// 追加绑定:绑定到具名动作(动作不存在则忽略)
void BindAction(const std::string& name, const ActionBinding& b);
// 就地重绑定(bindingIdx = 该动作绑定表下标;下帧生效)
bool RebindAction(const std::string& name, size_t bindingIdx,
const ActionBinding& b);
// 持久化(yaml;Load 文件不存在静默失败保持现状表)
bool SaveActionMap(const std::string& path);
bool LoadActionMap(const std::string& path);
// 每帧采样(Application::Run 专用单点;上层勿调)
void Poll(bool active);
// 查询(按动作名;未定义动作返回 false/0)。Button 用
// Pressed/Down/Released 边沿语义,Axis 用 Value/Value2
bool ActionPressed(const std::string& name);
bool ActionDown(const std::string& name);
bool ActionReleased(const std::string& name);
f32 ActionValue(const std::string& name);
glm::vec2 ActionValue2(const std::string& name);
// 手柄连接态(Poll 时更新)
bool IsGamepadConnected();
}
} // namespace Engine