GeezerVison is showing github.com — Open and closing tree nodes · Issue #6754 · ocornut/imgui
Open and closing tree nodes #6754DescriptionI followed the portion of the demo that uses buttons to open and close all tree nodes. Instead of two buttons, I wanted to control this with a menu item which works fine. The problem I have is if I try to open and close individual tree nodes like usual, they do not stay open or closed.
Standalone, minimal, complete and verifiable example:
int open_action = -1;
static bool open = false;
// Loop
if (ImGui::MenuItem("Open all", nullptr, &open))
open_action = 1;
else
open_action = 0;
if (open_action != -1)
ImGui::SetNextItemOpen(open_action != 0);
// Make tree node
ActivityPathogenDavid commented on Aug 27, 2023 ImGui::MenuItem returns true when it is clicked and false otherwise, so you're constantly setting open_action to 0 except on the frames where the menu item is clicked. (Which effectively means the tree nodes will be collapsed on every frame.)
talkalotwhenim commented on Aug 27, 2023 So is it possible to achieve this with a single widget or would I need two? GamingMinds-DanielC commented on Aug 27, 2023 Instead of the snippet above, try this:
static bool open = false;
// Loop
if (ImGui::MenuItem("Open all", nullptr, &open))
ImGui::SetNextItemOpen(open);
// Make tree node
The important part is to call SetNextItemOpen only when you want to expand or collapse, not every frame.
Edit: well, you can call it every frame if you want to, but then you need to supply if you want the item expanded, not a toggle of the status as you did before. The "every frame version" would look as simple as this:
static bool open = false;
// Loop
ImGui::MenuItem("Open all", nullptr, &open);
ImGui::SetNextItemOpen(open);
// Make tree nodetalkalotwhenim commented on Aug 27, 2023 Unfortunately none of these worked for me PathogenDavid commented on Aug 28, 2023
So is it possible to achieve this with a single widget or would I need two?
It really depends on the exact semantics you want out of this user interaction. My first instinct was to reply with something very similar to what Daniel recommended, so clearly there's a disconnect between our guesses and what you're envisioning.
It would be helpful to know what your actual UI looks like and what problem you're trying to solve rather than trying to apply seemingly arbitrary changes to the Dear ImGui demo. GamingMinds-DanielC commented on Aug 28, 2023
Unfortunately none of these worked for me
Both versions work when f.e. followed with a non-leaf ImGui::TreeNodeEx. So my best guess is a mistake while transporting these solutions to actual code with a different structure. As David says, seeing that code would help. ocornut commented on Jul 3, 2024 Closing as lacking feedback from OP. Don't hesitate to post here again if you need help, but right now we lacked details.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|