Complete event handler

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.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

jurgel wrote:or should I put in this thread?
please post here, that way future questions have the answers all in one place.
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

Is there any reason not to store it on the wiki instead of in the forum?
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

either way is fine with me. I personally dont use the wiki, just the forums, but since digital space is cheap, i say go crazy and post it in two or three places :) Since I havent seen the patch, I cant speak to it. When I get time, i will repair the original code and repost. Since we dont use that function of the handler, it never arose as an issue for us. I am glad that someone else saw it though.
jurgel
Posts: 7
Joined: Sat Oct 02, 2010 2:11 pm

Post by jurgel »

Seven wrote:
jurgel wrote:or should I put in this thread?
please post here, that way future questions have the answers all in one place.
ok,
I'm divide your code to .h and .cpp

here it's

.h

Code: Select all

#ifndef __frameListener_h__
#define __frameListener_h__

#include <irrlicht.h>


using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;


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};

   enum processStateENUM {STARTED, ENDED};

   // Keyboard key states.
   keyStatesENUM keyState[KEY_KEY_CODES_COUNT];

   processStateENUM eventState;

   // 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);

   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);

   virtual void startEventState();
   virtual void endEventState();

};


#endif //#ifndef __frameListener_h__
.cpp

Code: Select all

#include "frameListener.h"


//------------------------------------------------------------------------------
MyEventReceiver::MyEventReceiver(void)
{
    for (int i = 0; i < KEY_KEY_CODES_COUNT; i++) keyState[i] = UP;
    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;
};
//------------------------------------------------------------------------------
bool MyEventReceiver::OnEvent(const SEvent& e)
{
      switch (e.EventType)
      {
         case EET_KEY_INPUT_EVENT :
            {
                if (eventState == STARTED){
               if (e.KeyInput.PressedDown == true)
               {
                  if (keyState[e.KeyInput.Key] == PRESSED)
                     keyState[e.KeyInput.Key] = DOWN;
                  else if (keyState[e.KeyInput.Key] != DOWN)
                     keyState[e.KeyInput.Key] = PRESSED;
               }
               else
               {
                  if (keyState[e.KeyInput.Key] == RELEASED)
                     keyState[e.KeyInput.Key] = UP;
                  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;
}
//------------------------------------------------------------------------------
void MyEventReceiver::startEventState()
{
    eventState = STARTED;

    for (int i = 0; i < KEY_KEY_CODES_COUNT; i++)
      {
         if (keyState[i] == RELEASED)
         {
            keyState[i] = UP;
         }

         if (keyState[i] == PRESSED)
         {
            keyState[i] = DOWN;
         }
      }
}
//------------------------------------------------------------------------------
void MyEventReceiver::endEventState()
{
    eventState = ENDED;
}
//------------------------------------------------------------------------------
please correct if i'm wrong, :D


*edit
ah, I'm forgot to say,
you must add :
endEventState() at first "while(device->run())"
and :
startEventState() at last

it's to disable change when drawing
Post Reply