From dc8446d048b0dc021e4d0a407e05185424902f73 Mon Sep 17 00:00:00 2001 From: ocornut Date: Sun, 1 May 2016 17:55:04 +0200 Subject: [PATCH] Demo: Added simple tree node selection demo (#581, #516, #190) --- imgui.h | 4 ++-- imgui_demo.cpp | 43 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/imgui.h b/imgui.h index ae02c90e..ce484f8d 100644 --- a/imgui.h +++ b/imgui.h @@ -530,8 +530,8 @@ enum ImGuiTreeNodeFlags_ ImGuiTreeNodeFlags_NoTreePushOnOpen = 1 << 3, // Don't do a TreePush() when opened (e.g. for CollapsingHeader) = no extra indent nor pushing on ID stack ImGuiTreeNodeFlags_NoAutoOpenOnLog = 1 << 4, // Don't automatically and temporarily open node when Logging is active (by default logging will automatically open tree nodes). ImGuiTreeNodeFlags_DefaultOpen = 1 << 5, // Default node to be opened - ImGuiTreeNodeFlags_OpenOnDoubleClick = 1 << 6, // Double-click to open node - //ImGuiTreeNodeFlags_OpenOnArrowOnly = 1 << 7, // FIXME: TODO: Can only click by + ImGuiTreeNodeFlags_OpenOnDoubleClick = 1 << 6, // Need double-click to open node + //ImGuiTreeNodeFlags_OpenOnArrowOnly = 1 << 7, // FIXME: TODO //ImGuiTreeNodeFlags_UnindentArrow = 1 << 8, // FIXME: TODO //ImGuITreeNodeFlags_SpanAllAvailWidth = 1 << 9, // FIXME: TODO: Extend hit box horizontally even if not framed //ImGuiTreeNodeFlags_NoScrollOnOpen = 1 << 10, // FIXME: TODO: Automatically scroll on TreePop() if node got just opened and contents is not visible diff --git a/imgui_demo.cpp b/imgui_demo.cpp index ac88f842..3a9df979 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -270,16 +270,45 @@ void ImGui::ShowTestWindow(bool* p_opened) { if (ImGui::TreeNode("Trees")) { - for (int i = 0; i < 5; i++) + if (ImGui::TreeNode("Basic trees")) { - if (ImGui::TreeNode((void*)(intptr_t)i, "Child %d", i)) + for (int i = 0; i < 5; i++) + if (ImGui::TreeNode((void*)(intptr_t)i, "Child %d", i)) + { + ImGui::Text("blah blah"); + ImGui::SameLine(); + if (ImGui::SmallButton("print")) printf("Child %d pressed", i); + ImGui::TreePop(); + } + ImGui::TreePop(); + } + + if (ImGui::TreeNode("With selectable nodes")) + { + ShowHelpMarker("Click to select, CTRL+Click to toggle, double-click to open"); + static int selection_mask = 0x02; // Dumb representation of what may be user-side selection state. You may carry selection state inside or outside your objects in whatever format you see fit. + int node_clicked = -1; + for (int i = 0; i < 5; i++) { - ImGui::Text("blah blah"); - ImGui::SameLine(); - if (ImGui::SmallButton("print")) - printf("Child %d pressed", i); - ImGui::TreePop(); + ImGuiTreeNodeFlags node_flags = ((selection_mask & (1 << i)) ? ImGuiTreeNodeFlags_Selected : 0) | ImGuiTreeNodeFlags_OpenOnDoubleClick; + bool opened = ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, "Selectable Child %d", i); + if (ImGui::IsItemClicked()) + node_clicked = i; + if (opened) + { + ImGui::Text("blah blah"); + ImGui::TreePop(); + } } + if (node_clicked != -1) + { + // Update selection state. Process outside of tree loop to avoid visual inconsistencies during the clicking-frame. + if (ImGui::GetIO().KeyCtrl) + selection_mask ^= (1 << node_clicked); // CTRL+click to toggle + else + selection_mask = (1 << node_clicked); // Click to single-select + } + ImGui::TreePop(); } ImGui::TreePop(); }