I want to create a main menu for my game. I thought the built in GUI modules would work great, but I am having a problem doing something that is very simple in my old game engine (PyU3D):
I have created all my menu buttons in PS. They are simple “startup.png” and “startdown.png”. All I need to do is find out if the mouse is over the “startup” image, and if so change to the “startdown” image.
But I find there is no built in function to do this!?!
I tried using the built in buttons with an image overlay, but they add a crappy background to my images.
Oh, and how can I keep the screen from being resized?
This is very simple in Panda as well, although it probably could be more obvious .
The way I do it (and it probably isn’t the only way possible) is by using a DirectButton. To make it react to the mouse “hovering” over, I use this code:
myButton.bind(DGG.WITHIN, <my_callback_function>)
For hovering out it’s the same, but with DGG.WITHOUT.
Not sure why the name “within” and “without”… but this should do the trick and it works well for me.
Just make sure you have DirectGuiGlobals imported. That’s…
import direct.gui.DirectGui.DGG
As far as the “crappy background”, I suppose you mean the relief. I personally don’t like it too (even though it might be useful), so I just use this:
relief = DGG.FLAT
in my DirectButton definitions. That way I get a nice, flat button.
as for the screen resizeablitiy. you cant enforce it. in best case you can ask the OS to make the window non-resizeable. but there is no garantuee the OS will do what you request.
Well, I don’t think you can remove it, since that’s the “area” of the button (where the button reacts to input), but you can always make it transparent.
Setting relief = None removes the background altogether, which is perfectly reasonable if you are supplying your own background card.
Note that a DirectButton automatically does the substitution you describe. You don’t have to bind any events on within and without it make it switch to the rollover state; that’s what a DirectButton does all by itself.
Edit: put:
win-fixed-size 1
in your Config.prc to request the OS to give you a non-resizeable window. As Thomas says, this is not a guarantee, but it will do the right thing in almost all cases.