Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/event/event_handler.cc @ 8285

Last change on this file since 8285 was 8228, checked in by patrick, 18 years ago

trunk: merged the network branche back to trunk with command: svn merge branches/network trunk -r8150:HEAD

File size: 14.4 KB
RevLine 
[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"
[8148]23#include "key_names.h"
[4329]24
[4381]25#include "compiler.h"
26#include "debug.h"
[4873]27#include "class_list.h"
[4352]28
[7756]29#include <algorithm>
[4329]30
31/**
[7919]32 * @brief standard constructor
33 */
[4780]34EventHandler::EventHandler ()
[4329]35{
[5285]36  this->setClassID(CL_EVENT_HANDLER, "EventHandler");
37  this->setName("EventHandler");
38
[5236]39  SDL_InitSubSystem(SDL_INIT_JOYSTICK);
40  SDL_InitSubSystem(SDL_INIT_EVENTTHREAD);
[5371]41  SDL_SetEventFilter(EventHandler::eventFilter);
[5236]42
[4350]43
[4355]44  /* now initialize them all to zero */
[7919]45  for (unsigned int i = 0; i < ES_NUMBER; i++)
46    this->bUNICODE[i] = false;
[5978]47  this->grabEvents(false);
[5291]48
[4407]49  this->state = ES_GAME;
[6802]50  this->eventsGrabbed = false;
[4329]51}
52
[4407]53
[4329]54/**
[7919]55 * @brief the singleton reference to this class
[4329]56*/
[4346]57EventHandler* EventHandler::singletonRef = NULL;
[4329]58
[4407]59
[4329]60/**
[7919]61 * @brief standard deconstructor
62 */
[4780]63EventHandler::~EventHandler ()
[4329]64{
[8148]65  bool forgotToUnsubscribe = false;
66
[4817]67  for(int i = 0; i < ES_NUMBER; ++i)
68  {
[4866]69    for(int j = 0; j < EV_NUMBER; ++j)
[4817]70    {
[7756]71      if(!this->listeners[i][j].empty())
[4817]72      {
[8148]73        if (!forgotToUnsubscribe)
74        {
75          forgotToUnsubscribe = true;
76          PRINTF(2)("forgot to unsubscribe an EventListener!\n");// %s!\n", this->listeners[i][j]->getName());
77        }
[4817]78      }
79    }
80  }
[8148]81
82  if (forgotToUnsubscribe)
83  {
84    PRINTF(2)("Listing still subscribed EventListeners\n");
85    PRINTF(2)("========================================\n");
86    this->debug();
87    PRINTF(2)("========================================\n");
88  }
89
[5236]90  SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
91
[4866]92  EventHandler::singletonRef = NULL;
[4352]93}
[4329]94
[4352]95
[4450]96/**
[7919]97 * @brief initializes the event handler
[5285]98 *
99 * this has to be called before the use of the event handler
[4450]100*/
[7256]101void EventHandler::init()
[4407]102{
[7756]103  this->keyMapper.loadKeyBindings();
[4407]104}
105
[4450]106/**
[7919]107 * @param state: to which the event handler shall change
108 */
109void EventHandler::setState(elState state)
110{
111  if (state == this->state)
112    return;
113
114  /// When Changing the State, all the keys will be released.
115  /// This is done by sending each eventListener, that still
116  /// has an Event subscribed, a Release Event.
117  int keyCount;
118  Uint8 * pressedKeys = SDL_GetKeyState(&keyCount);
119  for (unsigned int i = 0; i < SDLK_LAST; i++)
120  {
121    if (pressedKeys[i])
122    {
123      Event ev;
124      ev.bPressed = false;
125      ev.type = i;
126      if (unlikely(this->bUNICODE[this->state]))
127        ev.x = i;
128      this->dispachEvent(this->state, ev );
129    }
130  }
131
132  // switching to the new State.
133  elState oldState = this->state;
134  this->state = state;
135
136  // in the End the Corresponding handler will be notified.
137  Event stateSwitchEvent;
138  stateSwitchEvent.type = EV_LEAVE_STATE;
139  this->dispachEvent(oldState, stateSwitchEvent);
140
141  SDL_EnableUNICODE(this->bUNICODE[state]);
142};
143
144
145/**
146 * @brief pushes the current State in the State-stack, and selects state
[5388]147 * @param state the new State to set
148 */
149void EventHandler::pushState(elState state)
150{
[7164]151  if (likely(state != ES_NULL && state != ES_ALL ))
[5388]152  {
[7164]153    this->stateStack.push(this->state);
[5388]154    this->setState(state);
155  }
156  else
157  {
158    PRINTF(2)("unable to push State\n");
159  }
160}
161
162/**
[7919]163 * @brief this removes the topmost stack-entry and select the underlying one
[5388]164 * @returns the next stack-entry
165 */
166elState EventHandler::popState()
167{
[7166]168  if (stateStack.empty())
169    return ES_NULL;
[7164]170  elState state =  (elState)stateStack.top();
171  this->stateStack.pop();
[5388]172  if (state == ES_NULL)
173  {
174    PRINTF(2)("No more states availiable. (unable to pop state)\n");
175    return ES_NULL;
176  }
177  else
178  {
179    this->setState(state);
180    return state;
181  }
182}
183
184
185/**
[7756]186 * @brief subscribe to an event
[4836]187 * @param el: the event listener that wants to subscribe itself, the listener that will be called when the evetn occures
188 * @param state: for which the listener wants to receive events
189 * @param eventType: the event type that wants to be listened for.
[4450]190
191   This is one of the most important function of the EventHandler. If you would like to subscribe for more
192   than one state, you have to subscribe for each state again. If you want to subscribe for all states, use
193   state = ES_ALL, which will subscribe your listener for all states together.
[5291]194 */
[4405]195void EventHandler::subscribe(EventListener* el, elState state, int eventType)
[4354]196{
[4450]197  PRINTF(4)("Subscribing event type: %i\n", eventType);
[4407]198  if( state == ES_ALL )
[6771]199  {
200    for(unsigned int i = 0; i < ES_NUMBER; i++)
[7919]201      if (!this->findListener( NULL, (elState)i, eventType, el))
202        this->listeners[i][eventType].push_back(el);
203      else
[7756]204      {
[7919]205        PRINTF(2)("%s::%s was already subscribed to state %d event %d\n", el->getClassName(), el->getName(), i, eventType);
[7756]206      }
[6771]207  }
208  else
[7756]209  {
[7919]210    if (!this->findListener( NULL, state, eventType, el))
211      this->listeners[state][eventType].push_back(el);
212    else
[4407]213    {
[7919]214      PRINTF(2)("%s::%s was already subscribed to state %d event %d\n", el->getClassName(), el->getName(), state, eventType);
[4407]215    }
[7756]216  }
[4354]217}
218
219
[4450]220/**
[7756]221 * @brief unsubscribe from the EventHandler
[4836]222 * @param state: the stat in which it has been subscribed
223 * @param eventType: the event, that shall be unsubscribed
[7919]224 *
225 * if you want to unsubscribe an event listener from all subscribed events, just use the
226 * unsubscribe(EventListener* el, elState state) function
227 */
[7868]228void EventHandler::unsubscribe(EventListener* el, elState state, int eventType)
[4355]229{
[4450]230  PRINTF(4)("Unsubscribing event type nr: %i\n", eventType);
[5291]231  if (state == ES_ALL)
232    for (unsigned int i = 0; i < ES_NUMBER; i++)
[7868]233    {
[7919]234      std::vector<EventListener*>::iterator listener;
235      if (this->findListener(&listener, (elState)i, eventType, el))
[7868]236        this->listeners[i][eventType].erase(listener);
237    }
[5291]238  else
[7868]239  {
[7919]240    std::vector<EventListener*>::iterator listener;
241    if (this->findListener(&listener, state, eventType, el))
[7868]242      this->listeners[state][eventType].erase(listener);
243  }
[4355]244}
[4354]245
[4450]246
247/**
[7868]248 * @brief unsubscribe all events from a specific listener
[4836]249 * @param el: the listener that wants to unsubscribe itself
250 * @param state: the state in which the events shall be unsubscribed
[7868]251 */
[4450]252void EventHandler::unsubscribe(EventListener* el, elState state)
[4355]253{
[7919]254  assert( el != NULL && state < ES_NUMBER);
[4364]255  if( state == ES_ALL)
[6771]256  {
257    for(unsigned int i = 0; i < ES_NUMBER; i++)
[4355]258    {
[6771]259      for(unsigned int j = 0; j < EV_NUMBER; j++)
260      {
[7756]261        std::vector<EventListener*>::iterator deller = std::find (this->listeners[i][j].begin(), this->listeners[i][j].end(), el);
262        if( deller != this->listeners[i][j].end())
263          this->listeners[i][j].erase(deller);
[6771]264      }
[4364]265    }
[6771]266  }
[4364]267  else
[6771]268  {
269    for(int j = 0; j < EV_NUMBER; j++)
[4364]270    {
[7756]271      std::vector<EventListener*>::iterator deller =  std::find (this->listeners[state][j].begin(), this->listeners[state][j].end(), el);
272      if( deller != this->listeners[state][j].end())
273        this->listeners[state][j].erase(deller);
[4355]274    }
[6771]275  }
[4355]276}
277
[7919]278/**
279 * @brief returns true if at state and eventType there is something subscribed.
280 * @param state the state to check in.
281 * @param eventType the eventtype to check.
282 * @returns true if a event is subscibed.
283 */
[7756]284bool EventHandler::isSubscribed(elState state, int eventType)
285{
286  return(listeners[state][eventType].empty()) ? false : true;
287};
[4450]288
[7756]289
290
[4450]291/**
[7919]292 * @brief flush all registered events
[4836]293 * @param state: a specific state
[4450]294*/
295void EventHandler::flush(elState state)
[4420]296{
297  if( state == ES_ALL)
[6771]298  {
299    for(int i = 0; i < ES_NUMBER; ++i)
[4420]300    {
[6771]301      for(int j = 0; j < EV_NUMBER; ++j)
302      {
[7756]303        this->listeners[i][j].clear();
[6771]304      }
[4420]305    }
[6771]306  }
[4420]307  else
[6771]308  {
309    for(int j = 0; j < EV_NUMBER; ++j)
[4420]310    {
[7756]311      this->listeners[state][j].clear();
[4420]312    }
[6771]313  }
[4420]314}
[4355]315
316
[7919]317bool EventHandler::findListener(std::vector<EventListener*>::iterator* it, elState state, int eventType, EventListener* listener)
[5786]318{
[7919]319  std::vector<EventListener*>::iterator findIterator =
320    std::find(this->listeners[state][eventType].begin(), this->listeners[state][eventType].end(), listener);
321  if (it != NULL)
322    *it = findIterator;
323  return ( findIterator != this->listeners[state][eventType].end());
324
[5786]325}
326
[7919]327
328
329/**
330 * @brief if the unicode characters should be recorded.
331 * @param state the State in whitch to set the new Value.
332 * @param enableUNICODE: enabled, or disabled.
333 */
334void EventHandler::withUNICODE(elState state, bool enableUNICODE)
335{
336  this->bUNICODE[state] = enableUNICODE;
337  if (this->state == state)
338    SDL_EnableUNICODE(enableUNICODE);
339}
340
341/**
342 * @brief grabs InputEvents.
343 * @param grabEvents if the Events should be grabbed(true) or released(false)
344 */
[5978]345void EventHandler::grabEvents(bool grabEvents)
346{
[6054]347  this->eventsGrabbed = grabEvents;
348  if(!grabEvents)
[6990]349  {
350    SDL_ShowCursor(SDL_ENABLE);
[6054]351    SDL_WM_GrabInput(SDL_GRAB_OFF);
[6990]352  }
[6054]353  else
[6990]354  {
[8228]355    SDL_WM_GrabInput(SDL_GRAB_ON);
[6990]356    SDL_ShowCursor(SDL_DISABLE);
357  }
[5978]358}
[5786]359
[7919]360
361
[4450]362/**
[7919]363 * @brief core function of event handler: receives all events from SDL
364 *
365 * The event from the SDL framework are collected here and distributed to all listeners.
366 */
367void EventHandler::process() const
[4355]368{
369  SDL_Event event;
[4361]370  Event ev;
[4407]371  EventListener* listener = NULL;
[4355]372  while( SDL_PollEvent (&event))
[6771]373  {
374    switch( event.type)
[4355]375    {
[6771]376      case SDL_KEYDOWN:
377        ev.bPressed = true;
378        ev.type = event.key.keysym.sym;
[7919]379        if (unlikely(this->bUNICODE[this->state]))
[6771]380          ev.x = event.key.keysym.unicode;
381        break;
382      case SDL_KEYUP:
383        ev.bPressed = false;
384        ev.type = event.key.keysym.sym;
[7919]385        if (unlikely(this->bUNICODE[this->state]))
[6771]386          ev.x = event.key.keysym.unicode;
387        break;
388      case SDL_MOUSEMOTION:
389        ev.bPressed = false;
390        ev.type = EV_MOUSE_MOTION;
391        ev.x = event.motion.x;
392        ev.y = event.motion.y;
393        ev.xRel = event.motion.xrel;
394        ev.yRel = event.motion.yrel;
395        break;
396      case SDL_MOUSEBUTTONUP:
397        ev.bPressed = false;
[8035]398        ev.x = event.motion.x;
399        ev.y = event.motion.y;
[6771]400        ev.type = event.button.button + SDLK_LAST;
401        break;
402      case SDL_MOUSEBUTTONDOWN:
403        ev.bPressed = true;
[8035]404        ev.x = event.motion.x;
405        ev.y = event.motion.y;
[6771]406        ev.type = event.button.button + SDLK_LAST;
407        break;
408      case SDL_JOYAXISMOTION:
409        ev.bPressed = false;
410        ev.type = EV_JOY_AXIS_MOTION;
411        break;
412      case SDL_JOYBALLMOTION:
413        ev.bPressed = false;
414        ev.type = EV_JOY_BALL_MOTION;
415        break;
416      case SDL_JOYHATMOTION:
417        ev.bPressed = false;
418        ev.type = EV_JOY_HAT_MOTION;
419        break;
420      case SDL_JOYBUTTONDOWN:
421        ev.bPressed = true;
422        ev.type = EV_JOY_BUTTON;
423        break;
424      case SDL_JOYBUTTONUP:
425        ev.bPressed = true;
426        ev.type = EV_JOY_BUTTON;
427        break;
[7919]428      case SDL_ACTIVEEVENT:
429        ev.type = EV_WINDOW_FOCUS;
430        ev.bPressed = (event.active.gain != 0);
431        break;
[6771]432      case SDL_VIDEORESIZE:
433        ev.resize = event.resize;
434        ev.type = EV_VIDEO_RESIZE;
435        break;
436      case SDL_QUIT:
437        ev.type = EV_MAIN_QUIT;
438        break;
439      default:
440        ev.type = EV_UNKNOWN;
441        break;
442    }
[7919]443    this->dispachEvent(this->state, ev);
444  }
445}
[4362]446
[4780]447
[7919]448/**
449 * @brief dispaches an Event.
450 * @param event the Event to dispach.
451 */
452void EventHandler::dispachEvent(elState state, const Event& event) const
453{
454  /* small debug routine: shows all events dispatched by the event handler */
455  PRINT(4)("\n==========================| EventHandler::process () |===\n");
456  PRINT(4)("=  Got Event nr %i, for state %i\n", event.type, state);
[7756]457
[7919]458  /// setting a temporary state in case of an EventListener's process changes the state.
459  for (unsigned int i = 0; i < this->listeners[state][event.type].size(); i++)
460  {
461    PRINT(4)("=  Event dispatcher msg: This event has been consumed\n");
462    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]);
463    PRINT(4)("=======================================================\n");
464    this->listeners[state][event.type][i]->process(event);
[6771]465  }
[7919]466  /*    else
467  {
468        PRINT(4)("=  Event dispatcher msg: This event has NOT been consumed\n");
469        PRINT(4)("=======================================================\n");
470  }*/
[4355]471}
[4388]472
[5371]473
[7919]474
475/**
476 * @brief An eventFilter.
477 * @param event the Event to be filtered.
478 * @returns 0 on filtered Event. 1 Otherwise.
479 */
[5237]480int EventHandler::eventFilter(const SDL_Event *event)
481{
[6802]482  if (likely(EventHandler::getInstance()->eventsGrabbed))
[5237]483  {
[6802]484    if (event->type == SDL_KEYDOWN &&  event->key.keysym.sym == SDLK_TAB && SDL_GetKeyState(NULL)[SDLK_LALT])
485    {
486      PRINTF(3)("Not sending event to the WindowManager\n");
487      EventHandler::getInstance()->grabEvents(false);
488      return 0;
489    }
490  }
491  else
492  {
493    if (event->type == SDL_MOUSEBUTTONDOWN)
494    {
495      EventHandler::getInstance()->grabEvents( true);
[8035]496      return 1;
[6802]497    }
498  }
[6054]499
[5237]500  return 1;
501}
502
[8148]503
[5237]504/**
[8148]505 * @param state The State to get the Name of.
506 * @returns the Name of the State.
507 */
508const std::string& EventHandler::ELStateToString(elState state)
509{
510  if (state < ES_NUMBER)
511    return EventHandler::stateNames[state];
512  else
513    return EventHandler::stateNames[5];
514}
515
516/**
517 * @param stateName the Name of the State to retrieve.
518 * @return the State given by the name
519 */
520elState EventHandler::StringToELState(const std::string& stateName)
521{
522  for (unsigned int i = 0 ; i < ES_NUMBER; i++)
523    if (stateName == EventHandler::stateNames[i])
524      return (elState)i;
525  return ES_NULL;
526}
527
528const std::string  EventHandler::stateNames[] =
529{
530  "game",
531  "game_menu",
532  "menu",
533  "shell",
534  "all",
535  "unknown",
536};
537
538
539/**
[7919]540 * @brief outputs some nice information about the EventHandler
[5237]541 */
[4872]542void EventHandler::debug() const
543{
544  PRINT(0)("===============================\n");
545  PRINT(0)(" EventHandle Debug Information \n");
546  PRINT(0)("===============================\n");
547  for(int i = 0; i < ES_NUMBER; ++i)
[7919]548  {
[4872]549    for(int j = 0; j < EV_NUMBER; ++j)
[7756]550      for (unsigned int evl = 0; evl < this->listeners[i][j].size(); evl++)
[8148]551        PRINT(0)("Event %s(%d) of State %s(%d) subscribed to %s (%p)\n",
552        EVToKeyName(j).c_str(), j,
553        ELStateToString((elState)i).c_str(), i,
554        this->listeners[i][j][evl]->getName(), this->listeners[i][j][evl]);
[7919]555  }
[4872]556  PRINT(0)("============================EH=\n");
557}
Note: See TracBrowser for help on using the repository browser.