[Orxonox-commit 1167] r5888 - in code/branches/core5/src: libraries/core modules/objects modules/objects/eventsystem

landauf at orxonox.net landauf at orxonox.net
Tue Oct 6 04:56:42 CEST 2009


Author: landauf
Date: 2009-10-06 04:56:42 +0200 (Tue, 06 Oct 2009)
New Revision: 5888

Added:
   code/branches/core5/src/modules/objects/eventsystem/EventFilter.cc
   code/branches/core5/src/modules/objects/eventsystem/EventFilter.h
   code/branches/core5/src/modules/objects/eventsystem/EventName.cc
   code/branches/core5/src/modules/objects/eventsystem/EventName.h
Modified:
   code/branches/core5/src/libraries/core/BaseObject.cc
   code/branches/core5/src/libraries/core/BaseObject.h
   code/branches/core5/src/libraries/core/Event.h
   code/branches/core5/src/libraries/core/EventIncludes.h
   code/branches/core5/src/modules/objects/ObjectsPrereqs.h
   code/branches/core5/src/modules/objects/eventsystem/CMakeLists.txt
   code/branches/core5/src/modules/objects/eventsystem/EventListener.cc
Log:
Added support for named events (so one object can fire multiple different events).
Added a new class, EventFilter, to filter differently named events and map them to different states.

I've wrapped the event names with a pretty useless macro, but I want to expand this in the future and register possible event names in the Identifier of the corresponding class. This allows us to get the possible event names in a static manner and thus build an editor upon it.

Modified: code/branches/core5/src/libraries/core/BaseObject.cc
===================================================================
--- code/branches/core5/src/libraries/core/BaseObject.cc	2009-10-06 02:51:08 UTC (rev 5887)
+++ code/branches/core5/src/libraries/core/BaseObject.cc	2009-10-06 02:56:42 UTC (rev 5888)
@@ -299,26 +299,26 @@
     /**
         @brief Fires an event (without a state).
     */
-    void BaseObject::fireEvent()
+    void BaseObject::fireEvent(const std::string& name)
     {
-        this->fireEvent(true);
-        this->fireEvent(false);
+        this->fireEvent(true, name);
+        this->fireEvent(false, name);
     }
 
     /**
         @brief Fires an event which activates or deactivates a state.
     */
-    void BaseObject::fireEvent(bool activate)
+    void BaseObject::fireEvent(bool activate, const std::string& name)
     {
-        this->fireEvent(activate, this);
+        this->fireEvent(activate, this, name);
     }
 
     /**
         @brief Fires an event which activates or deactivates a state with agiven originator (the object which triggered the event).
     */
