Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/libraries/core/BaseObject.h @ 5882

Last change on this file since 5882 was 5882, checked in by landauf, 15 years ago

Again some changes in the event-system:

  • Added "mainstate" event-state to BaseObject. It leads to the state which was defined with the mainstate attribute in XML.
  • Support event-forwarding to the mainstate of event-listeners.
  • The "targets" subsection of the EventDispatcher now supports all kinds of objects (not just EventTargets).
  • EventTarget now works in all places of the XML-file, not just in the "targets" section of EventDispatcher.

Added a sample XML-file which explains several aspects of the event-system.

  • Property svn:eol-style set to native
File size: 10.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file
31    @brief Definition of the BaseObject class.
32
33    The BaseObject is the parent of all classes representing an instance in the game.
34*/
35
36#ifndef _BaseObject_H__
37#define _BaseObject_H__
38
39#include "CorePrereqs.h"
40
41#include <map>
42#include <list>
43
44#include "util/mbool.h"
45#include "OrxonoxClass.h"
46#include "Super.h"
47#include "SmartPtr.h"
48
49namespace orxonox
50{
51    class Scene;
52    class Gametype;
53
54    //! The BaseObject is the parent of all classes representing an instance in the game.
55    class _CoreExport BaseObject : virtual public OrxonoxClass
56    {
57        template <class T> friend class XMLPortClassParamContainer;
58
59        public:
60            BaseObject(BaseObject* creator);
61            virtual ~BaseObject();
62            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
63            virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
64
65            /** @brief Returns if the object was initialized (passed the object registration). @return True was the object is initialized */
66            inline bool isInitialized() const { return this->bInitialized_; }
67
68            /** @brief Sets the name of the object. @param name The name */
69            inline void setName(const std::string& name) { this->oldName_ = this->name_; this->name_ = name; this->changedName(); }
70            /** @brief Returns the name of the object. */
71            inline const std::string& getName() const { return this->name_; }
72            /** @brief Returns the old name of the object. */
73            inline const std::string& getOldName() const { return this->oldName_; }
74            /** @brief This function gets called if the name of the object changes. */
75            virtual void changedName() {}
76
77            /** @brief Sets the state of the objects activity. @param bActive True = active */
78            inline void setActive(bool bActive)
79            {
80                if (this->bActive_ != bActive)
81                {
82                    this->bActive_ = bActive;
83                    this->changedActivity();
84                }
85            }
86            /** @brief Returns the state of the objects activity. @return The state of the activity */
87            inline const mbool& isActive() const { return this->bActive_; }
88            /** @brief This function gets called if the activity of the object changes. */
89            virtual void changedActivity() {}
90
91            /** @brief Sets the state of the objects visibility. @param bVisible True = visible */
92            inline void setVisible(bool bVisible)
93            {
94                if (this->bVisible_ != bVisible)
95                {
96                    this->bVisible_ = bVisible;
97                    this->changedVisibility();
98                }
99            }
100            /** @brief Returns the state of the objects visibility. @return The state of the visibility */
101            inline const mbool& isVisible() const { return this->bVisible_; }
102            /** @brief This function gets called if the visibility of the object changes. */
103            virtual void changedVisibility() {}
104
105            void setMainState(bool state);
106
107            /** @brief Sets the name of the main state (used for event reactions). */
108            void setMainStateName(const std::string& name)
109            {
110                if (this->mainStateName_ != name)
111                {
112                    this->mainStateName_ = name;
113                    this->changedMainStateName();
114                }
115            }
116            /** @brief Returns the name of the main state. */
117            inline const std::string& getMainStateName() const { return this->mainStateName_; }
118            /** @brief This function gets called if the main state name of the object changes. */
119            virtual void changedMainStateName();
120
121            /** @brief Sets a pointer to the xml file that loaded this object. @param file The pointer to the XMLFile */
122            inline void setFile(const XMLFile* file) { this->file_ = file; }
123            /** @brief Returns a pointer to the XMLFile that loaded this object. @return The XMLFile */
124            inline const XMLFile* getFile() const { return this->file_; }
125            const std::string& getFilename() const;
126
127            void addTemplate(const std::string& name);
128            void addTemplate(Template* temp);
129            /** @brief Returns the set of all aplied templates. */
130            inline const std::set<Template*>& getTemplates() const
131                { return this->templates_; }
132
133            virtual inline void setNamespace(Namespace* ns) { this->namespace_ = ns; }
134            inline Namespace* getNamespace() const { return this->namespace_; }
135
136            inline void setCreator(BaseObject* creator) { this->creator_ = creator; }
137            inline BaseObject* getCreator() const { return this->creator_; }
138
139            inline void setScene(const SmartPtr<Scene>& scene, uint32_t sceneID) { this->scene_ = scene; this->sceneID_=sceneID; }
140            inline const SmartPtr<Scene>& getScene() const { return this->scene_; }
141            inline virtual uint32_t getSceneID() const { return this->sceneID_; }
142
143            inline void setGametype(const SmartPtr<Gametype>& gametype)
144            {
145                if (gametype != this->gametype_)
146                {
147                    this->oldGametype_ = this->gametype_;
148                    this->gametype_ = gametype;
149                    this->changedGametype();
150                }
151            }
152            inline const SmartPtr<Gametype>& getGametype() const { return this->gametype_; }
153            inline Gametype* getOldGametype() const { return this->oldGametype_; }
154            virtual void changedGametype() {}
155
156            void addEventSource(BaseObject* source, const std::string& state);
157            void removeEventSource(BaseObject* source);
158            BaseObject* getEventSource(unsigned int index, const std::string& state) const;
159           
160            void addEventListener(BaseObject* listener);
161            BaseObject* getEventListener(unsigned int index) const;
162
163            void fireEvent();
164            void fireEvent(bool activate);
165            void fireEvent(bool activate, BaseObject* originator);
166            void fireEvent(Event& event);
167
168            virtual void processEvent(Event& event);
169
170            /** @brief Sets the indentation of the debug output in the Loader. @param indentation The indentation */
171            inline void setLoaderIndentation(const std::string& indentation) { this->loaderIndentation_ = indentation; }
172            /** @brief Returns the indentation of the debug output in the Loader. @return The indentation */
173            inline const std::string& getLoaderIndentation() const { return this->loaderIndentation_; }
174
175        protected:
176            /** @brief Adds an object which listens to the events of this object. */
177            inline void registerEventListener(BaseObject* object)
178                { this->eventListeners_.insert(object); }
179            /** @brief Removes an event listener from this object. */
180            inline void unregisterEventListener(BaseObject* object)
181                { this->eventListeners_.erase(object); }
182
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:
194            void setXMLName(const std::string& name);
195            Template* getTemplate(unsigned int index) const;
196            void registerEventStates();
197
198            bool                   bInitialized_;              //!< True if the object was initialized (passed the object registration)
199            const XMLFile*         file_;                      //!< The XMLFile that loaded this object
200            Element*               lastLoadedXMLElement_;      //!< Non 0 if the TinyXML attributes have already been copied to our own lowercase map
201            std::map<std::string, std::string> xmlAttributes_; //!< Lowercase XML attributes
202            std::string            loaderIndentation_;         //!< Indentation of the debug output in the Loader
203            Namespace*             namespace_;
204            BaseObject*            creator_;
205            SmartPtr<Scene>        scene_;
206            uint32_t               sceneID_;
207            SmartPtr<Gametype>     gametype_;
208            Gametype*              oldGametype_;
209            std::set<Template*>    templates_;
210           
211            std::map<BaseObject*, std::string>  eventSources_;           //!< List of objects which send events to this object, mapped to the state which they affect
212            std::set<BaseObject*>               eventListeners_;         //!< List of objects which listen to the events of this object
213            std::set<BaseObject*>               eventListenersXML_;      //!< List of objects which listen to the events of this object through the "eventlisteners" subsection in XML
214            std::map<std::string, EventState*>  eventStates_;            //!< Maps the name of the event states to their helper objects
215            bool                                bRegisteredEventStates_; //!< Becomes true after the object registered its event states (with XMLEventPort)
216    };
217
218    SUPER_FUNCTION(0, BaseObject, XMLPort, false);
219    SUPER_FUNCTION(2, BaseObject, changedActivity, false);
220    SUPER_FUNCTION(3, BaseObject, changedVisibility, false);
221    SUPER_FUNCTION(4, BaseObject, XMLEventPort, false);
222    SUPER_FUNCTION(8, BaseObject, changedName, false);
223    SUPER_FUNCTION(9, BaseObject, changedGametype, false);
224}
225
226#endif /* _BaseObject_H__ */
Note: See TracBrowser for help on using the repository browser.