Guest User

Untitled

a guest
Jan 11th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. struct TreeList
  2. {
  3.     static bool BeginTree(const char* const names)
  4.     {
  5.         // [TODO] put that ID in a deeper storage...
  6.         ImGuiID identation_id = ImGui::GetID("identation_int");
  7.         ImGuiStorage* storage = ImGui::GetStateStorage();
  8.         storage->SetInt(identation_id, 0);
  9.        
  10.         return ImGui::CollapsingHeader(names);
  11.     }
  12.  
  13.     // Returns true if the node is expanded.
  14.     static bool Node(
  15.         const char* const label, // The label of the node(mind 2 nodes with equal names).
  16.         const std::function<void()>& usrCode, // user desiered GUI, state storage is implemented by the user.
  17.         bool showExpandBtn) // aka. has any children.
  18.     {
  19.         ImGuiStorage* storage = ImGui::GetStateStorage();
  20.  
  21.         ImGuiID expanded_vect_id = ImGui::GetID("expanded_vect");
  22.         ImGuiID selection_vect_id = ImGui::GetID("selection_vect");
  23.         ImGuiID identation_id = ImGui::GetID("identation_int");
  24.        
  25.         std::vector<std::string>* pExpanded = (std::vector<std::string>*)storage->GetVoidPtr(expanded_vect_id);
  26.  
  27.         int lebelIdx = storage->GetInt(identation_id, 0);
  28.  
  29.         if(pExpanded == NULL)
  30.         {
  31.             pExpanded = new std::vector<std::string>();
  32.             storage->SetVoidPtr(expanded_vect_id, pExpanded);
  33.         }
  34.  
  35.         std::vector<std::string>& expanded = *pExpanded;
  36.         const auto itrExpanded = std::find(std::begin(expanded), std::end(expanded), label);
  37.         const bool wasExpanded = itrExpanded != expanded.end();
  38.  
  39.         ImGui::PushID(label);
  40.  
  41.         lebelIdx++;
  42.         storage->SetInt(identation_id, lebelIdx);
  43.  
  44.         bool newExpanded = false;
  45.         if(showExpandBtn)
  46.         {
  47.             newExpanded = ImGui::SmallButton(wasExpanded ? "-" : "+") ? !wasExpanded : wasExpanded;
  48.         }
  49.         else
  50.         {
  51.             ImGui::Text(""); // Something to be on the same line.
  52.         }
  53.        
  54.         usrCode();
  55.  
  56.         ImGui::PopID();
  57.  
  58.         if(newExpanded == false)
  59.         {
  60.             if(wasExpanded)
  61.             {
  62.                 expanded.erase(itrExpanded);
  63.             }
  64.         }
  65.         else if(wasExpanded == false)
  66.         {
  67.            
  68.             expanded.push_back(label);
  69.         }
  70.  
  71.         if(newExpanded)
  72.         {
  73.             ImGui::Indent();
  74.         }
  75.  
  76.         return newExpanded;
  77.     }
  78.    
  79.     static void NodeEnd()
  80.     {
  81.         ImGui::Unindent();
  82.     }
  83. };
Add Comment
Please, Sign In to add comment