[2072] | 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 | |
---|
[3196] | 31 | #include <tinyxml/tinyxml.h> |
---|
[2710] | 32 | #include <tinyxml/ticpp.h> |
---|
| 33 | |
---|
[3196] | 34 | #include "util/Debug.h" |
---|
[6746] | 35 | #include "CoreIncludes.h" |
---|
| 36 | #include "XMLPort.h" |
---|
[2072] | 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
| 40 | CreateFactory(Template); |
---|
| 41 | |
---|
[3196] | 42 | Template::Template(BaseObject* creator) : BaseObject(creator) |
---|
[2072] | 43 | { |
---|
[3196] | 44 | this->xmlelement_ = new TiXmlElement(""); |
---|
| 45 | |
---|
[2072] | 46 | RegisterObject(Template); |
---|
| 47 | |
---|
| 48 | this->bIsLink_ = false; |
---|
[2662] | 49 | this->bLoadDefaults_ = true; |
---|
[2072] | 50 | this->bIsReturningXMLElement_ = false; |
---|
| 51 | this->baseclassIdentifier_ = 0; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | Template::~Template() |
---|
| 55 | { |
---|
| 56 | Template::getTemplateMap().erase(this->getName()); |
---|
[3196] | 57 | delete this->xmlelement_; |
---|
[2072] | 58 | } |
---|
| 59 | |
---|
| 60 | void Template::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 61 | { |
---|
| 62 | SUPER(Template, XMLPort, xmlelement, mode); |
---|
| 63 | |
---|
[2662] | 64 | XMLPortParam(Template, "link", setLink, getLink, xmlelement, mode); |
---|
| 65 | XMLPortParam(Template, "baseclass", setBaseclass, getBaseclass, xmlelement, mode); |
---|
| 66 | XMLPortParam(Template, "defaults", setLoadDefaults, getLoadDefaults, xmlelement, mode).defaultValues(true); |
---|
[2072] | 67 | |
---|
| 68 | Element* element = xmlelement.FirstChildElement(false); |
---|
| 69 | if (element) |
---|
| 70 | { |
---|
[2896] | 71 | TiXmlElement* tixmlelement = static_cast<TiXmlElement*>(element->GetTiXmlPointer()); |
---|
[2072] | 72 | if (tixmlelement) |
---|
| 73 | this->setXMLElement(*tixmlelement); |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void Template::changedName() |
---|
| 78 | { |
---|
[2662] | 79 | SUPER(Template, changedName); |
---|
| 80 | |
---|
[6417] | 81 | if (!this->getName().empty()) |
---|
[2072] | 82 | { |
---|
| 83 | std::map<std::string, Template*>::iterator it; |
---|
| 84 | it = Template::getTemplateMap().find(this->getOldName()); |
---|
| 85 | if (it != Template::getTemplateMap().end()) |
---|
| 86 | Template::getTemplateMap().erase(it); |
---|
| 87 | |
---|
| 88 | it = Template::getTemplateMap().find(this->getName()); |
---|
| 89 | if (it != Template::getTemplateMap().end()) |
---|
| 90 | COUT(2) << "Warning: Template with name \"" << this->getName() << "\" already exists." << std::endl; |
---|
| 91 | else |
---|
| 92 | Template::getTemplateMap()[this->getName()] = this; |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
[3196] | 96 | void Template::setXMLElement(const TiXmlElement& xmlelement) |
---|
| 97 | { |
---|
| 98 | *this->xmlelement_ = xmlelement; |
---|
| 99 | } |
---|
| 100 | |
---|
[7163] | 101 | const TiXmlElement& Template::getXMLElement() |
---|
[2072] | 102 | { |
---|
| 103 | if (this->bIsLink_) |
---|
| 104 | { |
---|
| 105 | Template* temp = Template::getTemplate(this->link_); |
---|
| 106 | if (temp) |
---|
| 107 | { |
---|
[7163] | 108 | this->bLoadDefaults_ = temp->bLoadDefaults_; |
---|
| 109 | |
---|
[2072] | 110 | if (!temp->bIsReturningXMLElement_) |
---|
| 111 | { |
---|
| 112 | this->bIsReturningXMLElement_ = true; |
---|
| 113 | const TiXmlElement& element = temp->getXMLElement(); |
---|
| 114 | this->bIsReturningXMLElement_ = false; |
---|
| 115 | return element; |
---|
| 116 | } |
---|
| 117 | else |
---|
| 118 | { |
---|
| 119 | COUT(2) << "Warning: Linking from " << this->getName() << " to " << this->link_ << " leads to an infinite loop. Returning own element." << std::endl; |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | else |
---|
| 123 | { |
---|
| 124 | COUT(2) << "Warning: " << this->link_ << " is not an existing Template name. Returning own element." << std::endl; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
[3196] | 128 | return *this->xmlelement_; |
---|
[2072] | 129 | } |
---|
| 130 | |
---|
| 131 | void Template::setBaseclass(const std::string& baseclass) |
---|
| 132 | { |
---|
| 133 | this->baseclassIdentifier_ = ClassByString(baseclass); |
---|
| 134 | if (this->baseclassIdentifier_) |
---|
| 135 | this->baseclass_ = baseclass; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void Template::applyOn(BaseObject* object) |
---|
| 139 | { |
---|
| 140 | if (this->baseclassIdentifier_) |
---|
| 141 | { |
---|
| 142 | if (!object->isA(this->baseclassIdentifier_)) |
---|
| 143 | { |
---|
| 144 | 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; |
---|
| 145 | return; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | COUT(4) << object->getLoaderIndentation() << " aplying Template \"" << this->getName() << "\"..." << std::endl; |
---|
| 150 | |
---|
[2896] | 151 | Element temp = &const_cast<TiXmlElement&>(this->getXMLElement()); |
---|
[2662] | 152 | |
---|
| 153 | if (this->bLoadDefaults_) |
---|
| 154 | object->XMLPort(temp, XMLPort::LoadObject); |
---|
| 155 | else |
---|
| 156 | object->XMLPort(temp, XMLPort::ExpandObject); |
---|
[2072] | 157 | } |
---|
| 158 | |
---|
| 159 | std::map<std::string, Template*>& Template::getTemplateMap() |
---|
| 160 | { |
---|
| 161 | static std::map<std::string, Template*> templateMap; |
---|
| 162 | return templateMap; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | Template* Template::getTemplate(const std::string& name) |
---|
| 166 | { |
---|
| 167 | std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name); |
---|
| 168 | if (it != Template::getTemplateMap().end()) |
---|
| 169 | return it->second; |
---|
| 170 | else |
---|
| 171 | { |
---|
| 172 | COUT(2) << "Warning: Template with name " << name << " doesn't exist." << std::endl; |
---|
| 173 | return 0; |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | void Template::apply(const std::string& name, BaseObject* object) |
---|
| 178 | { |
---|
| 179 | std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name); |
---|
| 180 | if (it != Template::getTemplateMap().end()) |
---|
| 181 | it->second->applyOn(object); |
---|
| 182 | } |
---|
| 183 | } |
---|