Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/core/BaseObject.cc @ 2063

Last change on this file since 2063 was 2063, checked in by landauf, 16 years ago

added events but not yet connected with triggers

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