Event receiver

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Guest

Event receiver

Post by Guest »

I've got a small program that shows a splash screen, waits for a key to be pressed, then goes to a small gui menu. The key press event is recorded perfectly by the event receiver, but when i click on any of the buttons in the gui menu, no gui event is recieved. These are my event handling functions:

class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
return game->EventHandler(&event); //game is an instance of the Game class
}
};






bool Game::EventHandler(SEvent *event)
{
switch(stage)
{
case GS_SPLASHSCREEN:
{
if((event->EventType == EET_KEY_INPUT_EVENT && !event->KeyInput.PressedDown)
|| (event->EventType == EET_MOUSE_INPUT_EVENT && event->MouseInput.Event ==
EMIE_LMOUSE_PRESSED_DOWN))
{
stage = GS_MENU;
return true;
}
break;
}
case GS_MENU:
{
menu.EventHandler(event); //menu is an instance of the SSMenu class
return true;
}
}

return false;
}






void SSMenu::EventHandler(SEvent *event)
{
if(event->EventType == EET_GUI_EVENT)
{
s32 id = event->GUIEvent.Caller->getID();

switch(event->GUIEvent.EventType)
{
case EGET_BUTTON_CLICKED:
{
if(id == MB_PREV)
{
switch(menustage)
{
case MS_MAINPAGE:
menustage = MS_EXITED;
break;
}
}
break;
}
}
}
}
arokh
Posts: 9
Joined: Tue Oct 18, 2005 9:23 pm
Location: Germany

Post by arokh »

I am very new to Irrlicht but I am testing the gui functionality of Irrlicht, and I maybe had a similar problem. however:

Do your really mean that you have a button or is it a menu Item you are selecting (because you mentioned, that you show a "menu")? Because, if you are using menus and menuItems (e.g. menu = guienv->add(Context)Menu(); menu->addItem(L"Exit", 300, ...)) then you should catch this event like this:

Code: Select all

if (event.EventType == EET_GUI_EVENT)
{
  s32 id = event.GUIEvent.Caller->getID();
  IGUIEnvironment* env = device->getGUIEnvironment();
  switch(event.GUIEvent.EventType)
    {
      case EGET_MENU_ITEM_SELECTED:
      {
        IGUIContextMenu* menu = (IGUIContextMenu*)event.GUIEvent.Caller;
        s32 id = menu->getItemCommandId(menu->getSelectedItem());
        if (id == 300)  
        { 
          device->closeDevice();
          return true;
        }
      }
      break;
      ...
Guest

Post by Guest »

I meant that i have several buttons, that form a menu (eg singleplayer, multiplayer, options and exit buttons). the buttons don't have any functionality yet (except the exit button), but nothing happens when i click on them: they dont go down, and the exit button doesnt work either. Debugging showed that the Menu::EventHandler(SEvent *event) function was never called, and i never catch any events with type EET_GUI_EVENT.
arokh
Posts: 9
Joined: Tue Oct 18, 2005 9:23 pm
Location: Germany

Post by arokh »

hun, that they don't even go down sounds strange. can you post the code, where you add the buttons?
Guest

Post by Guest »

void SSMenu::Init()
{
menustage = MS_MAINPAGE;
level = 0;
ship = 0;
levels = 0;
ships = 0;
colors = 0;
gamemode = 0;
players = 0;
cpuplayers = 0;
difficulty = 0;

IGUISkin* skin = gl->guienv->getSkin();
IGUIFont* font = gl->guienv->getFont(
"Data/fonthaettenschweiler.bmp");
if(font)
skin->setFont(font);
skin->setColor(EGDC_BUTTON_TEXT, SColor(200,200,200,200));

gl->guienv->addButton(rect<s32>(10, 210, 150, 240),
0, MB_SP, L"Singleplayer");
gl->guienv->addButton(rect<s32>(10, 250, 150, 280),
0, MB_MP, L"Multiplayer");
gl->guienv->addButton(rect<s32>(10, 290, 150, 320),
0, MB_OPTIONS, L"Options");
gl->guienv->addButton(rect<s32>(10, 330, 150, 360),
0, MB_PREV, L"Exit");
gl->guienv->addStaticText(NAME, rect<s32>(5, 150, 300, 170), false, true, 0, 1234, 0);

background = gl->viddriver->getTexture(
"./Data/menuback.bmp");
}

as you can see, this code is part of a seperate "menu class". however, the buttons are added to the guienv variable, which is stored in a central "global variables" class.
Guest

Post by Guest »

Could it have something to do with my class structure? It is as follows:

Main.cpp: file that holds main function and event receiver. calls the functions of the Game class. File contains definitions of extern Game and Globals.

Globals class: holds global variables (device, scene mgr, gui env, etc), extern instance defined in main.cpp

Game class: holds functions for initializing and running the game. holds instances of f.e the Menu class. an event handler (which is called by the event receiver) and an extern instance defined in main.cpp

Menu class: class with variables and functions for the main menu screen. Holds a function that adds the buttons to the guienv in Globals.

If i have to post all my code, i will. I really hope someone can help me with this, cause till i solve this problem im stuck.
Guest

Post by Guest »

woops, guess i didn't try searching hard enough. There was another post in the forums with this problem: the solution is that my event handling function should return false if no event was handled, and true if it was. Now it works fine.
Post Reply