DirectButton hoverCommand and downCommand?

I remember using Panda years ago and using something like this for my buttons, so that my logic code would know not only when a mouse clicks on a button and releases, but also when the cursor was simply over the button and also when the button was pressed instead of pressed and already released.

Checking the docs and the code of , it seems I’m mistaken and this isn’t supported by default, and I’ve also forgotten how I added it before. How can this functionality can be added? I don’t mean help with inheriting from DirectButton, just how to acquire the necessary info of the mouse state over the button.

While I don’t think that there’s a simple “hoverCommand” or “downCommand”, you should be able to achieve much the same effect via the “bind” interface.

Specifically, you might use the “DGG.WITHIN” (or “DGG.ENTER”, if it suits your purposes better) and “DGG.B1PRESS” events to call your desired methods when the relevant events occur, I believe.

(See the API here for reference.)

Something like this:

from direct.gui.DirectGui import DirectButton, DGG

# In the relevant code:
    self.myButton = DirectButton(text = "Button", scale = 0.1)
    self.myButton.bind(DGG.WITHIN, self.hoverCallback, extraArgs = [<args here, if desired>])
    self.myButton.bind(DGG.B1PRESS, self.pressCallback, extraArgs = [<args here, if desired>])

def hoverCallback(self, <extra args, if specified,> mousePosition):
    # Your code here

def pressCallback(self, <extra args, if specified,> mousePosition):
    # Your code here

Yep, that was it, .bind().
I used it years ago, but I don’t think it’s currently mentioned in the manual, it really should be. It’s listed in the API reference, but not as easy to find. Not sure where DGG is, not listed in API reference under direct.gui.

1 Like

Yup, I agree: it would be useful to have a reference to the

(I mean, it would be infeasible to include manual entries for every useful function in Panda–but “bind” is one that I think might be worth including nevertheless.)

DGG is “DirectGuiGlobals”, I believe–but I do note that the API seems to omit the various constants defined by DGG (including the events that can be bound). It would be useful, I daresay, if the API did mention them!

For now, I believe that you can at least see those constants in the source.

It might be worth filing requests for the addition of these things on the documentation’s GitHub page. (If there aren’t already issues for such things present; I haven’t properly checked.)