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