Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/buildsystem2/src/core/BaseObject.cc @ 2567

Last change on this file since 2567 was 2567, checked in by rgrieder, 15 years ago

Added all paths of included external libraries to the include directories and used ≠ for the includes.
Also added src/orxonox binary directory for consistency.

  • Property svn:eol-style set to native
File size: 9.2 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 Implementation of the BaseObject class.
32*/
33
34#include "BaseObject.h"
35
36#include <tinyxml.h>
37
38#include "CoreIncludes.h"
39#include "EventIncludes.h"
40#include "XMLPort.h"
41#include "XMLFile.h"
42#include "XMLNameListener.h"
43#include "Template.h"
44#include "util/String.h"
45#include "util/mbool.h"
46
47namespace orxonox
48{
49    CreateFactory(BaseObject);
50
51    /**
52        @brief Constructor: Registers the object in the BaseObject-list.
53    */
54    BaseObject::BaseObject(BaseObject* creator) : bInitialized_(false)
55    {
56        RegisterRootObject(BaseObject);
57
58        this->bInitialized_ = true;
59
60        this->bActive_ = true;
61        this->bVisible_ = true;
62        this->oldGametype_ = 0;
63
64        this->setCreator(creator);
65        if (this->creator_)
66        {
67            this->setFile(this->creator_->getFile());
68            this->setNamespace(this->creator_->getNamespace());
69            this->setScene(this->creator_->getScene());
70            this->setGametype(this->creator_->getGametype());
71        }
72        else
73        {
74            this->file_ = 0;
75            this->namespace_ = 0;
76            this->scene_ = 0;
77            this->gametype_ = 0;
78        }
79    }
80
81    /**
82        @brief Destructor
83    */
84    BaseObject::~BaseObject()
85    {
86        for (std::list<BaseObject*>::const_iterator it = this->events_.begin(); it != this->events_.end(); ++it)
87            (*it)->unregisterEventListener(this);
88
89        for (std::map<BaseObject*, std::string>::const_iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
90            it->first->removeEvent(this);
91    }
92
93    /**
94        @brief XML loading and saving.
95        @param xmlelement The XML-element
96        @param loading Loading (true) or saving (false)
97        @return The XML-element
98    */
99    void BaseObject::XMLPort(Element& xmlelement, XMLPort::Mode mode)
100    {
101        XMLPortParam(BaseObject, "name", setXMLName, getName, xmlelement, mode);
102        XMLPortParam(BaseObject, "visible", setVisible, isVisible, xmlelement, mode);
103        XMLPortParam(BaseObject, "active", setActive, isActive, xmlelement, mode);
104
105        XMLPortObjectTemplate(BaseObject, Template, "templates", addTemplate, getTemplate, xmlelement, mode, Template*);
106
107        Element* events = xmlelement.FirstChildElement("events", false);
108
109        if (events)
110        {
111            std::list<std::string> eventnames;
112
113            if (mode == XMLPort::LoadObject)
114            {
115                for (ticpp::Iterator<ticpp::Element> child = events->FirstChildElement(false); child != child.end(); child++)
116                    eventnames.push_back(child->Value());
117            }
118            else if (mode == XMLPort::SaveObject)
119            {
120                for (std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->getIdentifier()->getXMLPortEventMapBegin(); it != this->getIdentifier()->getXMLPortEventMapEnd(); ++it)
121                    eventnames.push_back(it->first);
122            }
123
124            for (std::list<std::string>::iterator it = eventnames.begin(); it != eventnames.end(); ++it)
125            {
126                std::string sectionname = (*it);
127                ExecutorMember<BaseObject>* loadexecutor = createExecutor(createFunctor(&BaseObject::addEvent), std::string( "BaseObject" ) + "::" + "addEvent");
128                ExecutorMember<BaseObject>* saveexecutor = createExecutor(createFunctor(&BaseObject::getEvent), std::string( "BaseObject" ) + "::" + "getEvent");
129                loadexecutor->setDefaultValue(1, sectionname);
130
131                XMLPortClassObjectContainer<BaseObject, BaseObject>* container = 0;
132                container = (XMLPortClassObjectContainer<BaseObject, BaseObject>*)(this->getIdentifier()->getXMLPortEventContainer(sectionname));
133                if (!container)
134                {
135                    container = new XMLPortClassObjectContainer<BaseObject, BaseObject>(sectionname, this->getIdentifier(), loadexecutor, saveexecutor, false, true);
136                    this->getIdentifier()->addXMLPortEventContainer(sectionname, container);
137                }
138                container->port(this, *events, mode);
139            }
140        }
141    }
142
143    /**
144        @brief Loads the name of the object through XML and calls all XMLNameListener.
145        @param name The name of the object
146    */
147    void BaseObject::setXMLName(const std::string& name)
148    {
149        this->setName(name);
150
151        for (ObjectList<XMLNameListener>::iterator it = ObjectList<XMLNameListener>::begin(); it != ObjectList<XMLNameListener>::end(); ++it)
152            it->loadedNewXMLName(this);
153    }
154
155    /**
156        @brief Returns the levelfile that loaded this object.
157        @return The levelfile
158    */
159    const std::string& BaseObject::getFilename() const
160    {
161        if (this->file_)
162            return this->file_->getFilename();
163        else
164            return BLANKSTRING;
165    }
166
167    /**
168        @brief Adds a Template to the object.
169        @param name The name of the Template
170    */
171    void BaseObject::addTemplate(const std::string& name)
172    {
173        Template* temp = Template::getTemplate(name);
174        if (temp)
175            this->addTemplate(temp);
176        else
177            COUT(1) << "Error: \"" << name << "\" is not a valid Template name (in class: " << this->getIdentifier()->getName() << ", name: " << this->getName() << ")." << std::endl;
178    }
179
180    /**
181        @brief Adds a Template to the object.
182        @param temp The Template
183    */
184    void BaseObject::addTemplate(Template* temp)
185    {
186        this->templates_.insert(temp);
187        temp->applyOn(this);
188    }
189
190    /**
191        @brief Returns the Template with the given index.
192        @param index The index
193    */
194    Template* BaseObject::getTemplate(unsigned int index) const
195    {
196        unsigned int i = 0;
197        for (std::set<Template*>::const_iterator it = this->templates_.begin(); it != this->templates_.end(); ++it)
198        {
199            if (i == index)
200                return (*it);
201            i++;
202        }
203        return 0;
204    }
205
206    void BaseObject::addEvent(BaseObject* event, const std::string& sectionname)
207    {
208        event->registerEventListener(this, sectionname);
209        this->events_.push_back(event);
210    }
211
212    void BaseObject::removeEvent(BaseObject* event)
213    {
214        this->events_.remove(event);
215    }
216
217    BaseObject* BaseObject::getEvent(unsigned int index) const
218    {
219        unsigned int i = 0;
220        for (std::list<BaseObject*>::const_iterator it = this->events_.begin(); it != this->events_.end(); ++it)
221        {
222            if (i == index)
223                return (*it);
224            ++i;
225        }
226        return 0;
227    }
228
229    void BaseObject::addEventContainer(const std::string& sectionname, EventContainer* container)
230    {
231        std::map<std::string, EventContainer*>::const_iterator it = this->eventContainers_.find(sectionname);
232        if (it != this->eventContainers_.end())
233        {
234            COUT(2) << "Warning: Overwriting EventContainer in class " << this->getIdentifier()->getName() << "." << std::endl;
235            delete (it->second);
236        }
237
238        this->eventContainers_[sectionname] = container;
239    }
240
241    EventContainer* BaseObject::getEventContainer(const std::string& sectionname) const
242    {
243        std::map<std::string, EventContainer*>::const_iterator it = this->eventContainers_.find(sectionname);
244        if (it != this->eventContainers_.end())
245            return ((*it).second);
246        else
247            return 0;
248    }
249
250    void BaseObject::fireEvent()
251    {
252        this->fireEvent(true);
253        this->fireEvent(false);
254    }
255
256    void BaseObject::fireEvent(bool activate)
257    {
258        this->fireEvent(activate, this);
259    }
260
261    void BaseObject::fireEvent(bool activate, BaseObject* originator)
262    {
263        Event event(activate, originator);
264
265        for (std::map<BaseObject*, std::string>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
266        {
267            event.sectionname_ = it->second;
268            it->first->processEvent(event);
269        }
270    }
271
272    void BaseObject::fireEvent(Event& event)
273    {
274        for (std::map<BaseObject*, std::string>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
275            it->first->processEvent(event);
276    }
277
278    void BaseObject::processEvent(Event& event)
279    {
280        SetEvent(BaseObject, "activity", setActive, event);
281        SetEvent(BaseObject, "visibility", setVisible, event);
282    }
283}
Note: See TracBrowser for help on using the repository browser.