Skip to content

Commit b7e1b13

Browse files
committed
Update docs, FAQ, comments (mainly related to io.WantCaptureMouse / WantCaptureKeyboard flags).
1 parent 11116ee commit b7e1b13

File tree

4 files changed

+38
-26
lines changed

4 files changed

+38
-26
lines changed

docs/FAQ.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ e.g. `if (ImGui::GetIO().WantCaptureMouse) { ... }`
9696
**Note:** You should always pass your mouse/keyboard inputs to Dear ImGui, even when the io.WantCaptureXXX flag are set false.
9797
This is because imgui needs to detect that you clicked in the void to unfocus its own windows.
9898

99-
**Note:** The `io.WantCaptureMouse` is more accurate that any manual attempt to "check if the mouse is hovering a window" (don't do that!). It handle mouse dragging correctly (both dragging that started over your application or over an imgui window) and handle e.g. modal windows blocking inputs. Those flags are updated by `ImGui::NewFrame()`. Preferably read the flags after calling NewFrame() if you can afford it, but reading them before is also perfectly fine, as the bool toggle fairly rarely. If you have on a touch device, you might find use for an early call to `UpdateHoveredWindowAndCaptureFlags()`.
99+
**Note:** The `io.WantCaptureMouse` is more correct that any manual attempt to "check if the mouse is hovering a window" (don't do that!). It handle mouse dragging correctly (both dragging that started over your application or over a Dear ImGui window) and handle e.g. popup and modal windows blocking inputs.
100100

101-
**Note:** Text input widget releases focus on "Return KeyDown", so the subsequent "Return KeyUp" event that your application receive will typically have `io.WantCaptureKeyboard == false`. Depending on your application logic it may or not be inconvenient. You might want to track which key-downs were targeted for Dear ImGui, e.g. with an array of bool, and filter out the corresponding key-ups.)
101+
**Note:** Those flags are updated by `ImGui::NewFrame()`. However it is generally more correct and easier that you poll flags from the previous frame, then submit your inputs, then call `NewFrame()`. If you attempt to do the opposite (which is generally harder) you are likely going to submit your inputs after `NewFrame()`, and therefore too late.
102+
103+
**Note:** If you are using a touch device, you may find use for an early call to `UpdateHoveredWindowAndCaptureFlags()` to correctly dispatch your initial touch. We will work on better out-of-the-box touch support in the future.
104+
105+
**Note:** Text input widget releases focus on the "KeyDown" event of the Return key, so the subsequent "KeyUp" event that your application receive will typically have `io.WantCaptureKeyboard == false`. Depending on your application logic it may or not be inconvenient to receive that KeyUp event. You might want to track which key-downs were targeted for Dear ImGui, e.g. with an array of bool, and filter out the corresponding key-ups.)
102106

103107
##### [Return to Index](#index)
104108

