From 77fad80e9f367c37cf308cf3d12aaeb7ac91eb4b Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 2 Jul 2015 09:10:31 -0600 Subject: [PATCH] Added CaptureInputsFromApp() to manually enforce e.g. keyboard capturing --- imgui.cpp | 18 ++++++++++++++++-- imgui.h | 3 ++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index ce7add0d..59992268 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1333,6 +1333,8 @@ struct ImGuiState float FramerateSecPerFrame[120]; // calculate estimate of framerate for user int FramerateSecPerFrameIdx; float FramerateSecPerFrameAccum; + bool CaptureMouseNextFrame; // explicit capture via CaptureInputs() sets those flags + bool CaptureKeyboardNextFrame; char TempBuffer[1024*3+1]; // temporary text buffer ImGuiState() @@ -1394,6 +1396,7 @@ struct ImGuiState memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame)); FramerateSecPerFrameIdx = 0; FramerateSecPerFrameAccum = 0.0f; + CaptureMouseNextFrame = CaptureKeyboardNextFrame = false; } }; @@ -2189,9 +2192,10 @@ void ImGui::NewFrame() mouse_earliest_button_down = i; } bool mouse_owned_by_application = mouse_earliest_button_down != -1 && !g.IO.MouseDownOwned[mouse_earliest_button_down]; - g.IO.WantCaptureMouse = (!mouse_owned_by_application && g.HoveredWindow != NULL) || (!mouse_owned_by_application && mouse_any_down) || (g.ActiveId != 0) || (!g.OpenedPopupStack.empty()); - g.IO.WantCaptureKeyboard = (g.ActiveId != 0); + g.IO.WantCaptureMouse = (!mouse_owned_by_application && g.HoveredWindow != NULL) || (!mouse_owned_by_application && mouse_any_down) || (g.ActiveId != 0) || (!g.OpenedPopupStack.empty()) || (g.CaptureMouseNextFrame); + g.IO.WantCaptureKeyboard = (g.ActiveId != 0) || (g.CaptureKeyboardNextFrame); g.MouseCursor = ImGuiMouseCursor_Arrow; + g.CaptureMouseNextFrame = g.CaptureKeyboardNextFrame = false; // If mouse was first clicked outside of ImGui bounds we also cancel out hovering. if (mouse_owned_by_application) @@ -3003,6 +3007,12 @@ void ImGui::SetMouseCursor(ImGuiMouseCursor cursor_type) GImGui->MouseCursor = cursor_type; } +void ImGui::CaptureInputsFromApp(bool capture_mouse, bool capture_keyboard) +{ + GImGui->CaptureMouseNextFrame |= capture_mouse; + GImGui->CaptureKeyboardNextFrame |= capture_keyboard; +} + bool ImGui::IsItemHovered() { ImGuiWindow* window = GetCurrentWindow(); @@ -11857,6 +11867,10 @@ void ImGui::ShowTestWindow(bool* opened) ImGui::Text("WantCaptureMouse: %s", io.WantCaptureMouse ? "true" : "false"); ImGui::Text("WantCaptureKeyboard: %s", io.WantCaptureKeyboard ? "true" : "false"); + ImGui::Button("Hover me\nto enforce\ninputs capture"); + if (ImGui::IsItemHovered()) + ImGui::CaptureInputsFromApp(); + ImGui::TreePop(); } diff --git a/imgui.h b/imgui.h index 9c27dd6e..52255db0 100644 --- a/imgui.h +++ b/imgui.h @@ -382,9 +382,10 @@ namespace ImGui IMGUI_API bool IsMouseDragging(int button = 0, float lock_threshold = -1.0f); // is mouse dragging. if lock_threshold < -1.0f uses io.MouseDraggingThreshold IMGUI_API ImVec2 GetMousePos(); // shortcut to ImGui::GetIO().MousePos provided by user, to be consistent with other calls IMGUI_API ImVec2 GetMouseDragDelta(int button = 0, float lock_threshold = -1.0f); // dragging amount since clicking, also see: GetItemActiveDragDelta(). if lock_threshold < -1.0f uses io.MouseDraggingThreshold - IMGUI_API void ResetMouseDragDelta(int button = 0); + IMGUI_API void ResetMouseDragDelta(int button = 0); // IMGUI_API ImGuiMouseCursor GetMouseCursor(); // get desired cursor type, reset in ImGui::NewFrame(), this updated during the frame. valid before Render(). If you use software rendering by setting io.MouseDrawCursor ImGui will render those for you IMGUI_API void SetMouseCursor(ImGuiMouseCursor type); // set desired cursor type + IMGUI_API void CaptureInputsFromApp(bool mouse = true, bool keyboard = true); // manually enforce imgui setting the io.WantCaptureMouse / io.WantCaptureKeyboard flags next frame (your application needs to handle them). e.g. capture keyboard when your widget is being hovered. // Helpers functions to access the MemAllocFn/MemFreeFn pointers in ImGui::GetIO() IMGUI_API void* MemAlloc(size_t sz);