Unify input behind Engine::Input facade; fix Ctrl+P and Pause button

New Engine/Core/Input.h/.cpp: static input facade with a Key/MouseButton
enum (values map to GLFW key codes internally) so scripts and game logic
no longer touch GLFW directly - window backend becomes swappable and the
facade is the surface to expose when embedding scripting later. Wired up
in Application's constructor; Camera (editor camera WASD/RMB/MMB), the
WASDMove script and SandboxApp key handling now query Input only.

Ctrl+P hotkey fix: the !m_Playing guard made the hotkey dead exactly
when it was needed - after pressing Play. Now toggles both ways.

Toolbar Pause button now reads "Resume" while paused (tooltip follows).
This commit is contained in:
2026-10-05 11:34:27 +08:00
parent c083ee5cb7
commit 641b255cb2
7 changed files with 161 additions and 29 deletions
+5
View File
@@ -4,6 +4,7 @@
#include <Engine/Core/Application.h>
#include <Engine/Core/Window.h>
#include <Engine/Core/Input.h>
#include <Engine/Core/Log.h>
#include <Engine/ECS/Scene.h>
#include <Engine/Physics/PhysicsWorld.h>
@@ -39,6 +40,10 @@ namespace Engine {
// 资产层不感知 Vulkan 具体类。
// 声明在 Renderer 之后 → 析构先于 Renderer(它引用其子模块)
m_Assets = CreateScope<Assets::AssetManager>(m_Renderer->GetAssetUploader());
// 统一输入门面装配:脚本/游戏逻辑经 Input 查询键鼠,不直接
// 依赖 Window 后端(GLFW 解耦)
Input::Init(*m_Window);
}
Application::~Application()
+35
View File
@@ -0,0 +1,35 @@
//
// Input.cpp:统一输入门面实现(转发 Window 后端轮询接口)。
//
#include "Engine/Core/Input.h"
#include "Engine/Core/Window.h"
namespace Engine {
namespace Input {
namespace {
// Application 生命周期内有效(成员析构序:Input 无状态依赖,
// 仅 Application::Run 期间被查询)
const Window* s_Window = nullptr;
}
void Init(const Window& window)
{
s_Window = &window;
}
bool IsKeyDown(const Key key)
{
return s_Window != nullptr &&
s_Window->IsKeyPressed(static_cast<int>(key));
}
bool IsMouseButtonDown(const MouseButton button)
{
return s_Window != nullptr &&
s_Window->IsMouseButtonPressed(static_cast<int>(button));
}
}
} // namespace Engine
+10 -10
View File
@@ -10,7 +10,7 @@
#include <cmath>
#include "GLFW/glfw3.h"
#include "Engine/Core/Input.h" // 键查询走统一输入门面(不依赖 GLFW)
namespace Engine {
@@ -35,9 +35,9 @@ namespace Engine {
// 光标型交互互斥共用捕获;编辑器模式 flyRequiresRMB=true 时
// 移动键仅 RMB 按住生效(否则与 Gizmo W/E/R 热键冲突)----
const bool wantLook = m_MouseLookEnabled &&
window.IsMouseButtonPressed(GLFW_MOUSE_BUTTON_RIGHT);
Input::IsMouseButtonDown(Input::MouseButton::Right);
const bool wantOrbit = m_MouseLookEnabled &&
window.IsMouseButtonPressed(GLFW_MOUSE_BUTTON_MIDDLE);
Input::IsMouseButtonDown(Input::MouseButton::Middle);
if (wantLook || wantOrbit)
{
@@ -70,7 +70,7 @@ namespace Engine {
if (wantOrbit)
{
if (window.IsKeyPressed(GLFW_KEY_LEFT_SHIFT))
if (Input::IsKeyDown(Input::Key::LeftShift))
{
// ---- 屏幕平面平移:内容跟手(拖右 → 相机左移、
// 拖下 → 相机上移),pivot 同步平移保持环绕点的
@@ -120,12 +120,12 @@ namespace Engine {
if (wantLook || !flyRequiresRMB)
{
const f32 speed = m_MoveSpeed * deltaTime;
if (window.IsKeyPressed(GLFW_KEY_W)) { transform.SetPosition(transform.GetPosition() + forward * speed); }
if (window.IsKeyPressed(GLFW_KEY_S)) { transform.SetPosition(transform.GetPosition() - forward * speed); }
if (window.IsKeyPressed(GLFW_KEY_A)) { transform.SetPosition(transform.GetPosition() - right * speed); }
if (window.IsKeyPressed(GLFW_KEY_D)) { transform.SetPosition(transform.GetPosition() + right * speed); }
if (window.IsKeyPressed(GLFW_KEY_SPACE)) { transform.SetPosition(transform.GetPosition() + glm::vec3(0, speed, 0)); }
if (window.IsKeyPressed(GLFW_KEY_LEFT_SHIFT)) { transform.SetPosition(transform.GetPosition() - glm::vec3(0, speed, 0)); }
if (Input::IsKeyDown(Input::Key::W)) { transform.SetPosition(transform.GetPosition() + forward * speed); }
if (Input::IsKeyDown(Input::Key::S)) { transform.SetPosition(transform.GetPosition() - forward * speed); }
if (Input::IsKeyDown(Input::Key::A)) { transform.SetPosition(transform.GetPosition() - right * speed); }
if (Input::IsKeyDown(Input::Key::D)) { transform.SetPosition(transform.GetPosition() + right * speed); }
if (Input::IsKeyDown(Input::Key::Space)) { transform.SetPosition(transform.GetPosition() + glm::vec3(0, speed, 0)); }
if (Input::IsKeyDown(Input::Key::LeftShift)) { transform.SetPosition(transform.GetPosition() - glm::vec3(0, speed, 0)); }
}
// ---- 滚轮推拉:有 pivot 时改环绕距离(指数步进,近处精细/