Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

More changes in the event-system: processEvent() is now locally executed in BaseObject. The event states (like activity, visibility, …) are now defined in XMLEventPort, a function which closely resembles XMLPort. This function is used to define event states and to parse event sources from XML.

Connected the main-state directly with the event-system. After a state was declared as the "main state", the Functor from the corresponding EventState-object is used to call the function. This reduces the redundancy of declaring event-states and main-states separately. Of course only boolean event-states (like activity or visibility) can be used as main-state, while memoryless states (like spawn in ParticleSpawner) and individual states which need the triggering object (like execute in QuestEffectBeacon) won't work.

  • Property svn:eol-style set to native
File size: 11.9 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        this->bRegisteredEventStates_ = false;
66
67        this->lastLoadedXMLElement_ = 0;
68
69        this->mainStateFunctor_ = 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::map<BaseObject*, std::string>::const_iterator it = this->eventSources_.begin(); it != this->eventSources_.end(); )
97                this->removeEventSource((it++)->first);
98
99            for (std::set<BaseObject*>::const_iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); )
100                (*(it++))->removeEventSource(this);
101
102            for (std::map<std::string, EventState*>::const_iterator it = this->eventStates_.begin(); it != this->eventStates_.end(); ++it)
103                delete it->second;
104        }
105    }
106
107    /**
108        @brief XML loading and saving.
109        @param xmlelement The XML-element
110        @param loading Loading (true) or saving (false)
111    */
112    void BaseObject::XMLPort(Element& xmlelement, XMLPort::Mode mode)
113    {
114        XMLPortParam(BaseObject, "name", setXMLName, getName, xmlelement, mode);
115        XMLPortParam(BaseObject, "visible", setVisible, isVisible, xmlelement, mode);
116        XMLPortParam(BaseObject, "active", setActive, isActive, xmlelement, mode);
117        XMLPortParam(BaseObject, "mainstate", setMainStateName, getMainStateName, xmlelement, mode);
118
119        XMLPortObjectTemplate(BaseObject, Template, "templates", addTemplate, getTemplate, xmlelement, mode, Template*);
120       
121        Element* events = 0;
122        if (mode == XMLPort::LoadObject || mode == XMLPort::ExpandObject)
123            events = xmlelement.FirstChildElement("events", false);
124        else if (mode == XMLPort::SaveObject)
125            ;
126        if (events)
127            this->XMLEventPort(*events, mode);
128    }
129
130    /**
131        @brief Defines the possible event states of this object and parses eventsources from an XML file.
132        @param xmlelement The XML-element
133        @param loading Loading (true) or saving (false)
134    */
135    void BaseObject::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
136    {
137        XMLPortEventState(BaseObject, BaseObject, "activity", setActive, xmlelement, mode);
138        XMLPortEventState(BaseObject, BaseObject, "visibility", setVisible, xmlelement, mode);
139       
140        this->bRegisteredEventStates_ = true;
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    /**
207        @brief Adds a new event source for a specific state.
208        @param source The object which sends events to this object
209        @param state The state of this object which will be affected by the events
210    */
211    void BaseObject::addEventSource(BaseObject* source, const std::string& state)
212    {
213        this->eventSources_[source] = state;
214        source->registerEventListener(this);
215    }
216
217    /**
218        @brief Removes an eventsource (but doesn't unregister itself at the source).
219    */
220    void BaseObject::removeEventSource(BaseObject* source)
221    {
222        this->eventSources_.erase(source);
223        source->unregisterEventListener(this);
224    }
225
226    /**
227        @brief Returns an eventsource with a given index.
228    */
229    BaseObject* BaseObject::getEventSource(unsigned int index, const std::string& state) const
230    {
231        unsigned int i = 0;
232        for (std::map<BaseObject*, std::string>::const_iterator it = this->eventSources_.begin(); it != this->eventSources_.end(); ++it)
233        {
234            if (it->second != state)
235                continue;
236           
237            if (i == index)
238                return it->first;
239            ++i;
240        }
241        return 0;
242    }
243
244    void BaseObject::addEventState(const std::string& name, EventState* state)
245    {
246        std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(name);
247        if (it != this->eventStates_.end())
248        {
249            COUT(2) << "Warning: Overwriting EventState in class " << this->getIdentifier()->getName() << "." << std::endl;
250            delete (it->second);
251        }
252
253        this->eventStates_[name] = state;
254    }
255
256    EventState* BaseObject::getEventState(const std::string& name) const
257    {
258        std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(name);
259        if (it != this->eventStates_.end())
260            return ((*it).second);
261        else
262            return 0;
263    }
264
265    /**
266        @brief Fires an event (without a state).
267    */
268    void BaseObject::fireEvent()
269    {
270        this->fireEvent(true);
271        this->fireEvent(false);
272    }
273
274    /**
275        @brief Fires an event which activates or deactivates a state.
276    */
277    void BaseObject::fireEvent(bool activate)
278    {
279        this->fireEvent(activate, this);
280    }
281
282    /**
283        @brief Fires an event which activates or deactivates a state with agiven originator (the object which triggered the event).
284    */
285    void BaseObject::fireEvent(bool activate, BaseObject* originator)
286    {
287        Event event(activate, originator);
288
289        for (std::set<BaseObject*>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
290        {
291            event.statename_ = (*it)->eventSources_[this];
292            (*it)->processEvent(event);
293        }
294    }
295
296    /**
297        @brief Fires an event, using the Event struct.
298    */
299    void BaseObject::fireEvent(Event& event)
300    {
301        for (std::set<BaseObject*>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
302            (*it)->processEvent(event);
303    }
304
305    /**
306        @brief Processing an event by calling the right main state.
307        @param event The event struct which contains the information about the event
308    */
309    void BaseObject::processEvent(Event& event)
310    {
311        this->registerEventStates();
312       
313        std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(event.statename_);
314        if (it != this->eventStates_.end())
315            it->second->process(event, this);
316        else if (event.statename_ != "")
317            COUT(2) << "Warning: \"" << event.statename_ << "\" is not a valid state in object \"" << this->getName() << "\" of class " << this->getIdentifier()->getName() << "." << std::endl;
318        else
319            COUT(2) << "Warning: Event with invalid source sent to object \"" << this->getName() << "\" of class " << this->getIdentifier()->getName() << "." << std::endl;
320    }
321
322    /**
323        @brief Sets the main state of the object to a given boolean value.
324       
325        Note: The main state of an object can be set with the @ref setMainStateName function.
326        It's part of the eventsystem and used for event forwarding (when the target object can't specify a specific state,
327        the main state is used by default).
328    */
329    void BaseObject::setMainState(bool state)
330    {
331        if (this->mainStateFunctor_)
332            (*this->mainStateFunctor_)(state);
333        else
334            COUT(2) << "Warning: No MainState defined in object \"" << this->getName() << "\" (" << this->getIdentifier()->getName() << ")" << std::endl;
335    }
336
337    /**
338        @brief This function gets called if the main state name of the object changes.
339    */
340    void BaseObject::changedMainStateName()
341    {
342        this->registerEventStates();
343       
344        this->mainStateFunctor_ = 0;
345       
346        std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(this->mainStateName_);
347        if (it != this->eventStates_.end() && it->second->getFunctor() && it->second->getFunctor()->getParamCount() == 1)
348            this->mainStateFunctor_ = it->second->getFunctor();
349        else
350            COUT(2) << "Warning: \"" << this->mainStateName_ << "\" is not a valid MainState." << std::endl;
351    }
352   
353    /**
354        @brief Calls XMLEventPort with an empty XML-element to register the event states if necessary.
355    */
356    void BaseObject::registerEventStates()
357    {
358        if (!this->bRegisteredEventStates_)
359        {
360            Element xmlelement;
361            this->XMLEventPort(xmlelement, XMLPort::NOP);
362        }
363    }
364}
Note: See TracBrowser for help on using the repository browser.