gui events

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
aburt11
Posts: 66
Joined: Sun Jan 15, 2012 10:27 am

gui events

Post by aburt11 »

hey there,
im ahving trouble understanding how to use the ieventreciever for gui events: here is the reciever:

Code: Select all

 
class MyEventReceiver : public IEventReceiver
{
public:
 
    // Enumeration for UP, DOWN, PRESSED and RELEASED key states. Also used for mouse button states.
    enum keyStatesENUM {UP, DOWN, PRESSED, RELEASED};
 
    // Keyboard key states.
    keyStatesENUM keyState[KEY_KEY_CODES_COUNT];
 
    // Mouse data.
    struct mouseData
    {
        int X;
        int Y;
        bool LButtonDown;
        bool RButtonDown;
        bool MButtonDown;
        float Wheel; 
        float WheelDelta;
    };
 
    struct mouseData m_Mouse;
    int MouseX()         { return m_Mouse.X;            };
    int MouseY()         { return m_Mouse.Y;            };
    float MouseWheel()      { return m_Mouse.Wheel;         };
    float MouseWheelDelta()   { return m_Mouse.WheelDelta;   };
 
    MyEventReceiver(void)         
    {
        for (int i = 0; i < KEY_KEY_CODES_COUNT; i++) keyState[i] = RELEASED;
        m_Mouse.X = 0;
        m_Mouse.Y = 0;
        m_Mouse.Wheel = 0;
        m_Mouse.WheelDelta = 0;
        m_Mouse.LButtonDown = false;
        m_Mouse.RButtonDown = false;
        m_Mouse.MButtonDown = false;
    };
 
    virtual ~MyEventReceiver(void)   {};
 
    // keyboard events
    virtual bool OnKeyInputEvent(const SEvent& e)   {   return false;                                          };
    virtual bool IsKeyPressed(char keycode)         {   return (keyState[keycode] == PRESSED);                        };
    virtual bool IsKeyDown(char keycode)         {   return (keyState[keycode] == DOWN || keyState[keycode] == PRESSED); };
    virtual bool IsKeyUp(char keycode)            {   return (keyState[keycode] == UP || keyState[keycode] == RELEASED);   };
    virtual bool IsKeyReleased(char keycode)      {   return (keyState[keycode] == RELEASED);                        };
 
    // guievents
    virtual bool OnButtonClicked(const SEvent& e)         {   return false;   };
    virtual bool OnScrollBarChanged(const SEvent& e)      {   return false;   };
    virtual bool OnCheckBoxChanged(const SEvent& e)         {   return false;   };
    virtual bool OnListBoxChanged(const SEvent& e)         {   return false;   };
    virtual bool OnListBoxSelectedAgain(const SEvent& e)   {   return false;   };
    virtual bool OnFileSelected(const SEvent& e)         {   return false;   };
    virtual bool OnMessageBoxYes(const SEvent& e)         {   return false;   };
    virtual bool OnMessageBoxNo(const SEvent& e)         {   return false;   };
    virtual bool OnMessageBoxOk(const SEvent& e)         {   return false;   };
    virtual bool OnMessageBoxCancel(const SEvent& e)      {   return false;   };
    virtual bool OnEditBoxEnter(const SEvent& e)         {   return false;   };
    virtual bool OnTabChanged(const SEvent& e)            {   return false;   };
    virtual bool OnComboBoxChanged(const SEvent& e)         {   return false;   };
    virtual bool OnSpinBoxChanged(const SEvent& e)         {   return false;   };
 
    // mouse events
    virtual bool OnLMousePressedDown(const SEvent& e)      {   return false;   };
    virtual bool OnRMousePressedDown(const SEvent& e)      {   return false;   };
    virtual bool OnMMousePressedDown(const SEvent& e)      {   return false;   };
    virtual bool OnLMouseLeftUp(const SEvent& e)         {   return false;   };
    virtual bool OnRMouseLeftUp(const SEvent& e)         {   return false;   };
    virtual bool OnMMouseLeftUp(const SEvent& e)         {   return false;   };
    virtual bool OnMouseMoved(const SEvent& e)            {   return false;   };
    virtual bool OnMouseWheel(const SEvent& e)            {   return false;   };
 
    // user events
    virtual bool OnUserEvent(const SEvent& e)            {   return false;   };
 
