Added CaptureInputsFromApp() to manually enforce e.g. keyboard capturing

This commit is contained in:
ocornut
2015-07-02 09:10:31 -06:00
parent eaaab0120a
commit 77fad80e9f
2 changed files with 18 additions and 3 deletions

View File

@ -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();
}