Demo: improved Selectable() demos. (#9193)

This commit is contained in:
ocornut
2026-01-26 11:37:55 +01:00
parent c91bcea7a8
commit d12b1a938e
4 changed files with 47 additions and 11 deletions
+2
View File
@@ -198,6 +198,8 @@ Other Changes:
- Debug Log: fixed incorrectly printing characters in IO log when submitting - Debug Log: fixed incorrectly printing characters in IO log when submitting
non-ASCII values to `io.AddInputCharacter()`. (#9099) non-ASCII values to `io.AddInputCharacter()`. (#9099)
- Debug Log: can output to debugger on Windows. (#5855) - Debug Log: can output to debugger on Windows. (#5855)
- Demo:
- Slightly improve Selectable() demos. (#9193)
- Backends: - Backends:
- DirectX10: added `SamplerNearest` in `ImGui_ImplDX10_RenderState`. - DirectX10: added `SamplerNearest` in `ImGui_ImplDX10_RenderState`.
(+renamed `SamplerDefault` to `SamplerLinear`, which was tagged as beta API) (+renamed `SamplerDefault` to `SamplerLinear`, which was tagged as beta API)
+1 -1
View File
@@ -1880,7 +1880,7 @@ enum ImGuiColorEditFlags_
ImGuiColorEditFlags_NoTooltip = 1 << 6, // // ColorEdit, ColorPicker, ColorButton: disable tooltip when hovering the preview. ImGuiColorEditFlags_NoTooltip = 1 << 6, // // ColorEdit, ColorPicker, ColorButton: disable tooltip when hovering the preview.
ImGuiColorEditFlags_NoLabel = 1 << 7, // // ColorEdit, ColorPicker: disable display of inline text label (the label is still forwarded to the tooltip and picker). ImGuiColorEditFlags_NoLabel = 1 << 7, // // ColorEdit, ColorPicker: disable display of inline text label (the label is still forwarded to the tooltip and picker).
ImGuiColorEditFlags_NoSidePreview = 1 << 8, // // ColorPicker: disable bigger color preview on right side of the picker, use small color square preview instead. ImGuiColorEditFlags_NoSidePreview = 1 << 8, // // ColorPicker: disable bigger color preview on right side of the picker, use small color square preview instead.
ImGuiColorEditFlags_NoDragDrop = 1 << 9, // // ColorEdit: disable drag and drop target. ColorButton: disable drag and drop source. ImGuiColorEditFlags_NoDragDrop = 1 << 9, // // ColorEdit: disable drag and drop target/source. ColorButton: disable drag and drop source.
ImGuiColorEditFlags_NoBorder = 1 << 10, // // ColorButton: disable border (which is enforced by default) ImGuiColorEditFlags_NoBorder = 1 << 10, // // ColorButton: disable border (which is enforced by default)
ImGuiColorEditFlags_NoColorMarkers = 1 << 11, // // ColorEdit: disable rendering R/G/B/A color marker. May also be disabled globally by setting style.ColorMarkerSize = 0. ImGuiColorEditFlags_NoColorMarkers = 1 << 11, // // ColorEdit: disable rendering R/G/B/A color marker. May also be disabled globally by setting style.ColorMarkerSize = 0.
+43 -10
View File
@@ -2309,15 +2309,45 @@ static void DemoWindowWidgetsSelectables()
ImGui::TreePop(); ImGui::TreePop();
} }
IMGUI_DEMO_MARKER("Widgets/Selectables/Rendering more items on the same line"); IMGUI_DEMO_MARKER("Widgets/Selectables/Multiple items on the same line");
if (ImGui::TreeNode("Rendering more items on the same line")) if (ImGui::TreeNode("Multiple items on the same line"))
{ {
// (1) Using SetNextItemAllowOverlap() // (1)
// (2) Using the Selectable() override that takes "bool* p_selected" parameter, the bool value is toggled automatically. // - Using SetNextItemAllowOverlap()
static bool selected[3] = { false, false, false }; // - Using the Selectable() override that takes "bool* p_selected" parameter, the bool value is toggled automatically.
ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("main.c", &selected[0]); ImGui::SameLine(); ImGui::SmallButton("Link 1"); {
ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("Hello.cpp", &selected[1]); ImGui::SameLine(); ImGui::SmallButton("Link 2"); static bool selected[3] = {};
ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("Hello.h", &selected[2]); ImGui::SameLine(); ImGui::SmallButton("Link 3"); ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("main.c", &selected[0]); ImGui::SameLine(); ImGui::SmallButton("Link 1");
ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("hello.cpp", &selected[1]); ImGui::SameLine(); ImGui::SmallButton("Link 2");
ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("hello.h", &selected[2]); ImGui::SameLine(); ImGui::SmallButton("Link 3");
}
// (2)
// - Using ImGuiSelectableFlags_AllowOverlap is a shortcut for calling SetNextItemAllowOverlap()
// - No visible label, display contents inside the selectable bounds.
// - We don't maintain actual selection in this example to keep things simple.
ImGui::Spacing();
{
static bool checked[5] = {};
static int selected_n = 0;
const float color_marker_w = ImGui::CalcTextSize("x").x;
for (int n = 0; n < 5; n++)
{
ImGui::PushID(n);
ImGui::AlignTextToFramePadding();
if (ImGui::Selectable("##selectable", selected_n == n, ImGuiSelectableFlags_AllowOverlap))
selected_n = n;
ImGui::SameLine(0, 0);
ImGui::Checkbox("##check", &checked[n]);
ImGui::SameLine();
ImVec4 color((n & 1) ? 1.0f : 0.2f, (n & 2) ? 1.0f : 0.2f, 0.2f, 1.0f);
ImGui::ColorButton("##color", color, ImGuiColorEditFlags_NoTooltip, ImVec2(color_marker_w, 0));
ImGui::SameLine();
ImGui::Text("Some label");
ImGui::PopID();
}
}
ImGui::TreePop(); ImGui::TreePop();
} }
@@ -2368,13 +2398,14 @@ static void DemoWindowWidgetsSelectables()
if (winning_state) if (winning_state)
ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, ImVec2(0.5f + 0.5f * cosf(time * 2.0f), 0.5f + 0.5f * sinf(time * 3.0f))); ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, ImVec2(0.5f + 0.5f * cosf(time * 2.0f), 0.5f + 0.5f * sinf(time * 3.0f)));
const float size = ImGui::CalcTextSize("Sailor").x;
for (int y = 0; y < 4; y++) for (int y = 0; y < 4; y++)
for (int x = 0; x < 4; x++) for (int x = 0; x < 4; x++)
{ {
if (x > 0) if (x > 0)
ImGui::SameLine(); ImGui::SameLine();
ImGui::PushID(y * 4 + x); ImGui::PushID(y * 4 + x);
if (ImGui::Selectable("Sailor", selected[y][x] != 0, 0, ImVec2(50, 50))) if (ImGui::Selectable("Sailor", selected[y][x] != 0, 0, ImVec2(size, size)))
{ {
// Toggle clicked cell + toggle neighbors // Toggle clicked cell + toggle neighbors
selected[y][x] ^= 1; selected[y][x] ^= 1;
@@ -2397,7 +2428,9 @@ static void DemoWindowWidgetsSelectables()
"By default, Selectables uses style.SelectableTextAlign but it can be overridden on a per-item " "By default, Selectables uses style.SelectableTextAlign but it can be overridden on a per-item "
"basis using PushStyleVar(). You'll probably want to always keep your default situation to " "basis using PushStyleVar(). You'll probably want to always keep your default situation to "
"left-align otherwise it becomes difficult to layout multiple items on a same line"); "left-align otherwise it becomes difficult to layout multiple items on a same line");
static bool selected[3 * 3] = { true, false, true, false, true, false, true, false, true }; static bool selected[3 * 3] = { true, false, true, false, true, false, true, false, true };
const float size = ImGui::CalcTextSize("(1.0,1.0)").x;
for (int y = 0; y < 3; y++) for (int y = 0; y < 3; y++)
{ {
for (int x = 0; x < 3; x++) for (int x = 0; x < 3; x++)
@@ -2407,7 +2440,7 @@ static void DemoWindowWidgetsSelectables()
sprintf(name, "(%.1f,%.1f)", alignment.x, alignment.y); sprintf(name, "(%.1f,%.1f)", alignment.x, alignment.y);
if (x > 0) ImGui::SameLine(); if (x > 0) ImGui::SameLine();
ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, alignment); ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, alignment);
ImGui::Selectable(name, &selected[3 * y + x], ImGuiSelectableFlags_None, ImVec2(80, 80)); ImGui::Selectable(name, &selected[3 * y + x], ImGuiSelectableFlags_None, ImVec2(size, size));
ImGui::PopStyleVar(); ImGui::PopStyleVar();
} }
} }
+1
View File
@@ -6484,6 +6484,7 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl
} }
// Initialize/override default color options // Initialize/override default color options
// FIXME: Could be moved to a simple IO field.
void ImGui::SetColorEditOptions(ImGuiColorEditFlags flags) void ImGui::SetColorEditOptions(ImGuiColorEditFlags flags)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;