Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/core/Template.cc @ 2662

Last change on this file since 2662 was 2369, checked in by landauf, 16 years ago
  • Added a health bar
  • Some changes in CameraManager to handle the case when no camera exists after removing the last one, but this is still somehow buggy (freezes and later crashes reproducible but inexplicable after a few respawns)
  • Added PawnManager to handle destruction of Pawns without using delete within tick()
  • Property svn:eol-style set to native
  • Property svn:mergeinfo set to (toggle deleted branches)
    /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.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 "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->bLoadDefaults_ = true;
46        this->bIsReturningXMLElement_ = false;
47        this->baseclassIdentifier_ = 0;
48    }
49
50    Template::~Template()
51    {
52        Template::getTemplateMap().erase(this->getName());
53    }
54
55    void Template::XMLPort(Element& xmlelement, XMLPort::Mode mode)
56    {
57        SUPER(Template, XMLPort, xmlelement, mode);
58
59        XMLPortParam(Template, "link",      setLink,         getLink,         xmlelement, mode);
60        XMLPortParam(Template, "baseclass", setBaseclass,    getBaseclass,    xmlelement, mode);
61        XMLPortParam(Template, "defaults",  setLoadDefaults, getLoadDefaults, xmlelement, mode).defaultValues(true);
62
63        Element* element = xmlelement.FirstChildElement(false);
64        if (element)
65        {
66            TiXmlElement* tixmlelement = dynamic_cast<TiXmlElement*>(element->GetTiXmlPointer());
67            if (tixmlelement)
68                this->setXMLElement(*tixmlelement);
69        }
70    }
71
72    void Template::changedName()
73    {
74        SUPER(Template, changedName);
75
76        if (this->getName() != "")
77        {
78            std::map<std::string, Template*>::iterator it;
79            it = Template::getTemplateMap().find(this->getOldName());
80            if (it != Template::getTemplateMap().end())
81                Template::getTemplateMap().erase(it);
82
83            it = Template::getTemplateMap().find(this->getName());
84            if (it != Template::getTemplateMap().end())
85                COUT(2) << "Warning: Template with name \"" << this->getName() << "\" already exists." << std::endl;
86            else
87                Template::getTemplateMap()[this->getName()] = this;
88        }
89    }
90
91    const TiXmlElement& Template::getXMLElement() const
92    {
93        if (this->bIsLink_)
94        {
95            Template* temp = Template::getTemplate(this->link_);
96            if (temp)
97            {
98                if (!temp->bIsReturningXMLElement_)
99                {
100                    this->bIsReturningXMLElement_ = true;
101                    const TiXmlElement& element = temp->getXMLElement();
102                    this->bIsReturningXMLElement_ = false;
103                    return element;
104                }
105                else
106                {
107                    COUT(2) << "Warning: Linking from " << this->getName() << " to " << this->link_ << " leads to an infinite loop. Returning own element." << std::endl;
108                }
109            }
110            else
111            {
112                COUT(2) << "Warning: " << this->link_ << " is not an existing Template name. Returning own element." << std::endl;
113            }
114        }
115
116        return this->xmlelement_;
117    }
118
119    void Template::setBaseclass(const std::string& baseclass)
120    {
121        this->baseclassIdentifier_ = ClassByString(baseclass);
122        if (this->baseclassIdentifier_)
123            this->baseclass_ = baseclass;
124    }
125
126    void Template::applyOn(BaseObject* object)
127    {
128        if (this->baseclassIdentifier_)
129        {
130            if (!object->isA(this->baseclassIdentifier_))
131            {
132                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;
133                return;
134            }
135        }
136
137        COUT(4) << object->getLoaderIndentation() << " aplying Template \"" << this->getName() << "\"..." << std::endl;
138
139        Element temp = ((TiXmlElement*)&this->getXMLElement());
140
141        if (this->bLoadDefaults_)
142            object->XMLPort(temp, XMLPort::LoadObject);
143        else
144            object->XMLPort(temp, XMLPort::ExpandObject);
145    }
146
147    std::map<std::string, Template*>& Template::getTemplateMap()
148    {
149        static std::map<std::string, Template*> templateMap;
150        return templateMap;
151    }
152
153    Template* Template::getTemplate(const std::string& name)
154    {
155        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
156        if (it != Template::getTemplateMap().end())
157            return it->second;
158        else
159        {
160            COUT(2) << "Warning: Template with name " << name << " doesn't exist." << std::endl;
161            return 0;
162        }
163    }
164
165    void Template::apply(const std::string& name, BaseObject* object)
166    {
167        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
168        if (it != Template::getTemplateMap().end())
169            it->second->applyOn(object);
170    }
171}
Note: See TracBrowser for help on using the repository browser.