Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/core/Template.cc @ 2934

Last change on this file since 2934 was 2815, checked in by rgrieder, 16 years ago

various small changes:

  • Fixed a bug in the GameStates concerning return type of virtual functions
  • Found some more C-style casts
  • Property svn:eol-style set to native
  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/lodfinal/src/core/Template.ccmergedeligible
    /code/branches/pickups2/src/core/Template.ccmergedeligible
    /code/branches/buildsystem2/src/core/Template.cc2506-2658
    /code/branches/buildsystem3/src/core/Template.cc2662-2708
    /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/network/src/core/Template.cc2356
    /code/branches/network64/src/core/Template.cc2210-2355
    /code/branches/objecthierarchy/src/core/Template.cc2100,​2110-2169
    /code/branches/objecthierarchy2/src/core/Template.cc2171-2479
    /code/branches/overlay/src/core/Template.cc2117-2385
    /code/branches/physics/src/core/Template.cc2107-2439
    /code/branches/physics_merge/src/core/Template.cc2436-2457
    /code/branches/presentation/src/core/Template.cc2369-2652,​2654-2660
    /code/branches/questsystem2/src/core/Template.cc2107-2259
    /code/branches/script_trigger/src/orxonox/objects/Template.cc1295-1953,​1955
    /code/branches/weapon2/src/core/Template.cc2107-2488
File size: 5.7 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/ticpp.h>
32
33#include "core/CoreIncludes.h"
34#include "core/XMLPort.h"
35#include "util/Debug.h"
36
37namespace orxonox
38{
39    CreateFactory(Template);
40
41    Template::Template(BaseObject* creator) : BaseObject(creator), xmlelement_("")
42    {
43        RegisterObject(Template);
44
45        this->bIsLink_ = false;
46        this->bLoadDefaults_ = true;
47        this->bIsReturningXMLElement_ = false;
48        this->baseclassIdentifier_ = 0;
49    }
50
51    Template::~Template()
52    {
53        Template::getTemplateMap().erase(this->getName());
54    }
55
56    void Template::XMLPort(Element& xmlelement, XMLPort::Mode mode)
57    {
58        SUPER(Template, XMLPort, xmlelement, mode);
59
60        XMLPortParam(Template, "link",      setLink,         getLink,         xmlelement, mode);
61        XMLPortParam(Template, "baseclass", setBaseclass,    getBaseclass,    xmlelement, mode);
62        XMLPortParam(Template, "defaults",  setLoadDefaults, getLoadDefaults, xmlelement, mode).defaultValues(true);
63
64        Element* element = xmlelement.FirstChildElement(false);
65        if (element)
66        {
67            TiXmlElement* tixmlelement = static_cast<TiXmlElement*>(element->GetTiXmlPointer());
68            if (tixmlelement)
69                this->setXMLElement(*tixmlelement);
70        }
71    }
72
73    void Template::changedName()
74    {
75        SUPER(Template, changedName);
76
77        if (this->getName() != "")
78        {
79            std::map<std::string, Template*>::iterator it;
80            it = Template::getTemplateMap().find(this->getOldName());
81            if (it != Template::getTemplateMap().end())
82                Template::getTemplateMap().erase(it);
83
84            it = Template::getTemplateMap().find(this->getName());
85            if (it != Template::getTemplateMap().end())
86                COUT(2) << "Warning: Template with name \"" << this->getName() << "\" already exists." << std::endl;
87            else
88                Template::getTemplateMap()[this->getName()] = this;
89        }
90    }
91
92    const TiXmlElement& Template::getXMLElement() const
93    {
94        if (this->bIsLink_)
95        {
96            Template* temp = Template::getTemplate(this->link_);
97            if (temp)
98            {
99                if (!temp->bIsReturningXMLElement_)
100                {
101                    this->bIsReturningXMLElement_ = true;
102                    const TiXmlElement& element = temp->getXMLElement();
103                    this->bIsReturningXMLElement_ = false;
104                    return element;
105                }
106                else
107                {
108                    COUT(2) << "Warning: Linking from " << this->getName() << " to " << this->link_ << " leads to an infinite loop. Returning own element." << std::endl;
109                }
110            }
111            else
112            {
113                COUT(2) << "Warning: " << this->link_ << " is not an existing Template name. Returning own element." << std::endl;
114            }
115        }
116
117        return this->xmlelement_;
118    }
119
120    void Template::setBaseclass(const std::string& baseclass)
121    {
122        this->baseclassIdentifier_ = ClassByString(baseclass);
123        if (this->baseclassIdentifier_)
124            this->baseclass_ = baseclass;
125    }
126
127    void Template::applyOn(BaseObject* object)
128    {
129        if (this->baseclassIdentifier_)
130        {
131            if (!object->isA(this->baseclassIdentifier_))
132            {
133                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;
134                return;
135            }
136        }
137
138        COUT(4) << object->getLoaderIndentation() << " aplying Template \"" << this->getName() << "\"..." << std::endl;
139
140        Element temp = &const_cast<TiXmlElement&>(this->getXMLElement());
141
142        if (this->bLoadDefaults_)
143            object->XMLPort(temp, XMLPort::LoadObject);
144        else
145            object->XMLPort(temp, XMLPort::ExpandObject);
146    }
147
148    std::map<std::string, Template*>& Template::getTemplateMap()
149    {
150        static std::map<std::string, Template*> templateMap;
151        return templateMap;
152    }
153
154    Template* Template::getTemplate(const std::string& name)
155    {
156        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
157        if (it != Template::getTemplateMap().end())
158            return it->second;
159        else
160        {
161            COUT(2) << "Warning: Template with name " << name << " doesn't exist." << std::endl;
162            return 0;
163        }
164    }
165
166    void Template::apply(const std::string& name, BaseObject* object)
167    {
168        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
169        if (it != Template::getTemplateMap().end())
170            it->second->applyOn(object);
171    }
172}
Note: See TracBrowser for help on using the repository browser.