| [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 | */ |
|---|
| [4419] | 173 | void EventHandler::unsubscribe(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++) |
|---|
| [7756] | 178 | this->listeners[i][eventType].clear(); |
|---|
| [5291] | 179 | else |
|---|
| [7756] | 180 | this->listeners[state][eventType].clear(); |
|---|
| [4355] | 181 | } |
|---|
| [4354] | 182 | |
|---|
| [4450] | 183 | |
|---|
| 184 | /** |
|---|
| [5285] | 185 | * unsubscribe all events from a specific listener |
|---|
| [4836] | 186 | * @param el: the listener that wants to unsubscribe itself |
|---|
| 187 | * @param state: the state in which the events shall be unsubscribed |
|---|
| [4780] | 188 | |
|---|
| [4450] | 189 | */ |
|---|
| 190 | void EventHandler::unsubscribe(EventListener* el, elState state) |
|---|
| [4355] | 191 | { |
|---|
| [5291] | 192 | if( el == NULL || state >= ES_NUMBER) |
|---|
| [4816] | 193 | return; |
|---|
| [4364] | 194 | if( state == ES_ALL) |
|---|
| [6771] | 195 | { |
|---|
| 196 | for(unsigned int i = 0; i < ES_NUMBER; i++) |
|---|
| [4355] | 197 | { |
|---|
| [6771] | 198 | for(unsigned int j = 0; j < EV_NUMBER; j++) |
|---|
| 199 | { |
|---|
| [7756] | 200 | std::vector<EventListener*>::iterator deller = std::find (this->listeners[i][j].begin(), this->listeners[i][j].end(), el); |
|---|
| 201 | if( deller != this->listeners[i][j].end()) |
|---|
| 202 | this->listeners[i][j].erase(deller); |
|---|
| [6771] | 203 | } |
|---|
| [4364] | 204 | } |
|---|
| [6771] | 205 | } |
|---|
| [4364] | 206 | else |
|---|
| [6771] | 207 | { |
|---|
| 208 | for(int j = 0; j < EV_NUMBER; j++) |
|---|
| [4364] | 209 | { |
|---|
| [7756] | 210 | std::vector<EventListener*>::iterator deller = std::find (this->listeners[state][j].begin(), this->listeners[state][j].end(), el); |
|---|
| 211 | if( deller != this->listeners[state][j].end()) |
|---|
| 212 | this->listeners[state][j].erase(deller); |
|---|
| [4355] | 213 | } |
|---|
| [6771] | 214 | } |
|---|
| [4355] | 215 | } |
|---|
| 216 | |
|---|
| [7756] | 217 | bool EventHandler::isSubscribed(elState state, int eventType) |
|---|
| 218 | { |
|---|
| 219 | return(listeners[state][eventType].empty()) ? false : true; |
|---|
| 220 | }; |
|---|
| [4450] | 221 | |
|---|
| [7756] | 222 | |
|---|
| 223 | |
|---|
| [4450] | 224 | /** |
|---|
| [5285] | 225 | * flush all registered events |
|---|
| [4836] | 226 | * @param state: a specific state |
|---|
| [4450] | 227 | */ |
|---|
| 228 | void EventHandler::flush(elState state) |
|---|
| [4420] | 229 | { |
|---|
| 230 | if( state == ES_ALL) |
|---|
| [6771] | 231 | { |
|---|
| 232 | for(int i = 0; i < ES_NUMBER; ++i) |
|---|
| [4420] | 233 | { |
|---|
| [6771] | 234 | for(int j = 0; j < EV_NUMBER; ++j) |
|---|
| 235 | { |
|---|
| [7756] | 236 | this->listeners[i][j].clear(); |
|---|
| [6771] | 237 | } |
|---|
| [4420] | 238 | } |
|---|
| [6771] | 239 | } |
|---|
| [4420] | 240 | else |
|---|
| [6771] | 241 | { |
|---|
| 242 | for(int j = 0; j < EV_NUMBER; ++j) |
|---|
| [4420] | 243 | { |
|---|
| [7756] | 244 | this->listeners[state][j].clear(); |
|---|
| [4420] | 245 | } |
|---|
| [6771] | 246 | } |
|---|
| [4420] | 247 | } |
|---|
| [4355] | 248 | |
|---|
| 249 | |
|---|
| [5786] | 250 | void EventHandler::withUNICODE(bool enableUNICODE) |
|---|
| 251 | { |
|---|
| 252 | SDL_EnableUNICODE(enableUNICODE); |
|---|
| 253 | this->bUNICODE = enableUNICODE; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| [5978] | 256 | void EventHandler::grabEvents(bool grabEvents) |
|---|
| 257 | { |
|---|
| [6054] | 258 | this->eventsGrabbed = grabEvents; |
|---|
| 259 | if(!grabEvents) |
|---|
| [6990] | 260 | { |
|---|
| 261 | SDL_ShowCursor(SDL_ENABLE); |
|---|
| [6054] | 262 | SDL_WM_GrabInput(SDL_GRAB_OFF); |
|---|
| [6990] | 263 | } |
|---|
| [6054] | 264 | else |
|---|
| [6990] | 265 | { |
|---|
| [6813] | 266 | SDL_WM_GrabInput(SDL_GRAB_ON); |
|---|
| [6990] | 267 | SDL_ShowCursor(SDL_DISABLE); |
|---|
| 268 | } |
|---|
| [5978] | 269 | } |
|---|
| [5786] | 270 | |
|---|
| [4450] | 271 | /** |
|---|
| [4836] | 272 | * core function of event handler: receives all events from SDL |
|---|
| [4420] | 273 | |
|---|
| [4450] | 274 | The event from the SDL framework are collected here and distributed to all listeners. |
|---|
| 275 | */ |
|---|
| [4355] | 276 | void EventHandler::process() |
|---|
| 277 | { |
|---|
| 278 | SDL_Event event; |
|---|
| [4361] | 279 | Event ev; |
|---|
| [4407] | 280 | EventListener* listener = NULL; |
|---|
| [4355] | 281 | while( SDL_PollEvent (&event)) |
|---|
| [6771] | 282 | { |
|---|
| 283 | switch( event.type) |
|---|
| [4355] | 284 | { |
|---|
| [6771] | 285 | case SDL_KEYDOWN: |
|---|
| 286 | ev.bPressed = true; |
|---|
| 287 | ev.type = event.key.keysym.sym; |
|---|
| 288 | if (unlikely(this->bUNICODE)) |
|---|
| 289 | ev.x = event.key.keysym.unicode; |
|---|
| 290 | break; |
|---|
| 291 | case SDL_KEYUP: |
|---|
| 292 | ev.bPressed = false; |
|---|
| 293 | ev.type = event.key.keysym.sym; |
|---|
| 294 | if (unlikely(this->bUNICODE)) |
|---|
| 295 | ev.x = event.key.keysym.unicode; |
|---|
| 296 | break; |
|---|
| 297 | case SDL_MOUSEMOTION: |
|---|
| 298 | ev.bPressed = false; |
|---|
| 299 | ev.type = EV_MOUSE_MOTION; |
|---|
| 300 | ev.x = event.motion.x; |
|---|
| 301 | ev.y = event.motion.y; |
|---|
| 302 | ev.xRel = event.motion.xrel; |
|---|
| 303 | ev.yRel = event.motion.yrel; |
|---|
| 304 | break; |
|---|
| 305 | case SDL_MOUSEBUTTONUP: |
|---|
| 306 | ev.bPressed = false; |
|---|
| 307 | ev.type = event.button.button + SDLK_LAST; |
|---|
| 308 | break; |
|---|
| 309 | case SDL_MOUSEBUTTONDOWN: |
|---|
| 310 | ev.bPressed = true; |
|---|
| 311 | ev.type = event.button.button + SDLK_LAST; |
|---|
| 312 | break; |
|---|
| 313 | case SDL_JOYAXISMOTION: |
|---|
| 314 | ev.bPressed = false; |
|---|
| 315 | ev.type = EV_JOY_AXIS_MOTION; |
|---|
| 316 | break; |
|---|
| 317 | case SDL_JOYBALLMOTION: |
|---|
| 318 | ev.bPressed = false; |
|---|
| 319 | ev.type = EV_JOY_BALL_MOTION; |
|---|
| 320 | break; |
|---|
| 321 | case SDL_JOYHATMOTION: |
|---|
| 322 | ev.bPressed = false; |
|---|
| 323 | ev.type = EV_JOY_HAT_MOTION; |
|---|
| 324 | break; |
|---|
| 325 | case SDL_JOYBUTTONDOWN: |
|---|
| 326 | ev.bPressed = true; |
|---|
| 327 | ev.type = EV_JOY_BUTTON; |
|---|
| 328 | break; |
|---|
| 329 | case SDL_JOYBUTTONUP: |
|---|
| 330 | ev.bPressed = true; |
|---|
| 331 | ev.type = EV_JOY_BUTTON; |
|---|
| 332 | break; |
|---|
| 333 | case SDL_VIDEORESIZE: |
|---|
| 334 | ev.resize = event.resize; |
|---|
| 335 | ev.type = EV_VIDEO_RESIZE; |
|---|
| 336 | break; |
|---|
| 337 | case SDL_QUIT: |
|---|
| 338 | ev.type = EV_MAIN_QUIT; |
|---|
| 339 | break; |
|---|
| 340 | default: |
|---|
| 341 | ev.type = EV_UNKNOWN; |
|---|
| 342 | break; |
|---|
| 343 | } |
|---|
| [4362] | 344 | |
|---|
| [6771] | 345 | /* small debug routine: shows all events dispatched by the event handler */ |
|---|
| 346 | PRINT(4)("\n==========================| EventHandler::process () |===\n"); |
|---|
| 347 | PRINT(4)("= Got Event nr %i, for state %i", ev.type, this->state); |
|---|
| [4780] | 348 | |
|---|
| [7756] | 349 | |
|---|
| 350 | for (unsigned int i = 0; i < this->listeners[this->state][ev.type].size(); i++) |
|---|
| [6771] | 351 | { |
|---|
| 352 | PRINT(4)("= Event dispatcher msg: This event has been consumed\n"); |
|---|
| 353 | PRINT(4)("=======================================================\n"); |
|---|
| [7756] | 354 | listeners[this->state][ev.type][i]->process(ev); |
|---|
| [4355] | 355 | } |
|---|
| [7756] | 356 | /* else |
|---|
| 357 | { |
|---|
| 358 | PRINT(4)("= Event dispatcher msg: This event has NOT been consumed\n"); |
|---|
| 359 | PRINT(4)("=======================================================\n"); |
|---|
| 360 | }*/ |
|---|
| [6771] | 361 | } |
|---|
| [4355] | 362 | } |
|---|
| [4388] | 363 | |
|---|
| [5371] | 364 | |
|---|
| [5237] | 365 | int EventHandler::eventFilter(const SDL_Event *event) |
|---|
| 366 | { |
|---|
| [6802] | 367 | if (likely(EventHandler::getInstance()->eventsGrabbed)) |
|---|
| [5237] | 368 | { |
|---|
| [6802] | 369 | if (event->type == SDL_KEYDOWN && event->key.keysym.sym == SDLK_TAB && SDL_GetKeyState(NULL)[SDLK_LALT]) |
|---|
| 370 | { |
|---|
| 371 | PRINTF(3)("Not sending event to the WindowManager\n"); |
|---|
| 372 | EventHandler::getInstance()->grabEvents(false); |
|---|
| 373 | return 0; |
|---|
| 374 | } |
|---|
| 375 | } |
|---|
| 376 | else |
|---|
| 377 | { |
|---|
| 378 | if (event->type == SDL_MOUSEBUTTONDOWN) |
|---|
| 379 | { |
|---|
| 380 | EventHandler::getInstance()->grabEvents( true); |
|---|
| 381 | return 0; |
|---|
| 382 | } |
|---|
| 383 | } |
|---|
| [6054] | 384 | |
|---|
| [5237] | 385 | return 1; |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | /** |
|---|
| 389 | * outputs some nice information about the EventHandler |
|---|
| 390 | */ |
|---|
| [4872] | 391 | void EventHandler::debug() const |
|---|
| 392 | { |
|---|
| 393 | PRINT(0)("===============================\n"); |
|---|
| 394 | PRINT(0)(" EventHandle Debug Information \n"); |
|---|
| 395 | PRINT(0)("===============================\n"); |
|---|
| 396 | for(int i = 0; i < ES_NUMBER; ++i) |
|---|
| 397 | for(int j = 0; j < EV_NUMBER; ++j) |
|---|
| [7756] | 398 | for (unsigned int evl = 0; evl < this->listeners[i][j].size(); evl++) |
|---|
| 399 | 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] | 400 | PRINT(0)("============================EH=\n"); |
|---|
| 401 | } |
|---|