Hello,
I’m exploring the basics, and just starting with playing with some of the PGui elements. I’ve managed to create button with a callback by using the following:
PGButton *but;
but = new PGButton("button");
but->setup("my button!");
framework.define_key(but->get_click_event(MouseButton::one() ), "button press", button_callback, NULL);
NodePath butNP = window->get_aspect_2d().attach_new_node(but);
butNP.set_scale(0.25, 1.0, 0.25);
framework.main_loop();
Using a simple callback that just prints “pressed” to the console, I know that this works.
However, if I try to make a more interesting button with a custom appearance using:
// Load images for button states, and create 2d "cards" on which they will be rendered.
Texture *readyTex, *pressedTex, *rolloverTex;
readyTex = TexturePool::load_texture("ready.png");
pressedTex = TexturePool::load_texture("depressed.png");
rolloverTex = TexturePool::load_texture("rollover.png");
CardMaker cm("cardMaker");
PT(PandaNode) readyCard = cm.generate();
PT(PandaNode) pressCard = cm.generate();
PT(PandaNode) rollCard = cm.generate();
PT(PandaNode) inactCard = cm.generate();
// create node paths
NodePath readyPath(readyCard);
readyPath.set_texture( readyTex );
NodePath pressPath(pressCard);
pressPath.set_texture( pressedTex );
NodePath rollPath(rollCard);
rollPath.set_texture( rolloverTex );
NodePath inactPath( inactCard );
inactPath.set_texture( readyTex );
inactPath.set_color(0.5,0.5,0.5);
// create button
PGButton *but;
but = new PGButton("button");
// setup button with the above nodepaths
but->setup(readyPath, pressPath, rollPath, inactPath);
// setup such that event handler calls the button callback function
// when the botton click event occurs
framework.define_key(but->get_click_event(MouseButton::one() ), "button press", button_callback, NULL);
// attach the button to the 2d aspec-ratio corrected render node.
NodePath butNP = window->get_aspect_2d().attach_new_node(but);
// tweak the button size a wee bit
butNP.set_scale(0.25, 1.0, 0.075);
//do the main loop, iterating the event handler etc.
framework.main_loop();
then in this case, although correctly displayed, the button is not interactive (can’t be clicked, doesn’t change to rollover state etc).
So, what have I missed in the second approach that means the button doesn’t work?