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