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