Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/BaseObject.h @ 12301

Last change on this file since 12301 was 12028, checked in by merholzl, 6 years ago

added scriptable controller

  • Property svn:eol-style set to native
File size: 12.4 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    @defgroup BaseObject BaseObject
31    @ingroup Core
32*/
33
34/**
35    @file
36    @ingroup BaseObject
37    @brief Declaration of BaseObject, the base class of all objects in Orxonox.
38
39    The BaseObject is the parent of all classes representing an instance in the game.
40*/
41
42#ifndef _BaseObject_H__
43#define _BaseObject_H__
44
45#include "CorePrereqs.h"
46
47#include <map>
48#include <list>
49
50#include "util/mbool.h"
51#include "class/OrxonoxClass.h"
52#include "class/Super.h"
53#include "object/StrongPtr.h"
54
55namespace orxonox
56{
57    class Scene;
58    class Gametype;
59    class Level;
60    class ScriptableController;
61
62    /// The BaseObject is the parent of all classes representing an instance in the game.
63    class _CoreExport BaseObject : public OrxonoxClass
64    {
65        template <class T> friend class XMLPortClassParamContainer;
66
67        public:
68            template <class T>
69            class StrongOrWeakPtr
70            {
71                public:
72                    inline StrongOrWeakPtr();
73                    inline StrongOrWeakPtr(const StrongPtr<T>& ptr);
74                    inline StrongOrWeakPtr(const WeakPtr<T>& ptr);
75
76                    inline T* get() const;
77                    inline StrongPtr<T> createStrongPtr() const;
78
79                private:
80                    StrongPtr<T> strongPtr_;
81                    WeakPtr<T> weakPtr_;
82            };
83
84        public:
85            BaseObject(Context* context);
86            virtual ~BaseObject();
87            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
88            virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
89
90            /** @brief Returns if the object was initialized (passed the object registration). @return True was the object is initialized */
91            inline bool isInitialized() const { return this->bInitialized_; }
92
93            /** @brief Sets the name of the object. @param name The name */
94            inline void setName(const std::string& name) { this->oldName_ = this->name_; this->name_ = name; this->changedName(); }
95            /** @brief Returns the name of the object. */
96            inline const std::string& getName() const { return this->name_; }
97            /** @brief Returns the old name of the object. */
98            inline const std::string& getOldName() const { return this->oldName_; }
99            /** @brief This function gets called if the name of the object changes. */
100            virtual void changedName() {}
101
102            /** @brief Sets the state of the objects activity. @param bActive True = active */
103            inline void setActive(bool bActive)
104            {
105                if (this->bActive_ != bActive)
106                {
107                    this->bActive_ = bActive;
108                    this->changedActivity();
109                }
110            }
111            /** @brief Returns the state of the objects activity. @return The state of the activity */
112            inline const mbool& isActive() const { return this->bActive_; }
113            /** @brief This function gets called if the activity of the object changes. */
114            virtual void changedActivity() {}
115
116            /** @brief Sets the state of the objects visibility. @param bVisible True = visible */
117            inline void setVisible(bool bVisible)
118            {
119                if (this->bVisible_ != bVisible)
120                {
121                    this->bVisible_ = bVisible;
122                    this->changedVisibility();
123                }
124            }
125            /** @brief Returns the state of the objects visibility. @return The state of the visibility */
126            inline const mbool& isVisible() const { return this->bVisible_; }
127            /** @brief This function gets called if the visibility of the object changes. */
128            virtual void changedVisibility() {}
129
130            void setMainState(bool state);
131
132            /** @brief Sets the name of the main state (used for event reactions). */
133            void setMainStateName(const std::string& name)
134            {
135                if (this->mainStateName_ != name)
136                {
137                    this->mainStateName_ = name;
138                    this->changedMainStateName();
139                }
140            }
141            /** @brief Returns the name of the main state. */
142            inline const std::string& getMainStateName() const { return this->mainStateName_; }
143            /** @brief This function gets called if the main state name of the object changes. */
144            virtual void changedMainStateName();
145
146            /** @brief Sets a pointer to the xml file that loaded this object. @param file The pointer to the XMLFile */
147            inline void setFile(const XMLFile* file) { this->file_ = file; }
148            /** @brief Returns a pointer to the XMLFile that loaded this object. @return The XMLFile */
149            inline const XMLFile* getFile() const { return this->file_; }
150            const std::string& getFilename() const;
151
152            void addTemplate(const std::string& name);
153            void addTemplate(Template* temp);
154            /** @brief Returns the set of all aplied templates. */
155            inline const std::set<Template*>& getTemplates() const
156                { return this->templates_; }
157
158            inline void setNamespace(const StrongOrWeakPtr<Namespace>& ns) { this->namespace_ = ns; }
159            inline Namespace* getNamespace() const { return this->namespace_.get(); }
160
161            inline void setCreator(BaseObject* creator) { this->creator_ = creator; }
162            inline BaseObject* getCreator() const { return this->creator_; }
163
164            inline void setScene(const StrongOrWeakPtr<Scene>& scene, uint32_t sceneID) { this->scene_ = scene; this->sceneID_=sceneID; }
165            inline Scene* getScene() const { return this->scene_.get(); }
166            virtual inline uint32_t getSceneID() const { return this->sceneID_; }
167
168            inline void setGametype(const StrongOrWeakPtr<Gametype>& gametype) { this->gametype_ = gametype; }
169            inline Gametype* getGametype() const { return this->gametype_.get(); }
170
171            inline void setLevel(const StrongOrWeakPtr<Level>& level) { this->level_ = level; }
172            inline Level* getLevel() const { return this->level_.get(); }
173
174            void addEventSource(BaseObject* source, const std::string& state);
175            void removeEventSource(BaseObject* source);
176            BaseObject* getEventSource(unsigned int index, const std::string& state) const;
177
178            void addEventListener(BaseObject* listener);
179            BaseObject* getEventListener(unsigned int index) const;
180
181            void fireEvent(const std::string& name = "");
182            void fireEvent(bool activate, const std::string& name = "");
183            void fireEvent(bool activate, BaseObject* originator, const std::string& name = "");
184            void fireEvent(Event& event);
185
186            virtual void processEvent(Event& event);
187
188            /** @brief Sets the indentation of the debug output in the Loader. @param indentation The indentation */
189            inline void setLoaderIndentation(const std::string& indentation) { this->loaderIndentation_ = indentation; }
190            /** @brief Returns the indentation of the debug output in the Loader. @return The indentation */
191            inline const std::string& getLoaderIndentation() const { return this->loaderIndentation_; }
192
193            static void loadAllEventStates(Element& xmlelement, XMLPort::Mode mode, BaseObject* object, Identifier* identifier);
194
195
196        protected:
197            void addEventState(const std::string& name, EventState* container);
198            EventState* getEventState(const std::string& name) const;
199
200            std::string             name_;                     //!< The name of the object
201            std::string             oldName_;                  //!< The old name of the object
202            mbool                   bActive_;                  //!< True = the object is active
203            mbool                   bVisible_;                 //!< True = the object is visible
204            std::string             mainStateName_;
205            FunctorPtr              mainStateFunctor_;
206            std::set<std::string>   networkTemplateNames_;
207
208        private:
209            /** @brief Adds an object which listens to the events of this object. */
210            void registerEventListener(BaseObject* object);
211            /** @brief Removes an event listener from this object. */
212            inline void unregisterEventListener(BaseObject* object)
213                { this->eventListeners_.erase(object); }
214
215            void setXMLName(const std::string& name);
216            const std::string& getSingleTemplate(void) const;
217            Template* getTemplate(unsigned int index) const;
218            void registerEventStates();
219
220            bool                       bInitialized_;          //!< True if the object was initialized (passed the object registration)
221            const XMLFile*             file_;                  //!< The XMLFile that loaded this object
222            Element*                   lastLoadedXMLElement_;  //!< Non nullptr if the TinyXML attributes have already been copied to our own lowercase map
223            std::map<std::string, std::string> xmlAttributes_; //!< Lowercase XML attributes
224            std::string                loaderIndentation_;     //!< Indentation of the debug output in the Loader
225            StrongOrWeakPtr<Namespace> namespace_;
226            BaseObject*                creator_;
227            StrongOrWeakPtr<Scene>     scene_;
228            uint32_t                   sceneID_;
229            StrongOrWeakPtr<Gametype>  gametype_;
230            StrongOrWeakPtr<Level>     level_;
231            std::set<Template*>        templates_;
232
233            std::map<BaseObject*, std::string>  eventSources_;           //!< List of objects which send events to this object, mapped to the state which they affect
234            std::set<BaseObject*>               eventListeners_;         //!< List of objects which listen to the events of this object
235            std::set<BaseObject*>               eventListenersXML_;      //!< List of objects which listen to the events of this object through the "eventlisteners" subsection in XML
236            std::map<std::string, EventState*>  eventStates_;            //!< Maps the name of the event states to their helper objects
237            bool                                bRegisteredEventStates_; //!< Becomes true after the object registered its event states (with XMLEventPort)
238    };
239
240    SUPER_FUNCTION(0, BaseObject, XMLPort, false);
241    SUPER_FUNCTION(2, BaseObject, changedActivity, false);
242    SUPER_FUNCTION(3, BaseObject, changedVisibility, false);
243    SUPER_FUNCTION(4, BaseObject, XMLEventPort, false);
244    SUPER_FUNCTION(8, BaseObject, changedName, false);
245
246    template <class T>
247    BaseObject::StrongOrWeakPtr<T>::StrongOrWeakPtr()
248    {
249    }
250
251    template <class T>
252    BaseObject::StrongOrWeakPtr<T>::StrongOrWeakPtr(const StrongPtr<T>& ptr) : strongPtr_(ptr)
253    {
254    }
255
256    template <class T>
257    BaseObject::StrongOrWeakPtr<T>::StrongOrWeakPtr(const WeakPtr<T>& ptr) : weakPtr_(ptr)
258    {
259    }
260
261    template <class T>
262    T* BaseObject::StrongOrWeakPtr<T>::get() const
263    {
264        if (this->strongPtr_)
265            return this->strongPtr_;
266        else if (this->weakPtr_)
267            return this->weakPtr_;
268        else
269            return nullptr;
270    }
271
272    template <class T>
273    StrongPtr<T> BaseObject::StrongOrWeakPtr<T>::createStrongPtr() const
274    {
275        if (this->strongPtr_)
276            return this->strongPtr_; // creates a copy
277        else
278            return this->weakPtr_; // converts automatically to StrongPtr
279    }
280}
281
282#endif /* _BaseObject_H__ */
Note: See TracBrowser for help on using the repository browser.