    virtual bool OnEvent(const SEvent& e)
    {
 
        switch (e.EventType)
        {
        case EET_KEY_INPUT_EVENT : 
            {
                if (e.KeyInput.PressedDown == true)
                {
                    if (keyState[e.KeyInput.Key] != DOWN) 
                        keyState[e.KeyInput.Key] = PRESSED; 
                    else keyState[e.KeyInput.Key] = DOWN; 
                }
                else
                    if (keyState[e.KeyInput.Key] != UP)   
                        keyState[e.KeyInput.Key] = RELEASED; 
 
                if (e.KeyInput.PressedDown == true) return OnKeyInputEvent(e); 
            }    break;
 
        case EET_GUI_EVENT :
            {
                switch (e.GUIEvent.EventType)
                {
                case EGET_BUTTON_CLICKED         : return OnButtonClicked(e);
                case EGET_SCROLL_BAR_CHANGED      : return OnScrollBarChanged(e);
                case EGET_CHECKBOX_CHANGED         : return OnCheckBoxChanged(e);
                case EGET_LISTBOX_CHANGED         : return OnListBoxChanged(e);
                case EGET_LISTBOX_SELECTED_AGAIN   : return OnListBoxSelectedAgain(e);
                case EGET_FILE_SELECTED            : return OnFileSelected(e);
                case EGET_MESSAGEBOX_YES         : return OnMessageBoxYes(e);
                case EGET_MESSAGEBOX_NO            : return OnMessageBoxNo(e);
                case EGET_MESSAGEBOX_OK            : return OnMessageBoxOk(e);
                case EGET_MESSAGEBOX_CANCEL         : return OnMessageBoxCancel(e);
                case EGET_EDITBOX_ENTER            : return OnEditBoxEnter(e);
                case EGET_TAB_CHANGED            : return OnTabChanged(e);
                case EGET_COMBO_BOX_CHANGED         : return OnComboBoxChanged(e);
                case EGET_SPINBOX_CHANGED         : return OnSpinBoxChanged(e);
                default : return false;
                }
            } break; 
 
        case EET_MOUSE_INPUT_EVENT :
            {
                m_Mouse.X = e.MouseInput.X;
                m_Mouse.Y = e.MouseInput.Y;
 
                switch (e.MouseInput.Event)
                {
                case EMIE_LMOUSE_PRESSED_DOWN   :   m_Mouse.LButtonDown = true; return OnLMousePressedDown(e);
                case EMIE_RMOUSE_PRESSED_DOWN   :   m_Mouse.RButtonDown = true; return OnRMousePressedDown(e);
                case EMIE_MMOUSE_PRESSED_DOWN   :   m_Mouse.MButtonDown = true; return OnMMousePressedDown(e);
                case EMIE_LMOUSE_LEFT_UP      :   m_Mouse.LButtonDown = false; return OnLMouseLeftUp(e);
                case EMIE_RMOUSE_LEFT_UP      :   m_Mouse.RButtonDown = false; return OnRMouseLeftUp(e);
                case EMIE_MMOUSE_LEFT_UP      :   m_Mouse.MButtonDown = false; return OnMMouseLeftUp(e);
                case EMIE_MOUSE_MOVED         :   return OnMouseMoved(e);
                case EMIE_MOUSE_WHEEL         :   
                    {
                        m_Mouse.WheelDelta = m_Mouse.Wheel - e.MouseInput.Wheel; 
                        m_Mouse.Wheel += e.MouseInput.Wheel;
                        return OnMouseWheel(e);
                    }
                default : return false;
                }
            } break;
 
        case EET_USER_EVENT : 
            {
                return OnUserEvent(e);
        default : return false;
            } break;
        }
        return false;
    
    }
 
 
 
};
 
my question is how should the if statement look for this gui event
e.g.
if(receiver.onguibtnclick(GUI_ID_OR_SOMETHING))
{
//do stuff
}

thanks,
zerochen
Posts: 273
Joined: Wed Jan 07, 2009 1:17 am
Location: Germany

Re: gui events

Post by zerochen »

something like this:

Code: Select all

 
class MyClass : public MyEventReceiver
{
      virtual bool OnButtonClicked(const SEvent& event)         
      { 
        switch(event.GUIEvent.Caller->getID())
        {
        case EGE_BACK_BUTTON:
            //do stuff
            return true;
        default:
            return false;
        }
      };
}
 
