Skip to content

Commit ccf0cc8

Browse files
committed
Added ImGuiKeyModFlags. Added additional checks in EndFrame() to verify that io.KeyXXX values have not been tampered with between NewFrame() and EndFrame().
1 parent d8824f9 commit ccf0cc8

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

docs/CHANGELOG.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ Other Changes:
5757
- Window: Fixed a bug with child window inheriting ItemFlags from their parent when the child
5858
window also manipulate the ItemFlags stack. (#3024) [@Stanbroek]
5959
- Font: Fixed non-ASCII space occasionally creating unnecessary empty polygons.
60+
- Misc: Added additional checks in EndFrame() to verify that io.KeyXXX values have not been
61+
tampered with between NewFrame() and EndFrame().
6062
- Misc, Freetype: Fixed support for IMGUI_STB_RECT_PACK_FILENAME compile time directive
6163
in imgui_freetype.cpp (matching support in the regular code path). (#3062) [@DonKult]
6264
- Demo: Added black and white and color gradients to Demo>Examples>Custom Rendering.

imgui.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3704,6 +3704,17 @@ static void NewFrameSanityChecks()
37043704
g.IO.ConfigWindowsResizeFromEdges = false;
37053705
}
37063706

3707+
static ImGuiKeyModFlags GetMergedKeyModFlags()
3708+
{
3709+
ImGuiContext& g = *GImGui;
3710+
ImGuiKeyModFlags key_mod_flags = ImGuiKeyModFlags_None;
3711+
if (g.IO.KeyCtrl) { key_mod_flags |= ImGuiKeyModFlags_Ctrl; }
3712+
if (g.IO.KeyShift) { key_mod_flags |= ImGuiKeyModFlags_Shift; }
3713+
if (g.IO.KeyAlt) { key_mod_flags |= ImGuiKeyModFlags_Alt; }
3714+
if (g.IO.KeySuper) { key_mod_flags |= ImGuiKeyModFlags_Super; }
3715+
return key_mod_flags;
3716+
}
3717+
37073718
void ImGui::NewFrame()
37083719
{
37093720
IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?");
@@ -3805,6 +3816,8 @@ void ImGui::NewFrame()
38053816
g.DragDropWithinTarget = false;
38063817

38073818
// Update keyboard input state
3819+
// Synchronize io.KeyMods with individual modifiers io.KeyXXX bools
3820+
g.IO.KeyMods = GetMergedKeyModFlags();
38083821
memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration));
38093822
for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++)
38103823
g.IO.KeysDownDuration[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownDuration[i] < 0.0f ? 0.0f : g.IO.KeysDownDuration[i] + g.IO.DeltaTime) : -1.0f;
@@ -6720,9 +6733,15 @@ bool ImGui::DebugCheckVersionAndDataLayout(const char* version, size_t sz_io, si
67206733

67216734
static void ImGui::ErrorCheckEndFrame()
67226735
{
6736+
ImGuiContext& g = *GImGui;
6737+
6738+
// Verify that io.KeyXXX fields haven't been tampered with. Key mods shoudl not be modified between NewFrame() and EndFrame()
6739+
const ImGuiKeyModFlags expected_key_mod_flags = GetMergedKeyModFlags();
6740+
IM_ASSERT(g.IO.KeyMods == expected_key_mod_flags && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods");
6741+
IM_UNUSED(expected_key_mod_flags);
6742+
67236743
// Report when there is a mismatch of Begin/BeginChild vs End/EndChild calls. Important: Remember that the Begin/BeginChild API requires you
67246744
// to always call End/EndChild even if Begin/BeginChild returns false! (this is unfortunately inconsistent with most other Begin* API).
6725-
ImGuiContext& g = *GImGui;
67266745
if (g.CurrentWindowStack.Size != 1)
67276746
{
67286747
if (g.CurrentWindowStack.Size > 1)

imgui.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ typedef int ImGuiDragDropFlags; // -> enum ImGuiDragDropFlags_ // Flags: f
168168
typedef int ImGuiFocusedFlags; // -> enum ImGuiFocusedFlags_ // Flags: for IsWindowFocused()
169169
typedef int ImGuiHoveredFlags; // -> enum ImGuiHoveredFlags_ // Flags: for IsItemHovered(), IsWindowHovered() etc.
170170
typedef int ImGuiInputTextFlags; // -> enum ImGuiInputTextFlags_ // Flags: for InputText(), InputTextMultiline()
171+
typedef int ImGuiKeyModFlags; // -> enum ImGuiKeyModFlags_ // Flags: for io.KeyMods (Ctrl/Shift/Alt/Super)
171172
typedef int ImGuiSelectableFlags; // -> enum ImGuiSelectableFlags_ // Flags: for Selectable()
172173
typedef int ImGuiTabBarFlags; // -> enum ImGuiTabBarFlags_ // Flags: for BeginTabBar()
173174
typedef int ImGuiTabItemFlags; // -> enum ImGuiTabItemFlags_ // Flags: for BeginTabItem()
@@ -997,6 +998,16 @@ enum ImGuiKey_
997998
ImGuiKey_COUNT
998999
};
9991000

1001+
// To test io.KeyMods (which is a combination of individual fields io.KeyCtrl, io.KeyShift, io.KeyAlt set by user/back-end)
1002+
enum ImGuiKeyModFlags_
1003+
{
1004+
ImGuiKeyModFlags_None = 0,
1005+
ImGuiKeyModFlags_Ctrl = 1 << 0,
1006+
ImGuiKeyModFlags_Shift = 1 << 1,
1007+
ImGuiKeyModFlags_Alt = 1 << 2,
1008+
ImGuiKeyModFlags_Super = 1 << 3
1009+
};
1010+
10001011
// Gamepad/Keyboard directional navigation
10011012
// Keyboard: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays.
10021013
// Gamepad: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. Back-end: set ImGuiBackendFlags_HasGamepad and fill the io.NavInputs[] fields before calling NewFrame(). Note that io.NavInputs[] is cleared by EndFrame().
@@ -1496,6 +1507,7 @@ struct ImGuiIO
14961507
// [Internal] Dear ImGui will maintain those fields. Forward compatibility not guaranteed!
14971508
//------------------------------------------------------------------
14981509

1510+
ImGuiKeyModFlags KeyMods; // Key mods flags (same as io.KeyCtrl/KeyShift/KeyAlt/KeySuper but merged into flags), updated by NewFrame()
14991511
ImVec2 MousePosPrev; // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid)
15001512
ImVec2 MouseClickedPos[5]; // Position at time of clicking
15011513
double MouseClickedTime[5]; // Time of last click (used to figure out double-click)

0 commit comments

Comments
 (0)