imgui.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ DOCUMENTATION
4040
- READ FIRST
4141
- HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI
4242
- GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE
43-
- HOW A SIMPLE APPLICATION MAY LOOK LIKE (2 variations)
43+
- HOW A SIMPLE APPLICATION MAY LOOK LIKE
4444
- HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE
4545
- USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS
4646
- API BREAKING CHANGES (read me when you update!)
@@ -139,7 +139,7 @@ CODE
139139
- Call and read ImGui::ShowDemoWindow() for demo code demonstrating most features.
140140
- The library is designed to be built from sources. Avoid pre-compiled binaries and packaged versions. See imconfig.h to configure your build.
141141
- Dear ImGui is an implementation of the IMGUI paradigm (immediate-mode graphical user interface, a term coined by Casey Muratori).
142-
You can learn about IMGUI principles at http://www.johno.se/book/imgui.html, http://mollyrocket.com/861 & more links docs/README.md.
142+
You can learn about IMGUI principles at http://www.johno.se/book/imgui.html, http://mollyrocket.com/861 & more links in the FAQ.
143143
- Dear ImGui is a "single pass" rasterizing implementation of the IMGUI paradigm, aimed at ease of use and high-performances.
144144
For every application frame your UI code will be called only once. This is in contrast to e.g. Unity's own implementation of an IMGUI,
145145
where the UI code is called multiple times ("multiple passes") from a single entry point. There are pros and cons to both approaches.
@@ -153,6 +153,7 @@ CODE
153153
However, imgui_internal.h can optionally export math operators for ImVec2/ImVec4, which we use in this codebase.
154154
- C++: pay attention that ImVector<> manipulates plain-old-data and does not honor construction/destruction (avoid using it in your code!).
155155
156+
156157
HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI
157158
----------------------------------------------
158159
- Overwrite all the sources files except for imconfig.h (if you have made modification to your copy of imconfig.h)
@@ -163,6 +164,7 @@ CODE
163164
likely be a comment about it. Please report any issue to the GitHub page!
164165
- Try to keep your copy of dear imgui reasonably up to date.
165166
167+
166168
GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE
167169
---------------------------------------------------------------
168170
- Run and study the examples and demo in imgui_demo.cpp to get acquainted with the library.
@@ -177,9 +179,11 @@ CODE
177179
- Refer to the bindings and demo applications in the examples/ folder for instruction on how to setup your code.
178180
- If you are running over a standard OS with a common graphics API, you should be able to use unmodified imgui_impl_*** files from the examples/ folder.
179181
182+
180183
HOW A SIMPLE APPLICATION MAY LOOK LIKE
181184
--------------------------------------
182-
EXHIBIT 1: USING THE EXAMPLE BINDINGS (imgui_impl_XXX.cpp files from the examples/ folder).
185+
EXHIBIT 1: USING THE EXAMPLE BINDINGS (= imgui_impl_XXX.cpp files from the examples/ folder).
186+
The sub-folders in examples/ contains examples applications following this structure.
183187
184188
// Application init: create a dear imgui context, setup some options, load fonts
185189
ImGui::CreateContext();
@@ -268,8 +272,15 @@ CODE
268272
// Shutdown
269273
ImGui::DestroyContext();
270274
275+
To decide whether to dispatch mouse/keyboard inputs to Dear ImGui to the rest your application,
276+
you should read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags!
277+
Please read the FAQ and example applications for details about this!
278+
279+
271280
HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE
272281
---------------------------------------------
282+
The bindings in impl_impl_XXX.cpp files contains many working implementations of a rendering function.
283+
273284
void void MyImGuiRenderFunction(ImDrawData* draw_data)
274285
{
275286
// TODO: Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled
@@ -314,11 +325,6 @@ CODE
314325
}
315326
}
316327
317-
- The examples/ folders contains many actual implementation of the pseudo-codes above.
318-
- When calling NewFrame(), the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags are updated.
319-
They tell you if Dear ImGui intends to use your inputs. When a flag is set you want to hide the corresponding inputs from the
320-
rest of your application. In every cases you need to pass on the inputs to Dear ImGui.
321-
- Refer to the FAQ for more information. Amusingly, it is called a FAQ because people frequently run into the same issues!
322328
323329
USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS
324330
------------------------------------------
@@ -3797,7 +3803,7 @@ void ImGui::NewFrame()
37973803
for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++)
37983804
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;
37993805

3800-
// Update gamepad/keyboard directional navigation
3806+
// Update gamepad/keyboard navigation
38013807
NavUpdate();
38023808

38033809
// Update mouse input state
@@ -7982,7 +7988,7 @@ static void inline NavClampRectToVisibleAreaForMoveDir(ImGuiDir move_dir, ImRect
79827988
}
79837989
}
79847990

