Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/Template.cc @ 2509

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

merged questsystem2 back to trunk

  • Property svn:eol-style set to native
  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/questsystem2/src/core/Template.ccmergedeligible
    /code/branches/ceguilua/src/orxonox/objects/Template.cc1802-1808
    /code/branches/core3/src/orxonox/objects/Template.cc1572-1739
    /code/branches/gcc43/src/orxonox/objects/Template.cc1580
    /code/branches/gui/src/orxonox/objects/Template.cc1635-1723
    /code/branches/input/src/orxonox/objects/Template.cc1629-1636
    /code/branches/objecthierarchy/src/core/Template.cc2100,​2110-2169
    /code/branches/script_trigger/src/orxonox/objects/Template.cc1295-1953,​1955
File size: 5.4 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 "core/CoreIncludes.h"
32#include "core/XMLPort.h"
33#include "util/Debug.h"
34#include "tinyxml/ticpp.h"
35
36namespace orxonox
37{
38    CreateFactory(Template);
39
40    Template::Template(BaseObject* creator) : BaseObject(creator), xmlelement_("")
41    {
42        RegisterObject(Template);
43
44        this->bIsLink_ = false;
45        this->bIsReturningXMLElement_ = false;
46        this->baseclassIdentifier_ = 0;
47    }
48
49    Template::~Template()
50    {
51        Template::getTemplateMap().erase(this->getName());
52    }
53
54    void Template::XMLPort(Element& xmlelement, XMLPort::Mode mode)
55    {
56        SUPER(Template, XMLPort, xmlelement, mode);
57
58        XMLPortParam(Template, "link", setLink, getLink, xmlelement, mode);
59        XMLPortParam(Template, "baseclass", setBaseclass, getBaseclass, xmlelement, mode);
60
61        Element* element = xmlelement.FirstChildElement(false);
62        if (element)
63        {
64            TiXmlElement* tixmlelement = dynamic_cast<TiXmlElement*>(element->GetTiXmlPointer());
65            if (tixmlelement)
66                this->setXMLElement(*tixmlelement);
67        }
68    }
69
70    void Template::changedName()
71    {
72        if (this->getName() != "")
73        {
74            std::map<std::string, Template*>::iterator it;
75            it = Template::getTemplateMap().find(this->getOldName());
76            if (it != Template::getTemplateMap().end())
77                Template::getTemplateMap().erase(it);
78
79            it = Template::getTemplateMap().find(this->getName());
80            if (it != Template::getTemplateMap().end())
81                COUT(2) << "Warning: Template with name \"" << this->getName() << "\" already exists." << std::endl;
82            else
83                Template::getTemplateMap()[this->getName()] = this;
84        }
85    }
86
87    const TiXmlElement& Template::getXMLElement() const
88    {
89        if (this->bIsLink_)
90        {
91            Template* temp = Template::getTemplate(this->link_);
92            if (temp)
93            {
94                if (!temp->bIsReturningXMLElement_)
95                {
96                    this->bIsReturningXMLElement_ = true;
97                    const TiXmlElement& element = temp->getXMLElement();
98                    this->bIsReturningXMLElement_ = false;
99                    return element;
100                }
101                else
102                {
103                    COUT(2) << "Warning: Linking from " << this->getName() << " to " << this->link_ << " leads to an infinite loop. Returning own element." << std::endl;
104                }
105            }
106            else
107            {
108                COUT(2) << "Warning: " << this->link_ << " is not an existing Template name. Returning own element." << std::endl;
109            }
110        }
111
112        return this->xmlelement_;
113    }
114
115    void Template::setBaseclass(const std::string& baseclass)
116    {
117        this->baseclassIdentifier_ = ClassByString(baseclass);
118        if (this->baseclassIdentifier_)
119            this->baseclass_ = baseclass;
120    }
121
122    void Template::applyOn(BaseObject* object)
123    {
124        if (this->baseclassIdentifier_)
125        {
126            if (!object->isA(this->baseclassIdentifier_))
127            {
128                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;
129                return;
130            }
131        }
132
133        COUT(4) << object->getLoaderIndentation() << " aplying Template \"" << this->getName() << "\"..." << std::endl;
134
135        Element temp = ((TiXmlElement*)&this->getXMLElement());
136        object->XMLPort(temp, XMLPort::LoadObject);
137    }
138
139    std::map<std::string, Template*>& Template::getTemplateMap()
140    {
141        static std::map<std::string, Template*> templateMap;
142        return templateMap;
143    }
144
145    Template* Template::getTemplate(const std::string& name)
146    {
147        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
148        if (it != Template::getTemplateMap().end())
149            return it->second;
150        else
151        {
152            COUT(2) << "Warning: Template with name " << name << " doesn't exist." << std::endl;
153            return 0;
154        }
155    }
156
157    void Template::apply(const std::string& name, BaseObject* object)
158    {
159        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
160        if (it != Template::getTemplateMap().end())
161            it->second->applyOn(object);
162    }
163}
Note: See TracBrowser for help on using the repository browser.