00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "sdlmm_config.h"
00022
00023
00024 #include <SDL.h>
00025 #include "sdlmm_eventhandler.h"
00026 #include "sdlmm_event.h"
00027 #include <iostream>
00028 namespace SDLmm {
00029
00030 bool Event::Poll(bool fetch) {
00031 if(fetch) {
00032 return SDL_PollEvent(&me) != 0;
00033 } else {
00034 return SDL_PollEvent(0) != 0;
00035 }
00036 }
00037
00038 bool Event::Wait(bool fetch) {
00039 if(fetch) {
00040 return SDL_WaitEvent(&me) != 0;
00041 } else {
00042 return SDL_WaitEvent(0) != 0;
00043 }
00044 }
00045
00046 bool Event::Push() {
00047 return SDL_PushEvent(&me) == 0;
00048 }
00049
00050 void Event::PumpEvents() {
00051 SDL_PumpEvents();
00052 }
00053
00054 void Event::SetEventFilter(SDL_EventFilter filter) {
00055 SDL_SetEventFilter(filter);
00056 }
00057
00058 SDL_EventFilter Event::GetEventFilter() {
00059 return SDL_GetEventFilter();
00060 }
00061
00062 Uint8 Event::EventState(Uint8 type, int state) {
00063 return SDL_EventState(type, state);
00064 }
00065
00066 Uint8 *Event::GetKeyState(int &numkeys) {
00067 return SDL_GetKeyState(&numkeys);
00068 }
00069
00070 Uint8 *Event::GetKeyState() {
00071 return SDL_GetKeyState(0);
00072 }
00073
00074 SDLMod Event::GetModState() {
00075 return SDL_GetModState();
00076 }
00077
00078 char *Event::GetKeyName(SDLKey key) {
00079 return SDL_GetKeyName(key);
00080 }
00081
00082 void Event::SetModState(SDLMod modstate) {
00083 SDL_SetModState(modstate);
00084 }
00085
00086 bool Event::EnableKeyRepeat(int delay, int interval) {
00087 return SDL_EnableKeyRepeat(delay, interval) == 0;
00088 }
00089
00090 Uint8 Event::GetMouseState(int *x, int *y) {
00091 return SDL_GetMouseState(x, y);
00092 }
00093
00094 Uint8 Event::GetRelativeMouseState(int *x, int *y) {
00095 return SDL_GetRelativeMouseState(x, y);
00096 }
00097 Uint8 Event::GetAppState() {
00098 return SDL_GetAppState();
00099 }
00100 int Event::JoystickEventState(int state) {
00101 return SDL_JoystickEventState(state);
00102 }
00103
00104 void Event::HandleEvents(EventHandler &handler) {
00105 SDL_Event event;
00106 while(SDL_PollEvent(&event)) {
00107 HandleEvent(handler, event);
00108 }
00109 }
00110
00111 void Event::HandleEvent(EventHandler &handler, const SDL_Event& event) {
00112 bool ev_handled = false;
00113 switch(event.type) {
00114 case SDL_ACTIVEEVENT:
00115 ev_handled = handler.HandleActiveEvent(event.active.gain == 1,
00116 event.active.state);
00117 break;
00118 case SDL_KEYDOWN:
00119 ev_handled = handler.HandleKeyPressEvent(event.key.keysym);
00120 break;
00121 case SDL_KEYUP:
00122 ev_handled = handler.HandleKeyReleaseEvent(event.key.keysym);
00123 break;
00124 case SDL_MOUSEMOTION:
00125 ev_handled = handler.HandleMouseMotionEvent(event.motion.state,
00126 event.motion.x,
00127 event.motion.y,
00128 event.motion.xrel,
00129 event.motion.yrel);
00130 break;
00131 case SDL_MOUSEBUTTONDOWN:
00132 ev_handled = handler.HandleMouseButtonPressEvent(event.button.button,
00133 event.button.x,
00134 event.button.y);
00135 break;
00136 case SDL_MOUSEBUTTONUP:
00137 ev_handled = handler.HandleMouseButtonReleaseEvent(event.button.button,
00138 event.button.x,
00139 event.button.y);
00140 break;
00141 case SDL_JOYAXISMOTION:
00142 ev_handled = handler.HandleJoyAxisEvent(event.jaxis.which,
00143 event.jaxis.axis,
00144 event.jaxis.value);
00145 break;
00146 case SDL_JOYBALLMOTION:
00147 ev_handled = handler.HandleJoyBallEvent(event.jball.which,
00148 event.jball.ball,
00149 event.jball.xrel,
00150 event.jball.yrel);
00151 break;
00152 case SDL_JOYHATMOTION:
00153 ev_handled = handler.HandleJoyHatEvent(event.jhat.which,
00154 event.jhat.hat,
00155 event.jhat.value);
00156 break;
00157 case SDL_JOYBUTTONDOWN:
00158 ev_handled = handler.HandleJoyButtonPressEvent(event.jbutton.which,
00159 event.jbutton.button);
00160 break;
00161 case SDL_JOYBUTTONUP:
00162 ev_handled = handler.HandleJoyButtonReleaseEvent(event.jbutton.which,
00163 event.jbutton.button);
00164 break;
00165 case SDL_QUIT:
00166 ev_handled = handler.HandleQuitEvent();
00167 break;
00168 case SDL_SYSWMEVENT:
00169 ev_handled = handler.HandleSysWMEvent();
00170 break;
00171 case SDL_VIDEORESIZE:
00172 ev_handled = handler.HandleResizeEvent(event.resize.w, event.resize.h);
00173 break;
00174 case SDL_USEREVENT:
00175 ev_handled = handler.HandleUserEvent(event.user.type,
00176 event.user.code,
00177 event.user.data1,
00178 event.user.data2);
00179 break;
00180 }
00181 if(!ev_handled) {
00182 handler.HandleEvent(event);
00183 }
00184 }
00185 }
00186