-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Selected Tree Node/Left functionality #581
Comments
2/3 The way TreeNode() works is that it stores the open/closed state in imgui storage because it makes sense in the majority of cases. Whereas a "selected" state is most likely something that the users wants to manage themselves in the majority of cases. Right now the workaround is to do:
Which works but the mouse-detection region is incorrect. Basically we want to combine both into one. Note that both TreeNode and Selectable independently need more options to select how to behave in term of hit detection, in particularly for TreeNode we'd want an extra flag. So perhaps we need to introduce TreeNodeEx to allow for that flag and allow also for selection state. There we could also pass in extra flags for arrow placement.
Unfortunately as you pointed we can't return both "opened" and "clicked" (note that Selectable have both
As it would make more sense to let the user store that state in whichever way they see fit, the API can only give information about state change (e.g = node clicked) and Ctrl-state multiple selection is best left to the user to handle. About Shift, we don't have keyboard navigation controls (#323) at all yet anyway, and that alone would be a big task. |
OK, let me show you what I've done:
I am totally agree about the user managing most of the state but that is not the part I am stumped about. You see the TreeNode() functions already return some information, whether they are opened or not. If I need to return more information, like a node has been clicked, I can't do that via a return value like Button does. So I am wondering how else I would do it. The only thing I can think of is a std::function parameter so I can do a lambda. But I am wondering if there is a more ImGui way to handle this. I don't require the tree node system to store selected state and I can handle the Ctrl/Shift state outside too. Just want to know that a node has been clicked on. The final thing I have to fix is the Text call in my function to highlight when the mouse hovers over it. |
Your links provide some invaluable information - thanks! |
Either add an extra pointer parameter, or return flags. Perhaps returning flags would be acceptable for a TreeNodeEx() function, so it doesn't return a bool anymore but a set of flags. Quick notes and ideas:
|
Yeah you're right about the leaf path doesn't handle clicking. That was on my list of things to figure out. For some reason I forgot about it when inquiring above. Are you saying that CollapsingHeader can be used for a node without a open triangle, or would I have to adjust that function? I was going to copy & paste the code for that function to solve the selectable leaf problem and butcher it to do what I needed it to do. I wasn't planning on integrating it back into the project since I don't know enough about the internals of ImGui yet to feel comfortable about doing that. But if that was the case, a helper function to do handle leaves would be a better idea. Perhaps a BeginSelectable()...EndSelectable(). E.g.
Obviously this snippet wouldn't handle the colouring of the leaf node that was selected, but would highlight when the mouse hovered over it. |
I would suggest moving the bulk of CollapsingHeader into a new TreeNodeEx() and figuring out a way to share that code (adding a new set of flags). Copy and pasting make sense for working locally without worrying about further integration but ultimately we want to avoid it. User could also just use Selectable() or their own stuff if we want selection for lead, perhaps that isn't needed at all. It's ok if you don't do it in a way that is 100% integrable, posting your ideas/work here is helpful as I can eventually take the base idea and finish it. Sorry I am merely thinking out loud there! |
I'll write the code so that it contains ImGui-only code and get it to work for my needs, then I will post it and perhaps you can suggest changes to it so that it will be a better fit for integration, then I will do the work and start a pull request. Could you suggest the signature for the TreeNodeEx() function as you see it? |
About thought about it enough so I may be wrong there. |
And you would expect a TreeNodeV to call in to TreeNodeExV? |
Yes! |
I'm curious, what is the rationale with 'selected' being a input/output parameter? Currently, I've managed to do it with selected being an input parameter with the selection state being determined by the user. If that was the case, we could use ImGuiTreeNodeFlags_Selected to determine that. |
I guess that would solve the "did I click it" problem as that wouldn't be needed, since the only thing you'd want to know is whether it was selected or not. |
You are right - it doesn't have to. Sometimes it is convenient that it does the flipping, this is why for example I would say go for flag, my past experience has been that bool parameters have mostly been a bad idea. The only reason I would go for
Not sure to understand this sentence, sorry. |
Since the treenodes maintain whether they're open or not, the only other information a treenode can give is whether they're selected or not. The I/O parameter would give that information, so TreeNodeEx is free to still return a bool at whether the tree is open or not. |
It's a little tricky because if you want the user to react to selection (to apply their selection data which may not be a native bool or handle CTRL selection), then user having to check the output-bool means they probably need to also store the previous value and compare themselves, which would be a little cumbersome. So returning a flag allows to simplify that for the user. |
@Cthutu Been working on this (and various other tree related features today). After half a dozen of iterations I think I've got it all solved but I would be interested in seeing what you've done, out of curiosity and to see if there is an idea I may have overlooked. |
Here's the demo that now does this:
You can:
Looking at your code above I am not sure there is a need for a concept of Leaf, but we now have much more leeway for adding options via ImGuiTreeNodeFlags. Here are some unimplemented flags ideas:
|
Never mind, I got confused.
|
Added One problem in the color scheme here is that if you look at this out of context, there's no way to distinguish the collapsing header from the selected tree node. So may need to re-arrange the color enum. Remaining
|
From @Cthutu
|
Also agree with Cthutu that we don't want a bullet to display on the leaf nodes. |
Understood, will likely follow that. At the moment I yet have to fully understand what may constitute a leaf node aside from the trivial one-liner case, how it behave with layout of multiple widgets and how we can work out with indent. This is why I'm not worried about this flag right now (and |
…when clicking on single-item that's already part of selection (#581)
Removed the bullet from the _Leaf flag. FYI the only purpose of
But simple text display it'll work but not the more efficient code-path. So you can also use May be worth coming up with a helper to do this directly. And one that does that + display text. |
Added TreeAdvanceToLabelPos(). I think everything is this thread is complete now? |
I've finally converted my editor away from wxWidgets to use ImGui but have fallen at the first hurdle. I've scoured the demos and source code and can't see a way to do a basic tree. By basic tree I mean:
My only solution so far is to copy and paste the TreeNode, TreeNodeV and CollapsingHeader functions and alter them to provide the functionality I need. I don't mind sharing this functionality when I'm done but I am little stumped about how I would handle reacting to nodes being selected or unselected (with Shift and Ctrl keys supported) in the ImGui way (i.e. immediate).
Any advice on the matter?
The text was updated successfully, but these errors were encountered: