Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 31, 2008, 3:31:21 AM (16 years ago)
Author:
landauf
Message:
  • added EventListener: this class fires an event if another class with a specified name fires
  • added EventDispatcher: this class dispatches events to several other classes (EventTarget) if it gets an event itself
  • added EventTarget: used in EventDispatcher to access all classes with a specified name
  • added XMLNameListener: this listener gets called every time a new class was loaded through XML with a name != "". used to find classes with a given name even if they're not existing at the moment.
Location:
code/branches/objecthierarchy/src
Files:
8 added
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchy/src/core/BaseObject.cc

    r2069 r2074  
    3838#include "XMLPort.h"
    3939#include "XMLFile.h"
     40#include "XMLNameListener.h"
    4041#include "Template.h"
    4142#include "util/String.h"
     
    8182    {
    8283        for (std::list<BaseObject*>::const_iterator it = this->events_.begin(); it != this->events_.end(); ++it)
    83             (*it)->eventListeners_.erase(this);
     84            (*it)->unregisterEventListener(this);
     85
     86        for (std::map<BaseObject*, std::string>::const_iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
     87            it->first->removeEvent(this);
    8488    }
    8589
     
    9296    void BaseObject::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    9397    {
    94         XMLPortParam(BaseObject, "name", setName, getName, xmlelement, mode);
     98        XMLPortParam(BaseObject, "name", setXMLName, getName, xmlelement, mode);
    9599        XMLPortParam(BaseObject, "visible", setVisible, isVisible, xmlelement, mode);
    96100        XMLPortParam(BaseObject, "active", setActive, isActive, xmlelement, mode);
     
    135139
    136140    /**
     141        @brief Loads the name of the object through XML and calls all XMLNameListener.
     142        @param name The name of the object
     143    */
     144    void BaseObject::setXMLName(const std::string& name)
     145    {
     146        this->setName(name);
     147
     148        for (ObjectList<XMLNameListener>::iterator it = ObjectList<XMLNameListener>::begin(); it != ObjectList<XMLNameListener>::end(); ++it)
     149            it->loadedNewXMLName(this);
     150    }
     151
     152    /**
    137153        @brief Returns the levelfile that loaded this object.
    138154        @return The levelfile
     
    187203    void BaseObject::addEvent(BaseObject* event, const std::string& sectionname)
    188204    {
    189         event->eventListeners_[this] = sectionname;
     205        event->registerEventListener(this, sectionname);
    190206        this->events_.push_back(event);
     207    }
     208
     209    void BaseObject::removeEvent(BaseObject* event)
     210    {
     211        this->events_.remove(event);
    191212    }
    192213
     
    232253    void BaseObject::fireEvent(bool activate)
    233254    {
    234         Event event(activate, this);
     255        this->fireEvent(activate, this);
     256    }
     257
     258    void BaseObject::fireEvent(bool activate, BaseObject* originator)
     259    {
     260        Event event(activate, originator);
    235261
    236262        for (std::map<BaseObject*, std::string>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
     
    241267    }
    242268
     269    void BaseObject::fireEvent(Event& event)
     270    {
     271        for (std::map<BaseObject*, std::string>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
     272            it->first->processEvent(event);
     273    }
     274
    243275    void BaseObject::processEvent(Event& event)
    244276    {
  • code/branches/objecthierarchy/src/core/BaseObject.h

    r2065 r2074  
    115115            void fireEvent();
    116116            void fireEvent(bool activate);
     117            void fireEvent(bool activate, BaseObject* originator);
     118            void fireEvent(Event& event);
    117119
    118120            virtual void processEvent(Event& event);
    119121
     122            inline void registerEventListener(BaseObject* object, const std::string& sectionname)
     123                { this->eventListeners_[object] = sectionname; }
     124            inline void unregisterEventListener(BaseObject* object)
     125                { this->eventListeners_.erase(object); }
     126
    120127            void addEvent(BaseObject* event, const std::string& sectionname);
     128            void removeEvent(BaseObject* event);
    121129            BaseObject* getEvent(unsigned int index) const;
    122130
     
    136144
    137145        private:
     146            void setXMLName(const std::string& name);
    138147            Template* getTemplate(unsigned int index) const;
    139148
  • code/branches/objecthierarchy/src/core/CMakeLists.txt

    r2063 r2074  
    3434  Template.cc
    3535  XMLPort.cc
     36  XMLNameListener.cc
    3637
    3738  # shell
  • code/branches/objecthierarchy/src/core/CorePrereqs.h

    r2063 r2074  
    155155  class Tickable;
    156156  class XMLFile;
     157  class XMLNameListener;
    157158  template <class T, class O>
    158159  class XMLPortClassObjectContainer;
  • code/branches/objecthierarchy/src/orxonox/CMakeLists.txt

    r2073 r2074  
    4343  tools/WindowEventListener.cc
    4444
     45  objects/EventListener.cc
     46  objects/EventDispatcher.cc
     47  objects/EventTarget.cc
    4548  objects/Radar.cc
    4649  objects/RadarListener.cc
  • code/branches/objecthierarchy/src/orxonox/OrxonoxPrereqs.h

    r2073 r2074  
    112112    class EventTrigger;
    113113
     114    class EventListener;
     115    class EventDispatcher;
     116    class EventTarget;
     117
    114118    class Controller;
    115119    class HumanController;
Note: See TracChangeset for help on using the changeset viewer.