Skip to content

Commit cc3a220

Browse files
committed
Internals: invert logic of mods key<>bool translation to facilitate other experiments and put emphasis on new API. (#5923, #4921)
Should be no-op, this is mostly to make it easier to store state for ImGuiMod_Shortcut.
1 parent 1a497c2 commit cc3a220

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

imgui.cpp

+20-21
Original file line numberDiff line numberDiff line change
@@ -4160,16 +4160,15 @@ static void UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt)
41604160
rt->Entries.swap(rt->EntriesNext); // Swap new and old indexes
41614161
}
41624162

4163-
// [Internal] Do not use directly (should read io.KeyMods instead)
4164-
static ImGuiKeyChord GetMergedModsFromBools()
4163+
// [Internal] Do not use directly
4164+
static ImGuiKeyChord GetMergedModsFromKeys()
41654165
{
4166-
ImGuiContext& g = *GImGui;
4167-
ImGuiKeyChord key_chord = 0;
4168-
if (g.IO.KeyCtrl) { key_chord |= ImGuiMod_Ctrl; }
4169-
if (g.IO.KeyShift) { key_chord |= ImGuiMod_Shift; }
4170-
if (g.IO.KeyAlt) { key_chord |= ImGuiMod_Alt; }
4171-
if (g.IO.KeySuper) { key_chord |= ImGuiMod_Super; }
4172-
return key_chord;
4166+
ImGuiKeyChord mods = 0;
4167+
if (ImGui::IsKeyDown(ImGuiMod_Ctrl)) { mods |= ImGuiMod_Ctrl; }
4168+
if (ImGui::IsKeyDown(ImGuiMod_Shift)) { mods |= ImGuiMod_Shift; }
4169+
if (ImGui::IsKeyDown(ImGuiMod_Alt)) { mods |= ImGuiMod_Alt; }
4170+
if (ImGui::IsKeyDown(ImGuiMod_Super)) { mods |= ImGuiMod_Super; }
4171+
return mods;
41734172
}
41744173

41754174
static void ImGui::UpdateKeyboardInputs()
@@ -4244,13 +4243,22 @@ static void ImGui::UpdateKeyboardInputs()
42444243
#endif
42454244
#endif
42464245

4247-
// Synchronize io.KeyMods with individual modifiers io.KeyXXX bools, update aliases
4248-
io.KeyMods = GetMergedModsFromBools();
4246+
// Update aliases
42494247
for (int n = 0; n < ImGuiMouseButton_COUNT; n++)
42504248
UpdateAliasKey(MouseButtonToKey(n), io.MouseDown[n], io.MouseDown[n] ? 1.0f : 0.0f);
42514249
UpdateAliasKey(ImGuiKey_MouseWheelX, io.MouseWheelH != 0.0f, io.MouseWheelH);
42524250
UpdateAliasKey(ImGuiKey_MouseWheelY, io.MouseWheel != 0.0f, io.MouseWheel);
42534251

4252+
// Synchronize io.KeyMods and io.KeyXXX values.
4253+
// - New backends (1.87+): send io.AddKeyEvent(ImGuiMod_XXX) -> -> (here) deriving io.KeyMods + io.KeyXXX from key array.
4254+
// - Legacy backends: set io.KeyXXX bools -> (above) set key array from io.KeyXXX -> (here) deriving io.KeyMods + io.KeyXXX from key array.
4255+
// So with legacy backends the 4 values will do a unnecessary back-and-forth but it makes the code simpler and future facing.
4256+
io.KeyMods = GetMergedModsFromKeys();
4257+
io.KeyCtrl = (io.KeyMods & ImGuiMod_Ctrl) != 0;
4258+
io.KeyShift = (io.KeyMods & ImGuiMod_Shift) != 0;
4259+
io.KeyAlt = (io.KeyMods & ImGuiMod_Alt) != 0;
4260+
io.KeySuper = (io.KeyMods & ImGuiMod_Super) != 0;
4261+
42544262
// Clear gamepad data if disabled
42554263
if ((io.BackendFlags & ImGuiBackendFlags_HasGamepad) == 0)
42564264
for (int i = ImGuiKey_Gamepad_BEGIN; i < ImGuiKey_Gamepad_END; i++)
@@ -8485,15 +8493,6 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs)
84858493
key_changed = true;
84868494
key_changed_mask.SetBit(key_data_index);
84878495

8488-
if (key == ImGuiMod_Ctrl || key == ImGuiMod_Shift || key == ImGuiMod_Alt || key == ImGuiMod_Super)
8489-
{
8490-
if (key == ImGuiMod_Ctrl) { io.KeyCtrl = key_data->Down; }
8491-
if (key == ImGuiMod_Shift) { io.KeyShift = key_data->Down; }
8492-
if (key == ImGuiMod_Alt) { io.KeyAlt = key_data->Down; }
8493-
if (key == ImGuiMod_Super) { io.KeySuper = key_data->Down; }
8494-
io.KeyMods = GetMergedModsFromBools();
8495-
}
8496-
84978496
// Allow legacy code using io.KeysDown[GetKeyIndex()] with new backends
84988497
#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO
84998498
io.KeysDown[key_data_index] = key_data->Down;
@@ -8775,7 +8774,7 @@ static void ImGui::ErrorCheckEndFrameSanityChecks()
87758774
// send key release events mid-frame. This would normally trigger this assertion and lead to sheared inputs.
87768775
// We silently accommodate for this case by ignoring the case where all io.KeyXXX modifiers were released (aka key_mod_flags == 0),
87778776
// while still correctly asserting on mid-frame key press events.
8778-
const ImGuiKeyChord key_mods = GetMergedModsFromBools();
8777+
const ImGuiKeyChord key_mods = GetMergedModsFromKeys();
87798778
IM_ASSERT((key_mods == 0 || g.IO.KeyMods == key_mods) && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods");
87808779
IM_UNUSED(key_mods);
87818780

0 commit comments

Comments
 (0)