1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-11-28 01:20:55 +01:00

Demo: Added simple tree node selection demo (#581, #516, #190)

This commit is contained in:
ocornut 2016-05-01 17:55:04 +02:00
parent 4c880b7106
commit dc8446d048
2 changed files with 38 additions and 9 deletions

View File

@ -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

View File

@ -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();
}