-    void BaseObject::fireEvent(bool activate, BaseObject* originator)
+    void BaseObject::fireEvent(bool activate, BaseObject* originator, const std::string& name)
     {
-        Event event(activate, originator);
+        Event event(activate, originator, name);
 
         for (std::set<BaseObject*>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
         {

Modified: code/branches/core5/src/libraries/core/BaseObject.h
===================================================================
--- code/branches/core5/src/libraries/core/BaseObject.h	2009-10-06 02:51:08 UTC (rev 5887)
+++ code/branches/core5/src/libraries/core/BaseObject.h	2009-10-06 02:56:42 UTC (rev 5888)
@@ -160,9 +160,9 @@
             void addEventListener(BaseObject* listener);
             BaseObject* getEventListener(unsigned int index) const;
 
-            void fireEvent();
-            void fireEvent(bool activate);
-            void fireEvent(bool activate, BaseObject* originator);
+            void fireEvent(const std::string& name = "");
+            void fireEvent(bool activate, const std::string& name = "");
+            void fireEvent(bool activate, BaseObject* originator, const std::string& name = "");
             void fireEvent(Event& event);
 
             virtual void processEvent(Event& event);

Modified: code/branches/core5/src/libraries/core/Event.h
===================================================================
--- code/branches/core5/src/libraries/core/Event.h	2009-10-06 02:51:08 UTC (rev 5887)
+++ code/branches/core5/src/libraries/core/Event.h	2009-10-06 02:56:42 UTC (rev 5888)
@@ -39,11 +39,12 @@
     */
     struct _CoreExport Event
     {
-        Event(bool activate, BaseObject* originator) : activate_(activate), originator_(originator) {}
+        Event(bool activate, BaseObject* originator, const std::string& name) : activate_(activate), originator_(originator), name_(name) {}
 
         bool        activate_;   //!< True if this is an activating event (the event source was inactive before and just triggered the event) - false otherwise
         std::string statename_;  //!< The name of the state this event affects
         BaseObject* originator_; //!< The object which triggered this event
+        std::string name_;       //!< The name of this event
     };
 
     /**

Modified: code/branches/core5/src/libraries/core/EventIncludes.h
===================================================================
--- code/branches/core5/src/libraries/core/EventIncludes.h	2009-10-06 02:51:08 UTC (rev 5887)
+++ code/branches/core5/src/libraries/core/EventIncludes.h	2009-10-06 02:56:42 UTC (rev 5888)
@@ -67,5 +67,21 @@
     static orxonox::ExecutorMember<classname>* xmlsetfunctor##name = (orxonox::ExecutorMember<classname>*)&orxonox::createExecutor(orxonox::createFunctor(&classname::addEventSource), std::string( #classname ) + "::" + "addEventSource" + "(" + statename + ")")->setDefaultValue(1, statename); \
     static orxonox::ExecutorMember<classname>* xmlgetfunctor##name = (orxonox::ExecutorMember<classname>*)&orxonox::createExecutor(orxonox::createFunctor(&classname::getEventSource), std::string( #classname ) + "::" + "getEventSource" + "(" + statename + ")")->setDefaultValue(1, statename); \
     XMLPortObjectGeneric(xmlport##name, classname, orxonox::BaseObject, statename, xmlsetfunctor##name, xmlgetfunctor##name, xmlelement, mode, false, true)
+    
+
+/**
+    @brief Defines a new event name for a class. Named events can only have names which were defined with this macro.
+    
+    @param classname The name of the class
+    @param name      The name of the event
+*/
+#define CreateEventName(classname, name) \
+    static std::string eventname##classname##name = #name
+
+/**
+    @brief This macro is needed to fire an event with this name. The event name must previously be declared with @ref CreateEventName.
+*/    
+#define EventName(classname, name) \
+    eventname##classname##name
  
 #endif /* _EventIncludes_H__ */

Modified: code/branches/core5/src/modules/objects/ObjectsPrereqs.h
===================================================================
--- code/branches/core5/src/modules/objects/ObjectsPrereqs.h	2009-10-06 02:51:08 UTC (rev 5887)
+++ code/branches/core5/src/modules/objects/ObjectsPrereqs.h	2009-10-06 02:56:42 UTC (rev 5888)
@@ -77,7 +77,9 @@
 
     // eventsystem
     class EventDispatcher;
+    class EventFilter;
     class EventListener;
+    class EventName;
     class EventTarget;
 
     // triggers

Modified: code/branches/core5/src/modules/objects/eventsystem/CMakeLists.txt
===================================================================
--- code/branches/core5/src/modules/objects/eventsystem/CMakeLists.txt	2009-10-06 02:51:08 UTC (rev 5887)
+++ code/branches/core5/src/modules/objects/eventsystem/CMakeLists.txt	2009-10-06 02:56:42 UTC (rev 5888)
@@ -1,5 +1,7 @@
 ADD_SOURCE_FILES(OBJECTS_SRC_FILES
   EventDispatcher.cc
+  EventFilter.cc
   EventListener.cc
+  EventName.cc
   EventTarget.cc
 )

Added: code/branches/core5/src/modules/objects/eventsystem/EventFilter.cc
===================================================================
--- code/branches/core5/src/modules/objects/eventsystem/EventFilter.cc	                        (rev 0)
+++ code/branches/core5/src/modules/objects/eventsystem/EventFilter.cc	2009-10-06 02:56:42 UTC (rev 5888)
@@ -0,0 +1,122 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "EventFilter.h"
+
+#include "core/CoreIncludes.h"
+#include "core/XMLPort.h"
+#include "EventName.h"
+
+namespace orxonox
+{
+    CreateFactory(EventFilter);
+
+    EventFilter::EventFilter(BaseObject* creator) : BaseObject(creator)
+    {
+        RegisterObject(EventFilter);
+
+        this->bActive_ = false;
+    }
+
+    EventFilter::~EventFilter()
+    {
+        for (std::list<EventName*>::const_iterator it = this->names_.begin(); it != this->names_.end(); )
+            (*(it++))->destroy();
+    }
+
+    void EventFilter::XMLPort(Element& xmlelement, XMLPort::Mode mode)
+    {
+        SUPER(EventFilter, XMLPort, xmlelement, mode);
+
+        XMLPortObject(EventFilter, BaseObject, "", addFilterSource, getFilterSource, xmlelement, mode);
+        XMLPortObject(EventFilter, EventName, "names", addEventName, getEventName, xmlelement, mode);
+    }
+
+    void EventFilter::processEvent(Event& event)
+    {
+        if (this->bActive_)
+        {
+            COUT(2) << "Warning: Detected Event loop in EventFilter \"" << this->getName() << "\"" << std::endl;
+            return;
+        }
+
+        if (this->names_.size() > 0)
+        {
+            bool success = false;
+            for (std::list<EventName*>::const_iterator it = this->names_.begin(); it != this->names_.end(); ++it)
+            {
+                if ((*it)->getName() == event.name_)
+                {
+                    success = true;
+                    break;
+                }
+            }
+            if (!success)
+                return;
+        }
+
+        this->bActive_ = true;
+        this->fireEvent(event.activate_, event.originator_, event.name_);
+        this->bActive_ = false;
+    }
+
+    void EventFilter::addFilterSource(BaseObject* source)
+    {
+        this->sources_.push_back(source);
+        this->addEventSource(source, "");
+    }
+
+    BaseObject* EventFilter::getFilterSource(unsigned int index) const
+    {
+        unsigned int i = 0;
+        for (std::list<BaseObject*>::const_iterator it = this->sources_.begin(); it != this->sources_.end(); ++it)
+        {
+            if (i == index)
+                return (*it);
+            ++i;
+        }
+        return 0;
+    }
+
+    void EventFilter::addEventName(EventName* eventname)
+    {
+        this->names_.push_back(eventname);
+    }
+
+    EventName* EventFilter::getEventName(unsigned int index) const
+    {
+        unsigned int i = 0;
+        for (std::list<EventName*>::const_iterator it = this->names_.begin(); it != this->names_.end(); ++it)
+        {
+            if (i == index)
+                return (*it);
+            ++i;
+        }
+        return 0;
+    }
+}


Property changes on: code/branches/core5/src/modules/objects/eventsystem/EventFilter.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/branches/core5/src/modules/objects/eventsystem/EventFilter.h
===================================================================
--- code/branches/core5/src/modules/objects/eventsystem/EventFilter.h	                        (rev 0)
+++ code/branches/core5/src/modules/objects/eventsystem/EventFilter.h	2009-10-06 02:56:42 UTC (rev 5888)
@@ -0,0 +1,62 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef _EventFilter_H__
+#define _EventFilter_H__
+
+#include "objects/ObjectsPrereqs.h"
+
+#include <list>
+#include "core/BaseObject.h"
+
+namespace orxonox
+{
+    class _ObjectsExport EventFilter : public BaseObject
+    {
+        public:
+            EventFilter(BaseObject* creator);
+            virtual ~EventFilter();
+
+            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
+
+            virtual void processEvent(Event& event);
+
+            void addFilterSource(BaseObject* source);
+            BaseObject* getFilterSource(unsigned int index) const;
+
+            void addEventName(EventName* eventname);
+            EventName* getEventName(unsigned int index) const;
+
+        private:
+            std::list<BaseObject*> sources_;
+            std::list<EventName*> names_;
+            bool bActive_;
+    };
+}
+
+#endif /* _EventFilter_H__ */


Property changes on: code/branches/core5/src/modules/objects/eventsystem/EventFilter.h
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: code/branches/core5/src/modules/objects/eventsystem/EventListener.cc
===================================================================
--- code/branches/core5/src/modules/objects/eventsystem/EventListener.cc	2009-10-06 02:51:08 UTC (rev 5887)
+++ code/branches/core5/src/modules/objects/eventsystem/EventListener.cc	2009-10-06 02:56:42 UTC (rev 5888)
@@ -62,9 +62,7 @@
         }
 
         this->bActive_ = true;
-
-        this->fireEvent(event.activate_, event.originator_);
-
+        this->fireEvent(event.activate_, event.originator_, event.name_);
         this->bActive_ = false;
     }
 

Added: code/branches/core5/src/modules/objects/eventsystem/EventName.cc
===================================================================
--- code/branches/core5/src/modules/objects/eventsystem/EventName.cc	                        (rev 0)
+++ code/branches/core5/src/modules/objects/eventsystem/EventName.cc	2009-10-06 02:56:42 UTC (rev 5888)
@@ -0,0 +1,40 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "EventName.h"
+#include "core/CoreIncludes.h"
+
+namespace orxonox
+{
+    CreateFactory(EventName);
+
+    EventName::EventName(BaseObject* creator) : BaseObject(creator)
+    {
+        RegisterObject(EventName);
+    }
+}


Property changes on: code/branches/core5/src/modules/objects/eventsystem/EventName.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/branches/core5/src/modules/objects/eventsystem/EventName.h
===================================================================
--- code/branches/core5/src/modules/objects/eventsystem/EventName.h	                        (rev 0)
+++ code/branches/core5/src/modules/objects/eventsystem/EventName.h	2009-10-06 02:56:42 UTC (rev 5888)
@@ -0,0 +1,46 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef _EventName_H__
+#define _EventName_H__
+
+#include "objects/ObjectsPrereqs.h"
+
+#include "core/BaseObject.h"
+
+namespace orxonox
+{
+    class _ObjectsExport EventName : public BaseObject
+    {
+        public:
+            EventName(BaseObject* creator);
+            virtual ~EventName() {}
+    };
+}
+
+#endif /* _EventName_H__ */


Property changes on: code/branches/core5/src/modules/objects/eventsystem/EventName.h
___________________________________________________________________
Added: svn:eol-style
   + native




More information about the Orxonox-commit mailing list