7985-
// Scoring function for directional navigation. Based on https://gist.github.com/rygorous/6981057
7991+
// Scoring function for gamepad/keyboard directional navigation. Based on https://gist.github.com/rygorous/6981057
79867992
static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand)
79877993
{
79887994
ImGuiContext& g = *GImGui;

imgui.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -910,12 +910,12 @@ enum ImGuiFocusedFlags_
910910
ImGuiFocusedFlags_None = 0,
911911
ImGuiFocusedFlags_ChildWindows = 1 << 0, // IsWindowFocused(): Return true if any children of the window is focused
912912
ImGuiFocusedFlags_RootWindow = 1 << 1, // IsWindowFocused(): Test from root window (top most parent of the current hierarchy)
913-
ImGuiFocusedFlags_AnyWindow = 1 << 2, // IsWindowFocused(): Return true if any window is focused. Important: If you are trying to tell how to dispatch your low-level inputs, do NOT use this. Use ImGui::GetIO().WantCaptureMouse instead.
913+
ImGuiFocusedFlags_AnyWindow = 1 << 2, // IsWindowFocused(): Return true if any window is focused. Important: If you are trying to tell how to dispatch your low-level inputs, do NOT use this. Use 'io.WantCaptureMouse' instead! Please read the FAQ!
914914
ImGuiFocusedFlags_RootAndChildWindows = ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows
915915
};
916916

917917
// Flags for ImGui::IsItemHovered(), ImGui::IsWindowHovered()
918-
// Note: if you are trying to check whether your mouse should be dispatched to imgui or to your app, you should use the 'io.WantCaptureMouse' boolean for that. Please read the FAQ!
918+
// Note: if you are trying to check whether your mouse should be dispatched to Dear ImGui or to your app, you should use 'io.WantCaptureMouse' instead! Please read the FAQ!
919919
// Note: windows with the ImGuiWindowFlags_NoInputs flag are ignored by IsWindowHovered() calls.
920920
enum ImGuiHoveredFlags_
921921
{
@@ -1019,7 +1019,7 @@ enum ImGuiKeyModFlags_
10191019
ImGuiKeyModFlags_Super = 1 << 3
10201020
};
10211021

1022-
// Gamepad/Keyboard directional navigation
1022+
// Gamepad/Keyboard navigation
10231023
// Keyboard: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays.
10241024
// 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().
10251025
// Read instructions in imgui.cpp for more details. Download PNG/PSD at http://goo.gl/9LgVZW.
@@ -1500,17 +1500,19 @@ struct ImGuiIO
15001500
IMGUI_API void ClearInputCharacters(); // Clear the text input buffer manually
15011501

15021502
//------------------------------------------------------------------
1503-
// Output - Retrieve after calling NewFrame()
1503+
// Output - Updated by NewFrame() or EndFrame()/Render()
1504+
// (when reading from the io.WantCaptureMouse, io.WantCaptureKeyboard flags to dispatch your inputs, it is
1505+
// generally easier and more correct to use their state BEFORE calling NewFrame(). See FAQ for details!)
15041506
//------------------------------------------------------------------
15051507

