[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 | { |
---|
[5236] | 36 | SDL_InitSubSystem(SDL_INIT_JOYSTICK); |
---|
| 37 | SDL_InitSubSystem(SDL_INIT_EVENTTHREAD); |
---|
[5237] | 38 | // SDL_SetEventFilter(EventHandler::eventFilter); |
---|
[5236] | 39 | |
---|
[4780] | 40 | this->setClassID(CL_EVENT_HANDLER, "EventHandler"); |
---|
[4350] | 41 | |
---|
[4355] | 42 | /* now initialize them all to zero */ |
---|
[5210] | 43 | for(int i = 0; i < ES_NUMBER; i++) |
---|
[4355] | 44 | { |
---|
[5210] | 45 | for(int j = 0; j < EV_NUMBER; j++) |
---|
[4780] | 46 | { |
---|
| 47 | this->listeners[i][j] = NULL; |
---|
| 48 | } |
---|
[4355] | 49 | } |
---|
[4407] | 50 | this->state = ES_GAME; |
---|
[4329] | 51 | } |
---|
| 52 | |
---|
[4407] | 53 | |
---|
[4329] | 54 | /** |
---|
[4836] | 55 | * the singleton reference to this class |
---|
[4329] | 56 | */ |
---|
[4346] | 57 | EventHandler* EventHandler::singletonRef = NULL; |
---|
[4329] | 58 | |
---|
[4407] | 59 | |
---|
[4329] | 60 | /** |
---|
[4836] | 61 | * standard deconstructor |
---|
[4329] | 62 | |
---|
| 63 | */ |
---|
[4780] | 64 | EventHandler::~EventHandler () |
---|
[4329] | 65 | { |
---|
[4817] | 66 | for(int i = 0; i < ES_NUMBER; ++i) |
---|
| 67 | { |
---|
[4866] | 68 | for(int j = 0; j < EV_NUMBER; ++j) |
---|
[4817] | 69 | { |
---|
| 70 | if( this->listeners[i][j] != NULL) |
---|
| 71 | { |
---|
[4838] | 72 | PRINTF(2)("forgot to unsubscribe an EventListener %s!\n");//, this->listeners[i][j]->getName()); |
---|
[4817] | 73 | } |
---|
| 74 | } |
---|
| 75 | } |
---|
[4866] | 76 | delete this->keyMapper; |
---|
[5236] | 77 | |
---|
| 78 | SDL_QuitSubSystem(SDL_INIT_JOYSTICK); |
---|
| 79 | |
---|
[4866] | 80 | EventHandler::singletonRef = NULL; |
---|
[4352] | 81 | } |
---|
[4329] | 82 | |
---|
[4352] | 83 | |
---|
[4450] | 84 | /** |
---|
[4836] | 85 | * initializes the event handler |
---|
[4352] | 86 | |
---|
[4450] | 87 | this has to be called before the use of the event handler |
---|
| 88 | */ |
---|
[4866] | 89 | void EventHandler::init(IniParser* iniParser) |
---|
[4407] | 90 | { |
---|
| 91 | this->keyMapper = new KeyMapper(); |
---|
[4866] | 92 | this->keyMapper->loadKeyBindings(iniParser); |
---|
[4407] | 93 | } |
---|
| 94 | |
---|
[4450] | 95 | /** |
---|
[4836] | 96 | * subscribe to an event |
---|
| 97 | * @param el: the event listener that wants to subscribe itself, the listener that will be called when the evetn occures |
---|
| 98 | * @param state: for which the listener wants to receive events |
---|
| 99 | * @param eventType: the event type that wants to be listened for. |
---|
[4450] | 100 | |
---|
| 101 | This is one of the most important function of the EventHandler. If you would like to subscribe for more |
---|
| 102 | than one state, you have to subscribe for each state again. If you want to subscribe for all states, use |
---|
| 103 | state = ES_ALL, which will subscribe your listener for all states together. |
---|
[4780] | 104 | * |
---|
[4836] | 105 | * @todo this can also be done with the & operator, and checking for states, just set the esState to 1,2,4,8, and then 15 is equal to ES_ALL |
---|
[4450] | 106 | */ |
---|
[4405] | 107 | void EventHandler::subscribe(EventListener* el, elState state, int eventType) |
---|
[4354] | 108 | { |
---|
[4450] | 109 | PRINTF(4)("Subscribing event type: %i\n", eventType); |
---|
[4407] | 110 | if( state == ES_ALL ) |
---|
| 111 | { |
---|
| 112 | for(int i = 0; i < ES_NUMBER; ++i) |
---|
[4780] | 113 | if( likely(this->listeners[state][eventType] == NULL)) |
---|
| 114 | this->listeners[i][eventType] = el; |
---|
| 115 | else |
---|
| 116 | PRINTF(1)("%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] | 117 | } |
---|
[4780] | 118 | else |
---|
[4407] | 119 | if( likely(this->listeners[state][eventType] == NULL)) |
---|
| 120 | { |
---|
[4780] | 121 | this->listeners[state][eventType] = el; |
---|
[4407] | 122 | } |
---|
| 123 | else |
---|
[4780] | 124 | PRINTF(1)("% 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] | 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
[4450] | 128 | /** |
---|
[4836] | 129 | * unsubscribe from the EventHandler |
---|
| 130 | * @param state: the stat in which it has been subscribed |
---|
| 131 | * @param eventType: the event, that shall be unsubscribed |
---|
[4450] | 132 | |
---|
[4780] | 133 | if you want to unsubscribe an event listener from all subscribed events, just use the |
---|
[4450] | 134 | unsubscribe(EventListener* el, elState state) function |
---|
| 135 | */ |
---|
[4419] | 136 | void EventHandler::unsubscribe(elState state, int eventType) |
---|
[4355] | 137 | { |
---|
[4450] | 138 | PRINTF(4)("Unsubscribing event type nr: %i\n", eventType); |
---|
[4355] | 139 | this->listeners[state][eventType] = NULL; |
---|
| 140 | } |
---|
[4354] | 141 | |
---|
[4450] | 142 | |
---|
| 143 | /** |
---|
[4836] | 144 | * unsubscribe all events from a specific listener |
---|
| 145 | * @param el: the listener that wants to unsubscribe itself |
---|
| 146 | * @param state: the state in which the events shall be unsubscribed |
---|
[4780] | 147 | |
---|
[4450] | 148 | */ |
---|
| 149 | void EventHandler::unsubscribe(EventListener* el, elState state) |
---|
[4355] | 150 | { |
---|
[4816] | 151 | if( el == NULL) |
---|
| 152 | return; |
---|
[4364] | 153 | if( state == ES_ALL) |
---|
[4355] | 154 | { |
---|
[4364] | 155 | for(int i = 0; i < ES_NUMBER; ++i) |
---|
[4780] | 156 | { |
---|
| 157 | for(int j = 0; j < EV_NUMBER; ++j) |
---|
| 158 | { |
---|
| 159 | if( this->listeners[i][j] == el ) |
---|
| 160 | this->listeners[i][j] = NULL; |
---|
| 161 | } |
---|
| 162 | } |
---|
[4364] | 163 | } |
---|
| 164 | else |
---|
| 165 | { |
---|
[4420] | 166 | for(int j = 0; j < EV_NUMBER; ++j) |
---|
[4780] | 167 | { |
---|
| 168 | if( this->listeners[state][j] == el ) |
---|
| 169 | this->listeners[state][j] = NULL; |
---|
| 170 | } |
---|
[4355] | 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
[4450] | 174 | |
---|
| 175 | /** |
---|
[4836] | 176 | * flush all registered events |
---|
| 177 | * @param state: a specific state |
---|
[4450] | 178 | */ |
---|
| 179 | void EventHandler::flush(elState state) |
---|
[4420] | 180 | { |
---|
| 181 | if( state == ES_ALL) |
---|
| 182 | { |
---|
| 183 | for(int i = 0; i < ES_NUMBER; ++i) |
---|
[4780] | 184 | { |
---|
| 185 | for(int j = 0; j < EV_NUMBER; ++j) |
---|
| 186 | { |
---|
| 187 | this->listeners[i][j] = NULL; |
---|
| 188 | } |
---|
| 189 | } |
---|
[4420] | 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
| 193 | for(int j = 0; j < EV_NUMBER; ++j) |
---|
[4780] | 194 | { |
---|
| 195 | this->listeners[state][j] = NULL; |
---|
| 196 | } |
---|
[4420] | 197 | } |
---|
| 198 | } |
---|
[4355] | 199 | |
---|
| 200 | |
---|
[4450] | 201 | /** |
---|
[4836] | 202 | * core function of event handler: receives all events from SDL |
---|
[4420] | 203 | |
---|
[4450] | 204 | The event from the SDL framework are collected here and distributed to all listeners. |
---|
| 205 | */ |
---|
[4355] | 206 | void EventHandler::process() |
---|
| 207 | { |
---|
| 208 | SDL_Event event; |
---|
[4361] | 209 | Event ev; |
---|
[4407] | 210 | EventListener* listener = NULL; |
---|
[4355] | 211 | while( SDL_PollEvent (&event)) |
---|
| 212 | { |
---|
| 213 | switch( event.type) |
---|
[4780] | 214 | { |
---|
| 215 | case SDL_KEYDOWN: |
---|
| 216 | ev.bPressed = true; |
---|
| 217 | ev.type = event.key.keysym.sym; |
---|
| 218 | break; |
---|
| 219 | case SDL_KEYUP: |
---|
| 220 | ev.bPressed = false; |
---|
| 221 | ev.type = event.key.keysym.sym; |
---|
| 222 | break; |
---|
| 223 | case SDL_MOUSEMOTION: |
---|
| 224 | ev.bPressed = false; |
---|
| 225 | ev.type = EV_MOUSE_MOTION; |
---|
| 226 | ev.x = event.motion.x; |
---|
| 227 | ev.y = event.motion.y; |
---|
| 228 | ev.xRel = event.motion.xrel; |
---|
| 229 | ev.yRel = event.motion.yrel; |
---|
| 230 | break; |
---|
| 231 | case SDL_MOUSEBUTTONUP: |
---|
| 232 | ev.bPressed = false; |
---|
| 233 | ev.type = event.button.button + SDLK_LAST; |
---|
| 234 | break; |
---|
| 235 | case SDL_MOUSEBUTTONDOWN: |
---|
| 236 | ev.bPressed = true; |
---|
| 237 | ev.type = event.button.button + SDLK_LAST; |
---|
| 238 | break; |
---|
| 239 | case SDL_JOYAXISMOTION: |
---|
| 240 | ev.bPressed = false; |
---|
| 241 | ev.type = EV_JOY_AXIS_MOTION; |
---|
| 242 | break; |
---|
| 243 | case SDL_JOYBALLMOTION: |
---|
| 244 | ev.bPressed = false; |
---|
| 245 | ev.type = EV_JOY_BALL_MOTION; |
---|
| 246 | break; |
---|
| 247 | case SDL_JOYHATMOTION: |
---|
| 248 | ev.bPressed = false; |
---|
| 249 | ev.type = EV_JOY_HAT_MOTION; |
---|
| 250 | break; |
---|
| 251 | case SDL_JOYBUTTONDOWN: |
---|
| 252 | ev.bPressed = true; |
---|
| 253 | ev.type = EV_JOY_BUTTON; |
---|
| 254 | break; |
---|
| 255 | case SDL_JOYBUTTONUP: |
---|
| 256 | ev.bPressed = true; |
---|
| 257 | ev.type = EV_JOY_BUTTON; |
---|
| 258 | break; |
---|
[4782] | 259 | case SDL_VIDEORESIZE: |
---|
| 260 | ev.resize = event.resize; |
---|
[4783] | 261 | ev.type = EV_VIDEO_RESIZE; |
---|
[4782] | 262 | break; |
---|
[4780] | 263 | default: |
---|
| 264 | ev.type = EV_UNKNOWN; |
---|
| 265 | break; |
---|
| 266 | } |
---|
[4362] | 267 | |
---|
[4780] | 268 | /* small debug routine: shows all events dispatched by the event handler */ |
---|
[4450] | 269 | PRINT(4)("\n==========================| EventHandler::process () |===\n"); |
---|
[4780] | 270 | PRINT(4)("= Got Event nr %i, for state %i", ev.type, this->state); |
---|
| 271 | |
---|
[4873] | 272 | //! @todo fix this debug code away */ |
---|
| 273 | //////////////////////////////////////////////////////////////// |
---|
[4407] | 274 | listener = this->listeners[this->state][ev.type]; |
---|
[4873] | 275 | if (!ClassList::exists(listener, CL_EVENT_LISTENER) && listener != NULL) |
---|
| 276 | { |
---|
| 277 | ClassList::debug(3, CL_EVENT_LISTENER); |
---|
| 278 | this->debug(); |
---|
| 279 | printf("ERROR THIS EVENT DOES NOT EXIST\n"); |
---|
| 280 | return; |
---|
| 281 | } |
---|
| 282 | //////////////////////////////////////////////////////////////// |
---|
[4365] | 283 | if( listener != NULL) |
---|
[4780] | 284 | { |
---|
| 285 | PRINT(4)("= Event dispatcher msg: This event has been consumed\n"); |
---|
| 286 | PRINT(4)("=======================================================\n"); |
---|
| 287 | listener->process(ev); |
---|
| 288 | } |
---|
[4365] | 289 | else |
---|
[4780] | 290 | { |
---|
| 291 | PRINT(4)("= Event dispatcher msg: This event has NOT been consumed\n"); |
---|
| 292 | PRINT(4)("=======================================================\n"); |
---|
| 293 | } |
---|
[4355] | 294 | } |
---|
| 295 | } |
---|
[4388] | 296 | |
---|
[5237] | 297 | int EventHandler::eventFilter(const SDL_Event *event) |
---|
| 298 | { |
---|
| 299 | /* |
---|
| 300 | if (event->type == SDL_KEYDOWN && event->key.keysym.sym == SDLK_TAB && SDL_GetKeyState(NULL)[SDLK_LALT]) |
---|
| 301 | { |
---|
| 302 | printf("test\n"); |
---|
[4872] | 303 | |
---|
[5237] | 304 | } |
---|
| 305 | return 1; |
---|
| 306 | */ |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | /** |
---|
| 310 | * outputs some nice information about the EventHandler |
---|
| 311 | */ |
---|
[4872] | 312 | void EventHandler::debug() const |
---|
| 313 | { |
---|
| 314 | PRINT(0)("===============================\n"); |
---|
| 315 | PRINT(0)(" EventHandle Debug Information \n"); |
---|
| 316 | PRINT(0)("===============================\n"); |
---|
| 317 | for(int i = 0; i < ES_NUMBER; ++i) |
---|
| 318 | { |
---|
| 319 | for(int j = 0; j < EV_NUMBER; ++j) |
---|
| 320 | { |
---|
| 321 | if( this->listeners[i][j] != NULL) |
---|
| 322 | { |
---|
[4873] | 323 | PRINT(0)("Event %d of State %d subscribed to %s (%p)\n", j, i, this->listeners[i][j]->getName(), this->listeners[i][j]); |
---|
[4872] | 324 | } |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | PRINT(0)("============================EH=\n"); |
---|
| 328 | } |
---|