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