irrlichtEventReceiver.cpp

00001 
00002 
00003 #include "main.h"
00004 #include "irrlichtEventReceiver.h"
00005 
00006 
00007 // the maximum number of milliseconds allowed between mouse clicks for a double-click to be valid
00008 #define IRR_DOUBLE_CLICK_TIME   300
00009 
00010 
00011 
00012 
00013 IrrlichtEventReceiver::IrrlichtEventReceiver()
00014 : theOneToCallOnKeyEvent(NULL),
00015   theOneToCallOnMouseButtonLeftEvent(NULL),
00016   theOneToCallOnMouseButtonLeftReleaseEvent(NULL),
00017   theOneToCallOnMouseButtonLeftDoubleClickEvent(NULL),
00018   theOneToCallOnMouseButtonRightEvent(NULL),
00019   theOneToCallOnMouseButtonRightReleaseEvent(NULL),
00020   theOneToCallOnMouseMoveEvent(NULL)
00021 {
00022         logger->log( "IrrlichtEventReceiver ctor()" );
00023         device->setEventReceiver( this );
00024         
00025         // setup initial double click time
00026         if ( NULL != timer)
00027         {
00028                 this->previousMouseButtonLeftClick = timer->getRealTime();
00029         }
00030 }
00031 
00032 
00033 IrrlichtEventReceiver::~IrrlichtEventReceiver()
00034 {
00035         logger->log( "IrrlichtEventReceiver dtor()" );
00036         device->setEventReceiver( NULL );
00037         this->theOneToCallOnKeyEvent = NULL;
00038         this->theOneToCallOnMouseButtonLeftEvent = NULL;
00039         this->theOneToCallOnMouseButtonLeftReleaseEvent = NULL;
00040         this->theOneToCallOnMouseButtonLeftDoubleClickEvent = NULL;
00041         this->theOneToCallOnMouseButtonRightEvent = NULL;
00042         this->theOneToCallOnMouseMoveEvent = NULL;
00043 }
00044 
00045 
00046 ERR_TYPE IrrlichtEventReceiver::registerKeyEventCallback( irrlichtKeyEventCallback_TYPE anyCallBackFunction )
00047 {
00048         this->theOneToCallOnKeyEvent = anyCallBackFunction;
00049         return ERR_OK;
00050 }
00051 
00052 
00053 ERR_TYPE IrrlichtEventReceiver::registerMouseButtonLeftEventCallback( irrlichtMouseButtonEventCallback_TYPE anyCallBackFunction )
00054 {
00055         this->theOneToCallOnMouseButtonLeftEvent = anyCallBackFunction;
00056         return ERR_OK;
00057 }
00058 
00059 
00060 ERR_TYPE IrrlichtEventReceiver::registerMouseButtonLeftReleaseEventCallback( irrlichtMouseButtonReleaseEventCallback_TYPE anyCallBackFunction )
00061 {
00062         this->theOneToCallOnMouseButtonLeftReleaseEvent = anyCallBackFunction;
00063         return ERR_OK;
00064 }
00065 
00066 
00067 ERR_TYPE IrrlichtEventReceiver::registerMouseButtonLeftDoubleClickEventCallback( irrlichtMouseButtonEventCallback_TYPE anyCallBackFunction )
00068 {
00069         this->theOneToCallOnMouseButtonLeftDoubleClickEvent = anyCallBackFunction;
00070         return ERR_OK;
00071 }
00072 
00073 
00074 ERR_TYPE IrrlichtEventReceiver::registerMouseButtonRightEventCallback( irrlichtMouseButtonEventCallback_TYPE anyCallBackFunction )
00075 {
00076         this->theOneToCallOnMouseButtonRightEvent = anyCallBackFunction;
00077         return ERR_OK;
00078 }
00079 
00080 
00081 ERR_TYPE IrrlichtEventReceiver::registerMouseButtonRightReleaseEventCallback( irrlichtMouseButtonReleaseEventCallback_TYPE anyCallBackFunction )
00082 {
00083         this->theOneToCallOnMouseButtonRightReleaseEvent = anyCallBackFunction;
00084         return ERR_OK;
00085 }
00086 
00087 
00088 ERR_TYPE IrrlichtEventReceiver::registerMouseMoveEventCallback( irrlichtMouseMoveEventCallback_TYPE anyCallBackFunction )
00089 {
00090         this->theOneToCallOnMouseMoveEvent = anyCallBackFunction;
00091         return ERR_OK;
00092 }
00093 
00094 
00095 bool IrrlichtEventReceiver::OnEvent( SEvent event )
00096 {
00097         ERR_TYPE status = ERR_OK;
00098 
00099         // forward key event to registered function
00100         if ( event.EventType == EET_KEY_INPUT_EVENT && (! event.KeyInput.PressedDown) )
00101         {
00102                 if ( NULL != theOneToCallOnKeyEvent )
00103                 {
00104                         status = theOneToCallOnKeyEvent( event.KeyInput.Key );
00105                         if ( ERR_EVENT_CONSUMED == status )
00106                         {
00107                                 return true;
00108                         }
00109                 }
00110         }
00111 
00112         // forward mouse left button double click event to registered function
00113         if ( event.EventType == EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP )
00114         {
00115                 u32 now = timer->getRealTime();
00116 
00117                 // a double-click is detected if the the time elapsed since the last click is within doubleClickTime
00118                 if ( (now - this->previousMouseButtonLeftClick) <= IRR_DOUBLE_CLICK_TIME  )
00119                 {
00120                         if ( NULL != theOneToCallOnMouseButtonLeftDoubleClickEvent )
00121                         {
00122                                 status = theOneToCallOnMouseButtonLeftDoubleClickEvent( event.MouseInput.X, event.MouseInput.Y, event.MouseInput.Wheel );
00123                         }
00124                 }
00125                 
00126                 // set previousClick to now so that subsequent double-clicks can be detected
00127                 this->previousMouseButtonLeftClick = now;
00128 
00129                 if ( ERR_EVENT_CONSUMED == status )
00130                 {
00131                         return true;
00132                 }
00133         }
00134 
00135         // forward left button down event to registered function
00136         if ( event.EventType == EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN )
00137         {
00138                 if ( NULL != theOneToCallOnMouseButtonLeftEvent )
00139                 {
00140                         status = theOneToCallOnMouseButtonLeftEvent( event.MouseInput.X, event.MouseInput.Y, event.MouseInput.Wheel );
00141                         if ( ERR_EVENT_CONSUMED == status )
00142                         {
00143                                 return true;
00144                         }
00145                 }
00146         }
00147 
00148         // forward left button up event to registered function
00149         if ( event.EventType == EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP )
00150         {
00151                 if ( NULL != theOneToCallOnMouseButtonLeftReleaseEvent )
00152                 {
00153                         status = theOneToCallOnMouseButtonLeftReleaseEvent();
00154                         if ( ERR_EVENT_CONSUMED == status )
00155                         {
00156                                 return true;
00157                         }
00158                 }
00159         }
00160 
00161         // forward right button down event to registered function
00162         if ( event.EventType == EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN )
00163         {
00164                 if ( NULL != theOneToCallOnMouseButtonRightEvent )
00165                 {
00166                         status = theOneToCallOnMouseButtonRightEvent( event.MouseInput.X, event.MouseInput.Y, event.MouseInput.Wheel );
00167                         if ( ERR_EVENT_CONSUMED == status )
00168                         {
00169                                 return true;
00170                         }
00171                 }
00172         }
00173 
00174         // forward right button up event to registered function
00175         if ( event.EventType == EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP )
00176         {
00177                 if ( NULL != theOneToCallOnMouseButtonRightReleaseEvent )
00178                 {
00179                         status = theOneToCallOnMouseButtonRightReleaseEvent();
00180                         if ( ERR_EVENT_CONSUMED == status )
00181                         {
00182                                 return true;
00183                         }
00184                 }
00185         }
00186 
00187         // forward mouse move event to registered function
00188         if ( event.EventType == EET_MOUSE_INPUT_EVENT )
00189         {
00190                 if ( NULL != theOneToCallOnMouseMoveEvent && event.MouseInput.Event == EMIE_MOUSE_MOVED )
00191                 {
00192                         status = theOneToCallOnMouseMoveEvent( event.MouseInput.X, event.MouseInput.Y );
00193                         if ( ERR_EVENT_CONSUMED == status )
00194                         {
00195                                 return true;
00196                         }
00197                 }
00198         }
00199         
00200         // event not consumed
00201         return false;
00202 }

Generated on Sun Dec 2 03:10:23 2007 for TableTop by  doxygen 1.5.4