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