aburt11
Posts: 66
Joined: Sun Jan 15, 2012 10:27 am

Re: gui events

Post by aburt11 »

okay, ive added that stuff but still nothing happens when i press the button

Code: Select all

 
//gui id codes
enum 
{
    GUI_ID_SERVER_BROWSER = 1,
    GUI_ID_QUIT,
    GUI_ID_SETTINGS,
    GUI_ID_REFRESH_SERVER,
    GUI_ID_ENTER_SERVER,
    GUI_ID_BACK,
    INGAME_GUI_ID_EXIT_TO_MAIN,
 
};
 
 
class MyEventReceiver : public IEventReceiver
{
public:
 
    // Enumeration for UP, DOWN, PRESSED and RELEASED key states. Also used for mouse button states.
    enum keyStatesENUM {UP, DOWN, PRESSED, RELEASED};
 
    // Keyboard key states.
    keyStatesENUM keyState[KEY_KEY_CODES_COUNT];
 
    // Mouse data.
    struct mouseData
    {
        int X;
        int Y;
        bool LButtonDown;
        bool RButtonDown;
        bool MButtonDown;
        float Wheel; 
        float WheelDelta;
    };
 
    struct mouseData m_Mouse;
    int MouseX()         { return m_Mouse.X;            };
    int MouseY()         { return m_Mouse.Y;            };
    float MouseWheel()      { return m_Mouse.Wheel;         };
    float MouseWheelDelta()   { return m_Mouse.WheelDelta;   };
 
    MyEventReceiver(void)         
    {
        for (int i = 0; i < KEY_KEY_CODES_COUNT; i++) keyState[i] = RELEASED;
        m_Mouse.X = 0;
        m_Mouse.Y = 0;
        m_Mouse.Wheel = 0;
        m_Mouse.WheelDelta = 0;
        m_Mouse.LButtonDown = false;
        m_Mouse.RButtonDown = false;
        m_Mouse.MButtonDown = false;
    };
 
    virtual ~MyEventReceiver(void)   {};
 
    // keyboard events
    virtual bool OnKeyInputEvent(const SEvent& e)   {   return false;                                          };
    virtual bool IsKeyPressed(char keycode)         {   return (keyState[keycode] == PRESSED);                        };
    virtual bool IsKeyDown(char keycode)         {   return (keyState[keycode] == DOWN || keyState[keycode] == PRESSED); };
    virtual bool IsKeyUp(char keycode)            {   return (keyState[keycode] == UP || keyState[keycode] == RELEASED);   };
    virtual bool IsKeyReleased(char keycode)      {   return (keyState[keycode] == RELEASED);                        };
 
    // guievents
    //virtual bool OnButtonClicked(const SEvent& e)         {   return false;   };
    virtual bool OnButtonClicked(const SEvent& event)         
    { 
        switch(event.GUIEvent.Caller->getID())
        {
        case GUI_ID_QUIT:
            //do stuff
            shutdownGame();
            return true;
 
        case GUI_ID_SERVER_BROWSER:
            loadServerBrowserState();
            
            return true;
        default:
            return false;
        }
    };
 
    virtual bool OnScrollBarChanged(const SEvent& e)      {   return false;   };
    virtual bool OnCheckBoxChanged(const SEvent& e)         {   return false;   };
    virtual bool OnListBoxChanged(const SEvent& e)         {   return false;   };
    virtual bool OnListBoxSelectedAgain(const SEvent& e)   {   return false;   };
    virtual bool OnFileSelected(const SEvent& e)         {   return false;   };
    virtual bool OnMessageBoxYes(const SEvent& e)         {   return false;   };
    virtual bool OnMessageBoxNo(const SEvent& e)         {   return false;   };
    virtual bool OnMessageBoxOk(const SEvent& e)         {   return false;   };
    virtual bool OnMessageBoxCancel(const SEvent& e)      {   return false;   };
    virtual bool OnEditBoxEnter(const SEvent& e)         {   return false;   };
    virtual bool OnTabChanged(const SEvent& e)            {   return false;   };
    virtual bool OnComboBoxChanged(const SEvent& e)         {   return false;   };
    virtual bool OnSpinBoxChanged(const SEvent& e)         {   return false;   };
 
