前篇的框架中,有一个事件处理函数:
virtual bool HandleEvent( IEvent* pEvent );
这个回调函数用于在自己的应用程序中响应各种事件, IEvent的定义如下:
class IEvent { public: virtual std::wstring GetEventName( ) = 0; virtual eEVENT GetEventType( ) = 0; };
主要用到GetEventType来获取事件类型,目前支持的有:
enum eEVENT { //GUI_CURSOR_OVER, //GUI_CURSOR_ENTER, //GUI_CURSOR_LEAVE, //GUI_CURSOR_BUTTON_DOWN, //GUI_CURSOR_BUTTON_PRESSED, //GUI_CURSOR_BUTTON_UP, //GUI_UI_BUTTON_PRESSED, RENDER_FRAME_START, SYSTEM_LBUTTON_DOWN, SYSTEM_LBUTTON_UP, SYSTEM_MBUTTON_DOWN, SYSTEM_MBUTTON_UP, SYSTEM_RBUTTON_DOWN, SYSTEM_RBUTTON_UP, SYSTEM_MOUSE_LEAVE, SYSTEM_MOUSE_WHEEL, SYSTEM_MOUSE_MOVE, SYSTEM_KEYBOARD_KEYDOWN, SYSTEM_KEYBOARD_KEYUP, SYSTEM_KEYBOARD_CHAR, WINDOW_RESIZE, LOG_MESSAGE, INFO_MESSAGE, ERROR_MESSAGE, //TEXT_CONTROLLER_STATE_REQUEST, //ENTITY3D_CONTROLLER_STATE_REQUEST, NUM_EVENTS };
你可以根据需要自行添加.
让我们来看一个示例:
View Code
1 bool App::HandleEvent( IEvent* pEvent ) 2 { 3 eEVENT e = pEvent->GetEventType(); 4 5 if ( e == SYSTEM_KEYBOARD_KEYDOWN ) 6 { 7 EvtKeyDown* pKeyDown = (EvtKeyDown*)pEvent; 8 9 unsigned int key = pKeyDown->GetCharacterCode(); 10 11 return( true ); 12 } 13 else if ( e == SYSTEM_KEYBOARD_KEYUP ) 14 { 15 EvtKeyUp* pKeyUp = (EvtKeyUp*)pEvent; 16 17 unsigned int key = pKeyUp->GetCharacterCode(); 18 19 if ( key == VK_ESCAPE ) // 'Esc' Key - Exit the application 20 { 21 this->RequestTermination(); 22 return( true ); 23 } 24 else if ( key == 0x53 ) // 'S' Key - Save a screen shot for the next frame 25 { 26 m_bSaveScreenshot = true; 27 return( true ); 28 } 29 else 30 { 31 return( false ); 32 } 33 } 34 35 // Call the parent class's event handler if we haven't handled the event. 36 37 return( Application::HandleEvent( pEvent ) ); 38 }
那么这个回调何时被响应呢?答案是这个由你来控制,一般情况下,在update里调用这个方法就行了:
EventManager::Get()->ProcessEvent();