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 | |
---|
37 | namespace 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->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 | |
---|
62 | Element* element = xmlelement.FirstChildElement(false); |
---|
63 | if (element) |
---|
64 | { |
---|
65 | TiXmlElement* tixmlelement = dynamic_cast<TiXmlElement*>(element->GetTiXmlPointer()); |
---|
66 | if (tixmlelement) |
---|
67 | this->setXMLElement(*tixmlelement); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | void Template::changedName() |
---|
72 | { |
---|
73 | if (this->getName() != "") |
---|
74 | { |
---|
75 | std::map<std::string, Template*>::iterator it; |
---|
76 | it = Template::getTemplateMap().find(this->getOldName()); |
---|
77 | if (it != Template::getTemplateMap().end()) |
---|
78 | Template::getTemplateMap().erase(it); |
---|
79 | |
---|
80 | it = Template::getTemplateMap().find(this->getName()); |
---|
81 | if (it != Template::getTemplateMap().end()) |
---|
82 | COUT(2) << "Warning: Template with name \"" << this->getName() << "\" already exists." << std::endl; |
---|
83 | else |
---|
84 | Template::getTemplateMap()[this->getName()] = this; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | const TiXmlElement& Template::getXMLElement() const |
---|
89 | { |
---|
90 | if (this->bIsLink_) |
---|
91 | { |
---|
92 | Template* temp = Template::getTemplate(this->link_); |
---|
93 | if (temp) |
---|
94 | { |
---|
95 | if (!temp->bIsReturningXMLElement_) |
---|
96 | { |
---|
97 | this->bIsReturningXMLElement_ = true; |
---|
98 | const TiXmlElement& element = temp->getXMLElement(); |
---|
99 | this->bIsReturningXMLElement_ = false; |
---|
100 | return element; |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | COUT(2) << "Warning: Linking from " << this->getName() << " to " << this->link_ << " leads to an infinite loop. Returning own element." << std::endl; |
---|
105 | } |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | COUT(2) << "Warning: " << this->link_ << " is not an existing Template name. Returning own element." << std::endl; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | return this->xmlelement_; |
---|
114 | } |
---|
115 | |
---|
116 | void Template::setBaseclass(const std::string& baseclass) |
---|
117 | { |
---|
118 | this->baseclassIdentifier_ = ClassByString(baseclass); |
---|
119 | if (this->baseclassIdentifier_) |
---|
120 | this->baseclass_ = baseclass; |
---|
121 | } |
---|
122 | |
---|
123 | void Template::applyOn(BaseObject* object) |
---|
124 | { |
---|
125 | if (this->baseclassIdentifier_) |
---|
126 | { |
---|
127 | if (!object->isA(this->baseclassIdentifier_)) |
---|
128 | { |
---|
129 | 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; |
---|
130 | return; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | COUT(4) << object->getLoaderIndentation() << " aplying Template \"" << this->getName() << "\"..." << std::endl; |
---|
135 | |
---|
136 | Element temp = ((TiXmlElement*)&this->getXMLElement()); |
---|
137 | object->XMLPort(temp, XMLPort::LoadObject); |
---|
138 | } |
---|
139 | |
---|
140 | std::map<std::string, Template*>& Template::getTemplateMap() |
---|
141 | { |
---|
142 | static std::map<std::string, Template*> templateMap; |
---|
143 | return templateMap; |
---|
144 | } |
---|
145 | |
---|
146 | Template* Template::getTemplate(const std::string& name) |
---|
147 | { |
---|
148 | std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name); |
---|
149 | if (it != Template::getTemplateMap().end()) |
---|
150 | return it->second; |
---|
151 | else |
---|
152 | { |
---|
153 | COUT(2) << "Warning: Template with name " << name << " doesn't exist." << std::endl; |
---|
154 | return 0; |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | void Template::apply(const std::string& name, BaseObject* object) |
---|
159 | { |
---|
160 | std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name); |
---|
161 | if (it != Template::getTemplateMap().end()) |
---|
162 | it->second->applyOn(object); |
---|
163 | } |
---|
164 | } |
---|