Skip to content
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

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

Open
GreedyTactician opened this issue Apr 12, 2022 · 4 comments
Open

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

GreedyTactician opened this issue Apr 12, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@GreedyTactician
Copy link

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
Copy link
Author

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 Nelarius added the bug Something isn't working label Apr 13, 2022
@Nelarius
Copy link
Owner

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
Copy link
Owner

@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
Copy link
Author

GreedyTactician commented Apr 15, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants