Is there a way to get the DirectGUI item (if any) that’s under the mouse?
MouseWatcher provides the method “getOverRegion”, and the returned MouseWatcherRegion does seem to contain a reference to the associated PGItem–but it’s stored in a private variable, and thus not accessible.
To explain, I have a piece of code that I want to skip over whenever the player is hovering the mouse over a certain type of DirectGUI element (or the sub-components thereof). The trick, then, is to figure out what element the mouse is currently over, if any at all…
You could use an event like this:
guiObject.bind(DGG.ENTER,command=self.myCommand,extraArgs=[myArgs])
Then whenever the mouse hovers over the DirectGui object, your command could be triggered and do whatever you want it to do.
To see when the mouse has “exited” a gui-object’s bounds use “EXIT”:
guiObject.bind(DGG.EXIT,command=self.myCommand,extraArgs=[myArgs])
Which would then trigger your command as well, upon that event happening.
Of course this is just one way to get it done, there could be others.
Ah, I should perhaps have mentioned that: I don’t want to manually bind all of the elements in question. I’d rather do this by examining the current item.
Sorry about omitting that, and thank you nevertheless for the answer! ^^;
Here’s something I just tried and seems to work:
def __get_widget(self, task):
if not self.mouseWatcherNode.hasMouse():
return task.cont
region = self.mouseWatcherNode.getOverRegion()
if region:
print("region:", region.name)
widget = self.aspect2d.find(f"**/*{region.name}")
print("widget:", widget)
return task.cont
If you add the above task to your ShowBase-derived application class, it should print the name of the MouseWatcherRegion and the widget it found whose name ends with that name, attached to aspect2d
.
Perhaps this is what you were looking for?
Excellent–that does indeed seem to work, thank you! 
I’ll confess that I was a little hesitant about using an approach that employed scene-graph searching, but it looks as though it doesn’t significantly impact performance, and it works, so I’m happy I believe. 
1 Like