Skip to content

Commit

Permalink
Demo: Added simple tree node selection demo (#581, #516, #190)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut committed May 1, 2016
1 parent 4c880b7 commit dc8446d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
4 changes: 2 additions & 2 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 36 additions & 7 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit dc8446d

Please sign in to comment.