    // mouse events
    virtual bool OnLMousePressedDown(const SEvent& e)      {   return false;   };
    virtual bool OnRMousePressedDown(const SEvent& e)      {   return false;   };
    virtual bool OnMMousePressedDown(const SEvent& e)      {   return false;   };
    virtual bool OnLMouseLeftUp(const SEvent& e)         {   return false;   };
    virtual bool OnRMouseLeftUp(const SEvent& e)         {   return false;   };
    virtual bool OnMMouseLeftUp(const SEvent& e)         {   return false;   };
    virtual bool OnMouseMoved(const SEvent& e)            {   return false;   };
    virtual bool OnMouseWheel(const SEvent& e)            {   return false;   };
 
    // user events
    virtual bool OnUserEvent(const SEvent& e)            {   return false;   };
 
    virtual bool OnEvent(const SEvent& e)
    {
 
        switch (e.EventType)
        {
        case EET_KEY_INPUT_EVENT : 
            {
                if (e.KeyInput.PressedDown == true)
                {
                    if (keyState[e.KeyInput.Key] != DOWN) 
                        keyState[e.KeyInput.Key] = PRESSED; 
                    else keyState[e.KeyInput.Key] = DOWN; 
                }
                else
                    if (keyState[e.KeyInput.Key] != UP)   
                        keyState[e.KeyInput.Key] = RELEASED; 
 
                if (e.KeyInput.PressedDown == true) return OnKeyInputEvent(e); 
            }    break;
 
        case EET_GUI_EVENT :
            {
                switch (e.GUIEvent.EventType)
                {
                case EGET_BUTTON_CLICKED         : return OnButtonClicked(e);
                case EGET_SCROLL_BAR_CHANGED      : return OnScrollBarChanged(e);
                case EGET_CHECKBOX_CHANGED         : return OnCheckBoxChanged(e);
                case EGET_LISTBOX_CHANGED         : return OnListBoxChanged(e);
                case EGET_LISTBOX_SELECTED_AGAIN   : return OnListBoxSelectedAgain(e);
                case EGET_FILE_SELECTED            : return OnFileSelected(e);
                case EGET_MESSAGEBOX_YES         : return OnMessageBoxYes(e);
                case EGET_MESSAGEBOX_NO            : return OnMessageBoxNo(e);
                case EGET_MESSAGEBOX_OK            : return OnMessageBoxOk(e);
                case EGET_MESSAGEBOX_CANCEL         : return OnMessageBoxCancel(e);
                case EGET_EDITBOX_ENTER            : return OnEditBoxEnter(e);
                case EGET_TAB_CHANGED            : return OnTabChanged(e);
                case EGET_COMBO_BOX_CHANGED         : return OnComboBoxChanged(e);
                case EGET_SPINBOX_CHANGED         : return OnSpinBoxChanged(e);
                default : return false;
                }
            } break; 
 
        case EET_MOUSE_INPUT_EVENT :
            {
                m_Mouse.X = e.MouseInput.X;
                m_Mouse.Y = e.MouseInput.Y;
 
                switch (e.MouseInput.Event)
                {
                case EMIE_LMOUSE_PRESSED_DOWN   :   m_Mouse.LButtonDown = true; return OnLMousePressedDown(e);
                case EMIE_RMOUSE_PRESSED_DOWN   :   m_Mouse.RButtonDown = true; return OnRMousePressedDown(e);
                case EMIE_MMOUSE_PRESSED_DOWN   :   m_Mouse.MButtonDown = true; return OnMMousePressedDown(e);
                case EMIE_LMOUSE_LEFT_UP      :   m_Mouse.LButtonDown = false; return OnLMouseLeftUp(e);
                case EMIE_RMOUSE_LEFT_UP      :   m_Mouse.RButtonDown = false; return OnRMouseLeftUp(e);
                case EMIE_MMOUSE_LEFT_UP      :   m_Mouse.MButtonDown = false; return OnMMouseLeftUp(e);
                case EMIE_MOUSE_MOVED         :   return OnMouseMoved(e);
                case EMIE_MOUSE_WHEEL         :   
                    {
                        m_Mouse.WheelDelta = m_Mouse.Wheel - e.MouseInput.Wheel; 
                        m_Mouse.Wheel += e.MouseInput.Wheel;
                        return OnMouseWheel(e);
                    }
                default : return false;
                }
            } break;
 
        case EET_USER_EVENT : 
            {
                return OnUserEvent(e);
        default : return false;
            } break;
        }
        return false;
    
    }
 
 
 
};
 
 
ive also given the enum ids to the buttons like so
\

