From 105bb3ef8a4e2b764031f9fffe10aaeb860b5de8 Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 8 Jul 2022 17:44:19 +0200 Subject: [PATCH] Legacy: clear g.ActiveIdUsingNavInputMask when active id is clear + Internals: added helpers GetKeyChordName(), ImGuiModFlags_All. Amend 8b8a61b --- imgui.cpp | 35 ++++++++++++++++++++++++++--------- imgui.h | 3 ++- imgui_internal.h | 1 + 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 821ede36d..11ecf7811 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -4414,6 +4414,20 @@ void ImGui::NewFrame() g.ActiveIdUsingKeyInputMask.ClearAllBits(); } +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + if (g.ActiveId == 0) + g.ActiveIdUsingNavInputMask = 0; + else if (g.ActiveIdUsingNavInputMask != 0) + { + // If your custom widget code used: { g.ActiveIdUsingNavInputMask |= (1 << ImGuiNavInput_Cancel); } + // Since IMGUI_VERSION_NUM >= 18804 it should be: { SetActiveIdUsingKey(ImGuiKey_Escape); SetActiveIdUsingKey(ImGuiKey_NavGamepadCancel); } + if (g.ActiveIdUsingNavInputMask & (1 << ImGuiNavInput_Cancel)) + SetActiveIdUsingKey(ImGuiKey_Escape); + if (g.ActiveIdUsingNavInputMask & ~(1 << ImGuiNavInput_Cancel)) + IM_ASSERT(0); // Other values unsupported + } +#endif + // Drag and drop g.DragDropAcceptIdPrev = g.DragDropAcceptIdCurr; g.DragDropAcceptIdCurr = 0; @@ -7674,6 +7688,18 @@ const char* ImGui::GetKeyName(ImGuiKey key) return GKeyNames[key - ImGuiKey_NamedKey_BEGIN]; } +void ImGui::GetKeyChordName(ImGuiModFlags mods, ImGuiKey key, char* out_buf, int out_buf_size) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT((mods & ~ImGuiModFlags_All) == 0 && "Passing invalid ImGuiModFlags value!"); // A frequent mistake is to pass ImGuiKey_ModXXX instead of ImGuiModFlags_XXX + ImFormatString(out_buf, (size_t)out_buf_size, "%s%s%s%s%s", + (mods & ImGuiModFlags_Ctrl) ? "Ctrl+" : "", + (mods & ImGuiModFlags_Shift) ? "Shift+" : "", + (mods & ImGuiModFlags_Alt) ? "Alt+" : "", + (mods & ImGuiModFlags_Super) ? (g.IO.ConfigMacOSXBehaviors ? "Cmd+" : "Super+") : "", + GetKeyName(key)); +} + // t0 = previous time (e.g.: g.Time - g.IO.DeltaTime) // t1 = current time (e.g.: g.Time) // An event is triggered at: @@ -10614,15 +10640,6 @@ static void ImGui::NavUpdateCancelRequest() if (!IsKeyPressed(ImGuiKey_Escape, false) && !IsKeyPressed(ImGuiKey_NavGamepadCancel, false)) return; -#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO - // If your custom widget code used: { g.ActiveIdUsingNavInputMask |= (1 << ImGuiNavInput_Cancel); } - // Since IMGUI_VERSION_NUM >= 18804 it should be: { SetActiveIdUsingKey(ImGuiKey_Escape); SetActiveIdUsingKey(ImGuiKey_NavGamepadCancel); } - if (g.ActiveIdUsingNavInputMask & (1 << ImGuiNavInput_Cancel)) - SetActiveIdUsingKey(ImGuiKey_Escape); - if (g.ActiveIdUsingNavInputMask & ~(1 << ImGuiNavInput_Cancel)) - IM_ASSERT(0); // Other values unsupported -#endif - IMGUI_DEBUG_LOG_NAV("[nav] NavUpdateCancelRequest()\n"); if (g.ActiveId != 0) { diff --git a/imgui.h b/imgui.h index e90ac2f89..d8c7d7def 100644 --- a/imgui.h +++ b/imgui.h @@ -1459,8 +1459,9 @@ enum ImGuiModFlags_ ImGuiModFlags_None = 0, ImGuiModFlags_Ctrl = 1 << 0, ImGuiModFlags_Shift = 1 << 1, - ImGuiModFlags_Alt = 1 << 2, // Menu + ImGuiModFlags_Alt = 1 << 2, // Option/Menu key ImGuiModFlags_Super = 1 << 3, // Cmd/Super/Windows key + ImGuiModFlags_All = 0x0F }; #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO diff --git a/imgui_internal.h b/imgui_internal.h index baf55f696..32136cffb 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -2701,6 +2701,7 @@ namespace ImGui inline bool IsLegacyKey(ImGuiKey key) { return key >= ImGuiKey_LegacyNativeKey_BEGIN && key < ImGuiKey_LegacyNativeKey_END; } inline bool IsGamepadKey(ImGuiKey key) { return key >= ImGuiKey_Gamepad_BEGIN && key < ImGuiKey_Gamepad_END; } IMGUI_API ImGuiKeyData* GetKeyData(ImGuiKey key); + IMGUI_API void GetKeyChordName(ImGuiModFlags mods, ImGuiKey key, char* out_buf, int out_buf_size); IMGUI_API void SetItemUsingMouseWheel(); IMGUI_API void SetActiveIdUsingNavAndKeys(); inline bool IsActiveIdUsingNavDir(ImGuiDir dir) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavDirMask & (1 << dir)) != 0; }