MouseWatcherRegion

I’ve been searching the forum for info on mouse over object and read about MouseWatcherRegion which was used in Toontown. I would like to use this method, but don’t know where to start.
I’ve been searching all around for info on how to “create a MouseWatcherRegion for an object I want to be clickable.”
I looked at the info in the API ref, but that doesn’t help me to know what to do.
Can someone help me please?

The MouseWatcherRegion is the C++ class that implements the clickable regions used by the DirectGui system. To create a custom MouseWatcherRegion the way it was done in Toontown, you will need to write some custom C++ code to subclass MouseWatcherRegion and define the precise behavior you’d like to have happen when the region is clicked.

The easiest way to get this same behavior in Python, without writing any C++ code, is to use a DirectLabel, which can be customized to have any contents you like (including no contents at all, if you insist), and can call any Python function you like when it is clicked upon.

obj = DirectLabel('state' = DGG.NORMAL, 'relief' = None, geom = myContents, 'frameSize' = (left, right, bottom, top))
obj.bind(DGG.B1CLICK, myFunc)

You need to specify ‘state’ = DGG.NORMAL to make a DirectLabel clickable (it’s not clickable by default).

You can specify ‘relief’ = None to avoid creating a gray background card (it will create one by default).

You can specify ‘geom’ = myContents to load your customized contents. Leave this out if you don’t have any custom contents.

You can specify ‘frameSize’ = (left, right, bottom, top) to specify the screen region you’d like it to cover. These will be four coordinates in screen space: the left, right extents of the region in the X coordinate, and the bottom, top extents of the region in the Z coordinate. You can omit this if you have specified a geom with ‘geom’ = myContents, in which case these will be computed to fit the geom.

You can also specify pos, scale, and whatever as usual for DirectGui elements, if you like.

Finally, the bind() function specifies the function that should be called when the user clicks on your DirectLabel.

David

Thanks
Before I log off, I just wanted to know - Can I use this to make an event occur when the mouse enters this region (without clicking)?

Yes. Bind to DGG.ENTER instead of DGG.B1CLICK.

David

I’ll try my best. Thanks for the help.

Hi Panda People!

I saw this thread and, as I have some problem on DirectLabel and binding, here I come :smiley:
So, I have one DirectLabel, really basic
Then, I reparent 1 DirectButton and 1 DirectLabel to this first label.
I do element[“state”] = DGG.NORMAL and element.bind(DGG.B1CLICK, function) for the 2 labels, and I bind the button too.

The problem is : I can only click on the button and I don’t understand why. It worked on another case where my Label was reparented to the aspect2d, so does it come from here ? Else, any idea ?

It’s certainly true that all DirectGui elements, including DirectLabels, need to be parented to aspect2d (or to some node which is a descendant of aspect2d) in order to be clickable.

David

But the problem is that the DirectButton is not reparented to aspect2d but to the first DirectLabel too. And it works for it.

And, if it can help understanding, the DGG.ENTER and DGG.EXIT work for the DirectButton and both DIrectLabels in this case.

And the parent label is reparented to aspect2d

Perhaps your DirectLabel has a zero-size frame? Have you explicitly set its frame to anything?

Try the command:

base.mouseWatcherNode.showRegions(aspect2d, 'gui-popup', 0)

This will draw a blue rectangle around each clickable region.

David