Add option to TreeNode to enable button behaviour with key modifiers enabled. #2886

Closed
opened 2019-11-07 20:06:06 +00:00 by AlvaroBarua · 3 comments
AlvaroBarua commented 2019-11-07 20:06:06 +00:00 (Migrated from github.com)

Hi, I am working on a multiselection tree hierarchy and using LCTRL as the modifier to select multiple items. My idea is to be able to while holding down the LCTRL key, open a tree node if I click on the arrow. Right now, the default behaviour of the button behaviour inside the TreeNode is to include the ImGuiButtonFlags_NoKeyModifiers flag which prevents me from doing so.

I have manually removed the default flag, but I guess having a proper way of disabling this behaviour would be the ideal solution.

Thanks.

\\Won't change state when LCTRL is held
bool treeNodeOpen = ImGui::TreeNodeEx(name, treeNodeFlasg);
bool clickedOnArrow = (ImGui::GetMousePos().x - ImGui::GetItemRectMin().x) < ImGui::GetTreeNodeToLabelSpacing();

if (ImGui::IsItemClicked())
{
    if (Input::GetKey(KeyCode::LCTRL) && !clickedOnArrow)
   {
       //Add item to list
   }
}

if (treeNodeOpen)
    RecursiveBuild(...);

Hi, I am working on a multiselection tree hierarchy and using LCTRL as the modifier to select multiple items. My idea is to be able to while holding down the LCTRL key, open a tree node if I click on the arrow. Right now, the default behaviour of the button behaviour inside the TreeNode is to include the ImGuiButtonFlags_NoKeyModifiers flag which prevents me from doing so. I have manually removed the default flag, but I guess having a proper way of disabling this behaviour would be the ideal solution. Thanks. ``` \\Won't change state when LCTRL is held bool treeNodeOpen = ImGui::TreeNodeEx(name, treeNodeFlasg); bool clickedOnArrow = (ImGui::GetMousePos().x - ImGui::GetItemRectMin().x) < ImGui::GetTreeNodeToLabelSpacing(); if (ImGui::IsItemClicked()) { if (Input::GetKey(KeyCode::LCTRL) && !clickedOnArrow) { //Add item to list } } if (treeNodeOpen) RecursiveBuild(...); ```
ocornut commented 2019-11-08 14:00:38 +00:00 (Migrated from github.com)

Which tree node flags are you using? Are you using ImGuiTreeNodeFlags_OpenOnArrow, or are not your using it and expecting a different behavior based on whether CTRL and click are happening over the arrow or before/after ?

My gut feeling is that for tree node it would make sense to allow toggling the node open state with modifiers when clicking specifically on the arrow, so we could alter the behavior based on mouse position. I'll have to check with features/range_select and think about other side-effects or doing this.

Potential fix would be to move the arrow_x1/arrow_x2 declaration higher in the function and alter the button flags based on io.MousePos.x.

(Also need to consider ImGuiTreeNodeFlags_SpanFullWidth being used which mean we can click before the arrow.)

Also consider looking at #1861 and the features/range_select branch.

(PS: Please fill the issue template as requested when creating a new issue.)

Which tree node flags are you using? Are you using `ImGuiTreeNodeFlags_OpenOnArrow`, or are not your using it and expecting a different behavior based on whether CTRL and click are happening over the arrow or before/after ? My gut feeling is that for tree node it would make sense to allow toggling the node open state with modifiers when clicking specifically on the arrow, so we could alter the behavior based on mouse position. I'll have to check with `features/range_select` and think about other side-effects or doing this. Potential fix would be to move the arrow_x1/arrow_x2 declaration higher in the function and alter the button flags based on `io.MousePos.x`. (Also need to consider `ImGuiTreeNodeFlags_SpanFullWidth` being used which mean we can click _before_ the arrow.) Also consider looking at #1861 and the `features/range_select` branch. (PS: Please fill the issue template as requested when creating a new issue.)
ocornut commented 2019-11-08 14:36:14 +00:00 (Migrated from github.com)

I applied the change described above now.

In addition, I have added IsItemToggledOpen() to explicitely query if the node was just open/closed which is required to fully handle #1896.

I applied the change described above now. In addition, I have added `IsItemToggledOpen()` to explicitely query if the node was just open/closed which is required to fully handle #1896.
AlvaroBarua commented 2019-11-08 14:49:47 +00:00 (Migrated from github.com)

Thank you for the quick fix, it is exactly what I needed.

I was using ImGuiTreeNodeFlags_SpanFullWidth and doing a range check to see if the mouse was in the arrow area when clicked using the TreeDepth, but with IsItemToggledOpen() should be way easier and cleaner.

And sorry for the template, I must have cleared the text and forgot about it completely.

Thank you for the quick fix, it is exactly what I needed. I was using ImGuiTreeNodeFlags_SpanFullWidth and doing a range check to see if the mouse was in the arrow area when clicked using the TreeDepth, but with IsItemToggledOpen() should be way easier and cleaner. And sorry for the template, I must have cleared the text and forgot about it completely.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#2886
No description provided.