Nelarius / imnodes

A small, dependency-free node editor for dear imgui
MIT License
2.04k stars 249 forks source link

IsNodeHovered returns false when MouseClicked[0] is true #149

Open GreedyTactician opened 2 years ago

GreedyTactician commented 2 years ago

Title is self explanatory. I wanted to see which node the cursor was on when left clicking. It always returns false even when the cursor is over a node. Interestingly, if a popup is open, everything works as expected.

I think I could use GetSelectedNodes to do what I want (that is what I am looking into now). Regardless, that is beside the point, the behaviour is not what is expected from IsNodeHovered.

GreedyTactician commented 2 years ago

Used the following to fix the bug on my side.

bool isNodeHovered = false;// = ImNodes::IsNodeHovered(&idOfHoveredNode);

                int idOfHoveredNode;
                int vectorPositionOfidOfHoveredNode;

                for(int i = 0; i < nodes.size(); i++)
                {
                    if(isInRectangle(imguiVariables.MousePos, ImNodes::GetNodeScreenSpacePos(nodes[i].id), ImNodes::GetNodeDimensions(nodes[i].id)))
                    {
                        isNodeHovered = true;
                        vectorPositionOfidOfHoveredNode = i;
                        idOfHoveredNode = nodes[i].id;
                        break;
                    }
                }   

and

auto isInRectangle = [](ImVec2 pos, ImVec2 recpos, ImVec2 recsize)
    {
        return ((recpos.x < pos.x) and (recpos.x + recsize.x > pos.x)) and ((recpos.y < pos.y) and (recpos.y + recsize.y > pos.y));
    };

Not sure why isNodeHovered isn't already doing this.

Nelarius commented 2 years ago

Definitely not intended behaviour, since the doc comment in the header says The following functions return true if a UI element is being hovered over by the mouse cursor, and the function starts returning false after LeftMouseClicked.

The solution you presented would work, but the code actually already does a similar search for HoveredNodeIdx and would result in duplicated O(N) time complexity.

Nelarius commented 2 years ago

@GreedyTactician I sketched out a potential solution in the branch: https://github.com/Nelarius/imnodes/tree/is-node-hovered-fix

It implements a "semantic" hover, i.e. the node remains hovered even when the mouse cursor is temporarily outside of the node rectangle when dragging the node around the canvas very rapidly.

GreedyTactician commented 2 years ago

This is better, but one part of the bug remains.

-Node Editor selected + hover mouse over node = works

-Node Editor not selected + hover mouse over node = works

-Node Editor selected + hover mouse over node + click on node = works

-Node Editor not selected + hover mouse over node + click on node = does NOT work