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