Demo: MultiSelect: added tree demo. (#6990, #3823, #1861)

This commit is contained in:
ocornut 2023-08-21 11:26:19 +02:00
parent c2a3d5e47b
commit ce3a8d7c79

View File

@ -262,13 +262,14 @@ void* GImGuiDemoMarkerCallbackUserData = NULL;
struct ExampleTreeNode struct ExampleTreeNode
{ {
// Tree structure // Tree structure
char Name[28]; char Name[28] = "";
int UID = 0; int UID = 0;
ExampleTreeNode* Parent = NULL; ExampleTreeNode* Parent = NULL;
ImVector<ExampleTreeNode*> Childs; ImVector<ExampleTreeNode*> Childs;
unsigned short IndexInParent = 0; // Maintaining this allows us to implement linear traversal more easily
// Leaf Data // Leaf Data
bool HasData = false; // All leaves have data bool HasData = false; // All leaves have data
bool DataMyBool = true; bool DataMyBool = true;
int DataMyInt = 128; int DataMyInt = 128;
ImVec2 DataMyVec2 = ImVec2(0.0f, 3.141592f); ImVec2 DataMyVec2 = ImVec2(0.0f, 3.141592f);
@ -298,6 +299,7 @@ static ExampleTreeNode* ExampleTree_CreateNode(const char* name, int uid, Exampl
snprintf(node->Name, IM_ARRAYSIZE(node->Name), "%s", name); snprintf(node->Name, IM_ARRAYSIZE(node->Name), "%s", name);
node->UID = uid; node->UID = uid;
node->Parent = parent; node->Parent = parent;
node->IndexInParent = parent ? (unsigned short)parent->Childs.Size : 0;
if (parent) if (parent)
parent->Childs.push_back(node); parent->Childs.push_back(node);
return node; return node;
@ -3442,6 +3444,160 @@ static void ShowDemoWindowMultiSelect(DemoWindowData* demo_data)
ImGui::TreePop(); ImGui::TreePop();
} }
// Demonstrate supporting multiple-selection in a tree.
// - We don't use linear indices for selection user data, but our ExampleTreeNode* pointer directly!
// This showcase how SetNextItemSelectionUserData() never assume indices!
// - The difficulty here is to "interpolate" from RangeSrcItem to RangeDstItem in the SetAll/SetRange request.
// We want this interpolation to match what the user sees: in visible order, skipping closed nodes.
// This is implemented by our TreeGetNextNodeInVisibleOrder() user-space helper.
// - Important: In a real codebase aiming to implement full-featured selectable tree with custom filtering, you
// are more likely to build an array mapping sequential indices to visible tree nodes, since your
// filtering/search + clipping process will benefit from it. Having this will make this interpolation much easier.
// - Consider this a prototype: we are working toward simplifying some of it.
IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select (trees)");
if (ImGui::TreeNode("Multi-Select (trees)"))
{
HelpMarker(
"This is rather advanced and experimental. If you are getting started with multi-select,"
"please don't start by looking at how to use it for a tree!\n\n"
"Future versions will try to simplify and formalize some of this.");
struct ExampleTreeFuncs
{
static void DrawNode(ExampleTreeNode* node, ImGuiSelectionBasicStorage* selection)
{
ImGuiTreeNodeFlags tree_node_flags = ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick;
tree_node_flags |= ImGuiTreeNodeFlags_NavLeftJumpsBackHere; // Enable pressing left to jump to parent
if (node->Childs.Size == 0)
tree_node_flags |= ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_Leaf;
if (selection->Contains((ImGuiID)node->UID))
tree_node_flags |= ImGuiTreeNodeFlags_Selected;
// Using SetNextItemStorageID() to specify storage id, so we can easily peek into
// the storage holding open/close stage, using our TreeNodeGetOpen/TreeNodeSetOpen() functions.
ImGui::SetNextItemSelectionUserData((ImGuiSelectionUserData)(intptr_t)node);
ImGui::SetNextItemStorageID((ImGuiID)node->UID);
if (ImGui::TreeNodeEx(node->Name, tree_node_flags))
{
for (ExampleTreeNode* child : node->Childs)
DrawNode(child, selection);
ImGui::TreePop();
}
else if (ImGui::IsItemToggledOpen())
{
TreeCloseAndUnselectChildNodes(node, selection);
}
}
static bool TreeNodeGetOpen(ExampleTreeNode* node)
{
return ImGui::GetStateStorage()->GetBool((ImGuiID)node->UID);
}
static void TreeNodeSetOpen(ExampleTreeNode* node, bool open)
{
ImGui::GetStateStorage()->SetBool((ImGuiID)node->UID, open);
}
// When closing a node: 1) close and unselect all child nodes, 2) select parent if any child was selected.
// FIXME: This is currently handled by user logic but I'm hoping to eventually provide tree node
// features to do this automatically, e.g. a ImGuiTreeNodeFlags_AutoCloseChildNodes etc.
static int TreeCloseAndUnselectChildNodes(ExampleTreeNode* node, ImGuiSelectionBasicStorage* selection, int depth = 0)
{
// Recursive close (the test for depth == 0 is because we call this on a node that was just closed!)
int unselected_count = selection->Contains((ImGuiID)node->UID) ? 1 : 0;
if (depth == 0 || TreeNodeGetOpen(node))
{
for (ExampleTreeNode* child : node->Childs)
unselected_count += TreeCloseAndUnselectChildNodes(child, selection, depth + 1);
TreeNodeSetOpen(node, false);
}
// Select root node if any of its child was selected, otherwise unselect
selection->SetItemSelected((ImGuiID)node->UID, (depth == 0 && unselected_count > 0));
return unselected_count;
}
// Apply multi-selection requests
static void ApplySelectionRequests(ImGuiMultiSelectIO* ms_io, ExampleTreeNode* tree, ImGuiSelectionBasicStorage* selection)
{
for (ImGuiSelectionRequest& req : ms_io->Requests)
{
if (req.Type == ImGuiSelectionRequestType_SetAll)
{
if (req.Selected)
TreeSetAllInOpenNodes(tree, selection, req.Selected);
else
selection->Clear();
}
else if (req.Type == ImGuiSelectionRequestType_SetRange)
{
ExampleTreeNode* first_node = (ExampleTreeNode*)(intptr_t)req.RangeFirstItem;
ExampleTreeNode* last_node = (ExampleTreeNode*)(intptr_t)req.RangeLastItem;
for (ExampleTreeNode* node = first_node; node != NULL; node = TreeGetNextNodeInVisibleOrder(node, last_node))
selection->SetItemSelected((ImGuiID)node->UID, req.Selected);
}
}
}
static void TreeSetAllInOpenNodes(ExampleTreeNode* node, ImGuiSelectionBasicStorage* selection, bool selected)
{
if (node->Parent != NULL) // Root node isn't visible nor selectable in our scheme
selection->SetItemSelected((ImGuiID)node->UID, selected);
if (node->Parent == NULL || TreeNodeGetOpen(node))
for (ExampleTreeNode* child : node->Childs)
TreeSetAllInOpenNodes(child, selection, selected);
}
// Interpolate in *user-visible order* AND only *over opened nodes*.
// If you have a sequential mapping tables (e.g. generated after a filter/search pass) this would be simpler.
// Here the tricks are that:
// - we store/maintain ExampleTreeNode::IndexInParent which allows implementing a linear iterator easily, without searches, without recursion.
// this could be replaced by a search in parent, aka 'int index_in_parent = curr_node->Parent->Childs.find_index(curr_node)'
// which would only be called when crossing from child to a parent, aka not too much.
// - we call SetNextItemStorageID() before our TreeNode() calls with an ID which doesn't relate to UI stack,
// making it easier to call TreeNodeGetOpen()/TreeNodeSetOpen() from any location.
static ExampleTreeNode* TreeGetNextNodeInVisibleOrder(ExampleTreeNode* curr_node, ExampleTreeNode* last_node)
{
// Reached last node
if (curr_node == last_node)
return NULL;
// Recurse into childs. Query storage to tell if the node is open.
if (curr_node->Childs.Size > 0 && TreeNodeGetOpen(curr_node))
return curr_node->Childs[0];
// Next sibling, then into our own parent
while (curr_node->Parent != NULL)
{
if (curr_node->IndexInParent + 1 < curr_node->Parent->Childs.Size)
return curr_node->Parent->Childs[curr_node->IndexInParent + 1];
curr_node = curr_node->Parent;
}
return NULL;
}
}; // ExampleTreeFuncs
static ImGuiSelectionBasicStorage selection;
static ExampleTreeNode* tree = ExampleTree_CreateDemoTree(); // Create tree once
ImGui::Text("Selection size: %d", selection.Size);
if (ImGui::BeginChild("##Tree", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY))
{
ImGuiMultiSelectFlags ms_flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect2d;
ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(ms_flags, selection.Size, -1);
ExampleTreeFuncs::ApplySelectionRequests(ms_io, tree, &selection);
for (ExampleTreeNode* node : tree->Childs)
ExampleTreeFuncs::DrawNode(node, &selection);
ms_io = ImGui::EndMultiSelect();
ExampleTreeFuncs::ApplySelectionRequests(ms_io, tree, &selection);
}
ImGui::EndChild();
ImGui::TreePop();
}
// Advanced demonstration of BeginMultiSelect() // Advanced demonstration of BeginMultiSelect()
// - Showcase clipping. // - Showcase clipping.
// - Showcase deletion. // - Showcase deletion.