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