[1505] | 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 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Implementation of the BaseObject class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "BaseObject.h" |
---|
[2710] | 35 | |
---|
| 36 | #include <tinyxml/tinyxml.h> |
---|
| 37 | |
---|
[3196] | 38 | #include "util/String.h" |
---|
[1505] | 39 | #include "CoreIncludes.h" |
---|
[3196] | 40 | #include "Event.h" |
---|
[2087] | 41 | #include "EventIncludes.h" |
---|
[2662] | 42 | #include "Functor.h" |
---|
[3196] | 43 | #include "Iterator.h" |
---|
| 44 | #include "Template.h" |
---|
[2087] | 45 | #include "XMLFile.h" |
---|
| 46 | #include "XMLNameListener.h" |
---|
[3196] | 47 | #include "XMLPort.h" |
---|
[1505] | 48 | |
---|
| 49 | namespace orxonox |
---|
| 50 | { |
---|
| 51 | CreateFactory(BaseObject); |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | @brief Constructor: Registers the object in the BaseObject-list. |
---|
| 55 | */ |
---|
[2087] | 56 | BaseObject::BaseObject(BaseObject* creator) : bInitialized_(false) |
---|
[1505] | 57 | { |
---|
| 58 | RegisterRootObject(BaseObject); |
---|
[1558] | 59 | |
---|
| 60 | this->bInitialized_ = true; |
---|
[1559] | 61 | |
---|
| 62 | this->bActive_ = true; |
---|
| 63 | this->bVisible_ = true; |
---|
[2087] | 64 | this->oldGametype_ = 0; |
---|
[1559] | 65 | |
---|
[2662] | 66 | this->lastLoadedXMLElement_ = 0; |
---|
| 67 | |
---|
| 68 | this->functorSetMainState_ = 0; |
---|
| 69 | this->functorGetMainState_ = 0; |
---|
| 70 | |
---|
[2087] | 71 | this->setCreator(creator); |
---|
| 72 | if (this->creator_) |
---|
| 73 | { |
---|
| 74 | this->setFile(this->creator_->getFile()); |
---|
| 75 | this->setNamespace(this->creator_->getNamespace()); |
---|
| 76 | this->setScene(this->creator_->getScene()); |
---|
| 77 | this->setGametype(this->creator_->getGametype()); |
---|
| 78 | } |
---|
| 79 | else |
---|
| 80 | { |
---|
| 81 | this->file_ = 0; |
---|
| 82 | this->namespace_ = 0; |
---|
| 83 | this->scene_ = 0; |
---|
| 84 | this->gametype_ = 0; |
---|
| 85 | } |
---|
[1505] | 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | @brief Destructor |
---|
| 90 | */ |
---|
| 91 | BaseObject::~BaseObject() |
---|
| 92 | { |
---|
[2662] | 93 | if (this->isInitialized()) |
---|
| 94 | { |
---|
| 95 | for (std::list<BaseObject*>::const_iterator it = this->events_.begin(); it != this->events_.end(); ++it) |
---|
| 96 | (*it)->unregisterEventListener(this); |
---|
[2087] | 97 | |
---|
[2662] | 98 | for (std::map<BaseObject*, std::string>::const_iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it) |
---|
| 99 | it->first->removeEvent(this); |
---|
| 100 | |
---|
| 101 | if (this->functorSetMainState_) |
---|
| 102 | delete this->functorSetMainState_; |
---|
| 103 | if (this->functorGetMainState_) |
---|
| 104 | delete this->functorGetMainState_; |
---|
| 105 | } |
---|
[1505] | 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | @brief XML loading and saving. |
---|
| 110 | @param xmlelement The XML-element |
---|
| 111 | @param loading Loading (true) or saving (false) |
---|
| 112 | @return The XML-element |
---|
| 113 | */ |
---|
| 114 | void BaseObject::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 115 | { |
---|
[2087] | 116 | XMLPortParam(BaseObject, "name", setXMLName, getName, xmlelement, mode); |
---|
[1625] | 117 | XMLPortParam(BaseObject, "visible", setVisible, isVisible, xmlelement, mode); |
---|
| 118 | XMLPortParam(BaseObject, "active", setActive, isActive, xmlelement, mode); |
---|
[2662] | 119 | XMLPortParam(BaseObject, "mainstate", setMainStateName, getMainStateName, xmlelement, mode); |
---|
[2087] | 120 | |
---|
| 121 | XMLPortObjectTemplate(BaseObject, Template, "templates", addTemplate, getTemplate, xmlelement, mode, Template*); |
---|
| 122 | |
---|
| 123 | Element* events = xmlelement.FirstChildElement("events", false); |
---|
| 124 | |
---|
| 125 | if (events) |
---|
| 126 | { |
---|
| 127 | std::list<std::string> eventnames; |
---|
| 128 | |
---|
[2662] | 129 | if (mode == XMLPort::LoadObject || mode == XMLPort::ExpandObject) |
---|
[2087] | 130 | { |
---|
| 131 | for (ticpp::Iterator<ticpp::Element> child = events->FirstChildElement(false); child != child.end(); child++) |
---|
| 132 | eventnames.push_back(child->Value()); |
---|
| 133 | } |
---|
| 134 | else if (mode == XMLPort::SaveObject) |
---|
| 135 | { |
---|
| 136 | for (std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->getIdentifier()->getXMLPortEventMapBegin(); it != this->getIdentifier()->getXMLPortEventMapEnd(); ++it) |
---|
| 137 | eventnames.push_back(it->first); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | for (std::list<std::string>::iterator it = eventnames.begin(); it != eventnames.end(); ++it) |
---|
| 141 | { |
---|
| 142 | std::string sectionname = (*it); |
---|
| 143 | ExecutorMember<BaseObject>* loadexecutor = createExecutor(createFunctor(&BaseObject::addEvent), std::string( "BaseObject" ) + "::" + "addEvent"); |
---|
| 144 | ExecutorMember<BaseObject>* saveexecutor = createExecutor(createFunctor(&BaseObject::getEvent), std::string( "BaseObject" ) + "::" + "getEvent"); |
---|
| 145 | loadexecutor->setDefaultValue(1, sectionname); |
---|
| 146 | |
---|
| 147 | XMLPortClassObjectContainer<BaseObject, BaseObject>* container = 0; |
---|
| 148 | container = (XMLPortClassObjectContainer<BaseObject, BaseObject>*)(this->getIdentifier()->getXMLPortEventContainer(sectionname)); |
---|
| 149 | if (!container) |
---|
| 150 | { |
---|
| 151 | container = new XMLPortClassObjectContainer<BaseObject, BaseObject>(sectionname, this->getIdentifier(), loadexecutor, saveexecutor, false, true); |
---|
| 152 | this->getIdentifier()->addXMLPortEventContainer(sectionname, container); |
---|
| 153 | } |
---|
| 154 | container->port(this, *events, mode); |
---|
| 155 | } |
---|
| 156 | } |
---|
[1505] | 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
[2087] | 160 | @brief Loads the name of the object through XML and calls all XMLNameListener. |
---|
| 161 | @param name The name of the object |
---|
| 162 | */ |
---|
| 163 | void BaseObject::setXMLName(const std::string& name) |
---|
| 164 | { |
---|
| 165 | this->setName(name); |
---|
| 166 | |
---|
| 167 | for (ObjectList<XMLNameListener>::iterator it = ObjectList<XMLNameListener>::begin(); it != ObjectList<XMLNameListener>::end(); ++it) |
---|
| 168 | it->loadedNewXMLName(this); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
[1505] | 172 | @brief Returns the levelfile that loaded this object. |
---|
| 173 | @return The levelfile |
---|
| 174 | */ |
---|
[2087] | 175 | const std::string& BaseObject::getFilename() const |
---|
[1505] | 176 | { |
---|
[2087] | 177 | if (this->file_) |
---|
| 178 | return this->file_->getFilename(); |
---|
| 179 | else |
---|
| 180 | return BLANKSTRING; |
---|
[1505] | 181 | } |
---|
[2087] | 182 | |
---|
| 183 | /** |
---|
| 184 | @brief Adds a Template to the object. |
---|
| 185 | @param name The name of the Template |
---|
| 186 | */ |
---|
| 187 | void BaseObject::addTemplate(const std::string& name) |
---|
| 188 | { |
---|
| 189 | Template* temp = Template::getTemplate(name); |
---|
| 190 | if (temp) |
---|
| 191 | this->addTemplate(temp); |
---|
| 192 | else |
---|
| 193 | COUT(1) << "Error: \"" << name << "\" is not a valid Template name (in class: " << this->getIdentifier()->getName() << ", name: " << this->getName() << ")." << std::endl; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | @brief Adds a Template to the object. |
---|
| 198 | @param temp The Template |
---|
| 199 | */ |
---|
| 200 | void BaseObject::addTemplate(Template* temp) |
---|
| 201 | { |
---|
| 202 | this->templates_.insert(temp); |
---|
| 203 | temp->applyOn(this); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | /** |
---|
| 207 | @brief Returns the Template with the given index. |
---|
| 208 | @param index The index |
---|
| 209 | */ |
---|
| 210 | Template* BaseObject::getTemplate(unsigned int index) const |
---|
| 211 | { |
---|
| 212 | unsigned int i = 0; |
---|
| 213 | for (std::set<Template*>::const_iterator it = this->templates_.begin(); it != this->templates_.end(); ++it) |
---|
| 214 | { |
---|
| 215 | if (i == index) |
---|
| 216 | return (*it); |
---|
| 217 | i++; |
---|
| 218 | } |
---|
| 219 | return 0; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | void BaseObject::addEvent(BaseObject* event, const std::string& sectionname) |
---|
| 223 | { |
---|
| 224 | event->registerEventListener(this, sectionname); |
---|
| 225 | this->events_.push_back(event); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | void BaseObject::removeEvent(BaseObject* event) |
---|
| 229 | { |
---|
| 230 | this->events_.remove(event); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | BaseObject* BaseObject::getEvent(unsigned int index) const |
---|
| 234 | { |
---|
| 235 | unsigned int i = 0; |
---|
| 236 | for (std::list<BaseObject*>::const_iterator it = this->events_.begin(); it != this->events_.end(); ++it) |
---|
| 237 | { |
---|
| 238 | if (i == index) |
---|
| 239 | return (*it); |
---|
| 240 | ++i; |
---|
| 241 | } |
---|
| 242 | return 0; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | void BaseObject::addEventContainer(const std::string& sectionname, EventContainer* container) |
---|
| 246 | { |
---|
| 247 | std::map<std::string, EventContainer*>::const_iterator it = this->eventContainers_.find(sectionname); |
---|
| 248 | if (it != this->eventContainers_.end()) |
---|
| 249 | { |
---|
| 250 | COUT(2) << "Warning: Overwriting EventContainer in class " << this->getIdentifier()->getName() << "." << std::endl; |
---|
| 251 | delete (it->second); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | this->eventContainers_[sectionname] = container; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | EventContainer* BaseObject::getEventContainer(const std::string& sectionname) const |
---|
| 258 | { |
---|
| 259 | std::map<std::string, EventContainer*>::const_iterator it = this->eventContainers_.find(sectionname); |
---|
| 260 | if (it != this->eventContainers_.end()) |
---|
| 261 | return ((*it).second); |
---|
| 262 | else |
---|
| 263 | return 0; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | void BaseObject::fireEvent() |
---|
| 267 | { |
---|
| 268 | this->fireEvent(true); |
---|
| 269 | this->fireEvent(false); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | void BaseObject::fireEvent(bool activate) |
---|
| 273 | { |
---|
| 274 | this->fireEvent(activate, this); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | void BaseObject::fireEvent(bool activate, BaseObject* originator) |
---|
| 278 | { |
---|
| 279 | Event event(activate, originator); |
---|
| 280 | |
---|
| 281 | for (std::map<BaseObject*, std::string>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it) |
---|
| 282 | { |
---|
| 283 | event.sectionname_ = it->second; |
---|
| 284 | it->first->processEvent(event); |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | void BaseObject::fireEvent(Event& event) |
---|
| 289 | { |
---|
| 290 | for (std::map<BaseObject*, std::string>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it) |
---|
| 291 | it->first->processEvent(event); |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | void BaseObject::processEvent(Event& event) |
---|
| 295 | { |
---|
[3196] | 296 | ORXONOX_SET_EVENT(BaseObject, "activity", setActive, event); |
---|
| 297 | ORXONOX_SET_EVENT(BaseObject, "visibility", setVisible, event); |
---|
[2087] | 298 | } |
---|
[2662] | 299 | |
---|
| 300 | void BaseObject::setMainStateName(const std::string& name) |
---|
| 301 | { |
---|
| 302 | if (this->mainStateName_ != name) |
---|
| 303 | { |
---|
| 304 | this->mainStateName_ = name; |
---|
| 305 | if (this->functorSetMainState_) |
---|
| 306 | delete this->functorSetMainState_; |
---|
| 307 | if (this->functorGetMainState_) |
---|
| 308 | delete this->functorGetMainState_; |
---|
| 309 | this->changedMainState(); |
---|
| 310 | if (!this->functorSetMainState_) |
---|
| 311 | COUT(2) << "Warning: \"" << name << "\" is not a valid MainState." << std::endl; |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | void BaseObject::setMainState(bool state) |
---|
| 316 | { |
---|
| 317 | if (this->functorSetMainState_) |
---|
| 318 | (*this->functorSetMainState_)(state); |
---|
| 319 | else |
---|
| 320 | COUT(2) << "Warning: No MainState defined in object \"" << this->getName() << "\" (" << this->getIdentifier()->getName() << ")" << std::endl; |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | bool BaseObject::getMainState() const |
---|
| 324 | { |
---|
| 325 | if (this->functorGetMainState_) |
---|
| 326 | { |
---|
| 327 | (*this->functorGetMainState_)(); |
---|
| 328 | return this->functorGetMainState_->getReturnvalue(); |
---|
| 329 | } |
---|
| 330 | else |
---|
| 331 | { |
---|
| 332 | COUT(2) << "Warning: No MainState defined in object \"" << this->getName() << "\" (" << this->getIdentifier()->getName() << ")" << std::endl; |
---|
| 333 | return false; |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | void BaseObject::changedMainState() |
---|
| 338 | { |
---|
| 339 | SetMainState(BaseObject, "activity", setActive, isActive); |
---|
| 340 | SetMainState(BaseObject, "visibility", setVisible, isVisible); |
---|
| 341 | } |
---|
[1505] | 342 | } |
---|