Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7756 in orxonox.OLD for trunk/src/lib


Ignore:
Timestamp:
May 21, 2006, 6:42:09 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: Multiple Event Subscription Method
@patrick, do you approve of this, or do you think, that it is too much??

This is something i proposed some time ago, and it is nice, because one can overload the Commandos

Location:
trunk/src/lib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/event/event_handler.cc

    r7256 r7756  
    2626#include "class_list.h"
    2727
    28 using namespace std;
    29 
     28#include <algorithm>
    3029
    3130/**
     
    4342
    4443  /* now initialize them all to zero */
    45   this->flush(ES_ALL);
    4644  this->withUNICODE(false);
    4745  this->grabEvents(false);
    4846
    4947  this->state = ES_GAME;
    50   this->keyMapper = NULL;
    5148  this->eventsGrabbed = false;
    5249}
     
    6966    for(int j = 0; j < EV_NUMBER; ++j)
    7067    {
    71       if( this->listeners[i][j] != NULL)
     68      if(!this->listeners[i][j].empty())
    7269      {
    7370        PRINTF(2)("forgot to unsubscribe an EventListener!\n");// %s!\n", this->listeners[i][j]->getName());
     
    7572    }
    7673  }
    77   delete this->keyMapper;
    78 
    7974  SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
    8075
     
    9085void EventHandler::init()
    9186{
    92   if (this->keyMapper == NULL)
    93   {
    94     this->keyMapper = new KeyMapper();
    95     this->keyMapper->loadKeyBindings();
    96   }
     87  this->keyMapper.loadKeyBindings();
    9788}
    9889
     
    138129
    139130/**
    140  * subscribe to an event
     131 * @brief subscribe to an event
    141132 * @param el: the event listener that wants to subscribe itself, the listener that will be called when the evetn occures
    142133 * @param state: for which the listener wants to receive events
     
    153144  {
    154145    for(unsigned int i = 0; i < ES_NUMBER; i++)
    155       if( likely(this->listeners[i][eventType] == NULL))
    156         this->listeners[i][eventType] = el;
    157       else
    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   else
    161     if( likely(this->listeners[state][eventType] == NULL))
    162     {
    163       this->listeners[state][eventType] = el;
    164     }
    165     else
    166       PRINTF(2)("% 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);
    167 }
    168 
    169 
    170 /**
    171  *  unsubscribe from the EventHandler
     146    {
     147      if( !likely(this->listeners[i][eventType].empty()))
     148      {
     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);
     150      }
     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);
     161  }
     162}
     163
     164
     165/**
     166 * @brief unsubscribe from the EventHandler
    172167 * @param state: the stat in which it has been subscribed
    173168 * @param eventType: the event, that shall be unsubscribed
     
    181176  if (state == ES_ALL)
    182177    for (unsigned int i = 0; i < ES_NUMBER; i++)
    183       this->listeners[i][eventType] = NULL;
    184   else
    185     this->listeners[state][eventType] = NULL;
     178      this->listeners[i][eventType].clear();
     179  else
     180    this->listeners[state][eventType].clear();
    186181}
    187182
     
    203198      for(unsigned int j = 0; j < EV_NUMBER; j++)
    204199      {
    205         if( this->listeners[i][j] == el )
    206           this->listeners[i][j] = NULL;
     200        std::vector<EventListener*>::iterator deller = std::find (this->listeners[i][j].begin(), this->listeners[i][j].end(), el);
     201        if( deller != this->listeners[i][j].end())
     202          this->listeners[i][j].erase(deller);
    207203      }
    208204    }
     
    212208    for(int j = 0; j < EV_NUMBER; j++)
    213209    {
    214       if( this->listeners[state][j] == el )
    215         this->listeners[state][j] = NULL;
    216     }
    217   }
    218 }
     210      std::vector<EventListener*>::iterator deller =  std::find (this->listeners[state][j].begin(), this->listeners[state][j].end(), el);
     211      if( deller != this->listeners[state][j].end())
     212        this->listeners[state][j].erase(deller);
     213    }
     214  }
     215}
     216
     217bool EventHandler::isSubscribed(elState state, int eventType)
     218{
     219  return(listeners[state][eventType].empty()) ? false : true;
     220};
     221
    219222
    220223
     
    231234      for(int j = 0; j < EV_NUMBER; ++j)
    232235      {
    233         this->listeners[i][j] = NULL;
     236        this->listeners[i][j].clear();
    234237      }
    235238    }
     
    239242    for(int j = 0; j < EV_NUMBER; ++j)
    240243    {
    241       this->listeners[state][j] = NULL;
     244      this->listeners[state][j].clear();
    242245    }
    243246  }
     
    344347    PRINT(4)("=  Got Event nr %i, for state %i", ev.type, this->state);
    345348
    346     listener = this->listeners[this->state][ev.type];
    347     if( listener != NULL)
     349
     350    for (unsigned int i = 0; i < this->listeners[this->state][ev.type].size(); i++)
    348351    {
    349352      PRINT(4)("=  Event dispatcher msg: This event has been consumed\n");
    350353      PRINT(4)("=======================================================\n");
    351       listener->process(ev);
    352     }
    353     else
    354     {
    355       PRINT(4)("=  Event dispatcher msg: This event has NOT been consumed\n");
    356       PRINT(4)("=======================================================\n");
    357     }
     354      listeners[this->state][ev.type][i]->process(ev);
     355    }
     356    /*    else
     357        {
     358          PRINT(4)("=  Event dispatcher msg: This event has NOT been consumed\n");
     359          PRINT(4)("=======================================================\n");
     360        }*/
    358361  }
    359362}
     
    393396  for(int i = 0; i < ES_NUMBER; ++i)
    394397    for(int j = 0; j < EV_NUMBER; ++j)
    395       if( this->listeners[i][j] != NULL)
    396         PRINT(0)("Event %d of State %d subscribed to %s (%p)\n", j, i, this->listeners[i][j]->getName(), this->listeners[i][j]);
     398      for (unsigned int evl = 0; evl < this->listeners[i][j].size(); evl++)
     399        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]);
    397400  PRINT(0)("============================EH=\n");
    398401}
  • trunk/src/lib/event/event_handler.h

    r7256 r7756  
    1212#include "event_def.h"
    1313#include <stack>
     14#include <vector>
    1415
    1516// FORWARD DECLARATION
     
    3839  void flush(elState state);
    3940  /** @returns true, if the @param state has @param eventType subscribed?? */
    40   inline bool isSubscribed(elState state, int eventType) { return(listeners[state][eventType] == NULL)?false:true; };
     41  bool isSubscribed(elState state, int eventType);
    4142
    4243
     
    5455
    5556 private:
    56   static EventHandler*       singletonRef;                    //!< the singleton reference
     57  static EventHandler*         singletonRef;                    //!< the singleton reference
    5758
    58   EventListener*             listeners[ES_NUMBER][EV_NUMBER]; //!< a list of registered listeners.
    59   elState                    state;                           //!< the state of the event handlder.
    60   std::stack<short>          stateStack;                      //!< a stack for the States we are in.
    61   KeyMapper*                 keyMapper;                       //!< reference to the key mapper.
     59  std::vector<EventListener*>  listeners[ES_NUMBER][EV_NUMBER]; //!< a list of registered listeners.
     60  elState                      state;                           //!< the state of the event handlder.
     61  std::stack<short>            stateStack;                      //!< a stack for the States we are in.
     62  KeyMapper                    keyMapper;                       //!< reference to the key mapper.
    6263
    63   bool                       bUNICODE;                        //!< If unicode should be enabled.
    64   bool                       eventsGrabbed;                   //!< If the events should be grabbed
     64  bool                         bUNICODE;                        //!< If unicode should be enabled.
     65  bool                         eventsGrabbed;                   //!< If the events should be grabbed
    6566};
    6667
  • trunk/src/lib/graphics/text_engine/multi_line_text.cc

    r7754 r7756  
    136136    if (width > maxWidth || this->getText()[i] == '\n')
    137137    {
    138       this->lineEnds.push_back(i);
    139       width = 0.0f;
     138      if (likely(i > 0))
     139      {
     140        this->lineEnds.push_back( i -1 );
     141        width = this->getFont()->getGlyphArray()[this->getText()[i-1]]->advance;
     142      }
     143      else
     144        width = 0.0f;
    140145    }
     146
    141147    // Advance the Text.
    142148    if(this->getFont()->getGlyphArray()[this->getText()[i]] != NULL)
Note: See TracChangeset for help on using the changeset viewer.