The scene graph is a 3-D scene graph, so it has (x, y, z). It’s Panda3D, after all. For the 2-D interfaces, we don’t use the y component, so it’s usually (x, 0, z).
So, you could (almost) take the mouse coordinates and insert an extra 0 in the middle to go to screen coordinates, and simply drop the middle coordinate to go back to mouse coordinates.
I say almost because there’s another complication: your DirectGui widgets are parented to aspect2d, which introduces a scale to compensate for the non-square aspect ratio of the window. So you need to get the coordinates in the space of render2d, which is where the mouse coordinates are relative to. Fortunately, Panda makes this easy.
To get the position of your button in mouse coordinates:
def checkForTooltip(self, task):
"""Showing tooltip if mouse is over button"""
if self.button['state'] == "normal":
if base.mouseWatcherNode.hasMouse():
mousePos = base.mouseWatcherNode.getMouse()
mousePos3d = VBase3(mousePos[0], 0, mousePos[1])
mousePosButton = self.button.getRelativePoint(render, mousePos3d)
x = mousePosButton[0]
y = mousePosButton[2]
if x < self.b_width/2 and x > -self.b_width/2 and y < self.b_height/2 and y > -self.b_height/2:
self.tooltip.show()
else:
self.tooltip.hide()
return Task.cont
Note that you can also achieve a similar effect, perhaps more easily, simply by binding on the mouse enter and exit events for the button, as described in this recent thread.