Changeset 5774 for code/trunk/src/libraries/core/BaseObject.cc
- Timestamp:
- Sep 23, 2009, 11:31:02 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/libraries/core/BaseObject.cc
r5738 r5774 33 33 34 34 #include "BaseObject.h" 35 36 #include <tinyxml/tinyxml.h>37 38 #include "util/StringUtils.h"39 35 #include "CoreIncludes.h" 40 #include "Event.h"41 #include "EventIncludes.h"42 #include "Functor.h"43 #include "Iterator.h"44 #include "Template.h"45 #include "XMLFile.h"46 #include "XMLNameListener.h"47 #include "XMLPort.h"48 36 49 37 namespace orxonox … … 59 47 60 48 this->bInitialized_ = true; 61 62 this->bActive_ = true;63 this->bVisible_ = true;64 this->oldGametype_ = 0;65 66 this->lastLoadedXMLElement_ = 0;67 68 this->functorSetMainState_ = 0;69 this->functorGetMainState_ = 0;70 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 else80 {81 this->file_ = 0;82 this->namespace_ = 0;83 this->scene_ = 0;84 this->gametype_ = 0;85 }86 49 } 87 50 … … 93 56 if (this->isInitialized()) 94 57 { 95 for (std::list<BaseObject*>::const_iterator it = this->events_.begin(); it != this->events_.end(); ++it)96 (*it)->unregisterEventListener(this);97 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 58 } 106 59 } 107 108 /**109 @brief XML loading and saving.110 @param xmlelement The XML-element111 @param loading Loading (true) or saving (false)112 @return The XML-element113 */114 void BaseObject::XMLPort(Element& xmlelement, XMLPort::Mode mode)115 {116 XMLPortParam(BaseObject, "name", setXMLName, getName, xmlelement, mode);117 XMLPortParam(BaseObject, "visible", setVisible, isVisible, xmlelement, mode);118 XMLPortParam(BaseObject, "active", setActive, isActive, xmlelement, mode);119 XMLPortParam(BaseObject, "mainstate", setMainStateName, getMainStateName, xmlelement, mode);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 129 if (mode == XMLPort::LoadObject || mode == XMLPort::ExpandObject)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 }157 }158 159 /**160 @brief Loads the name of the object through XML and calls all XMLNameListener.161 @param name The name of the object162 */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 /**172 @brief Returns the levelfile that loaded this object.173 @return The levelfile174 */175 const std::string& BaseObject::getFilename() const176 {177 if (this->file_)178 return this->file_->getFilename();179 else180 return BLANKSTRING;181 }182 183 /**184 @brief Adds a Template to the object.185 @param name The name of the Template186 */187 void BaseObject::addTemplate(const std::string& name)188 {189 Template* temp = Template::getTemplate(name);190 if (temp)191 this->addTemplate(temp);192 else193 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 Template199 */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 index209 */210 Template* BaseObject::getTemplate(unsigned int index) const211 {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) const234 {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) const258 {259 std::map<std::string, EventContainer*>::const_iterator it = this->eventContainers_.find(sectionname);260 if (it != this->eventContainers_.end())261 return ((*it).second);262 else263 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 {296 ORXONOX_SET_EVENT(BaseObject, "activity", setActive, event);297 ORXONOX_SET_EVENT(BaseObject, "visibility", setVisible, event);298 }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 else320 COUT(2) << "Warning: No MainState defined in object \"" << this->getName() << "\" (" << this->getIdentifier()->getName() << ")" << std::endl;321 }322 323 bool BaseObject::getMainState() const324 {325 if (this->functorGetMainState_)326 {327 (*this->functorGetMainState_)();328 return this->functorGetMainState_->getReturnvalue();329 }330 else331 {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 }342 60 }
Note: See TracChangeset
for help on using the changeset viewer.