imnodes icon indicating copy to clipboard operation
imnodes copied to clipboard

IsNodeHovered returns false when MouseClicked[0] is true

Open GreedyTactician opened this issue 2 years ago • 4 comments

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 avatar Apr 12 '22 05:04 GreedyTactician

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.

GreedyTactician avatar Apr 12 '22 16:04 GreedyTactician

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 avatar Apr 13 '22 06:04 Nelarius

@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.

Nelarius avatar Apr 13 '22 06:04 Nelarius

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

GreedyTactician avatar Apr 15 '22 04:04 GreedyTactician