Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2266 was 2173, checked in by landauf, 16 years ago
  • Added new XMLPort mode, ExpandObject, which works like LoadObject but doesn't use default values if there are missing parameters. This is used if an object is expanded by templates (like adding CameraPositions).
  • Changed default level to sample3.oxw in objecthierarchy2 branch
  • Small fix with doubly added clients in Level- and PlayerManager
  • Property svn:eol-style set to native
  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/objecthierarchy/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/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        if (this->getName() != "")
75        {
76            std::map<std::string, Template*>::iterator it;
77            it = Template::getTemplateMap().find(this->getOldName());
78            if (it != Template::getTemplateMap().end())
79                Template::getTemplateMap().erase(it);
80
81            it = Template::getTemplateMap().find(this->getName());
82            if (it != Template::getTemplateMap().end())
83                COUT(2) << "Warning: Template with name \"" << this->getName() << "\" already exists." << std::endl;
84            else
85                Template::getTemplateMap()[this->getName()] = this;
86        }
87    }
88
89    const TiXmlElement& Template::getXMLElement() const
90    {
91        if (this->bIsLink_)
92        {
93            Template* temp = Template::getTemplate(this->link_);
94            if (temp)
95            {
96                if (!temp->bIsReturningXMLElement_)
97                {
98                    this->bIsReturningXMLElement_ = true;
99                    const TiXmlElement& element = temp->getXMLElement();
100                    this->bIsReturningXMLElement_ = false;
101                    return element;
102                }
103                else
104                {
105                    COUT(2) << "Warning: Linking from " << this->getName() << " to " << this->link_ << " leads to an infinite loop. Returning own element." << std::endl;
106                }
107            }
108            else
109            {
110                COUT(2) << "Warning: " << this->link_ << " is not an existing Template name. Returning own element." << std::endl;
111            }
112        }
113
114        return this->xmlelement_;
115    }
116
117    void Template::setBaseclass(const std::string& baseclass)
118    {
119        this->baseclassIdentifier_ = ClassByString(baseclass);
120        if (this->baseclassIdentifier_)
121            this->baseclass_ = baseclass;
122    }
123
124    void Template::applyOn(BaseObject* object)
125    {
126        if (this->baseclassIdentifier_)
127        {
128            if (!object->isA(this->baseclassIdentifier_))
129            {
130                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;
131                return;
132            }
133        }
134
135        COUT(4) << object->getLoaderIndentation() << " aplying Template \"" << this->getName() << "\"..." << std::endl;
136
137        Element temp = ((TiXmlElement*)&this->getXMLElement());
138
139        if (this->bLoadDefaults_)
140            object->XMLPort(temp, XMLPort::LoadObject);
141        else
142            object->XMLPort(temp, XMLPort::ExpandObject);
143    }
144
145    std::map<std::string, Template*>& Template::getTemplateMap()
146    {
147        static std::map<std::string, Template*> templateMap;
148        return templateMap;
149    }
150
151    Template* Template::getTemplate(const std::string& name)
152    {
153        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
154        if (it != Template::getTemplateMap().end())
155            return it->second;
156        else
157        {
158            COUT(2) << "Warning: Template with name " << name << " doesn't exist." << std::endl;
159            return 0;
160        }
161    }
162
163    void Template::apply(const std::string& name, BaseObject* object)
164    {
165        std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name);
166        if (it != Template::getTemplateMap().end())
167            it->second->applyOn(object);
168    }
169}
Note: See TracBrowser for help on using the repository browser.