Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7919 in orxonox.OLD for trunk/src/lib/event


Ignore:
Timestamp:
May 28, 2006, 3:48:13 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the gui branche back
merged with command:
https://svn.orxonox.net/orxonox/branches/gui
no conflicts

Location:
trunk/src/lib/event
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/event/event_def.h

    r7868 r7919  
    2828  EV_JOY_BUTTON,
    2929
     30  EV_WINDOW_FOCUS,
    3031  EV_VIDEO_RESIZE,
    3132
     33  EV_LEAVE_STATE,
    3234  EV_MAIN_QUIT,
    3335
  • trunk/src/lib/event/event_handler.cc

    r7868 r7919  
    2929
    3030/**
    31  * standard constructor
    32 */
     31 * @brief standard constructor
     32 */
    3333EventHandler::EventHandler ()
    3434{
     
    4242
    4343  /* 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;
    4546  this->grabEvents(false);
    4647
     
    5152
    5253/**
    53  * the singleton reference to this class
     54 * @brief the singleton reference to this class
    5455*/
    5556EventHandler* EventHandler::singletonRef = NULL;
     
    5758
    5859/**
    59  *  standard deconstructor
    60 
    61 */
     60 * @brief standard deconstructor
     61 */
    6262EventHandler::~EventHandler ()
    6363{
     
    7979
    8080/**
    81  * initializes the event handler
     81 * @brief initializes the event handler
    8282 *
    8383 * this has to be called before the use of the event handler
     
    8989
    9090/**
    91  * pushes the current State in the State-stack, and selects state
     91 * @param state: to which the event handler shall change
     92 */
     93void 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
    92131 * @param state the new State to set
    93132 */
     
    106145
    107146/**
    108  * this removes the topmost stack-entry and select the underlying one
     147 * @brief this removes the topmost stack-entry and select the underlying one
    109148 * @returns the next stack-entry
    110149 */
     
    144183  {
    145184    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
    148188      {
    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);
    150190      }
    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    }
    161200  }
    162201}
     
    167206 * @param state: the stat in which it has been subscribed
    168207 * @param eventType: the event, that shall be unsubscribed
    169 
    170    if you want to unsubscribe an event listener from all subscribed events, just use the
    171    unsubscribe(EventListener* el, elState state) function
    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 */
    173212void EventHandler::unsubscribe(EventListener* el, elState state, int eventType)
    174213{
     
    177216    for (unsigned int i = 0; i < ES_NUMBER; i++)
    178217    {
    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))
    184220        this->listeners[i][eventType].erase(listener);
    185221    }
    186222  else
    187223  {
    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))
    193226      this->listeners[state][eventType].erase(listener);
    194227  }
     
    203236void EventHandler::unsubscribe(EventListener* el, elState state)
    204237{
    205   if( el == NULL || state >= ES_NUMBER)
    206     return;
     238  assert( el != NULL && state < ES_NUMBER);
    207239  if( state == ES_ALL)
    208240  {
     
    228260}
    229261
     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 */
    230268bool EventHandler::isSubscribed(elState state, int eventType)
    231269{
     
    236274
    237275/**
    238  * flush all registered events
     276 * @brief flush all registered events
    239277 * @param state: a specific state
    240278*/
     
    261299
    262300
    263 void EventHandler::withUNICODE(bool enableUNICODE)
    264 {
    265   SDL_EnableUNICODE(enableUNICODE);
    266   this->bUNICODE = enableUNICODE;
    267 }
    268 
     301bool 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 */
     318void 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 */
    269329void EventHandler::grabEvents(bool grabEvents)
    270330{
     
    282342}
    283343
    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 */
     351void EventHandler::process() const
    290352{
    291353  SDL_Event event;
     
    299361        ev.bPressed = true;
    300362        ev.type = event.key.keysym.sym;
    301         if (unlikely(this->bUNICODE))
     363        if (unlikely(this->bUNICODE[this->state]))
    302364          ev.x = event.key.keysym.unicode;
    303365        break;
     
    305367        ev.bPressed = false;
    306368        ev.type = event.key.keysym.sym;
    307         if (unlikely(this->bUNICODE))
     369        if (unlikely(this->bUNICODE[this->state]))
    308370          ev.x = event.key.keysym.unicode;
    309371        break;
     
    344406        ev.type = EV_JOY_BUTTON;
    345407        break;
     408      case SDL_ACTIVEEVENT:
     409        ev.type = EV_WINDOW_FOCUS;
     410        ev.bPressed = (event.active.gain != 0);
     411        break;
    346412      case SDL_VIDEORESIZE:
    347413        ev.resize = event.resize;
     
    355421        break;
    356422    }
    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 */
     432void 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 */
    378460int EventHandler::eventFilter(const SDL_Event *event)
    379461{
     
    400482
    401483/**
    402  * outputs some nice information about the EventHandler
     484 * @brief outputs some nice information about the EventHandler
    403485 */
    404486void EventHandler::debug() const
     
    408490  PRINT(0)("===============================\n");
    409491  for(int i = 0; i < ES_NUMBER; ++i)
     492  {
    410493    for(int j = 0; j < EV_NUMBER; ++j)
    411494      for (unsigned int evl = 0; evl < this->listeners[i][j].size(); evl++)
    412495        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  }
    413497  PRINT(0)("============================EH=\n");
    414498}
  • trunk/src/lib/event/event_handler.h

    r7868 r7919  
    1111#include "key_mapper.h"
    1212#include "event_def.h"
     13#include "event.h"
    1314#include <stack>
    1415#include <vector>
     
    2627  void init();
    2728
    28   /** @param state: to which the event handler shall change */
    29   inline void setState(elState state) { this->state = state; };
     29  void setState(elState state);
    3030  /** @returns the current state */
    3131  inline elState getState() const { return this->state; };
     
    4242
    4343
    44   void withUNICODE(bool enableUNICODE);
     44  void withUNICODE(elState state, bool enableUNICODE);
    4545  void grabEvents(bool grabEvents);
    4646  bool grabbedEvents() const { return this->eventsGrabbed; };
    4747
    48   void process();
     48  void process() const;
     49  void dispachEvent(elState state, const Event& event) const;
    4950
    5051  static int eventFilter(const SDL_Event *event);
     
    5354 private:
    5455  EventHandler();
     56
     57  bool findListener(std::vector<EventListener*>::iterator* it, elState state, int eventType, EventListener* listener);
    5558
    5659 private:
     
    6265  KeyMapper                    keyMapper;                       //!< reference to the key mapper.
    6366
    64   bool                         bUNICODE;                        //!< If unicode should be enabled.
     67  bool                         bUNICODE[ES_NUMBER];             //!< If unicode should be enabled.
    6568  bool                         eventsGrabbed;                   //!< If the events should be grabbed
    6669};
Note: See TracChangeset for help on using the changeset viewer.