Imgui: How to recursively fold all TreeNodes?

Created on 4 May 2017  路  5Comments  路  Source: ocornut/imgui

I can use ImGui::SetNextTreeNodeOpen(false);, but if I do, the node's children won't be expanded so I won't be able set their open state...
Is there anyway to access the internal tree behavior?
Thanks!

tree

Most helpful comment

OK, that was pretty easy. All I needed to do was change the Show function to:

void Node::Show() {
  if (foldAll)
    SetNextTreeNodeOpen(false);
  if (TreeNode(mName) || foldAll) {
    if (foldAll)
      TreePush(mName);
    for (child : mChildren)
      child->Show();
  }
}

I have no idea what's the best way to allow this with the current api style. Maybe a new flag to ImGuiTreeNodeFlags_? or a new state value to ImGuiContext::SetNextTreeNodeOpenVal and a BeginFold()/EndFold()?

All 5 comments

don't call it every frame

@ratchetfreak I don't understand. I'm already not calling it every frame, only when the user clicks the "fold tree" button.

@liorda Please explain what you are trying to do in more clarity, it is hard to understand at the moment.

  1. Each TreeNode stores an integer in the window storage _when clicked_. Each TreeNode push the its identifier to the id stack for children tree nodes would be the combinaison of those stacked identifiers. Based on that you can infer the full ID of any of your children if you know their path.

So ImGui::GetStateStorage()->SetInt(id, 0) would effectively close a node. With e.g. id = ImGui::GetId("treenodename") for a direct children.

Except this will create many '0' entries in the storage for tree nodes that haven't been touched, so it will grow the storage unnecessarily, which is not desirable. So you can do if (GetId(id) == 1) SetId(id, 0).
(We are missing a int* GetIntRefIfExist(ImGuiIdKey key) function to allow doing that optimally (only closing nodes that were opened). But that solution above will work. )

  1. You may swap the underlying storage for a given tree block using SetStateStorage() to your own storage, and then call SetAllInt() on your own storage, but this is sort of tricky/risky if not done well because any other user of that same storage will get data effected.

(

  1. We are missing a larger concept of being able to blindly opening into a hierarchy while ignoring the render. This is not trivial to implement, we will implement some form of that for the Shortcuts API #45. And then it might extend into having helpers to magically have TreeNode() return true while not existing to allow your code to run. Note that trees can perfectly be recursive etc. so it's not exactly a trivial thing to do safely, and it's probably not what you need now. I'm just musing around with future ideas here.
    )

Sorry for being vague, I'll try to explain better:
I have an hierarchy of objects, which I show on screen via simple recursion:
void Node::Show() { if (TreeNode(mName)) { for (child : mChildren) child->Show(); } }.
Whenever I fold/expand any node, its children "remember" their open/closed state, and that's pretty cool.
Now, I implemented an "Expand all" button, which was very easy, by just calling SetNextTreeNodeOpen(true) before calling TreeNode() in the recursive function. So far so good. The problem is that for implementing a recursive "fold all", I can't use that. If I call SetNextTreeNodeOpen(false), the TreeNode() call returns false, so the children are rendered / pushed to the id stack.

@ocornut I wasn't aware of ImGui::GetStateStorage()->Set*(), but I'm not sure I want to go this route since it would be hard finding out the current context data. Your 3rd point is exactly the issue I'm trying to solve. I guess I'll try to solve it myself, but I'll try to do it in a generic way.

OK, that was pretty easy. All I needed to do was change the Show function to:

void Node::Show() {
  if (foldAll)
    SetNextTreeNodeOpen(false);
  if (TreeNode(mName) || foldAll) {
    if (foldAll)
      TreePush(mName);
    for (child : mChildren)
      child->Show();
  }
}

I have no idea what's the best way to allow this with the current api style. Maybe a new flag to ImGuiTreeNodeFlags_? or a new state value to ImGuiContext::SetNextTreeNodeOpenVal and a BeginFold()/EndFold()?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ILoveImgui picture ILoveImgui  路  3Comments

ghost picture ghost  路  3Comments

mkanakis picture mkanakis  路  3Comments

ocornut picture ocornut  路  3Comments

GrammarLord picture GrammarLord  路  3Comments