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
+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