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!
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.
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. )
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.(
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()?
Most helpful comment
OK, that was pretty easy. All I needed to do was change the Show function to:
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 toImGuiContext::SetNextTreeNodeOpenValand aBeginFold()/EndFold()?