Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5887


Ignore:
Timestamp:
Oct 6, 2009, 4:51:08 AM (15 years ago)
Author:
landauf
Message:

Fixed an issue with the new event-system: even states which weren't declared are not loaded. While this is normally fine, it is a problem in EventDispatcher and EventTarget which have to pipe events to other objects and therefore have to support any state, not just the native ones.

Location:
code/branches/core5/src
Files:
5 edited

Legend:

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

    r5883 r5887  
    245245
    246246    /**
    247         @brief Adds an object which listens to the events of this object.
     247        @brief Adds an object which listens to the events of this object. The events are sent to the other objects mainstate.
    248248    */
    249249    void BaseObject::addEventListener(BaseObject* listener)
     
    268268    }
    269269
     270    /**
     271        @brief Adds a new event-state to the object. Event-states are states which can be changed by events.
     272        @param name  The name of the event
     273        @param state The object containing information about the event-state
     274    */
    270275    void BaseObject::addEventState(const std::string& name, EventState* state)
    271276    {
     
    280285    }
    281286
     287    /**
     288        @brief Returns the event-state with the given name.
     289    */
    282290    EventState* BaseObject::getEventState(const std::string& name) const
    283291    {
     
    406414        }
    407415    }
     416   
     417    /**
     418        @brief Manually loads all event states, even if the class doesn't officially support them. This is needed by some classes like @ref EventDispatcher or @ref EventTarget.
     419    */
     420    void BaseObject::loadAllEventStates(Element& xmlelement, XMLPort::Mode mode, BaseObject* object, Identifier* identifier)
     421    {
     422        Element* events = xmlelement.FirstChildElement("events", false);
     423        if (events)
     424        {
     425            // get the list of all states present
     426            std::list<std::string> eventnames;
     427            if (mode == XMLPort::LoadObject || mode == XMLPort::ExpandObject)
     428            {
     429                for (ticpp::Iterator<ticpp::Element> child = events->FirstChildElement(false); child != child.end(); child++)
     430                    eventnames.push_back(child->Value());
     431            }
     432            else if (mode == XMLPort::SaveObject)
     433            {
     434            }
     435
     436            // iterate through all states and get the event sources
     437            for (std::list<std::string>::iterator it = eventnames.begin(); it != eventnames.end(); ++it)
     438            {
     439                std::string statename = (*it);
     440
     441                // if the event state is already known, continue with the next state
     442                orxonox::EventState* eventstate = object->getEventState(statename);
     443                if (eventstate)
     444                    continue;
     445
     446                XMLPortClassObjectContainer<BaseObject, BaseObject>* container = (XMLPortClassObjectContainer<BaseObject, BaseObject>*)(identifier->getXMLPortObjectContainer(statename));
     447                if (!container)
     448                {
     449                    ExecutorMember<BaseObject>* setfunctor = createExecutor(createFunctor(&BaseObject::addEventSource), std::string( "BaseObject" ) + "::" + "addEventSource" + "(" + statename + ")");
     450                    ExecutorMember<BaseObject>* getfunctor = createExecutor(createFunctor(&BaseObject::getEventSource), std::string( "BaseObject" ) + "::" + "getEventSource" + "(" + statename + ")");
     451                    setfunctor->setDefaultValue(1, statename);
     452                    getfunctor->setDefaultValue(1, statename);
     453
     454                    container = new XMLPortClassObjectContainer<BaseObject, BaseObject>(statename, identifier, setfunctor, getfunctor, false, true);
     455                    identifier->addXMLPortObjectContainer(statename, container);
     456                }
     457                container->port(object, *events, mode);
     458            }
     459        }
     460    }
    408461}
  • code/branches/core5/src/libraries/core/BaseObject.h

    r5882 r5887  
    172172            /** @brief Returns the indentation of the debug output in the Loader. @return The indentation */
    173173            inline const std::string& getLoaderIndentation() const { return this->loaderIndentation_; }
     174           
     175            static void loadAllEventStates(Element& xmlelement, XMLPort::Mode mode, BaseObject* object, Identifier* identifier);
    174176
    175177        protected:
     178            void addEventState(const std::string& name, EventState* container);
     179            EventState* getEventState(const std::string& name) const;
     180
     181            std::string name_;                                 //!< The name of the object
     182            std::string oldName_;                              //!< The old name of the object
     183            mbool       bActive_;                              //!< True = the object is active
     184            mbool       bVisible_;                             //!< True = the object is visible
     185            std::string mainStateName_;
     186            Functor*    mainStateFunctor_;
     187
     188        private:
    176189            /** @brief Adds an object which listens to the events of this object. */
    177190            inline void registerEventListener(BaseObject* object)
     
    181194                { this->eventListeners_.erase(object); }
    182195
    183             void addEventState(const std::string& name, EventState* container);
    184             EventState* getEventState(const std::string& name) const;
    185 
    186             std::string name_;                                 //!< The name of the object
    187             std::string oldName_;                              //!< The old name of the object
    188             mbool       bActive_;                              //!< True = the object is active
    189             mbool       bVisible_;                             //!< True = the object is visible
    190             std::string mainStateName_;
    191             Functor*    mainStateFunctor_;
    192 
    193         private:
    194196            void setXMLName(const std::string& name);
    195197            Template* getTemplate(unsigned int index) const;
  • code/branches/core5/src/modules/objects/eventsystem/EventDispatcher.cc

    r5886 r5887  
    5454
    5555        XMLPortObject(EventDispatcher, BaseObject, "targets", addTarget, getTarget, xmlelement, mode);
     56
     57        // since we need event sources mapped to any state, we have to parse XML by ourselves
     58        this->loadAllEventStates(xmlelement, mode, this, Class(EventDispatcher));
    5659    }
    5760
  • code/branches/core5/src/modules/objects/eventsystem/EventTarget.cc

    r5882 r5887  
    2929#include "EventTarget.h"
    3030#include "core/CoreIncludes.h"
     31#include "core/XMLPort.h"
    3132
    3233namespace orxonox
     
    4344    }
    4445   
     46    void EventTarget::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     47    {
     48        SUPER(EventTarget, XMLPort, xmlelement, mode);
     49
     50        // since we need event sources mapped to any state, we have to parse XML by ourselves
     51        this->loadAllEventStates(xmlelement, mode, this, Class(EventTarget));
     52    }
     53
    4554    void EventTarget::processEvent(Event& event)
    4655    {
  • code/branches/core5/src/modules/objects/eventsystem/EventTarget.h

    r5882 r5887  
    4343            virtual ~EventTarget();
    4444           
     45            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     46           
    4547            virtual void processEvent(Event& event);
    4648
Note: See TracChangeset for help on using the changeset viewer.