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