mirror of
https://github.com/Drezil/imgui.git
synced 2024-11-14 17:07:01 +00:00
Comments
This commit is contained in:
parent
c461401b21
commit
77e234d9df
98
imgui.cpp
98
imgui.cpp
@ -11,9 +11,10 @@
|
||||
// If you work for a company, please consider financial support, see README. For individuals: https://www.patreon.com/imgui
|
||||
|
||||
// It is recommended that you don't modify imgui.cpp! It will become difficult for you to update the library.
|
||||
// Note that 'ImGui::' is a namespace and so you can add functions into it from your own source files without modifying imgui.h or imgui.cpp.
|
||||
// You may include imgui_internal.h to access internal data structures, but it doesn't come with any guarantee of forward compatibility.
|
||||
// Discussing your changes on the GitHub Issue Tracker may lead you to a better solution or official support for them.
|
||||
// Note that 'ImGui::' being a namespace, you can add functions into the namespace from your own source files, without
|
||||
// modifying imgui.h or imgui.cpp. You may include imgui_internal.h to access internal data structures, but it doesn't
|
||||
// come with any guarantee of forward compatibility. Discussing your changes on the GitHub Issue Tracker may lead you
|
||||
// to a better solution or official support for them.
|
||||
|
||||
/*
|
||||
|
||||
@ -53,8 +54,8 @@
|
||||
- Minimize setup and maintenance
|
||||
- Minimize state storage on user side
|
||||
- Portable, minimize dependencies, run on target (consoles, phones, etc.)
|
||||
- Efficient runtime and memory consumption (NB- we do allocate when "growing" content e.g. creating a window, opening a tree node
|
||||
for the first time, etc. but a typical frame won't allocate anything)
|
||||
- Efficient runtime and memory consumption (NB- we do allocate when "growing" content e.g. creating a window,
|
||||
opening a tree node for the first time, etc. but a typical frame should not allocate anything)
|
||||
|
||||
Designed for developers and content-creators, not the typical end-user! Some of the weaknesses includes:
|
||||
- Doesn't look fancy, doesn't animate
|
||||
@ -91,8 +92,8 @@
|
||||
READ FIRST
|
||||
|
||||
- Read the FAQ below this section!
|
||||
- Your code creates the UI, if your code doesn't run the UI is gone! == very dynamic UI, no construction/destructions steps, less data retention
|
||||
on your side, no state duplication, less sync, less bugs.
|
||||
- Your code creates the UI, if your code doesn't run the UI is gone! The UI can be highly dynamic, there are no construction
|
||||
or destruction steps, less data retention on your side, less state duplication, less state synchronization, less bugs.
|
||||
- Call and read ImGui::ShowDemoWindow() for demo code demonstrating most features.
|
||||
- You can learn about immediate-mode gui principles at http://www.johno.se/book/imgui.html or watch http://mollyrocket.com/861
|
||||
|
||||
@ -100,18 +101,18 @@
|
||||
|
||||
- Overwrite all the sources files except for imconfig.h (if you have made modification to your copy of imconfig.h)
|
||||
- Read the "API BREAKING CHANGES" section (below). This is where we list occasional API breaking changes.
|
||||
If a function/type has been renamed / or marked obsolete, try to fix the name in your code before it is permanently removed from the public API.
|
||||
If you have a problem with a missing function/symbols, search for its name in the code, there will likely be a comment about it.
|
||||
Please report any issue to the GitHub page!
|
||||
If a function/type has been renamed / or marked obsolete, try to fix the name in your code before it is permanently removed
|
||||
from the public API. If you have a problem with a missing function/symbols, search for its name in the code, there will
|
||||
likely be a comment about it. Please report any issue to the GitHub page!
|
||||
- Try to keep your copy of dear imgui reasonably up to date.
|
||||
|
||||
GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE
|
||||
|
||||
- Run and study the examples and demo to get acquainted with the library.
|
||||
- Add the Dear ImGui source files to your projects, using your preferred build system.
|
||||
It is recommended you build the .cpp files as part of your project and not as a library.
|
||||
- You can later customize the imconfig.h file to tweak some compilation time behavior, such as integrating imgui types with your own maths types.
|
||||
- See examples/ folder for standalone sample applications.
|
||||
- You may be able to grab and copy a ready made imgui_impl_*** file from the examples/.
|
||||
- You may be able to grab and copy a ready made imgui_impl_*** file from the examples/ folder.
|
||||
- When using Dear ImGui, your programming IDE is your friend: follow the declaration of variables, functions and types to find comments about them.
|
||||
|
||||
- Init: retrieve the ImGuiIO structure with ImGui::GetIO() and fill the fields marked 'Settings': at minimum you need to set io.DisplaySize
|
||||
@ -199,7 +200,8 @@
|
||||
MyEngineBindTexture(pcmd->TextureId);
|
||||
|
||||
// We are using scissoring to clip some objects. All low-level graphics API supports it.
|
||||
// If your engine doesn't support scissoring yet, you will get some small glitches (some elements outside their bounds) which you can fix later.
|
||||
// If your engine doesn't support scissoring yet, you may ignore this at first. You will get some small glitches
|
||||
// (some elements visible outside their bounds) but you can fix that once everywhere else works!
|
||||
MyEngineScissor((int)pcmd->ClipRect.x, (int)pcmd->ClipRect.y, (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
|
||||
|
||||
// Render 'pcmd->ElemCount/3' indexed triangles.
|
||||
@ -215,22 +217,25 @@
|
||||
- When calling NewFrame(), the 'io.WantCaptureMouse'/'io.WantCaptureKeyboard'/'io.WantTextInput' flags are updated.
|
||||
They tell you if ImGui intends to use your inputs. So for example, if 'io.WantCaptureMouse' is set you would typically want to hide
|
||||
mouse inputs from the rest of your application. Read the FAQ below for more information about those flags.
|
||||
- Please read the FAQ above. Amusingly, it is called a FAQ because people frequently have the same issues!
|
||||
|
||||
USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS [BETA]
|
||||
|
||||
- The gamepad/keyboard navigation is in Beta. Ask questions and report issues at https://github.com/ocornut/imgui/issues/787
|
||||
- The initial focus was to support game controllers, but keyboard is becoming increasingly and decently usable.
|
||||
- Keyboard:
|
||||
- Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. NewFrame() will automatically fill io.NavInputs[] based on your io.KeyDown[] + io.KeyMap[] arrays.
|
||||
- When keyboard navigation is active (io.NavActive + ImGuiConfigFlags_NavEnableKeyboard), the io.WantCaptureKeyboard flag will be set.
|
||||
For more advanced uses, you may want to read from:
|
||||
- Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable.
|
||||
NewFrame() will automatically fill io.NavInputs[] based on your io.KeyDown[] + io.KeyMap[] arrays.
|
||||
- When keyboard navigation is active (io.NavActive + ImGuiConfigFlags_NavEnableKeyboard), the io.WantCaptureKeyboard flag
|
||||
will be set. For more advanced uses, you may want to read from:
|
||||
- io.NavActive: true when a window is focused and it doesn't have the ImGuiWindowFlags_NoNavInputs flag set.
|
||||
- io.NavVisible: true when the navigation cursor is visible (and usually goes false when mouse is used).
|
||||
- or query focus information with e.g. IsWindowFocused(), IsItemFocused() etc. functions.
|
||||
Please reach out if you think the game vs navigation input sharing could be improved.
|
||||
- Gamepad:
|
||||
- Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable.
|
||||
- Backend: Set io.BackendFlags |= ImGuiBackendFlags_HasGamepad + fill the io.NavInputs[] fields before calling NewFrame(). Note that io.NavInputs[] is cleared by EndFrame().
|
||||
- Backend: Set io.BackendFlags |= ImGuiBackendFlags_HasGamepad + fill the io.NavInputs[] fields before calling NewFrame().
|
||||
Note that io.NavInputs[] is cleared by EndFrame().
|
||||
- See 'enum ImGuiNavInput_' in imgui.h for a description of inputs. For each entry of io.NavInputs[], set the following values:
|
||||
0.0f= not held. 1.0f= fully held. Pass intermediate 0.0f..1.0f values for analog triggers/sticks.
|
||||
- We uses a simple >0.0f test for activation testing, and won't attempt to test for a dead-zone.
|
||||
@ -444,14 +449,15 @@
|
||||
|
||||
- Elements that are typically not clickable, such as Text() items don't need an ID.
|
||||
|
||||
- Interactive widgets require state to be carried over multiple frames (most typically Dear ImGui often needs to remember what is
|
||||
the "active" widget). To do so they need a unique ID. Unique ID are typically derived from a string label, an integer index or a pointer.
|
||||
- Interactive widgets require state to be carried over multiple frames (most typically Dear ImGui
|
||||
often needs to remember what is the "active" widget). To do so they need a unique ID. Unique ID
|
||||
are typically derived from a string label, an integer index or a pointer.
|
||||
|
||||
Button("OK"); // Label = "OK", ID = top of id stack + hash of "OK"
|
||||
Button("Cancel"); // Label = "Cancel", ID = top of id stack + hash of "Cancel"
|
||||
|
||||
- ID are uniquely scoped within windows, tree nodes, etc. which all push to the ID stack. So having two buttons labeled "OK"
|
||||
in two different windows or in two different locations of a tree is fine.
|
||||
- ID are uniquely scoped within windows, tree nodes, etc. which all pushes to the ID stack. Having
|
||||
two buttons labeled "OK" in different windows or different tree locations is fine.
|
||||
|
||||
- If you have a same ID twice in the same location, you'll have a conflict:
|
||||
|
||||
@ -461,9 +467,10 @@
|
||||
Fear not! this is easy to solve and there are many ways to solve it!
|
||||
|
||||
- Solving ID conflict in a simple/local context:
|
||||
When passing a label you can optionally specify extra unique ID information within string itself.
|
||||
When passing a label you can optionally specify extra ID information within string itself.
|
||||
Use "##" to pass a complement to the ID that won't be visible to the end-user.
|
||||
This helps solving the simple collision cases when you know which items are going to be created.
|
||||
This helps solving the simple collision cases when you know e.g. at compilation time which items
|
||||
are going to be created:
|
||||
|
||||
Button("Play"); // Label = "Play", ID = top of id stack + hash of "Play"
|
||||
Button("Play##foo1"); // Label = "Play", ID = top of id stack + hash of "Play##foo1" (different from above)
|
||||
@ -473,9 +480,9 @@
|
||||
|
||||
Checkbox("##On", &b); // Label = "", ID = top of id stack + hash of "##On" (no label!)
|
||||
|
||||
- Occasionally/rarely you might want change a label while preserving a constant ID. This allows you to animate labels.
|
||||
For example you may want to include varying information in a window title bar, but windows are uniquely identified by their ID..
|
||||
Use "###" to pass a label that isn't part of ID:
|
||||
- Occasionally/rarely you might want change a label while preserving a constant ID. This allows
|
||||
you to animate labels. For example you may want to include varying information in a window title bar,
|
||||
but windows are uniquely identified by their ID. Use "###" to pass a label that isn't part of ID:
|
||||
|
||||
Button("Hello###ID"; // Label = "Hello", ID = top of id stack + hash of "ID"
|
||||
Button("World###ID"; // Label = "World", ID = top of id stack + hash of "ID" (same as above)
|
||||
@ -484,8 +491,9 @@
|
||||
Begin(buf); // Variable label, ID = hash of "MyGame"
|
||||
|
||||
- Solving ID conflict in a more general manner:
|
||||
Use PushID() / PopID() to create scopes and manipulate the ID stack, as to avoid ID conflicts within the same Window.
|
||||
This is the most convenient way of distinguishing ID if you are iterating and creating many UI elements.
|
||||
Use PushID() / PopID() to create scopes and manipulate the ID stack, as to avoid ID conflicts
|
||||
within the same window. This is the most convenient way of distinguishing ID when iterating and
|
||||
creating many UI elements programmatically.
|
||||
You can push a pointer, a string or an integer value into the ID stack.
|
||||
Remember that ID are formed from the concatenation of _everything_ in the ID stack!
|
||||
|
||||
@ -533,25 +541,28 @@
|
||||
|
||||
- When working with trees, ID are used to preserve the open/close state of each tree node.
|
||||
Depending on your use cases you may want to use strings, indices or pointers as ID.
|
||||
e.g. when following a single pointer that may change over time, using a static string as ID will preserve your
|
||||
node open/closed state when the targeted object change.
|
||||
e.g. when displaying a list of objects, using indices or pointers as ID will preserve the node open/closed state differently.
|
||||
experiment and see what makes more sense in your situation!
|
||||
e.g. when following a single pointer that may change over time, using a static string as ID
|
||||
will preserve your node open/closed state when the targeted object change.
|
||||
e.g. when displaying a list of objects, using indices or pointers as ID will preserve the
|
||||
node open/closed state differently. See what makes more sense in your situation!
|
||||
|
||||
Q: How can I load a different font than the default? (default is an embedded version of ProggyClean.ttf, rendered at size 13)
|
||||
Q: How can I load a different font than the default?
|
||||
A: Use the font atlas to load the TTF/OTF file you want:
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels);
|
||||
io.Fonts->GetTexDataAsRGBA32() or GetTexDataAsAlpha8()
|
||||
(default is ProggyClean.ttf, rendered at size 13, embedded in dear imgui's source code)
|
||||
|
||||
New programmers: remember that in C/C++ and most programming languages if you want to use a backslash \ in a string literal you need to write a double backslash "\\":
|
||||
io.Fonts->AddFontFromFileTTF("MyDataFolder\MyFontFile.ttf", size_in_pixels); // WRONG
|
||||
New programmers: remember that in C/C++ and most programming languages if you want to use a
|
||||
backslash \ within a string literal, you need to write it double backslash "\\":
|
||||
io.Fonts->AddFontFromFileTTF("MyDataFolder\MyFontFile.ttf", size_in_pixels); // WRONG (you are escape the M here!)
|
||||
io.Fonts->AddFontFromFileTTF("MyDataFolder\\MyFontFile.ttf", size_in_pixels); // CORRECT
|
||||
io.Fonts->AddFontFromFileTTF("MyDataFolder/MyFontFile.ttf", size_in_pixels); // ALSO CORRECT
|
||||
|
||||
Q: How can I easily use icons in my application?
|
||||
A: The most convenient and practical way is to merge an icon font such as FontAwesome inside you main font. Then you can refer to icons within your
|
||||
strings. Read 'How can I load multiple fonts?' and the file 'misc/fonts/README.txt' for instructions and useful header files.
|
||||
A: The most convenient and practical way is to merge an icon font such as FontAwesome inside you
|
||||
main font. Then you can refer to icons within your strings. Read 'How can I load multiple fonts?'
|
||||
and the file 'misc/fonts/README.txt' for instructions and useful header files.
|
||||
|
||||
Q: How can I load multiple fonts?
|
||||
A: Use the font atlas to pack them into a single texture:
|
||||
@ -596,13 +607,16 @@
|
||||
builder.BuildRanges(&ranges); // Build the final result (ordered ranges with all the unique characters submitted)
|
||||
io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels, NULL, ranges.Data);
|
||||
|
||||
All your strings needs to use UTF-8 encoding. In C++11 you can encode a string literal in UTF-8 by using the u8"hello" syntax.
|
||||
Specifying literal in your source code using a local code page (such as CP-923 for Japanese or CP-1251 for Cyrillic) will NOT work!
|
||||
All your strings needs to use UTF-8 encoding. In C++11 you can encode a string literal in UTF-8
|
||||
by using the u8"hello" syntax. Specifying literal in your source code using a local code page
|
||||
(such as CP-923 for Japanese or CP-1251 for Cyrillic) will NOT work!
|
||||
Otherwise you can convert yourself to UTF-8 or load text data from file already saved as UTF-8.
|
||||
|
||||
Text input: it is up to your application to pass the right character code to io.AddInputCharacter(). The applications in examples/ are doing that.
|
||||
For languages using IME, on Windows you can copy the Hwnd of your application to io.ImeWindowHandle.
|
||||
The default implementation of io.ImeSetInputScreenPosFn() on Windows will set your IME position correctly.
|
||||
Text input: it is up to your application to pass the right character code by calling
|
||||
io.AddInputCharacter(). The applications in examples/ are doing that. For languages relying
|
||||
on an Input Method Editor (IME), on Windows you can copy the Hwnd of your application in the
|
||||
io.ImeWindowHandle field. The default implementation of io.ImeSetInputScreenPosFn() will set
|
||||
your Microsoft IME position correctly.
|
||||
|
||||
Q: How can I use the drawing facilities without an ImGui window? (using ImDrawList API)
|
||||
A: - You can create a dummy window. Call SetNextWindowBgAlpha(0.0f), call Begin() with NoTitleBar|NoResize|NoMove|NoScrollbar|NoSavedSettings|NoInputs flags.
|
||||
|
Loading…
Reference in New Issue
Block a user