-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
158 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "imgui.h" | ||
#include <hello_imgui/hello_imgui.h> | ||
|
||
class AppState { | ||
}; | ||
|
||
void Gui(AppState& appState) { | ||
} | ||
|
||
int main() { | ||
AppState appState; | ||
auto gui_fn = [&]() { Gui(appState); }; | ||
HelloImGui::Run(gui_fn, "TEMPLATE", true); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Advices to layout your GUI | ||
|
||
* Widget labels need to be unique, so you can use a suffix to create a unique identifier for a widget by adding a suffix "##some_suffix", which will not be displayed : see [FAQ](https://github.com/ocornut/imgui/blob/master/docs/FAQ.md#q-about-the-id-stack-system) | ||
* You can use horizontal and vertical layouts to create columns and rows. | ||
* You can use the `imgui.set_next_item_with` function to set the width of the next widget. | ||
|
||
**Using em units** | ||
|
||
It is important to use em units (see https://en.wikipedia.org/wiki/Em_(typography) ), in order to create application whose layout adapts to the font size, and does not depend on the screen resolution and scaling. | ||
|
||
Hello ImGui provides several helper functions to convert between pixels and em units: | ||
|
||
*In Python* | ||
```python | ||
hello_imgui.em_to_vec2(em_width: float, em_height: float) -> ImVec2 | ||
hello_imgui.em_size(em: float = 1.0) -> float | ||
hello_imgui.pixels_to_em(px_width: float, px_height: float) -> ImVec2 | ||
``` | ||
|
||
*In C++* | ||
```cpp | ||
ImVec2 HelloImGui::EmToVec2(float em_width, float em_height); | ||
float HelloImGui::EmSize(float em = 1.0f); | ||
ImVec2 HelloImGui::PixelsToEm(float px_width, float px_height); | ||
``` | ||
The code below demonstrates these advices and create this application: | ||
 | ||
**Python** | ||
```{literalinclude} layout_advices.py | ||
``` | ||
|
||
|
||
**C++** | ||
```{literalinclude} layout_advices.cpp | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from imgui_bundle import imgui, hello_imgui, imgui_ctx | ||
|
||
|
||
class AppState: | ||
"""Our application state""" | ||
name: str = "" | ||
first_name: str = "" | ||
city: str = "" | ||
age: int = 0 | ||
|
||
|
||
def input_text_aligned(label: str, value: str, width_pixel: float) -> tuple[bool, str]: | ||
"""A helper function to create a label and an input text field in the same row, | ||
where the label is placed to the left, and the input field is right-aligned. | ||
It returns a tuple of (changed: bool, value: str), following the pattern of imgui.input_text. | ||
""" | ||
# We will use a horizontal layout to place the label and the input field in the same row | ||
# (by default, imgui places widgets vertically) | ||
with imgui_ctx.begin_horizontal(label): | ||
# Display the label on the left side | ||
imgui.text(label) | ||
# Add a spring, which will occupy all remaining space: | ||
# this will push the input field to the right | ||
imgui.spring() | ||
imgui.set_next_item_width(width_pixel) | ||
# Note: by default, imgui.input_text will place the label to the right of the input field. | ||
# We do not want that, so we will use "##" to hide the label. We still need to make it unique, | ||
# so we append the user provided label to it (which will not be displayed). | ||
changed, value = imgui.input_text("##" + label, value) | ||
return changed, value | ||
|
||
|
||
def gui(app_state: AppState): | ||
# Set the width of the widgets: we will use 10 em for most widgets | ||
widgets_width = hello_imgui.em_size(10) | ||
|
||
# Add some spacing at the top: we will skip 1 line of text | ||
imgui.dummy(hello_imgui.em_to_vec2(0, 1)) | ||
|
||
# Enclose the widgets in a vertical layout: this is important, so that | ||
# the inner horizontal layouts use their parent's layout width. | ||
with imgui_ctx.begin_vertical("main"): | ||
imgui.separator_text("Personal information") | ||
imgui.dummy(hello_imgui.em_to_vec2(20, 0)) | ||
_, app_state.name = input_text_aligned("Name", app_state.name, widgets_width) | ||
_, app_state.first_name = input_text_aligned("First name", app_state.first_name, widgets_width) | ||
_, app_state.city = input_text_aligned("City", app_state.city, widgets_width) | ||
|
||
# Add a slider to input the age: we enclose it in a horizontal layout | ||
with imgui_ctx.begin_horizontal("Age"): | ||
imgui.text("Age") | ||
# Add a spring to push the slider to the right | ||
imgui.spring() | ||
# Set the width of the slider to 10 em (by default, it would be the full width of the window) | ||
imgui.set_next_item_width(widgets_width) | ||
# use imgui.slider_int with a hidden label, and a range from 0 to 120 | ||
_, app_state.age = imgui.slider_int("##Age", app_state.age, 0, 120) | ||
|
||
|
||
def main(): | ||
app_state = AppState() | ||
hello_imgui.run(lambda: gui(app_state), window_size=(400, 200)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# What's next? | ||
|
||
- There are **many** other widgets in Dear ImGui, and Dear ImGui Bundle. Look at the next pages of this tutorial for more examples. | ||
- Look at [Dear ImGui Manual](https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.