mirror of
https://github.com/ocornut/imgui.git
synced 2024-11-12 02:00:58 +01:00
CaptureKeyboardFromApp() / CaptureMouseFromApp(): allow to enforce clearing the capture flag (#533) + demo + made code a little less messy
This commit is contained in:
parent
cf12bc7dea
commit
d45044fe54
23
imgui.cpp
23
imgui.cpp
@ -1990,7 +1990,7 @@ void ImGui::NewFrame()
|
||||
}
|
||||
|
||||
// Are we using inputs? Tell user so they can capture/discard the inputs away from the rest of their application.
|
||||
// When clicking outside of a window we assume the click is owned by the application and won't request capture.
|
||||
// When clicking outside of a window we assume the click is owned by the application and won't request capture. We need to track click ownership.
|
||||
int mouse_earliest_button_down = -1;
|
||||
bool mouse_any_down = false;
|
||||
for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++)
|
||||
@ -2002,16 +2002,19 @@ void ImGui::NewFrame()
|
||||
if (mouse_earliest_button_down == -1 || g.IO.MouseClickedTime[mouse_earliest_button_down] > g.IO.MouseClickedTime[i])
|
||||
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.CaptureMouseNextFrame);
|
||||
g.IO.WantCaptureKeyboard = (g.ActiveId != 0) || (g.CaptureKeyboardNextFrame);
|
||||
bool mouse_avail_to_imgui = (mouse_earliest_button_down == -1) || g.IO.MouseDownOwned[mouse_earliest_button_down];
|
||||
if (g.CaptureMouseNextFrame != -1)
|
||||
g.IO.WantCaptureMouse = (g.CaptureMouseNextFrame != 0);
|
||||
else
|
||||
g.IO.WantCaptureMouse = (mouse_avail_to_imgui && (g.HoveredWindow != NULL || mouse_any_down)) || (g.ActiveId != 0) || (!g.OpenedPopupStack.empty());
|
||||
g.IO.WantCaptureKeyboard = (g.CaptureKeyboardNextFrame != -1) ? (g.CaptureKeyboardNextFrame != 0) : (g.ActiveId != 0);
|
||||
g.IO.WantTextInput = (g.ActiveId != 0 && g.InputTextState.Id == g.ActiveId);
|
||||
g.MouseCursor = ImGuiMouseCursor_Arrow;
|
||||
g.CaptureMouseNextFrame = g.CaptureKeyboardNextFrame = false;
|
||||
g.CaptureMouseNextFrame = g.CaptureKeyboardNextFrame = -1;
|
||||
g.OsImePosRequest = ImVec2(1.0f, 1.0f); // OS Input Method Editor showing on top-left of our window by default
|
||||
|
||||
// If mouse was first clicked outside of ImGui bounds we also cancel out hovering.
|
||||
if (mouse_owned_by_application)
|
||||
if (!mouse_avail_to_imgui)
|
||||
g.HoveredWindow = g.HoveredRootWindow = NULL;
|
||||
|
||||
// Scale & Scrolling
|
||||
@ -2990,14 +2993,14 @@ void ImGui::SetMouseCursor(ImGuiMouseCursor cursor_type)
|
||||
GImGui->MouseCursor = cursor_type;
|
||||
}
|
||||
|
||||
void ImGui::CaptureKeyboardFromApp()
|
||||
void ImGui::CaptureKeyboardFromApp(bool capture)
|
||||
{
|
||||
GImGui->CaptureKeyboardNextFrame = true;
|
||||
GImGui->CaptureKeyboardNextFrame = capture ? 1 : 0;
|
||||
}
|
||||
|
||||
void ImGui::CaptureMouseFromApp()
|
||||
void ImGui::CaptureMouseFromApp(bool capture)
|
||||
{
|
||||
GImGui->CaptureMouseNextFrame = true;
|
||||
GImGui->CaptureMouseNextFrame = capture ? 1 : 0;
|
||||
}
|
||||
|
||||
bool ImGui::IsItemHovered()
|
||||
|
4
imgui.h
4
imgui.h
@ -415,8 +415,8 @@ namespace ImGui
|
||||
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 CaptureKeyboardFromApp(); // manually enforce imgui setting the io.WantCaptureKeyboard flag next frame (your application needs to handle it). e.g. capture keyboard when your widget is being hovered.
|
||||
IMGUI_API void CaptureMouseFromApp(); // manually enforce imgui setting the io.WantCaptureMouse flag next frame (your application needs to handle it).
|
||||
IMGUI_API void CaptureKeyboardFromApp(bool capture = true); // manually override io.WantCaptureKeyboard flag next frame (said flag is entirely left for your application handle). e.g. force capture keyboard when your widget is being hovered.
|
||||
IMGUI_API void CaptureMouseFromApp(bool capture = true); // manually override io.WantCaptureMouse flag next frame (said flag is entirely left for your application handle).
|
||||
|
||||
// Helpers functions to access functions pointers in ImGui::GetIO()
|
||||
IMGUI_API void* MemAlloc(size_t sz);
|
||||
|
@ -1503,10 +1503,14 @@ void ImGui::ShowTestWindow(bool* p_opened)
|
||||
ImGui::Text("WantCaptureKeyboard: %s", io.WantCaptureKeyboard ? "true" : "false");
|
||||
ImGui::Text("WantTextInput: %s", io.WantTextInput ? "true" : "false");
|
||||
|
||||
ImGui::Button("Hover me\nto enforce\ninputs capture");
|
||||
ImGui::Button("Hovering me sets the\nkeyboard capture flag");
|
||||
if (ImGui::IsItemHovered())
|
||||
ImGui::CaptureKeyboardFromApp();
|
||||
|
||||
ImGui::CaptureKeyboardFromApp(true);
|
||||
ImGui::SameLine();
|
||||
ImGui::Button("Holding me clears the\nthe keyboard capture flag");
|
||||
if (ImGui::IsItemActive())
|
||||
ImGui::CaptureKeyboardFromApp(false);
|
||||
|
||||
ImGui::TreePop();
|
||||
}
|
||||
|
||||
|
@ -434,8 +434,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;
|
||||
int CaptureMouseNextFrame; // explicit capture via CaptureInputs() sets those flags
|
||||
int CaptureKeyboardNextFrame;
|
||||
char TempBuffer[1024*3+1]; // temporary text buffer
|
||||
|
||||
ImGuiState()
|
||||
@ -501,7 +501,7 @@ struct ImGuiState
|
||||
memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame));
|
||||
FramerateSecPerFrameIdx = 0;
|
||||
FramerateSecPerFrameAccum = 0.0f;
|
||||
CaptureMouseNextFrame = CaptureKeyboardNextFrame = false;
|
||||
CaptureMouseNextFrame = CaptureKeyboardNextFrame = -1;
|
||||
memset(TempBuffer, 0, sizeof(TempBuffer));
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user