Skip to content

Commit 9a15730

Browse files
committed
Demo: better showcase use of SetNextItemAllowOverlap(). (ocornut#6574, ocornut#6512, ocornut#3909, ocornut#517)
+ Merge some shallow changes from range-select branch.
1 parent 3fe4319 commit 9a15730

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

docs/CHANGELOG.txt

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Breaking changes:
3838

3939
Other changes:
4040

41+
- Demo: Better showcase use of SetNextItemAllowOverlap(). (#6574, #6512, #3909, #517)
42+
4143

4244
-----------------------------------------------------------------------
4345
VERSION 1.89.7 (Released 2023-07-04)

imgui_demo.cpp

+46-14
Original file line numberDiff line numberDiff line change
@@ -1262,16 +1262,16 @@ static void ShowDemoWindowWidgets()
12621262
IMGUI_DEMO_MARKER("Widgets/Selectables/Basic");
12631263
if (ImGui::TreeNode("Basic"))
12641264
{
1265-
static bool selection[5] = { false, true, false, false, false };
1265+
static bool selection[5] = { false, true, false, false };
12661266
ImGui::Selectable("1. I am selectable", &selection[0]);
12671267
ImGui::Selectable("2. I am selectable", &selection[1]);
1268-
ImGui::Text("(I am not selectable)");
1269-
ImGui::Selectable("4. I am selectable", &selection[3]);
1270-
if (ImGui::Selectable("5. I am double clickable", selection[4], ImGuiSelectableFlags_AllowDoubleClick))
1268+
ImGui::Selectable("3. I am selectable", &selection[2]);
1269+
if (ImGui::Selectable("4. I am double clickable", selection[3], ImGuiSelectableFlags_AllowDoubleClick))
12711270
if (ImGui::IsMouseDoubleClicked(0))
1272-
selection[4] = !selection[4];
1271+
selection[3] = !selection[3];
12731272
ImGui::TreePop();
12741273
}
1274+
12751275
IMGUI_DEMO_MARKER("Widgets/Selectables/Single Selection");
12761276
if (ImGui::TreeNode("Selection State: Single Selection"))
12771277
{
@@ -1303,17 +1303,18 @@ static void ShowDemoWindowWidgets()
13031303
}
13041304
ImGui::TreePop();
13051305
}
1306-
IMGUI_DEMO_MARKER("Widgets/Selectables/Rendering more text into the same line");
1307-
if (ImGui::TreeNode("Rendering more text into the same line"))
1306+
IMGUI_DEMO_MARKER("Widgets/Selectables/Rendering more items on the same line");
1307+
if (ImGui::TreeNode("Rendering more items on the same line"))
13081308
{
1309-
// Using the Selectable() override that takes "bool* p_selected" parameter,
1310-
// this function toggle your bool value automatically.
1309+
// (1) Using SetNextItemAllowOverlap()
1310+
// (2) Using the Selectable() override that takes "bool* p_selected" parameter, the bool value is toggled automatically.
13111311
static bool selected[3] = { false, false, false };
1312-
ImGui::Selectable("main.c", &selected[0]); ImGui::SameLine(300); ImGui::Text(" 2,345 bytes");
1313-
ImGui::Selectable("Hello.cpp", &selected[1]); ImGui::SameLine(300); ImGui::Text("12,345 bytes");
1314-
ImGui::Selectable("Hello.h", &selected[2]); ImGui::SameLine(300); ImGui::Text(" 2,345 bytes");
1312+
ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("main.c", &selected[0]); ImGui::SameLine(); ImGui::SmallButton("Link 1");
1313+
ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("Hello.cpp", &selected[1]); ImGui::SameLine(); ImGui::SmallButton("Link 2");
1314+
ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("Hello.h", &selected[2]); ImGui::SameLine(); ImGui::SmallButton("Link 3");
13151315
ImGui::TreePop();
13161316
}
1317+
13171318
IMGUI_DEMO_MARKER("Widgets/Selectables/In columns");
13181319
if (ImGui::TreeNode("In columns"))
13191320
{
@@ -1349,6 +1350,7 @@ static void ShowDemoWindowWidgets()
13491350
}
13501351
ImGui::TreePop();
13511352
}
1353+
13521354
IMGUI_DEMO_MARKER("Widgets/Selectables/Grid");
13531355
if (ImGui::TreeNode("Grid"))
13541356
{
@@ -2795,11 +2797,11 @@ static void ShowDemoWindowLayout()
27952797
// Text
27962798
IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout/SameLine");
27972799
ImGui::Text("Two items: Hello"); ImGui::SameLine();
2798-
ImGui::TextColored(ImVec4(1,1,0,1), "Sailor");
2800+
ImGui::TextColored(ImVec4(1, 1, 0, 1), "Sailor");
27992801

28002802
// Adjust spacing
28012803
ImGui::Text("More spacing: Hello"); ImGui::SameLine(0, 20);
2802-
ImGui::TextColored(ImVec4(1,1,0,1), "Sailor");
2804+
ImGui::TextColored(ImVec4(1, 1, 0, 1), "Sailor");
28032805

28042806
// Button
28052807
ImGui::AlignTextToFramePadding();
@@ -3397,6 +3399,36 @@ static void ShowDemoWindowLayout()
33973399

33983400
ImGui::TreePop();
33993401
}
3402+
3403+
IMGUI_DEMO_MARKER("Layout/Overlap Mode");
3404+
if (ImGui::TreeNode("Overlap Mode"))
3405+
{
3406+
static bool enable_allow_overlap = true;
3407+
3408+
HelpMarker(
3409+
"Hit-testing is by default performed in item submission order, which generally is perceived as 'back-to-front'.\n\n"
3410+
"By using SetNextItemAllowOverlap() you can notify that an item may be overlapped by another. Doing so alters the hovering logic: items using AllowOverlap mode requires an extra frame to accept hovered state.");
3411+
ImGui::Checkbox("Enable AllowOverlap", &enable_allow_overlap);
3412+
3413+
ImVec2 button1_pos = ImGui::GetCursorScreenPos();
3414+
ImVec2 button2_pos = ImVec2(button1_pos.x + 50.0f, button1_pos.y + 50.0f);
3415+
if (enable_allow_overlap)
3416+
ImGui::SetNextItemAllowOverlap();
3417+
ImGui::Button("Button 1", ImVec2(80, 80));
3418+
ImGui::SetCursorScreenPos(button2_pos);
3419+
ImGui::Button("Button 2", ImVec2(80, 80));
3420+
3421+
// This is typically used with width-spanning items.
3422+
// (note that Selectable() has a dedicated flag ImGuiSelectableFlags_AllowOverlap, which is a shortcut
3423+
// for using SetNextItemAllowOverlap(). For demo purpose we use SetNextItemAllowOverlap() here.)
3424+
if (enable_allow_overlap)
3425+
ImGui::SetNextItemAllowOverlap();
3426+
ImGui::Selectable("Some Selectable", false);
3427+
ImGui::SameLine();
3428+
ImGui::SmallButton("++");
3429+
3430+
ImGui::TreePop();
3431+
}
34003432
}
34013433

34023434
static void ShowDemoWindowPopups()

0 commit comments

Comments
 (0)