| [4780] | 1 | /* | 
|---|
| [4329] | 2 | orxonox - the future of 3D-vertical-scrollers | 
|---|
|  | 3 |  | 
|---|
|  | 4 | Copyright (C) 2004 orx | 
|---|
|  | 5 |  | 
|---|
|  | 6 | This program is free software; you can redistribute it and/or modify | 
|---|
|  | 7 | it under the terms of the GNU General Public License as published by | 
|---|
|  | 8 | the Free Software Foundation; either version 2, or (at your option) | 
|---|
|  | 9 | any later version. | 
|---|
|  | 10 |  | 
|---|
|  | 11 | ### File Specific: | 
|---|
| [4346] | 12 | main-programmer: Patrick Boenzli | 
|---|
| [4780] | 13 | co-programmer: | 
|---|
| [4329] | 14 | */ | 
|---|
|  | 15 |  | 
|---|
| [8340] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT | 
|---|
| [4329] | 17 |  | 
|---|
| [4346] | 18 | #include "event_handler.h" | 
|---|
| [4388] | 19 |  | 
|---|
| [4350] | 20 | #include "event_listener.h" | 
|---|
| [4361] | 21 | #include "event.h" | 
|---|
| [4388] | 22 | #include "key_mapper.h" | 
|---|
| [8148] | 23 | #include "key_names.h" | 
|---|
| [4329] | 24 |  | 
|---|
| [4381] | 25 | #include "compiler.h" | 
|---|
|  | 26 | #include "debug.h" | 
|---|
| [4352] | 27 |  | 
|---|
| [7756] | 28 | #include <algorithm> | 
|---|
| [4329] | 29 |  | 
|---|
|  | 30 | /** | 
|---|
| [7919] | 31 | * @brief standard constructor | 
|---|
|  | 32 | */ | 
|---|
| [4780] | 33 | EventHandler::EventHandler () | 
|---|
| [4329] | 34 | { | 
|---|
| [5285] | 35 | this->setClassID(CL_EVENT_HANDLER, "EventHandler"); | 
|---|
|  | 36 | this->setName("EventHandler"); | 
|---|
|  | 37 |  | 
|---|
| [5236] | 38 | SDL_InitSubSystem(SDL_INIT_JOYSTICK); | 
|---|
|  | 39 | SDL_InitSubSystem(SDL_INIT_EVENTTHREAD); | 
|---|
| [5371] | 40 | SDL_SetEventFilter(EventHandler::eventFilter); | 
|---|
| [5236] | 41 |  | 
|---|
| [4350] | 42 |  | 
|---|
| [4355] | 43 | /* now initialize them all to zero */ | 
|---|
| [7919] | 44 | for (unsigned int i = 0; i < ES_NUMBER; i++) | 
|---|
|  | 45 | this->bUNICODE[i] = false; | 
|---|
| [5978] | 46 | this->grabEvents(false); | 
|---|
| [5291] | 47 |  | 
|---|
| [4407] | 48 | this->state = ES_GAME; | 
|---|
| [6802] | 49 | this->eventsGrabbed = false; | 
|---|
| [8743] | 50 |  | 
|---|
|  | 51 | this->keyState = SDL_GetKeyState(NULL); | 
|---|
| [4329] | 52 | } | 
|---|
|  | 53 |  | 
|---|
| [4407] | 54 |  | 
|---|
| [4329] | 55 | /** | 
|---|
| [7919] | 56 | * @brief the singleton reference to this class | 
|---|
| [4329] | 57 | */ | 
|---|
| [4346] | 58 | EventHandler* EventHandler::singletonRef = NULL; | 
|---|
| [4329] | 59 |  | 
|---|
| [4407] | 60 |  | 
|---|
| [4329] | 61 | /** | 
|---|
| [7919] | 62 | * @brief standard deconstructor | 
|---|
|  | 63 | */ | 
|---|
| [4780] | 64 | EventHandler::~EventHandler () | 
|---|
| [4329] | 65 | { | 
|---|
| [8148] | 66 | bool forgotToUnsubscribe = false; | 
|---|
|  | 67 |  | 
|---|
| [4817] | 68 | for(int i = 0; i < ES_NUMBER; ++i) | 
|---|
|  | 69 | { | 
|---|
| [4866] | 70 | for(int j = 0; j < EV_NUMBER; ++j) | 
|---|
| [4817] | 71 | { | 
|---|
| [7756] | 72 | if(!this->listeners[i][j].empty()) | 
|---|
| [4817] | 73 | { | 
|---|
| [8148] | 74 | if (!forgotToUnsubscribe) | 
|---|
|  | 75 | { | 
|---|
|  | 76 | forgotToUnsubscribe = true; | 
|---|
|  | 77 | PRINTF(2)("forgot to unsubscribe an EventListener!\n");// %s!\n", this->listeners[i][j]->getName()); | 
|---|
|  | 78 | } | 
|---|
| [4817] | 79 | } | 
|---|
|  | 80 | } | 
|---|
|  | 81 | } | 
|---|
| [8148] | 82 |  | 
|---|
|  | 83 | if (forgotToUnsubscribe) | 
|---|
|  | 84 | { | 
|---|
|  | 85 | PRINTF(2)("Listing still subscribed EventListeners\n"); | 
|---|
|  | 86 | PRINTF(2)("========================================\n"); | 
|---|
|  | 87 | this->debug(); | 
|---|
|  | 88 | PRINTF(2)("========================================\n"); | 
|---|
|  | 89 | } | 
|---|
|  | 90 |  | 
|---|
| [5236] | 91 | SDL_QuitSubSystem(SDL_INIT_JOYSTICK); | 
|---|
|  | 92 |  | 
|---|
| [4866] | 93 | EventHandler::singletonRef = NULL; | 
|---|
| [4352] | 94 | } | 
|---|
| [4329] | 95 |  | 
|---|
| [4352] | 96 |  | 
|---|
| [4450] | 97 | /** | 
|---|
| [7919] | 98 | * @brief initializes the event handler | 
|---|
| [5285] | 99 | * | 
|---|
|  | 100 | * this has to be called before the use of the event handler | 
|---|
| [4450] | 101 | */ | 
|---|
| [7256] | 102 | void EventHandler::init() | 
|---|
| [4407] | 103 | { | 
|---|
| [7756] | 104 | this->keyMapper.loadKeyBindings(); | 
|---|
| [4407] | 105 | } | 
|---|
|  | 106 |  | 
|---|
| [4450] | 107 | /** | 
|---|
| [7919] | 108 | * @param state: to which the event handler shall change | 
|---|
|  | 109 | */ | 
|---|
|  | 110 | void EventHandler::setState(elState state) | 
|---|
|  | 111 | { | 
|---|
|  | 112 | if (state == this->state) | 
|---|
|  | 113 | return; | 
|---|
|  | 114 |  | 
|---|
| [8339] | 115 | PRINTF(4)("Switching to State %s\n", EventHandler::ELStateToString(state).c_str()); | 
|---|
|  | 116 |  | 
|---|
| [7919] | 117 | /// When Changing the State, all the keys will be released. | 
|---|
|  | 118 | /// This is done by sending each eventListener, that still | 
|---|
|  | 119 | /// has an Event subscribed, a Release Event. | 
|---|
|  | 120 | int keyCount; | 
|---|
|  | 121 | Uint8 * pressedKeys = SDL_GetKeyState(&keyCount); | 
|---|
|  | 122 | for (unsigned int i = 0; i < SDLK_LAST; i++) | 
|---|
|  | 123 | { | 
|---|
|  | 124 | if (pressedKeys[i]) | 
|---|
|  | 125 | { | 
|---|
|  | 126 | Event ev; | 
|---|
|  | 127 | ev.bPressed = false; | 
|---|
|  | 128 | ev.type = i; | 
|---|
|  | 129 | if (unlikely(this->bUNICODE[this->state])) | 
|---|
|  | 130 | ev.x = i; | 
|---|
|  | 131 | this->dispachEvent(this->state, ev ); | 
|---|
|  | 132 | } | 
|---|
|  | 133 | } | 
|---|
|  | 134 |  | 
|---|
|  | 135 | // switching to the new State. | 
|---|
|  | 136 | elState oldState = this->state; | 
|---|
|  | 137 | this->state = state; | 
|---|
|  | 138 |  | 
|---|
|  | 139 | // in the End the Corresponding handler will be notified. | 
|---|
|  | 140 | Event stateSwitchEvent; | 
|---|
|  | 141 | stateSwitchEvent.type = EV_LEAVE_STATE; | 
|---|
|  | 142 | this->dispachEvent(oldState, stateSwitchEvent); | 
|---|
|  | 143 |  | 
|---|
|  | 144 | SDL_EnableUNICODE(this->bUNICODE[state]); | 
|---|
|  | 145 | }; | 
|---|
|  | 146 |  | 
|---|
|  | 147 |  | 
|---|
|  | 148 | /** | 
|---|
|  | 149 | * @brief pushes the current State in the State-stack, and selects state | 
|---|
| [5388] | 150 | * @param state the new State to set | 
|---|
|  | 151 | */ | 
|---|
|  | 152 | void EventHandler::pushState(elState state) | 
|---|
|  | 153 | { | 
|---|
| [7164] | 154 | if (likely(state != ES_NULL && state != ES_ALL )) | 
|---|
| [5388] | 155 | { | 
|---|
| [7164] | 156 | this->stateStack.push(this->state); | 
|---|
| [5388] | 157 | this->setState(state); | 
|---|
|  | 158 | } | 
|---|
|  | 159 | else | 
|---|
|  | 160 | { | 
|---|
|  | 161 | PRINTF(2)("unable to push State\n"); | 
|---|
|  | 162 | } | 
|---|
|  | 163 | } | 
|---|
|  | 164 |  | 
|---|
|  | 165 | /** | 
|---|
| [7919] | 166 | * @brief this removes the topmost stack-entry and select the underlying one | 
|---|
| [5388] | 167 | * @returns the next stack-entry | 
|---|
|  | 168 | */ | 
|---|
|  | 169 | elState EventHandler::popState() | 
|---|
|  | 170 | { | 
|---|
| [7166] | 171 | if (stateStack.empty()) | 
|---|
|  | 172 | return ES_NULL; | 
|---|
| [7164] | 173 | elState state =  (elState)stateStack.top(); | 
|---|
|  | 174 | this->stateStack.pop(); | 
|---|
| [5388] | 175 | if (state == ES_NULL) | 
|---|
|  | 176 | { | 
|---|
|  | 177 | PRINTF(2)("No more states availiable. (unable to pop state)\n"); | 
|---|
|  | 178 | return ES_NULL; | 
|---|
|  | 179 | } | 
|---|
|  | 180 | else | 
|---|
|  | 181 | { | 
|---|
|  | 182 | this->setState(state); | 
|---|
|  | 183 | return state; | 
|---|
|  | 184 | } | 
|---|
|  | 185 | } | 
|---|
|  | 186 |  | 
|---|
|  | 187 |  | 
|---|
|  | 188 | /** | 
|---|
| [7756] | 189 | * @brief subscribe to an event | 
|---|
| [4836] | 190 | * @param el: the event listener that wants to subscribe itself, the listener that will be called when the evetn occures | 
|---|
|  | 191 | * @param state: for which the listener wants to receive events | 
|---|
|  | 192 | * @param eventType: the event type that wants to be listened for. | 
|---|
| [4450] | 193 |  | 
|---|
|  | 194 | This is one of the most important function of the EventHandler. If you would like to subscribe for more | 
|---|
|  | 195 | than one state, you have to subscribe for each state again. If you want to subscribe for all states, use | 
|---|
|  | 196 | state = ES_ALL, which will subscribe your listener for all states together. | 
|---|
| [5291] | 197 | */ | 
|---|
| [4405] | 198 | void EventHandler::subscribe(EventListener* el, elState state, int eventType) | 
|---|
| [4354] | 199 | { | 
|---|
| [4450] | 200 | PRINTF(4)("Subscribing event type: %i\n", eventType); | 
|---|
| [4407] | 201 | if( state == ES_ALL ) | 
|---|
| [6771] | 202 | { | 
|---|
|  | 203 | for(unsigned int i = 0; i < ES_NUMBER; i++) | 
|---|
| [7919] | 204 | if (!this->findListener( NULL, (elState)i, eventType, el)) | 
|---|
|  | 205 | this->listeners[i][eventType].push_back(el); | 
|---|
|  | 206 | else | 
|---|
| [7756] | 207 | { | 
|---|
| [9406] | 208 | PRINTF(2)("%s::%s was already subscribed to state %d event %d\n", el->getClassCName(), el->getCName(), i, eventType); | 
|---|
| [7756] | 209 | } | 
|---|
| [6771] | 210 | } | 
|---|
|  | 211 | else | 
|---|
| [7756] | 212 | { | 
|---|
| [7919] | 213 | if (!this->findListener( NULL, state, eventType, el)) | 
|---|
|  | 214 | this->listeners[state][eventType].push_back(el); | 
|---|
|  | 215 | else | 
|---|
| [4407] | 216 | { | 
|---|
| [9406] | 217 | PRINTF(2)("%s::%s was already subscribed to state %d event %d\n", el->getClassCName(), el->getCName(), state, eventType); | 
|---|
| [4407] | 218 | } | 
|---|
| [7756] | 219 | } | 
|---|
| [4354] | 220 | } | 
|---|
|  | 221 |  | 
|---|
|  | 222 |  | 
|---|
| [4450] | 223 | /** | 
|---|
| [7756] | 224 | * @brief unsubscribe from the EventHandler | 
|---|
| [4836] | 225 | * @param state: the stat in which it has been subscribed | 
|---|
|  | 226 | * @param eventType: the event, that shall be unsubscribed | 
|---|
| [7919] | 227 | * | 
|---|
|  | 228 | * if you want to unsubscribe an event listener from all subscribed events, just use the | 
|---|
|  | 229 | * unsubscribe(EventListener* el, elState state) function | 
|---|
|  | 230 | */ | 
|---|
| [7868] | 231 | void EventHandler::unsubscribe(EventListener* el, elState state, int eventType) | 
|---|
| [4355] | 232 | { | 
|---|
| [4450] | 233 | PRINTF(4)("Unsubscribing event type nr: %i\n", eventType); | 
|---|
| [5291] | 234 | if (state == ES_ALL) | 
|---|
|  | 235 | for (unsigned int i = 0; i < ES_NUMBER; i++) | 
|---|
| [7868] | 236 | { | 
|---|
| [7919] | 237 | std::vector<EventListener*>::iterator listener; | 
|---|
|  | 238 | if (this->findListener(&listener, (elState)i, eventType, el)) | 
|---|
| [7868] | 239 | this->listeners[i][eventType].erase(listener); | 
|---|
|  | 240 | } | 
|---|
| [5291] | 241 | else | 
|---|
| [7868] | 242 | { | 
|---|
| [7919] | 243 | std::vector<EventListener*>::iterator listener; | 
|---|
|  | 244 | if (this->findListener(&listener, state, eventType, el)) | 
|---|
| [7868] | 245 | this->listeners[state][eventType].erase(listener); | 
|---|
|  | 246 | } | 
|---|
| [4355] | 247 | } | 
|---|
| [4354] | 248 |  | 
|---|
| [4450] | 249 |  | 
|---|
|  | 250 | /** | 
|---|
| [7868] | 251 | * @brief unsubscribe all events from a specific listener | 
|---|
| [4836] | 252 | * @param el: the listener that wants to unsubscribe itself | 
|---|
|  | 253 | * @param state: the state in which the events shall be unsubscribed | 
|---|
| [7868] | 254 | */ | 
|---|
| [4450] | 255 | void EventHandler::unsubscribe(EventListener* el, elState state) | 
|---|
| [4355] | 256 | { | 
|---|
| [7919] | 257 | assert( el != NULL && state < ES_NUMBER); | 
|---|
| [4364] | 258 | if( state == ES_ALL) | 
|---|
| [6771] | 259 | { | 
|---|
|  | 260 | for(unsigned int i = 0; i < ES_NUMBER; i++) | 
|---|
| [4355] | 261 | { | 
|---|
| [6771] | 262 | for(unsigned int j = 0; j < EV_NUMBER; j++) | 
|---|
|  | 263 | { | 
|---|
| [7756] | 264 | std::vector<EventListener*>::iterator deller = std::find (this->listeners[i][j].begin(), this->listeners[i][j].end(), el); | 
|---|
|  | 265 | if( deller != this->listeners[i][j].end()) | 
|---|
|  | 266 | this->listeners[i][j].erase(deller); | 
|---|
| [6771] | 267 | } | 
|---|
| [4364] | 268 | } | 
|---|
| [6771] | 269 | } | 
|---|
| [4364] | 270 | else | 
|---|
| [6771] | 271 | { | 
|---|
|  | 272 | for(int j = 0; j < EV_NUMBER; j++) | 
|---|
| [4364] | 273 | { | 
|---|
| [7756] | 274 | std::vector<EventListener*>::iterator deller =  std::find (this->listeners[state][j].begin(), this->listeners[state][j].end(), el); | 
|---|
|  | 275 | if( deller != this->listeners[state][j].end()) | 
|---|
|  | 276 | this->listeners[state][j].erase(deller); | 
|---|
| [4355] | 277 | } | 
|---|
| [6771] | 278 | } | 
|---|
| [4355] | 279 | } | 
|---|
|  | 280 |  | 
|---|
| [7919] | 281 | /** | 
|---|
|  | 282 | * @brief returns true if at state and eventType there is something subscribed. | 
|---|
|  | 283 | * @param state the state to check in. | 
|---|
|  | 284 | * @param eventType the eventtype to check. | 
|---|
|  | 285 | * @returns true if a event is subscibed. | 
|---|
|  | 286 | */ | 
|---|
| [7756] | 287 | bool EventHandler::isSubscribed(elState state, int eventType) | 
|---|
|  | 288 | { | 
|---|
|  | 289 | return(listeners[state][eventType].empty()) ? false : true; | 
|---|
|  | 290 | }; | 
|---|
| [4450] | 291 |  | 
|---|
| [7756] | 292 |  | 
|---|
|  | 293 |  | 
|---|
| [4450] | 294 | /** | 
|---|
| [7919] | 295 | * @brief flush all registered events | 
|---|
| [4836] | 296 | * @param state: a specific state | 
|---|
| [4450] | 297 | */ | 
|---|
|  | 298 | void EventHandler::flush(elState state) | 
|---|
| [4420] | 299 | { | 
|---|
|  | 300 | if( state == ES_ALL) | 
|---|
| [6771] | 301 | { | 
|---|
|  | 302 | for(int i = 0; i < ES_NUMBER; ++i) | 
|---|
| [4420] | 303 | { | 
|---|
| [6771] | 304 | for(int j = 0; j < EV_NUMBER; ++j) | 
|---|
|  | 305 | { | 
|---|
| [7756] | 306 | this->listeners[i][j].clear(); | 
|---|
| [6771] | 307 | } | 
|---|
| [4420] | 308 | } | 
|---|
| [6771] | 309 | } | 
|---|
| [4420] | 310 | else | 
|---|
| [6771] | 311 | { | 
|---|
|  | 312 | for(int j = 0; j < EV_NUMBER; ++j) | 
|---|
| [4420] | 313 | { | 
|---|
| [7756] | 314 | this->listeners[state][j].clear(); | 
|---|
| [4420] | 315 | } | 
|---|
| [6771] | 316 | } | 
|---|
| [4420] | 317 | } | 
|---|
| [4355] | 318 |  | 
|---|
|  | 319 |  | 
|---|
| [7919] | 320 | bool EventHandler::findListener(std::vector<EventListener*>::iterator* it, elState state, int eventType, EventListener* listener) | 
|---|
| [5786] | 321 | { | 
|---|
| [7919] | 322 | std::vector<EventListener*>::iterator findIterator = | 
|---|
|  | 323 | std::find(this->listeners[state][eventType].begin(), this->listeners[state][eventType].end(), listener); | 
|---|
|  | 324 | if (it != NULL) | 
|---|
|  | 325 | *it = findIterator; | 
|---|
|  | 326 | return ( findIterator != this->listeners[state][eventType].end()); | 
|---|
|  | 327 |  | 
|---|
| [5786] | 328 | } | 
|---|
|  | 329 |  | 
|---|
| [7919] | 330 |  | 
|---|
|  | 331 |  | 
|---|
|  | 332 | /** | 
|---|
|  | 333 | * @brief if the unicode characters should be recorded. | 
|---|
|  | 334 | * @param state the State in whitch to set the new Value. | 
|---|
|  | 335 | * @param enableUNICODE: enabled, or disabled. | 
|---|
|  | 336 | */ | 
|---|
|  | 337 | void EventHandler::withUNICODE(elState state, bool enableUNICODE) | 
|---|
|  | 338 | { | 
|---|
|  | 339 | this->bUNICODE[state] = enableUNICODE; | 
|---|
|  | 340 | if (this->state == state) | 
|---|
|  | 341 | SDL_EnableUNICODE(enableUNICODE); | 
|---|
|  | 342 | } | 
|---|
|  | 343 |  | 
|---|
|  | 344 | /** | 
|---|
|  | 345 | * @brief grabs InputEvents. | 
|---|
|  | 346 | * @param grabEvents if the Events should be grabbed(true) or released(false) | 
|---|
|  | 347 | */ | 
|---|
| [5978] | 348 | void EventHandler::grabEvents(bool grabEvents) | 
|---|
|  | 349 | { | 
|---|
| [6054] | 350 | this->eventsGrabbed = grabEvents; | 
|---|
|  | 351 | if(!grabEvents) | 
|---|
| [6990] | 352 | { | 
|---|
|  | 353 | SDL_ShowCursor(SDL_ENABLE); | 
|---|
| [6054] | 354 | SDL_WM_GrabInput(SDL_GRAB_OFF); | 
|---|
| [6990] | 355 | } | 
|---|
| [6054] | 356 | else | 
|---|
| [6990] | 357 | { | 
|---|
| [8992] | 358 | SDL_WM_GrabInput(SDL_GRAB_ON); | 
|---|
|  | 359 | SDL_ShowCursor(SDL_DISABLE); | 
|---|
| [6990] | 360 | } | 
|---|
| [5978] | 361 | } | 
|---|
| [5786] | 362 |  | 
|---|
| [7919] | 363 |  | 
|---|
|  | 364 |  | 
|---|
| [4450] | 365 | /** | 
|---|
| [7919] | 366 | * @brief core function of event handler: receives all events from SDL | 
|---|
|  | 367 | * | 
|---|
|  | 368 | * The event from the SDL framework are collected here and distributed to all listeners. | 
|---|
|  | 369 | */ | 
|---|
|  | 370 | void EventHandler::process() const | 
|---|
| [4355] | 371 | { | 
|---|
|  | 372 | SDL_Event event; | 
|---|
| [4361] | 373 | Event ev; | 
|---|
| [4355] | 374 | while( SDL_PollEvent (&event)) | 
|---|
| [6771] | 375 | { | 
|---|
|  | 376 | switch( event.type) | 
|---|
| [4355] | 377 | { | 
|---|
| [6771] | 378 | case SDL_KEYDOWN: | 
|---|
|  | 379 | ev.bPressed = true; | 
|---|
|  | 380 | ev.type = event.key.keysym.sym; | 
|---|
| [7919] | 381 | if (unlikely(this->bUNICODE[this->state])) | 
|---|
| [6771] | 382 | ev.x = event.key.keysym.unicode; | 
|---|
|  | 383 | break; | 
|---|
|  | 384 | case SDL_KEYUP: | 
|---|
|  | 385 | ev.bPressed = false; | 
|---|
|  | 386 | ev.type = event.key.keysym.sym; | 
|---|
| [7919] | 387 | if (unlikely(this->bUNICODE[this->state])) | 
|---|
| [6771] | 388 | ev.x = event.key.keysym.unicode; | 
|---|
|  | 389 | break; | 
|---|
|  | 390 | case SDL_MOUSEMOTION: | 
|---|
|  | 391 | ev.bPressed = false; | 
|---|
|  | 392 | ev.type = EV_MOUSE_MOTION; | 
|---|
|  | 393 | ev.x = event.motion.x; | 
|---|
|  | 394 | ev.y = event.motion.y; | 
|---|
|  | 395 | ev.xRel = event.motion.xrel; | 
|---|
|  | 396 | ev.yRel = event.motion.yrel; | 
|---|
|  | 397 | break; | 
|---|
|  | 398 | case SDL_MOUSEBUTTONUP: | 
|---|
|  | 399 | ev.bPressed = false; | 
|---|
| [8035] | 400 | ev.x = event.motion.x; | 
|---|
|  | 401 | ev.y = event.motion.y; | 
|---|
| [6771] | 402 | ev.type = event.button.button + SDLK_LAST; | 
|---|
|  | 403 | break; | 
|---|
|  | 404 | case SDL_MOUSEBUTTONDOWN: | 
|---|
|  | 405 | ev.bPressed = true; | 
|---|
| [8035] | 406 | ev.x = event.motion.x; | 
|---|
|  | 407 | ev.y = event.motion.y; | 
|---|
| [6771] | 408 | ev.type = event.button.button + SDLK_LAST; | 
|---|
|  | 409 | break; | 
|---|
|  | 410 | case SDL_JOYAXISMOTION: | 
|---|
|  | 411 | ev.bPressed = false; | 
|---|
|  | 412 | ev.type = EV_JOY_AXIS_MOTION; | 
|---|
|  | 413 | break; | 
|---|
|  | 414 | case SDL_JOYBALLMOTION: | 
|---|
|  | 415 | ev.bPressed = false; | 
|---|
|  | 416 | ev.type = EV_JOY_BALL_MOTION; | 
|---|
|  | 417 | break; | 
|---|
|  | 418 | case SDL_JOYHATMOTION: | 
|---|
|  | 419 | ev.bPressed = false; | 
|---|
|  | 420 | ev.type = EV_JOY_HAT_MOTION; | 
|---|
|  | 421 | break; | 
|---|
|  | 422 | case SDL_JOYBUTTONDOWN: | 
|---|
|  | 423 | ev.bPressed = true; | 
|---|
|  | 424 | ev.type = EV_JOY_BUTTON; | 
|---|
|  | 425 | break; | 
|---|
|  | 426 | case SDL_JOYBUTTONUP: | 
|---|
|  | 427 | ev.bPressed = true; | 
|---|
|  | 428 | ev.type = EV_JOY_BUTTON; | 
|---|
|  | 429 | break; | 
|---|
| [7919] | 430 | case SDL_ACTIVEEVENT: | 
|---|
|  | 431 | ev.type = EV_WINDOW_FOCUS; | 
|---|
|  | 432 | ev.bPressed = (event.active.gain != 0); | 
|---|
|  | 433 | break; | 
|---|
| [6771] | 434 | case SDL_VIDEORESIZE: | 
|---|
|  | 435 | ev.resize = event.resize; | 
|---|
|  | 436 | ev.type = EV_VIDEO_RESIZE; | 
|---|
|  | 437 | break; | 
|---|
|  | 438 | case SDL_QUIT: | 
|---|
|  | 439 | ev.type = EV_MAIN_QUIT; | 
|---|
|  | 440 | break; | 
|---|
|  | 441 | default: | 
|---|
|  | 442 | ev.type = EV_UNKNOWN; | 
|---|
|  | 443 | break; | 
|---|
|  | 444 | } | 
|---|
| [7919] | 445 | this->dispachEvent(this->state, ev); | 
|---|
|  | 446 | } | 
|---|
|  | 447 | } | 
|---|
| [4362] | 448 |  | 
|---|
| [4780] | 449 |  | 
|---|
| [7919] | 450 | /** | 
|---|
|  | 451 | * @brief dispaches an Event. | 
|---|
|  | 452 | * @param event the Event to dispach. | 
|---|
|  | 453 | */ | 
|---|
|  | 454 | void EventHandler::dispachEvent(elState state, const Event& event) const | 
|---|
|  | 455 | { | 
|---|
|  | 456 | /* small debug routine: shows all events dispatched by the event handler */ | 
|---|
|  | 457 | PRINT(4)("\n==========================| EventHandler::process () |===\n"); | 
|---|
|  | 458 | PRINT(4)("=  Got Event nr %i, for state %i\n", event.type, state); | 
|---|
| [7756] | 459 |  | 
|---|
| [7919] | 460 | /// setting a temporary state in case of an EventListener's process changes the state. | 
|---|
|  | 461 | for (unsigned int i = 0; i < this->listeners[state][event.type].size(); i++) | 
|---|
|  | 462 | { | 
|---|
|  | 463 | PRINT(4)("=  Event dispatcher msg: This event has been consumed\n"); | 
|---|
| [9406] | 464 | PRINT(4)("=  Got Event nr %i, for state %i (%d registered) to %s::%s(%p)\n", | 
|---|
|  | 465 | event.type, | 
|---|
|  | 466 | i, state, | 
|---|
|  | 467 | this->listeners[state][event.type][i]->getClassCName(), | 
|---|
|  | 468 | this->listeners[state][event.type][i]->getCName(), | 
|---|
|  | 469 | this->listeners[state][event.type][i]); | 
|---|
| [7919] | 470 | PRINT(4)("=======================================================\n"); | 
|---|
|  | 471 | this->listeners[state][event.type][i]->process(event); | 
|---|
| [6771] | 472 | } | 
|---|
| [7919] | 473 | /*    else | 
|---|
|  | 474 | { | 
|---|
|  | 475 | PRINT(4)("=  Event dispatcher msg: This event has NOT been consumed\n"); | 
|---|
|  | 476 | PRINT(4)("=======================================================\n"); | 
|---|
|  | 477 | }*/ | 
|---|
| [4355] | 478 | } | 
|---|
| [4388] | 479 |  | 
|---|
| [5371] | 480 |  | 
|---|
| [7919] | 481 |  | 
|---|
|  | 482 | /** | 
|---|
|  | 483 | * @brief An eventFilter. | 
|---|
|  | 484 | * @param event the Event to be filtered. | 
|---|
|  | 485 | * @returns 0 on filtered Event. 1 Otherwise. | 
|---|
|  | 486 | */ | 
|---|
| [5237] | 487 | int EventHandler::eventFilter(const SDL_Event *event) | 
|---|
|  | 488 | { | 
|---|
| [6802] | 489 | if (likely(EventHandler::getInstance()->eventsGrabbed)) | 
|---|
| [5237] | 490 | { | 
|---|
| [6802] | 491 | if (event->type == SDL_KEYDOWN &&  event->key.keysym.sym == SDLK_TAB && SDL_GetKeyState(NULL)[SDLK_LALT]) | 
|---|
|  | 492 | { | 
|---|
|  | 493 | PRINTF(3)("Not sending event to the WindowManager\n"); | 
|---|
|  | 494 | EventHandler::getInstance()->grabEvents(false); | 
|---|
|  | 495 | return 0; | 
|---|
|  | 496 | } | 
|---|
|  | 497 | } | 
|---|
|  | 498 | else | 
|---|
|  | 499 | { | 
|---|
|  | 500 | if (event->type == SDL_MOUSEBUTTONDOWN) | 
|---|
|  | 501 | { | 
|---|
|  | 502 | EventHandler::getInstance()->grabEvents( true); | 
|---|
| [8035] | 503 | return 1; | 
|---|
| [6802] | 504 | } | 
|---|
|  | 505 | } | 
|---|
| [6054] | 506 |  | 
|---|
| [5237] | 507 | return 1; | 
|---|
|  | 508 | } | 
|---|
|  | 509 |  | 
|---|
| [8148] | 510 |  | 
|---|
| [8623] | 511 | int EventHandler::releaseMouse(void* p) | 
|---|
|  | 512 | { | 
|---|
| [9240] | 513 | SDL_ShowCursor(SDL_ENABLE); | 
|---|
| [8623] | 514 | SDL_WM_GrabInput(SDL_GRAB_OFF); | 
|---|
|  | 515 | return 0; | 
|---|
|  | 516 | } | 
|---|
|  | 517 |  | 
|---|
|  | 518 |  | 
|---|
| [5237] | 519 | /** | 
|---|
| [8148] | 520 | * @param state The State to get the Name of. | 
|---|
|  | 521 | * @returns the Name of the State. | 
|---|
|  | 522 | */ | 
|---|
|  | 523 | const std::string& EventHandler::ELStateToString(elState state) | 
|---|
|  | 524 | { | 
|---|
|  | 525 | if (state < ES_NUMBER) | 
|---|
|  | 526 | return EventHandler::stateNames[state]; | 
|---|
|  | 527 | else | 
|---|
|  | 528 | return EventHandler::stateNames[5]; | 
|---|
|  | 529 | } | 
|---|
|  | 530 |  | 
|---|
|  | 531 | /** | 
|---|
|  | 532 | * @param stateName the Name of the State to retrieve. | 
|---|
|  | 533 | * @return the State given by the name | 
|---|
|  | 534 | */ | 
|---|
|  | 535 | elState EventHandler::StringToELState(const std::string& stateName) | 
|---|
|  | 536 | { | 
|---|
|  | 537 | for (unsigned int i = 0 ; i < ES_NUMBER; i++) | 
|---|
|  | 538 | if (stateName == EventHandler::stateNames[i]) | 
|---|
|  | 539 | return (elState)i; | 
|---|
|  | 540 | return ES_NULL; | 
|---|
|  | 541 | } | 
|---|
|  | 542 |  | 
|---|
|  | 543 | const std::string  EventHandler::stateNames[] = | 
|---|
|  | 544 | { | 
|---|
|  | 545 | "game", | 
|---|
|  | 546 | "game_menu", | 
|---|
|  | 547 | "menu", | 
|---|
|  | 548 | "shell", | 
|---|
|  | 549 | "all", | 
|---|
|  | 550 | "unknown", | 
|---|
|  | 551 | }; | 
|---|
|  | 552 |  | 
|---|
|  | 553 |  | 
|---|
|  | 554 | /** | 
|---|
| [7919] | 555 | * @brief outputs some nice information about the EventHandler | 
|---|
| [5237] | 556 | */ | 
|---|
| [4872] | 557 | void EventHandler::debug() const | 
|---|
|  | 558 | { | 
|---|
|  | 559 | PRINT(0)("===============================\n"); | 
|---|
|  | 560 | PRINT(0)(" EventHandle Debug Information \n"); | 
|---|
|  | 561 | PRINT(0)("===============================\n"); | 
|---|
|  | 562 | for(int i = 0; i < ES_NUMBER; ++i) | 
|---|
| [7919] | 563 | { | 
|---|
| [4872] | 564 | for(int j = 0; j < EV_NUMBER; ++j) | 
|---|
| [7756] | 565 | for (unsigned int evl = 0; evl < this->listeners[i][j].size(); evl++) | 
|---|
| [8148] | 566 | PRINT(0)("Event %s(%d) of State %s(%d) subscribed to %s (%p)\n", | 
|---|
|  | 567 | EVToKeyName(j).c_str(), j, | 
|---|
|  | 568 | ELStateToString((elState)i).c_str(), i, | 
|---|
| [9406] | 569 | this->listeners[i][j][evl]->getCName(), this->listeners[i][j][evl]); | 
|---|
| [7919] | 570 | } | 
|---|
| [4872] | 571 | PRINT(0)("============================EH=\n"); | 
|---|
|  | 572 | } | 
|---|