diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index ed227d2aa431..155a85bbada6 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -65,6 +65,9 @@ Other Changes: or interacting with a game/3D view). - IO: Fixed input queue trickling of mouse wheel events: multiple wheel events are merged, while a mouse pos followed by a mouse wheel are now trickled. (#4921, #4821) +- IO: Added io.SetAppAcceptingEvents() to set a master flag for accepting key/mouse/characters + events (default to true). Useful if you have native dialog boxes that are interrupting your + application loop/refresh, and you want to disable events being queued while your app is frozen. - Windows: Fixed first-time windows appearing in negative coordinates from being initialized with a wrong size. This would most often be noticeable in multi-viewport mode (docking branch) when spawning a window in a monitor with negative coordinates. (#5215, #3414) [@DimaKoltun] diff --git a/imgui.cpp b/imgui.cpp index eb2eb18f8f5a..4af42dd45e93 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1169,6 +1169,7 @@ ImGuiIO::ImGuiIO() for (int i = 0; i < IM_ARRAYSIZE(MouseDownDuration); i++) MouseDownDuration[i] = MouseDownDurationPrev[i] = -1.0f; for (int i = 0; i < IM_ARRAYSIZE(KeysData); i++) { KeysData[i].DownDuration = KeysData[i].DownDurationPrev = -1.0f; } for (int i = 0; i < IM_ARRAYSIZE(NavInputsDownDuration); i++) NavInputsDownDuration[i] = -1.0f; + AppAcceptingEvents = true; BackendUsingLegacyKeyArrays = (ImS8)-1; BackendUsingLegacyNavInputArray = true; // assume using legacy array until proven wrong } @@ -1181,7 +1182,7 @@ void ImGuiIO::AddInputCharacter(unsigned int c) { ImGuiContext& g = *GImGui; IM_ASSERT(&g.IO == this && "Can only add events to current context."); - if (c == 0) + if (c == 0 || !AppAcceptingEvents) return; ImGuiInputEvent e; @@ -1195,7 +1196,7 @@ void ImGuiIO::AddInputCharacter(unsigned int c) // we should save the high surrogate. void ImGuiIO::AddInputCharacterUTF16(ImWchar16 c) { - if (c == 0 && InputQueueSurrogate == 0) + if ((c == 0 && InputQueueSurrogate == 0) || !AppAcceptingEvents) return; if ((c & 0xFC00) == 0xD800) // High surrogate, must save @@ -1229,6 +1230,8 @@ void ImGuiIO::AddInputCharacterUTF16(ImWchar16 c) void ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars) { + if (!AppAcceptingEvents) + return; while (*utf8_chars != 0) { unsigned int c = 0; @@ -1267,7 +1270,7 @@ void ImGuiIO::ClearInputKeys() void ImGuiIO::AddKeyAnalogEvent(ImGuiKey key, bool down, float analog_value) { //if (e->Down) { IMGUI_DEBUG_LOG("AddKeyEvent() Key='%s' %d, NativeKeycode = %d, NativeScancode = %d\n", ImGui::GetKeyName(e->Key), e->Down, e->NativeKeycode, e->NativeScancode); } - if (key == ImGuiKey_None) + if (key == ImGuiKey_None || !AppAcceptingEvents) return; ImGuiContext& g = *GImGui; IM_ASSERT(&g.IO == this && "Can only add events to current context."); @@ -1308,6 +1311,8 @@ void ImGuiIO::AddKeyAnalogEvent(ImGuiKey key, bool down, float analog_value) void ImGuiIO::AddKeyEvent(ImGuiKey key, bool down) { + if (!AppAcceptingEvents) + return; AddKeyAnalogEvent(key, down, down ? 1.0f : 0.0f); } @@ -1336,11 +1341,19 @@ void ImGuiIO::SetKeyEventNativeData(ImGuiKey key, int native_keycode, int native #endif } +// Set master flag for accepting key/mouse/text events (default to true). Useful if you have native dialog boxes that are interrupting your application loop/refresh, and you want to disable events being queued while your app is frozen. +void ImGuiIO::SetAppAcceptingEvents(bool accepting_events) +{ + AppAcceptingEvents = accepting_events; +} + // Queue a mouse move event void ImGuiIO::AddMousePosEvent(float x, float y) { ImGuiContext& g = *GImGui; IM_ASSERT(&g.IO == this && "Can only add events to current context."); + if (!AppAcceptingEvents) + return; ImGuiInputEvent e; e.Type = ImGuiInputEventType_MousePos; @@ -1355,6 +1368,8 @@ void ImGuiIO::AddMouseButtonEvent(int mouse_button, bool down) ImGuiContext& g = *GImGui; IM_ASSERT(&g.IO == this && "Can only add events to current context."); IM_ASSERT(mouse_button >= 0 && mouse_button < ImGuiMouseButton_COUNT); + if (!AppAcceptingEvents) + return; ImGuiInputEvent e; e.Type = ImGuiInputEventType_MouseButton; @@ -1369,7 +1384,7 @@ void ImGuiIO::AddMouseWheelEvent(float wheel_x, float wheel_y) { ImGuiContext& g = *GImGui; IM_ASSERT(&g.IO == this && "Can only add events to current context."); - if (wheel_x == 0.0f && wheel_y == 0.0f) + if ((wheel_x == 0.0f && wheel_y == 0.0f) || !AppAcceptingEvents) return; ImGuiInputEvent e; diff --git a/imgui.h b/imgui.h index 488b5d1fd0a0..25854867a2e2 100644 --- a/imgui.h +++ b/imgui.h @@ -65,7 +65,7 @@ Index of this file: // Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) #define IMGUI_VERSION "1.88 WIP" -#define IMGUI_VERSION_NUM 18725 +#define IMGUI_VERSION_NUM 18726 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) #define IMGUI_HAS_TABLE @@ -1975,9 +1975,10 @@ struct ImGuiIO IMGUI_API void AddInputCharacterUTF16(ImWchar16 c); // Queue a new character input from an UTF-16 character, it can be a surrogate IMGUI_API void AddInputCharactersUTF8(const char* str); // Queue a new characters input from an UTF-8 string + IMGUI_API void SetKeyEventNativeData(ImGuiKey key, int native_keycode, int native_scancode, int native_legacy_index = -1); // [Optional] Specify index for legacy <1.87 IsKeyXXX() functions with native indices + specify native keycode, scancode. + IMGUI_API void SetAppAcceptingEvents(bool accepting_events); // Set master flag for accepting key/mouse/text events (default to true). Useful if you have native dialog boxes that are interrupting your application loop/refresh, and you want to disable events being queued while your app is frozen. IMGUI_API void ClearInputCharacters(); // [Internal] Clear the text input buffer manually IMGUI_API void ClearInputKeys(); // [Internal] Release all keys - IMGUI_API void SetKeyEventNativeData(ImGuiKey key, int native_keycode, int native_scancode, int native_legacy_index = -1); // [Optional] Specify index for legacy <1.87 IsKeyXXX() functions with native indices + specify native keycode, scancode. //------------------------------------------------------------------ // Output - Updated by NewFrame() or EndFrame()/Render() @@ -2044,7 +2045,8 @@ struct ImGuiIO float NavInputsDownDuration[ImGuiNavInput_COUNT]; float NavInputsDownDurationPrev[ImGuiNavInput_COUNT]; float PenPressure; // Touch/Pen pressure (0.0f to 1.0f, should be >0.0f only when MouseDown[0] == true). Helper storage currently unused by Dear ImGui. - bool AppFocusLost; + bool AppFocusLost; // Only modify via AddFocusEvent() + bool AppAcceptingEvents; // Only modify via SetAppAcceptingEvents() ImS8 BackendUsingLegacyKeyArrays; // -1: unknown, 0: using AddKeyEvent(), 1: using legacy io.KeysDown[] bool BackendUsingLegacyNavInputArray; // 0: using AddKeyAnalogEvent(), 1: writing to legacy io.NavInputs[] directly ImWchar16 InputQueueSurrogate; // For AddInputCharacterUTF16()