feat: 输入隔离——ImGui WantCapture 在 Window 层门控事件与轮询

This commit is contained in:
2026-09-22 19:18:51 +08:00
parent 952ba8135d
commit 601fd8997c
3 changed files with 37 additions and 0 deletions
+8
View File
@@ -11,6 +11,8 @@
#include <Engine/Vulkan/Device.h>
#include <Engine/ImGui/ImGuiLayer.h>
#include <imgui.h>
#include <algorithm>
namespace Engine {
@@ -57,6 +59,12 @@ namespace Engine {
m_ImGuiLayer = CreateScope<ImGuiLayer>(GetWindow(), *m_Renderer);
m_Renderer->SetImGuiLayer(m_ImGuiLayer.get());
// 输入隔离:ImGui 想要的输入不再透给游戏侧
//(事件回调与轮询接口在 Window 层统一门控)
GetWindow().SetInputCapture(
[] { return ImGui::GetIO().WantCaptureKeyboard; },
[] { return ImGui::GetIO().WantCaptureMouse; });
}
void Application::Run()
+14
View File
@@ -29,6 +29,9 @@ namespace Engine
auto* self = static_cast<Window*>(glfwGetWindowUserPointer(window));
if (!self->m_EventCallback || key == GLFW_KEY_UNKNOWN) return;
// UI 捕获键盘时,游戏侧不收键盘事件(ImGui 后端链式回调自行处理)
if (self->m_KeyboardCapture && self->m_KeyboardCapture()) return;
Event e;
e.key = key;
e.type = (action == GLFW_PRESS) ? EventType::KeyPress
@@ -41,6 +44,7 @@ namespace Engine
{
auto* self = static_cast<Window*>(glfwGetWindowUserPointer(window));
if (!self->m_EventCallback) return;
if (self->m_MouseCapture && self->m_MouseCapture()) return;
Event e;
e.button = button;
@@ -53,6 +57,7 @@ namespace Engine
{
auto* self = static_cast<Window*>(glfwGetWindowUserPointer(window));
if (!self->m_EventCallback) return;
if (self->m_MouseCapture && self->m_MouseCapture()) return;
Event e;
e.type = EventType::MouseMove;
@@ -65,6 +70,7 @@ namespace Engine
{
auto* self = static_cast<Window*>(glfwGetWindowUserPointer(window));
if (!self->m_EventCallback) return;
if (self->m_MouseCapture && self->m_MouseCapture()) return;
Event e;
e.type = EventType::MouseScroll;
@@ -165,6 +171,8 @@ namespace Engine
bool Window::IsKeyPressed(int key) const
{
// UI 捕获键盘时,轮询接口同样静默(相机等轮询型消费方)
if (m_KeyboardCapture && m_KeyboardCapture()) return false;
return glfwGetKey(m_Window, key) == GLFW_PRESS;
}
@@ -191,5 +199,11 @@ namespace Engine
lastX = mx;
lastY = my;
// UI 捕获鼠标时 delta 归零;lastX/Y 照常更新,避免释放后跳变
if (m_MouseCapture && m_MouseCapture())
{
dx = dy = 0.0f;
}
}
} // namespace Engine