Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/Template.cc @ 6749

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

Merged gamestates2 branch back to trunk.
This brings in some heavy changes in the GUI framework.
It should also fix problems with triggered asserts in the InputManager.

Note: PickupInventory does not seem to work —> Segfault when showing because before, the owner in GUIOverlay::setGUIName is already NULL.
I haven't tested it before, so I can't tell whether it's my changes.

  • Property svn:eol-style set to native
File size: 5.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#include "Template.h"
30
31#include <tinyxml/tinyxml.h>
32#include <tinyxml/ticpp.h>
33
34#include "util/Debug.h"
35#include "CoreIncludes.h"
36#include "XMLPort.h"
37
38namespace orxonox
39{
40    CreateFactory(Template);
41
42    Template::Template(BaseObject* creator) : BaseObject(creator)
43    {
44        this->xmlelement_ = new TiXmlElement("");
45
46        RegisterObject(Template);
47
48        this->bIsLink_ = false;
49        this->bLoadDefaults_ = true;
50        this->bIsReturningXMLElement_ = false;
51        this->baseclassIdentifier_ = 0;
52    }
53
54    Template::~Template()
55    {
56        Template::getTemplateMap().erase(this->getName());
57        delete this->xmlelement_;
58    }
59
60    void Template::XMLPort(Element& xmlelement, XMLPort::Mode mode)
61    {
62        SUPER(Template, XMLPort, xmlelement, mode);
63
64        XMLPortParam(Template, "link",      setLink,         getLink,         xmlelement, mode);
65        XMLPortParam(Template, "baseclass", setBaseclass,    getBaseclass,    xmlelement, mode);
66        XMLPortParam(Template, "defaults",  setLoadDefaults, getLoadDefaults, xmlelement, mode).defaultValues(true);
67
68        Element* element = xmlelement.FirstChildElement(false);
69        if (element)
70        {
71            TiXmlElement* tixmlelement = static_cast<TiXmlElement*>(element->GetTiXmlPointer());
72            if (tixmlelement)
73                this->setXMLElement(*tixmlelement);
74        }
75    }
76
77    void Template::changedName()
78    {
79        SUPER(Template, changedName);
80
81        if (!this->getName().empty())
82        {
83            std::map<std::string, Template*>::iterator it;
84            it = Template::getTemplateMap().find(this->getOldName());
85            if (it != Template::getTemplateMap().end())
86                Template::getTemplateMap().erase(it);
87
88            it = Template::getTemplateMap().find(this->getName());
89            if (it != Template::getTemplateMap().end())
90                COUT(2) << "Warning: Template with name \"" << this->getName() << "\" already exists." << std::endl;
91            else
92                Template::getTemplateMap()[this->getName()] = this;
93        }
94    }
95
96    void Template::setXMLElement(const TiXmlElement& xmlelement)
97    {
98        *this->xmlelement_ = xmlelement;
99    }
100
101    const TiXmlElement& Template::getXMLElement() const
102    {
103        if (this->bIsLink_)
104        {
105            Template* temp = Template::getTemplate(this->link_);
106            if (temp)
107            {
108                if (!temp->bIsReturningXMLElement_)
109                {
110                    this->bIsReturningXMLElement_ = true;
111                    const TiXmlElement& element = temp->getXMLElement();
112                    this->bIsReturningXMLElement_ = false;
113                    return element;
114                }
115                else
116                {
117                    COUT(2) << "Warning: Linking from " << this->getName() << " to " << this->link_ << " leads to an infinite loop. Returning own element." << std::endl;
118                }
119            }
120            else
121            {
122                COUT(2) << "Warning: " << this->link_ << " is not an existing Template name. Returning own element." << std::endl;
123            }
124        }
125
126        return *this->xmlelement_;
127    }
128
129    void Template::setBaseclass(const std::string& baseclass)
130    {
131        this->baseclassIdentifier_ = ClassByString(baseclass);
132        if (this->baseclassIdentifier_)
133            this->baseclass_ = baseclass;
134    }
135
136    void Template::applyOn(BaseObject* object)
137    {
138        if (this->baseclassIdentifier_)
139        {
140            if (!object->isA(this->baseclassIdentifier_))
141            {
142                COUT(1) << "Error: Can't apply template (name: " << this->getName() << "), object (name: " << object->getName() << ", class: " << object->getIdentifier()->getName() << ") is not a " << this->baseclassIdentifier_->getName() << std::endl;
143                return;
144            }
145        }
146
147        COUT(4) << object->getLoaderIndentation() << " aplying Template \"" << this->getName() << "\"..." << std::endl;
148
149        Element temp = &const_cast<TiXmlElement&>(this->getXMLElement());
150
151        if (this->bLoadDefaults_)
152            object->XMLPort(temp, XMLPort::LoadObject);
153        else
154            object->XMLPort(temp, XMLPort::ExpandObject);
155    }
156
157    std::map<std::string, Template*>& Template::getTemplateMap()
158    {
159        static std::map<std::string, Template*> templateMap;
160        return templateMap;
161    }
162
163    Template* Template::getTemplate(const std::string& name)
164    {
165        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
166        if (it != Template::getTemplateMap().end())
167            return it->second;
168        else
169        {
170            COUT(2) << "Warning: Template with name " << name << " doesn't exist." << std::endl;
171            return 0;
172        }
173    }
174
175    void Template::apply(const std::string& name, BaseObject* object)
176    {
177        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
178        if (it != Template::getTemplateMap().end())
179            it->second->applyOn(object);
180    }
181}
Note: See TracBrowser for help on using the repository browser.