Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/core/BaseObject.cc @ 10571

Last change on this file since 10571 was 10571, checked in by landauf, 9 years ago

BaseObject returns plain pointers instead of StrongPtrs for Namespace, Level, Scene, and Gametype.

  • Property svn:eol-style set to native
File size: 18.1 KB
RevLine 
[1505]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"
[5781]35
36#include <tinyxml/tinyxml.h>
37
[1505]38#include "CoreIncludes.h"
[5781]39#include "Event.h"
40#include "EventIncludes.h"
41#include "Template.h"
42#include "XMLFile.h"
43#include "XMLNameListener.h"
44#include "XMLPort.h"
[7284]45#include "command/Functor.h"
[9667]46#include "object/Iterator.h"
[1505]47
[9667]48#include "class/OrxonoxInterface.h" // we include this only to include OrxonoxInterface.h at least once in core to keep MSVC happy...
49
[1505]50namespace orxonox
51{
[9667]52    RegisterClass(BaseObject);
[1505]53
54    /**
55        @brief Constructor: Registers the object in the BaseObject-list.
56    */
[9667]57    BaseObject::BaseObject(Context* context) : bInitialized_(false)
[1505]58    {
[9667]59        RegisterObject(BaseObject);
[1558]60
61        this->bInitialized_ = true;
[5781]62
63        this->bActive_ = true;
64        this->bVisible_ = true;
65        this->oldGametype_ = 0;
[5929]66        this->bRegisteredEventStates_ = false;
[5781]67
68        this->lastLoadedXMLElement_ = 0;
69
[5929]70        this->mainStateFunctor_ = 0;
[5781]71
[9667]72        if (context)
73            this->setContext(context);
74
75        BaseObject* creator = orxonox_cast<BaseObject*>(context);
[5781]76        this->setCreator(creator);
77        if (this->creator_)
78        {
79            this->setFile(this->creator_->getFile());
[10571]80            this->setNamespace(this->creator_->namespace_);
81            this->setScene(this->creator_->scene_, this->creator_->sceneID_);
82            this->setGametype(this->creator_->gametype_);
83            this->setLevel(this->creator_->level_);
[5781]84        }
85        else
86        {
87            this->file_ = 0;
[5929]88            this->sceneID_ = OBJECTID_UNKNOWN;
[5781]89        }
[1505]90    }
91
92    /**
93        @brief Destructor
94    */
95    BaseObject::~BaseObject()
96    {
[2662]97        if (this->isInitialized())
98        {
[5929]99            for (std::map<BaseObject*, std::string>::const_iterator it = this->eventSources_.begin(); it != this->eventSources_.end(); )
100                this->removeEventSource((it++)->first);
[5781]101
[5929]102            for (std::set<BaseObject*>::const_iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); )
103                (*(it++))->removeEventSource(this);
[5781]104
[5929]105            for (std::map<std::string, EventState*>::const_iterator it = this->eventStates_.begin(); it != this->eventStates_.end(); ++it)
106                delete it->second;
[2662]107        }
[1505]108    }
[7163]109
[6800]110    /** @brief Adds an object which listens to the events of this object. */
111    void BaseObject::registerEventListener(BaseObject* object)
112    {
[8858]113        orxout(verbose, context::events) << "New EventListener: " << object->getIdentifier()->getName() << " &(" << object << ")." << endl;
[6800]114        this->eventListeners_.insert(object);
115    }
[5781]116
117    /**
118        @brief XML loading and saving.
119        @param xmlelement The XML-element
[7401]120        @param mode The mode defines the operation that is being executed: loading or saving the object (from or to XML respectively)
[5781]121    */
122    void BaseObject::XMLPort(Element& xmlelement, XMLPort::Mode mode)
123    {
124        XMLPortParam(BaseObject, "name", setXMLName, getName, xmlelement, mode);
125        XMLPortParam(BaseObject, "visible", setVisible, isVisible, xmlelement, mode);
126        XMLPortParam(BaseObject, "active", setActive, isActive, xmlelement, mode);
127        XMLPortParam(BaseObject, "mainstate", setMainStateName, getMainStateName, xmlelement, mode);
[6524]128        XMLPortParamTemplate(BaseObject, "template", addTemplate, getSingleTemplate, xmlelement, mode, const std::string&);
[7163]129
[5781]130        XMLPortObjectTemplate(BaseObject, Template, "templates", addTemplate, getTemplate, xmlelement, mode, Template*);
[5929]131        XMLPortObject(BaseObject, BaseObject, "eventlisteners", addEventListener, getEventListener, xmlelement, mode);
[6417]132
[5929]133        Element* events = 0;
134        if (mode == XMLPort::LoadObject || mode == XMLPort::ExpandObject)
135            events = xmlelement.FirstChildElement("events", false);
136        else if (mode == XMLPort::SaveObject)
137            {}
[5781]138        if (events)
[5929]139            this->XMLEventPort(*events, mode);
140    }
[5781]141
[5929]142    /**
143        @brief Defines the possible event states of this object and parses eventsources from an XML file.
144        @param xmlelement The XML-element
[7401]145        @param mode The mode defines the operation that is being executed: loading or saving the object (from or to XML respectively)
[5929]146    */
147    void BaseObject::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
148    {
149        XMLPortEventState(BaseObject, BaseObject, "activity", setActive, xmlelement, mode);
150        XMLPortEventState(BaseObject, BaseObject, "visibility", setVisible, xmlelement, mode);
151        XMLPortEventState(BaseObject, BaseObject, "mainstate", setMainState, xmlelement, mode);
[6417]152
[5929]153        this->bRegisteredEventStates_ = true;
[5781]154    }
155
156    /**
157        @brief Loads the name of the object through XML and calls all XMLNameListener.
158        @param name The name of the object
159    */
160    void BaseObject::setXMLName(const std::string& name)
161    {
162        this->setName(name);
163
164        for (ObjectList<XMLNameListener>::iterator it = ObjectList<XMLNameListener>::begin(); it != ObjectList<XMLNameListener>::end(); ++it)
165            it->loadedNewXMLName(this);
166    }
167
168    /**
169        @brief Returns the levelfile that loaded this object.
170        @return The levelfile
171    */
172    const std::string& BaseObject::getFilename() const
173    {
174        if (this->file_)
175            return this->file_->getFilename();
176        else
177            return BLANKSTRING;
178    }
179
180    /**
181        @brief Adds a Template to the object.
182        @param name The name of the Template
183    */
184    void BaseObject::addTemplate(const std::string& name)
185    {
186        Template* temp = Template::getTemplate(name);
187        if (temp)
188            this->addTemplate(temp);
189        else
[8858]190            orxout(internal_error) << "\"" << name << "\" is not a valid Template name (in class: " << this->getIdentifier()->getName() << ", name: " << this->getName() << ")." << endl;
[5781]191    }
192
193    /**
194        @brief Adds a Template to the object.
195        @param temp The Template
196    */
197    void BaseObject::addTemplate(Template* temp)
198    {
[8706]199        // network
200        if (temp->isLink())
[7163]201        {
[8706]202            this->networkTemplateNames_.insert(temp->getLink());
[9667]203
[8706]204            Template* link;
205            assert(!(link = Template::getTemplate(temp->getLink())) || !link->isLink());
206            link = NULL;
[7163]207        }
208        else
[8706]209            this->networkTemplateNames_.insert(temp->getName());
210
211        // add template
212        this->templates_.insert(temp);
[5781]213        temp->applyOn(this);
214    }
215
216    /**
[6524]217        @brief Returns the name of the first Template.
218        @return The name as string.
219    */
220    const std::string& BaseObject::getSingleTemplate(void) const
221    {
222        if(this->templates_.empty())
223            return BLANKSTRING;
224
225        return (*this->templates_.begin())->getName();
226    }
227
228    /**
[5781]229        @brief Returns the Template with the given index.
230        @param index The index
231    */
232    Template* BaseObject::getTemplate(unsigned int index) const
233    {
234        unsigned int i = 0;
235        for (std::set<Template*>::const_iterator it = this->templates_.begin(); it != this->templates_.end(); ++it)
236        {
237            if (i == index)
238                return (*it);
239            i++;
240        }
241        return 0;
242    }
243
[5929]244    /**
245        @brief Adds a new event source for a specific state.
246        @param source The object which sends events to this object
247        @param state The state of this object which will be affected by the events
248    */
249    void BaseObject::addEventSource(BaseObject* source, const std::string& state)
[5781]250    {
[5929]251        this->eventSources_[source] = state;
252        source->registerEventListener(this);
[5781]253    }
254
[5929]255    /**
256        @brief Removes an eventsource (but doesn't unregister itself at the source).
257    */
258    void BaseObject::removeEventSource(BaseObject* source)
[5781]259    {
[5929]260        this->eventSources_.erase(source);
261        source->unregisterEventListener(this);
[5781]262    }
263
[5929]264    /**
265        @brief Returns an eventsource with a given index.
266    */
267    BaseObject* BaseObject::getEventSource(unsigned int index, const std::string& state) const
[5781]268    {
269        unsigned int i = 0;
[5929]270        for (std::map<BaseObject*, std::string>::const_iterator it = this->eventSources_.begin(); it != this->eventSources_.end(); ++it)
[5781]271        {
[5929]272            if (it->second != state)
273                continue;
[6417]274
[5781]275            if (i == index)
[5929]276                return it->first;
[5781]277            ++i;
278        }
279        return 0;
280    }
281
[5929]282    /**
283        @brief Adds an object which listens to the events of this object. The events are sent to the other objects mainstate.
284    */
285    void BaseObject::addEventListener(BaseObject* listener)
[5781]286    {
[5929]287        this->eventListenersXML_.insert(listener);
288        listener->addEventSource(this, "mainstate");
289    }
[6417]290
[5929]291    /**
292        @brief Returns an event listener with a given index.
293    */
294    BaseObject* BaseObject::getEventListener(unsigned int index) const
295    {
296        unsigned int i = 0;
297        for (std::set<BaseObject*>::const_iterator it = this->eventListenersXML_.begin(); it != this->eventListenersXML_.end(); ++it)
[5781]298        {
[5929]299            if (i == index)
300                return *it;
301            ++i;
302        }
303        return 0;
304    }
305
306    /**
307        @brief Adds a new event-state to the object. Event-states are states which can be changed by events.
308        @param name  The name of the event
309        @param state The object containing information about the event-state
310    */
311    void BaseObject::addEventState(const std::string& name, EventState* state)
312    {
313        std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(name);
314        if (it != this->eventStates_.end())
315        {
[8858]316            orxout(internal_warning, context::events) << "Overwriting EventState in class " << this->getIdentifier()->getName() << '.' << endl;
[5781]317            delete (it->second);
318        }
319
[5929]320        this->eventStates_[name] = state;
[5781]321    }
322
[5929]323    /**
324        @brief Returns the event-state with the given name.
325    */
326    EventState* BaseObject::getEventState(const std::string& name) const
[5781]327    {
[5929]328        std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(name);
329        if (it != this->eventStates_.end())
[6417]330            return (it->second);
[5781]331        else
332            return 0;
333    }
334
[5929]335    /**
336        @brief Fires an event (without a state).
337    */
338    void BaseObject::fireEvent(const std::string& name)
[5781]339    {
[5929]340        this->fireEvent(true, name);
341        this->fireEvent(false, name);
[5781]342    }
343
[5929]344    /**
345        @brief Fires an event which activates or deactivates a state.
346    */
347    void BaseObject::fireEvent(bool activate, const std::string& name)
[5781]348    {
[5929]349        this->fireEvent(activate, this, name);
[5781]350    }
351
[5929]352    /**
353        @brief Fires an event which activates or deactivates a state with agiven originator (the object which triggered the event).
354    */
355    void BaseObject::fireEvent(bool activate, BaseObject* originator, const std::string& name)
[5781]356    {
[5929]357        Event event(activate, originator, name);
[5781]358
[5929]359        for (std::set<BaseObject*>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
[5781]360        {
[5929]361            event.statename_ = (*it)->eventSources_[this];
362            (*it)->processEvent(event);
[5781]363        }
364    }
365
[5929]366    /**
367        @brief Fires an event, using the Event struct.
368    */
[5781]369    void BaseObject::fireEvent(Event& event)
370    {
[5929]371        for (std::set<BaseObject*>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
372            (*it)->processEvent(event);
[5781]373    }
374
[5929]375    /**
376        @brief Processing an event by calling the right main state.
377        @param event The event struct which contains the information about the event
378    */
[5781]379    void BaseObject::processEvent(Event& event)
380    {
[5929]381        this->registerEventStates();
[7163]382
[8858]383        orxout(verbose, context::events) << this->getIdentifier()->getName() << " (&" << this << ") processing event. originator: " << event.originator_->getIdentifier()->getName() << " (&" << event.originator_ << "), activate: " << event.activate_ << ", name: " << event.name_ << ", statename: " << event.statename_ << "." << endl;
[6417]384
[5929]385        std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(event.statename_);
386        if (it != this->eventStates_.end())
387            it->second->process(event, this);
[6417]388        else if (!event.statename_.empty())
[8858]389            orxout(internal_warning, context::events) << "\"" << event.statename_ << "\" is not a valid state in object \"" << this->getName() << "\" of class " << this->getIdentifier()->getName() << "." << endl;
[5929]390        else
[8858]391            orxout(internal_warning, context::events) << "Event with invalid source sent to object \"" << this->getName() << "\" of class " << this->getIdentifier()->getName() << "." << endl;
[5781]392    }
393
[5929]394    /**
395        @brief Sets the main state of the object to a given boolean value.
[6417]396
[5929]397        Note: The main state of an object can be set with the @ref setMainStateName function.
398        It's part of the eventsystem and used for event forwarding (when the target object can't specify a specific state,
399        the main state is used by default).
400    */
401    void BaseObject::setMainState(bool state)
[5781]402    {
[5929]403        if (this->mainStateFunctor_)
[5781]404        {
[5929]405            if (this->mainStateFunctor_->getParamCount() == 0)
406            {
407                if (state)
408                    (*this->mainStateFunctor_)();
409            }
410            else
411            {
412                (*this->mainStateFunctor_)(state);
413            }
[5781]414        }
415        else
[8858]416            orxout(internal_warning, context::events) << "No MainState defined in object \"" << this->getName() << "\" (" << this->getIdentifier()->getName() << ")" << endl;
[5781]417    }
418
[5929]419    /**
420        @brief This function gets called if the main state name of the object changes.
421    */
422    void BaseObject::changedMainStateName()
[5781]423    {
[5929]424        this->mainStateFunctor_ = 0;
425
[6417]426        if (!this->mainStateName_.empty())
[5781]427        {
[5929]428            this->registerEventStates();
[6417]429
[5929]430            std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(this->mainStateName_);
431            if (it != this->eventStates_.end() && it->second->getFunctor())
432            {
433                if (it->second->getFunctor()->getParamCount() <= 1)
434                    this->mainStateFunctor_ = it->second->getFunctor();
435                else
[8858]436                    orxout(internal_warning, context::events) << "Can't use \"" << this->mainStateName_ << "\" as MainState because it needs a second argument." << endl;
[5929]437            }
438            else
[8858]439                orxout(internal_warning, context::events) << "\"" << this->mainStateName_ << "\" is not a valid MainState." << endl;
[5781]440        }
[5929]441    }
[6417]442
[5929]443    /**
444        @brief Calls XMLEventPort with an empty XML-element to register the event states if necessary.
445    */
446    void BaseObject::registerEventStates()
447    {
448        if (!this->bRegisteredEventStates_)
[5781]449        {
[5929]450            Element xmlelement;
451            this->XMLEventPort(xmlelement, XMLPort::NOP);
[5781]452        }
453    }
[6417]454
[5929]455    /**
456        @brief Manually loads all event states, even if the class doesn't officially support them. This is needed by some classes like @ref EventDispatcher or @ref EventTarget.
457    */
458    void BaseObject::loadAllEventStates(Element& xmlelement, XMLPort::Mode mode, BaseObject* object, Identifier* identifier)
459    {
460        Element* events = xmlelement.FirstChildElement("events", false);
461        if (events)
462        {
463            // get the list of all states present
464            std::list<std::string> eventnames;
465            if (mode == XMLPort::LoadObject || mode == XMLPort::ExpandObject)
466            {
467                for (ticpp::Iterator<ticpp::Element> child = events->FirstChildElement(false); child != child.end(); child++)
468                    eventnames.push_back(child->Value());
469            }
470            else if (mode == XMLPort::SaveObject)
471            {
472            }
[5781]473
[5929]474            // iterate through all states and get the event sources
475            for (std::list<std::string>::iterator it = eventnames.begin(); it != eventnames.end(); ++it)
476            {
[6417]477                const std::string& statename = (*it);
[5929]478
479                // if the event state is already known, continue with the next state
480                orxonox::EventState* eventstate = object->getEventState(statename);
481                if (eventstate)
482                    continue;
483
484                XMLPortClassObjectContainer<BaseObject, BaseObject>* container = (XMLPortClassObjectContainer<BaseObject, BaseObject>*)(identifier->getXMLPortObjectContainer(statename));
485                if (!container)
486                {
[7284]487                    const ExecutorMemberPtr<BaseObject>& setfunctor = createExecutor(createFunctor(&BaseObject::addEventSource), std::string( "BaseObject" ) + "::" + "addEventSource" + '(' + statename + ')');
488                    const ExecutorMemberPtr<BaseObject>& getfunctor = createExecutor(createFunctor(&BaseObject::getEventSource), std::string( "BaseObject" ) + "::" + "getEventSource" + '(' + statename + ')');
[5929]489                    setfunctor->setDefaultValue(1, statename);
490                    getfunctor->setDefaultValue(1, statename);
491
492                    container = new XMLPortClassObjectContainer<BaseObject, BaseObject>(statename, identifier, setfunctor, getfunctor, false, true);
493                    identifier->addXMLPortObjectContainer(statename, container);
494                }
495                container->port(object, *events, mode);
496            }
497        }
[5781]498    }
[1505]499}
Note: See TracBrowser for help on using the repository browser.