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