Code: Select all

//quit
    BtnQuit = guienv->addButton(core::recti(0,450,507,607),0,GUI_ID_QUIT);
    BtnQuit->setImage(driver->getTexture("assets/gui/menu/quit_unselected.png"));
    BtnQuit->setUseAlphaChannel(true);
    BtnQuit->setDrawBorder(0);
 
    //settings
    BtnSettings = guienv->addButton(core::recti(0,450,507,507),0,GUI_ID_SETTINGS);
    BtnSettings->setImage(driver->getTexture("assets/gui/menu/settings_unselected.png"));
    BtnSettings->setUseAlphaChannel(true);
    BtnSettings->setDrawBorder(0);
    
could anyone help me with this dilemma, im starting to get quite confused and angry haha
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: gui events

Post by CuteAlien »

Do you register your eventreceiver? Also make sure your OnMouse functions return false so they don't catch the mouse-events before they even reach the gui.

If that's not it then set a breakpoint in your eventreceiver and step through it in your debugger to see what's going on.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
aburt11
Posts: 66
Joined: Sun Jan 15, 2012 10:27 am

Re: gui events

Post by aburt11 »

i've tried all that but still no luck, set breakpoints and the terms arent even given as true or false, is there any way to initialize it or utilise the onbuttonclicked in a loop?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: gui events

Post by CuteAlien »

aburt11 wrote:i've tried all that but still no luck, set breakpoints and the terms arent even given as true or false
Sorry, I don't know what you mean with that sentence. A breakpoint stops the program at the point where you set it. So you can then step through it line by line and simply see what is going on instead of having to guess. So set one in the first line of the eventreceiver. If it never stops then you have not added the evenreceiver correct. If it stops then set next one inside the EET_GUI_EVENT case in the first line there. And then clicks something and step through it line by line in the debugger to see what is happening.

You really don't need our help for figuring out such simple stuff, just learn to use the debugger - it is really, really easy to use - you need to click about 5 buttons, that's all.

- Set breakpoint (or toggle breakpoint) or even just clicking at the start of the line (depends on your IDE) sets a breakpoint in the line where you want the program to stop
- With start debugging your start your program in debugging mode (or F5 in VS or F8 in c::b). Same key also continues it once it is stopped.
And then for going through your program line-by-line you have:
- Step over (or next line) which jumps to the next line in the current function (F10 in VS)
- Step into goes to the next line executed - going into functions instead of just jumping over them (and as parameters are often passed around with copy-constructors you might land a few times in constructors and stuff - just step out again of those and then do another step into )
- Step out does jump out of the current function (so you are back one level higher on the callstack.
Additionally in VS you can (sometimes) even set any line which should be executed next so you really change the order of execution, but that's already stuff you won't need for now.

Knowing those things above is already enough to figure out what is going on in your case. Note than in old C::B versions you sometimes had to press continue twice after program start to get the program running as it stopped automatically for no good reason (just as long as it doesn't stop at your breakpoint press continue).

There's certainly more a debugger can show - you can enable debug-windows which have information like:
- Callstack, that's often the most useful one, it shows the current stack of functions. So main() is always at the bottom and above it are the function it runs right now and the function that function is running right now up to the top-function which is the function in which you are in this moment. It also shows the value of the function parameters when the functions where called.
- Watches - you can watch which values each variable has right now (in VS there is also autos and locals to see the most interesting watches automatically). Usually you also see the values by going over it with the mouse. You can even change the value of variables that way.
- Memory - you can directly give a memory address and see what is in there (often useful for debugging binary stream stuff for example)

There's more stuff, for example conditional breakpoints which don't stop each time and you can set even breakpoints that check constantly if a variable ever changes. And more output for advanced things etc... but you rarely will need those (at least for now).

Ok, now please figure out what is happening - set breakpoints, run in debug and use the 3 commands to step around - those 5 buttons should really be all you need in your case.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
aburt11
Posts: 66
Joined: Sun Jan 15, 2012 10:27 am

Re: gui events

Post by aburt11 »

what i meant was is that i dont think the method is being called?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: gui events

Post by CuteAlien »

If the eventreceiver is not called then your problem is that you do not add it correctly.

edit: Or you never call device->run().
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply