GUI in C++

Here comes my weekly C++ question :slight_smile:

I’m trying to use Panda’s GUI classes (button, entry, etc) in C++, but i’m having issues with the event handling.

Let’s take PGEntry for example…
This is how I create it:

PT(PGEntry) input = new PGEntry("ConsoleInput");
input->setup(50, 1);	
input->set_focus(true);	
NodePath inputNP = window->get_render_2d().attach_new_node(input);

However, this basic setup doesn’t seem to handle any mouse clicks or keystrokes at all. How to accomplish this I can’t quite understand, or why I need to.

I searched around the forums and tried this:


framework.get_event_handler().add_hook(input->get_keystroke_event(), event_test);

and elsewhere:
void event_test(const Event* e) 
{
	nout << "LOl?";
}

The syntax is right, because it compiles right, and if I attach it to input->get_focus_in_event(), it does execute when I call input->set_focus(true). But I have no luck with either keystrokes or press events.

Seeing how neither any text I enter is actually entered, and that my keyboard/mouse events don’t fire, I suspect that for some reason I need to assign it some sort of a watcher, or something like that, but I couldn’t find anything about how to do this, not in the manual or forums.

Ps, i’ve tried most of the functions in the class that didn’t require some obscure parameters to call, for example:
input->set_accept_enabled(true);
input->set_active(true);
input->set_cursor_keys_active(true);
input->set_max_chars(1000);
with no luck.

Pps, the event handlers are definetly registered, when I call
framework.get_event_handler().has_hook(input->get_keystroke_event()); it returns true

ppps… I’ve tried with and without window->enable_keyboard();

You’re right, you were almost there. The magic change you need to make is to replace:

NodePath inputNP = window->get_render_2d().attach_new_node(input);

With:

NodePath inputNP = window->get_aspect_2d().attach_new_node(input);

The reason this is necessary is that all of the PGItem objects have to be a child, either directly or indirectly, of a PGTop node in order to hook up to the MouseWatcher properly. As it happens, render2d is not a PGTop node, but aspect2d is.

David

Sweet, that worked perfectly, thanks, once again!