Changeset 7919 in orxonox.OLD for trunk/src/lib/event
- Timestamp:
- May 28, 2006, 3:48:13 PM (19 years ago)
- Location:
- trunk/src/lib/event
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/event/event_def.h
r7868 r7919 28 28 EV_JOY_BUTTON, 29 29 30 EV_WINDOW_FOCUS, 30 31 EV_VIDEO_RESIZE, 31 32 33 EV_LEAVE_STATE, 32 34 EV_MAIN_QUIT, 33 35 -
trunk/src/lib/event/event_handler.cc
r7868 r7919 29 29 30 30 /** 31 * standard constructor32 */31 * @brief standard constructor 32 */ 33 33 EventHandler::EventHandler () 34 34 { … … 42 42 43 43 /* now initialize them all to zero */ 44 this->withUNICODE(false); 44 for (unsigned int i = 0; i < ES_NUMBER; i++) 45 this->bUNICODE[i] = false; 45 46 this->grabEvents(false); 46 47 … … 51 52 52 53 /** 53 * the singleton reference to this class54 * @brief the singleton reference to this class 54 55 */ 55 56 EventHandler* EventHandler::singletonRef = NULL; … … 57 58 58 59 /** 59 * standard deconstructor 60 61 */ 60 * @brief standard deconstructor 61 */ 62 62 EventHandler::~EventHandler () 63 63 { … … 79 79 80 80 /** 81 * initializes the event handler81 * @brief initializes the event handler 82 82 * 83 83 * this has to be called before the use of the event handler … … 89 89 90 90 /** 91 * pushes the current State in the State-stack, and selects state 91 * @param state: to which the event handler shall change 92 */ 93 void EventHandler::setState(elState state) 94 { 95 if (state == this->state) 96 return; 97 98 /// When Changing the State, all the keys will be released. 99 /// This is done by sending each eventListener, that still 100 /// has an Event subscribed, a Release Event. 101 int keyCount; 102 Uint8 * pressedKeys = SDL_GetKeyState(&keyCount); 103 for (unsigned int i = 0; i < SDLK_LAST; i++) 104 { 105 if (pressedKeys[i]) 106 { 107 Event ev; 108 ev.bPressed = false; 109 ev.type = i; 110 if (unlikely(this->bUNICODE[this->state])) 111 ev.x = i; 112 this->dispachEvent(this->state, ev ); 113 } 114 } 115 116 // switching to the new State. 117 elState oldState = this->state; 118 this->state = state; 119 120 // in the End the Corresponding handler will be notified. 121 Event stateSwitchEvent; 122 stateSwitchEvent.type = EV_LEAVE_STATE; 123 this->dispachEvent(oldState, stateSwitchEvent); 124 125 SDL_EnableUNICODE(this->bUNICODE[state]); 126 }; 127 128 129 /** 130 * @brief pushes the current State in the State-stack, and selects state 92 131 * @param state the new State to set 93 132 */ … … 106 145 107 146 /** 108 * this removes the topmost stack-entry and select the underlying one147 * @brief this removes the topmost stack-entry and select the underlying one 109 148 * @returns the next stack-entry 110 149 */ … … 144 183 { 145 184 for(unsigned int i = 0; i < ES_NUMBER; i++) 146 { 147 if( !likely(this->listeners[i][eventType].empty())) 185 if (!this->findListener( NULL, (elState)i, eventType, el)) 186 this->listeners[i][eventType].push_back(el); 187 else 148 188 { 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);189 PRINTF(2)("%s::%s was already subscribed to state %d event %d\n", el->getClassName(), el->getName(), i, eventType); 150 190 } 151 this->listeners[i][eventType].push_back(el); 152 } 153 } 154 else 155 { 156 if( likely(!this->listeners[state][eventType].empty())) 157 { 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); 159 } 160 this->listeners[state][eventType].push_back(el); 191 } 192 else 193 { 194 if (!this->findListener( NULL, state, eventType, el)) 195 this->listeners[state][eventType].push_back(el); 196 else 197 { 198 PRINTF(2)("%s::%s was already subscribed to state %d event %d\n", el->getClassName(), el->getName(), state, eventType); 199 } 161 200 } 162 201 } … … 167 206 * @param state: the stat in which it has been subscribed 168 207 * @param eventType: the event, that shall be unsubscribed 169 170 171 172 */208 * 209 * if you want to unsubscribe an event listener from all subscribed events, just use the 210 * unsubscribe(EventListener* el, elState state) function 211 */ 173 212 void EventHandler::unsubscribe(EventListener* el, elState state, int eventType) 174 213 { … … 177 216 for (unsigned int i = 0; i < ES_NUMBER; i++) 178 217 { 179 std::vector<EventListener*>::iterator listener = 180 std::find(this->listeners[i][eventType].begin(), 181 this->listeners[i][eventType].end(), 182 el); 183 if (listener != this->listeners[i][eventType].end()) 218 std::vector<EventListener*>::iterator listener; 219 if (this->findListener(&listener, (elState)i, eventType, el)) 184 220 this->listeners[i][eventType].erase(listener); 185 221 } 186 222 else 187 223 { 188 std::vector<EventListener*>::iterator listener = 189 std::find(this->listeners[state][eventType].begin(), 190 this->listeners[state][eventType].end(), 191 el); 192 if (listener != this->listeners[state][eventType].end()) 224 std::vector<EventListener*>::iterator listener; 225 if (this->findListener(&listener, state, eventType, el)) 193 226 this->listeners[state][eventType].erase(listener); 194 227 } … … 203 236 void EventHandler::unsubscribe(EventListener* el, elState state) 204 237 { 205 if( el == NULL || state >= ES_NUMBER) 206 return; 238 assert( el != NULL && state < ES_NUMBER); 207 239 if( state == ES_ALL) 208 240 { … … 228 260 } 229 261 262 /** 263 * @brief returns true if at state and eventType there is something subscribed. 264 * @param state the state to check in. 265 * @param eventType the eventtype to check. 266 * @returns true if a event is subscibed. 267 */ 230 268 bool EventHandler::isSubscribed(elState state, int eventType) 231 269 { … … 236 274 237 275 /** 238 * flush all registered events276 * @brief flush all registered events 239 277 * @param state: a specific state 240 278 */ … … 261 299 262 300 263 void EventHandler::withUNICODE(bool enableUNICODE) 264 { 265 SDL_EnableUNICODE(enableUNICODE); 266 this->bUNICODE = enableUNICODE; 267 } 268 301 bool EventHandler::findListener(std::vector<EventListener*>::iterator* it, elState state, int eventType, EventListener* listener) 302 { 303 std::vector<EventListener*>::iterator findIterator = 304 std::find(this->listeners[state][eventType].begin(), this->listeners[state][eventType].end(), listener); 305 if (it != NULL) 306 *it = findIterator; 307 return ( findIterator != this->listeners[state][eventType].end()); 308 309 } 310 311 312 313 /** 314 * @brief if the unicode characters should be recorded. 315 * @param state the State in whitch to set the new Value. 316 * @param enableUNICODE: enabled, or disabled. 317 */ 318 void EventHandler::withUNICODE(elState state, bool enableUNICODE) 319 { 320 this->bUNICODE[state] = enableUNICODE; 321 if (this->state == state) 322 SDL_EnableUNICODE(enableUNICODE); 323 } 324 325 /** 326 * @brief grabs InputEvents. 327 * @param grabEvents if the Events should be grabbed(true) or released(false) 328 */ 269 329 void EventHandler::grabEvents(bool grabEvents) 270 330 { … … 282 342 } 283 343 284 /** 285 * core function of event handler: receives all events from SDL 286 287 The event from the SDL framework are collected here and distributed to all listeners. 288 */ 289 void EventHandler::process() 344 345 346 /** 347 * @brief core function of event handler: receives all events from SDL 348 * 349 * The event from the SDL framework are collected here and distributed to all listeners. 350 */ 351 void EventHandler::process() const 290 352 { 291 353 SDL_Event event; … … 299 361 ev.bPressed = true; 300 362 ev.type = event.key.keysym.sym; 301 if (unlikely(this->bUNICODE ))363 if (unlikely(this->bUNICODE[this->state])) 302 364 ev.x = event.key.keysym.unicode; 303 365 break; … … 305 367 ev.bPressed = false; 306 368 ev.type = event.key.keysym.sym; 307 if (unlikely(this->bUNICODE ))369 if (unlikely(this->bUNICODE[this->state])) 308 370 ev.x = event.key.keysym.unicode; 309 371 break; … … 344 406 ev.type = EV_JOY_BUTTON; 345 407 break; 408 case SDL_ACTIVEEVENT: 409 ev.type = EV_WINDOW_FOCUS; 410 ev.bPressed = (event.active.gain != 0); 411 break; 346 412 case SDL_VIDEORESIZE: 347 413 ev.resize = event.resize; … … 355 421 break; 356 422 } 357 358 /* small debug routine: shows all events dispatched by the event handler */ 359 PRINT(4)("\n==========================| EventHandler::process () |===\n"); 360 PRINT(4)("= Got Event nr %i, for state %i", ev.type, this->state); 361 362 363 for (unsigned int i = 0; i < this->listeners[this->state][ev.type].size(); i++) 364 { 365 PRINT(4)("= Event dispatcher msg: This event has been consumed\n"); 366 PRINT(4)("=======================================================\n"); 367 listeners[this->state][ev.type][i]->process(ev); 368 } 369 /* else 370 { 371 PRINT(4)("= Event dispatcher msg: This event has NOT been consumed\n"); 372 PRINT(4)("=======================================================\n"); 373 }*/ 374 } 375 } 376 377 423 this->dispachEvent(this->state, ev); 424 } 425 } 426 427 428 /** 429 * @brief dispaches an Event. 430 * @param event the Event to dispach. 431 */ 432 void EventHandler::dispachEvent(elState state, const Event& event) const 433 { 434 /* small debug routine: shows all events dispatched by the event handler */ 435 PRINT(4)("\n==========================| EventHandler::process () |===\n"); 436 PRINT(4)("= Got Event nr %i, for state %i\n", event.type, state); 437 438 /// setting a temporary state in case of an EventListener's process changes the state. 439 for (unsigned int i = 0; i < this->listeners[state][event.type].size(); i++) 440 { 441 PRINT(4)("= Event dispatcher msg: This event has been consumed\n"); 442 PRINT(4)("= Got Event nr %i, for state %i (%d registered) to %s::%s(%p)\n", event.type, i, state, this->listeners[state][event.type][i]->getClassName(), this->listeners[state][event.type][i]->getName(), this->listeners[state][event.type][i]); 443 PRINT(4)("=======================================================\n"); 444 this->listeners[state][event.type][i]->process(event); 445 } 446 /* else 447 { 448 PRINT(4)("= Event dispatcher msg: This event has NOT been consumed\n"); 449 PRINT(4)("=======================================================\n"); 450 }*/ 451 } 452 453 454 455 /** 456 * @brief An eventFilter. 457 * @param event the Event to be filtered. 458 * @returns 0 on filtered Event. 1 Otherwise. 459 */ 378 460 int EventHandler::eventFilter(const SDL_Event *event) 379 461 { … … 400 482 401 483 /** 402 * outputs some nice information about the EventHandler484 * @brief outputs some nice information about the EventHandler 403 485 */ 404 486 void EventHandler::debug() const … … 408 490 PRINT(0)("===============================\n"); 409 491 for(int i = 0; i < ES_NUMBER; ++i) 492 { 410 493 for(int j = 0; j < EV_NUMBER; ++j) 411 494 for (unsigned int evl = 0; evl < this->listeners[i][j].size(); evl++) 412 495 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]); 496 } 413 497 PRINT(0)("============================EH=\n"); 414 498 } -
trunk/src/lib/event/event_handler.h
r7868 r7919 11 11 #include "key_mapper.h" 12 12 #include "event_def.h" 13 #include "event.h" 13 14 #include <stack> 14 15 #include <vector> … … 26 27 void init(); 27 28 28 /** @param state: to which the event handler shall change */ 29 inline void setState(elState state) { this->state = state; }; 29 void setState(elState state); 30 30 /** @returns the current state */ 31 31 inline elState getState() const { return this->state; }; … … 42 42 43 43 44 void withUNICODE( bool enableUNICODE);44 void withUNICODE(elState state, bool enableUNICODE); 45 45 void grabEvents(bool grabEvents); 46 46 bool grabbedEvents() const { return this->eventsGrabbed; }; 47 47 48 void process(); 48 void process() const; 49 void dispachEvent(elState state, const Event& event) const; 49 50 50 51 static int eventFilter(const SDL_Event *event); … … 53 54 private: 54 55 EventHandler(); 56 57 bool findListener(std::vector<EventListener*>::iterator* it, elState state, int eventType, EventListener* listener); 55 58 56 59 private: … … 62 65 KeyMapper keyMapper; //!< reference to the key mapper. 63 66 64 bool bUNICODE ;//!< If unicode should be enabled.67 bool bUNICODE[ES_NUMBER]; //!< If unicode should be enabled. 65 68 bool eventsGrabbed; //!< If the events should be grabbed 66 69 };
Note: See TracChangeset
for help on using the changeset viewer.