1506-
bool WantCaptureMouse; // When io.WantCaptureMouse is true, imgui will use the mouse inputs, do not dispatch them to your main game/application (in both cases, always pass on mouse inputs to imgui). (e.g. unclicked mouse is hovering over an imgui window, widget is active, mouse was clicked over an imgui window, etc.).
1507-
bool WantCaptureKeyboard; // When io.WantCaptureKeyboard is true, imgui will use the keyboard inputs, do not dispatch them to your main game/application (in both cases, always pass keyboard inputs to imgui). (e.g. InputText active, or an imgui window is focused and navigation is enabled, etc.).
1508-
bool WantTextInput; // Mobile/console: when io.WantTextInput is true, you may display an on-screen keyboard. This is set by ImGui when it wants textual keyboard input to happen (e.g. when a InputText widget is active).
1509-
bool WantSetMousePos; // MousePos has been altered, back-end should reposition mouse on next frame. Set only when ImGuiConfigFlags_NavEnableSetMousePos flag is enabled.
1510-
bool WantSaveIniSettings; // When manual .ini load/save is active (io.IniFilename == NULL), this will be set to notify your application that you can call SaveIniSettingsToMemory() and save yourself. IMPORTANT: You need to clear io.WantSaveIniSettings yourself.
1511-
bool NavActive; // Directional navigation is currently allowed (will handle ImGuiKey_NavXXX events) = a window is focused and it doesn't use the ImGuiWindowFlags_NoNavInputs flag.
1512-
bool NavVisible; // Directional navigation is visible and allowed (will handle ImGuiKey_NavXXX events).
1513-
float Framerate; // Application framerate estimation, in frame per second. Solely for convenience. Rolling average estimation based on IO.DeltaTime over 120 frames
1508+
bool WantCaptureMouse; // Set when Dear ImGui will use mouse inputs, in this case do not dispatch them to your main game/application (either way, always pass on mouse inputs to imgui). (e.g. unclicked mouse is hovering over an imgui window, widget is active, mouse was clicked over an imgui window, etc.).
1509+
bool WantCaptureKeyboard; // Set when Dear ImGui will use keyboard inputs, in this case do not dispatch them to your main game/application (either way, always pass keyboard inputs to imgui). (e.g. InputText active, or an imgui window is focused and navigation is enabled, etc.).
1510+
bool WantTextInput; // Mobile/console: when set, you may display an on-screen keyboard. This is set by Dear ImGui when it wants textual keyboard input to happen (e.g. when a InputText widget is active).
1511+
bool WantSetMousePos; // MousePos has been altered, back-end should reposition mouse on next frame. Rarely used! Set only when ImGuiConfigFlags_NavEnableSetMousePos flag is enabled.
1512+
bool WantSaveIniSettings; // When manual .ini load/save is active (io.IniFilename == NULL), this will be set to notify your application that you can call SaveIniSettingsToMemory() and save yourself. Important: clear io.WantSaveIniSettings yourself after saving!
1513+
bool NavActive; // Keyboard/Gamepad navigation is currently allowed (will handle ImGuiKey_NavXXX events) = a window is focused and it doesn't use the ImGuiWindowFlags_NoNavInputs flag.
1514+
bool NavVisible; // Keyboard/Gamepad navigation is visible and allowed (will handle ImGuiKey_NavXXX events).
1515+
float Framerate; // Application framerate estimate, in frame per second. Solely for convenience. Rolling average estimation based on io.DeltaTime over 120 frames.
15141516
int MetricsRenderVertices; // Vertices output during last call to Render()
15151517
int MetricsRenderIndices; // Indices output during last call to Render() = number of triangles * 3
15161518
int MetricsRenderWindows; // Number of visible windows

imgui_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ struct ImGuiGroupData; // Stacked storage data for BeginGroup()/End
8686
struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box
8787
struct ImGuiItemHoveredDataBackup; // Backup and restore IsItemHovered() internal data
8888
struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only
89-
struct ImGuiNavMoveResult; // Result of a directional navigation move query result
89+
struct ImGuiNavMoveResult; // Result of a gamepad/keyboard directional navigation move query result
9090
struct ImGuiNextWindowData; // Storage for SetNextWindow** functions
9191
struct ImGuiNextItemData; // Storage for SetNextItem** functions
9292
struct ImGuiPopupData; // Storage for current popup stack
@@ -1062,7 +1062,7 @@ struct ImGuiContext
10621062
bool ActiveIdHasBeenPressedBefore; // Track whether the active id led to a press (this is to allow changing between PressOnClick and PressOnRelease without pressing twice). Used by range_select branch.
10631063
bool ActiveIdHasBeenEditedBefore; // Was the value associated to the widget Edited over the course of the Active state.
10641064
bool ActiveIdHasBeenEditedThisFrame;
1065-
ImU32 ActiveIdUsingNavDirMask; // Active widget will want to read those directional navigation requests (e.g. can activate a button and move away from it)
1065+
ImU32 ActiveIdUsingNavDirMask; // Active widget will want to read those nav move requests (e.g. can activate a button and move away from it)
10661066
ImU32 ActiveIdUsingNavInputMask; // Active widget will want to read those nav inputs.
10671067
ImU64 ActiveIdUsingKeyInputMask; // Active widget will want to read those key inputs. When we grow the ImGuiKey enum we'll need to either to order the enum to make useful keys come first, either redesign this into e.g. a small array.
10681068
ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior)

0 commit comments

Comments
 (0)