Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct TreeList
- {
- static bool BeginTree(const char* const names)
- {
- // [TODO] put that ID in a deeper storage...
- ImGuiID identation_id = ImGui::GetID("identation_int");
- ImGuiStorage* storage = ImGui::GetStateStorage();
- storage->SetInt(identation_id, 0);
- return ImGui::CollapsingHeader(names);
- }
- // Returns true if the node is expanded.
- static bool Node(
- const char* const label, // The label of the node(mind 2 nodes with equal names).
- const std::function<void()>& usrCode, // user desiered GUI, state storage is implemented by the user.
- bool showExpandBtn) // aka. has any children.
- {
- ImGuiStorage* storage = ImGui::GetStateStorage();
- ImGuiID expanded_vect_id = ImGui::GetID("expanded_vect");
- ImGuiID selection_vect_id = ImGui::GetID("selection_vect");
- ImGuiID identation_id = ImGui::GetID("identation_int");
- std::vector<std::string>* pExpanded = (std::vector<std::string>*)storage->GetVoidPtr(expanded_vect_id);
- int lebelIdx = storage->GetInt(identation_id, 0);
- if(pExpanded == NULL)
- {
- pExpanded = new std::vector<std::string>();
- storage->SetVoidPtr(expanded_vect_id, pExpanded);
- }
- std::vector<std::string>& expanded = *pExpanded;
- const auto itrExpanded = std::find(std::begin(expanded), std::end(expanded), label);
- const bool wasExpanded = itrExpanded != expanded.end();
- ImGui::PushID(label);
- lebelIdx++;
- storage->SetInt(identation_id, lebelIdx);
- bool newExpanded = false;
- if(showExpandBtn)
- {
- newExpanded = ImGui::SmallButton(wasExpanded ? "-" : "+") ? !wasExpanded : wasExpanded;
- }
- else
- {
- ImGui::Text(""); // Something to be on the same line.
- }
- usrCode();
- ImGui::PopID();
- if(newExpanded == false)
- {
- if(wasExpanded)
- {
- expanded.erase(itrExpanded);
- }
- }
- else if(wasExpanded == false)
- {
- expanded.push_back(label);
- }
- if(newExpanded)
- {
- ImGui::Indent();
- }
- return newExpanded;
- }
- static void NodeEnd()
- {
- ImGui::Unindent();
- }
- };
Add Comment
Please, Sign In to add comment