[1970] | 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 | |
---|
| 36 | namespace orxonox |
---|
| 37 | { |
---|
| 38 | CreateFactory(Template); |
---|
| 39 | |
---|
| 40 | Template::Template() |
---|
| 41 | { |
---|
| 42 | RegisterObject(Template); |
---|
| 43 | |
---|
| 44 | this->bIsLink_ = false; |
---|
| 45 | this->bIsReturningXMLElement_ = false; |
---|
| 46 | this->baseclassIdentifier_ = 0; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | Template::~Template() |
---|
| 50 | { |
---|
[1989] | 51 | Template::getTemplateMap().erase(this->getName()); |
---|
[1970] | 52 | } |
---|
| 53 | |
---|
| 54 | void Template::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 55 | { |
---|
| 56 | SUPER(Template, XMLPort, xmlelement, mode); |
---|
| 57 | |
---|
| 58 | XMLPortParam(Template, "link", setLink, getLink, xmlelement, mode); |
---|
| 59 | XMLPortParam(Template, "baseclass", setBaseclass, getBaseclass, xmlelement, mode); |
---|
| 60 | |
---|
| 61 | this->setXMLElement(*xmlelement.FirstChildElement(false)); |
---|
| 62 | } |
---|
| 63 | |
---|
[1989] | 64 | void Template::changedName() |
---|
| 65 | { |
---|
| 66 | if (this->getName() != "") |
---|
| 67 | { |
---|
| 68 | std::map<std::string, Template*>::iterator it; |
---|
| 69 | it = Template::getTemplateMap().find(this->getOldName()); |
---|
| 70 | if (it != Template::getTemplateMap().end()) |
---|
| 71 | Template::getTemplateMap().erase(it); |
---|
| 72 | |
---|
| 73 | it = Template::getTemplateMap().find(this->getName()); |
---|
| 74 | if (it != Template::getTemplateMap().end()) |
---|
| 75 | COUT(2) << "Warning: Template with name \"" << this->getName() << "\" already exists." << std::endl; |
---|
| 76 | else |
---|
| 77 | Template::getTemplateMap()[this->getName()] = this; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
[1970] | 81 | const Element& Template::getXMLElement() const |
---|
| 82 | { |
---|
| 83 | if (this->bIsLink_) |
---|
| 84 | { |
---|
| 85 | Template* temp = Template::getTemplate(this->link_); |
---|
| 86 | if (temp) |
---|
| 87 | { |
---|
| 88 | if (!temp->bIsReturningXMLElement_) |
---|
| 89 | { |
---|
| 90 | this->bIsReturningXMLElement_ = true; |
---|
| 91 | const Element& element = temp->getXMLElement(); |
---|
| 92 | this->bIsReturningXMLElement_ = false; |
---|
| 93 | return element; |
---|
| 94 | } |
---|
| 95 | else |
---|
| 96 | { |
---|
| 97 | COUT(2) << "Warning: Linking from " << this->getName() << " to " << this->link_ << " leads to an infinite loop. Returning own element." << std::endl; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | else |
---|
| 101 | { |
---|
| 102 | COUT(2) << "Warning: " << this->link_ << " is not an existing Template name. Returning own element." << std::endl; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | return this->xmlelement_; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | void Template::setBaseclass(const std::string& baseclass) |
---|
| 110 | { |
---|
| 111 | this->baseclassIdentifier_ = ClassByString(baseclass); |
---|
| 112 | if (this->baseclassIdentifier_) |
---|
| 113 | this->baseclass_ = baseclass; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void Template::applyOn(BaseObject* object) |
---|
| 117 | { |
---|
| 118 | if (this->baseclassIdentifier_) |
---|
| 119 | { |
---|
| 120 | if (!object->isA(this->baseclassIdentifier_)) |
---|
| 121 | { |
---|
| 122 | 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; |
---|
| 123 | return; |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | COUT(4) << object->getLoaderIndentation() << " aplying Template \"" << this->getName() << "\"..." << std::endl; |
---|
| 128 | |
---|
| 129 | Element temp = this->getXMLElement(); |
---|
| 130 | object->XMLPort(temp, XMLPort::LoadObject); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | std::map<std::string, Template*>& Template::getTemplateMap() |
---|
| 134 | { |
---|
| 135 | static std::map<std::string, Template*> templateMap; |
---|
| 136 | return templateMap; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | Template* Template::getTemplate(const std::string& name) |
---|
| 140 | { |
---|
| 141 | std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name); |
---|
| 142 | if (it != Template::getTemplateMap().end()) |
---|
| 143 | return it->second; |
---|
| 144 | else |
---|
| 145 | return 0; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | void Template::apply(const std::string& name, BaseObject* object) |
---|
| 149 | { |
---|
| 150 | std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name); |
---|
| 151 | if (it != Template::getTemplateMap().end()) |
---|
| 152 | it->second->applyOn(object); |
---|
| 153 | } |
---|
| 154 | } |
---|