Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/libraries/core/BaseObject.cc @ 5858

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

Cleanup in *Prereqs.h files

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