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" |
---|
35 | |
---|
36 | #include <tinyxml/tinyxml.h> |
---|
37 | |
---|
38 | #include "CoreIncludes.h" |
---|
39 | #include "Event.h" |
---|
40 | #include "EventIncludes.h" |
---|
41 | #include "Template.h" |
---|
42 | #include "XMLFile.h" |
---|
43 | #include "XMLNameListener.h" |
---|
44 | #include "XMLPort.h" |
---|
45 | #include "command/Functor.h" |
---|
46 | #include "object/Iterator.h" |
---|
47 | |
---|
48 | #include "class/OrxonoxInterface.h" // we include this only to include OrxonoxInterface.h at least once in core to keep MSVC happy... |
---|
49 | |
---|
50 | namespace orxonox |
---|
51 | { |
---|
52 | CreateFactory(BaseObject); |
---|
53 | |
---|
54 | /** |
---|
55 | @brief Constructor: Registers the object in the BaseObject-list. |
---|
56 | */ |
---|
57 | BaseObject::BaseObject(BaseObject* creator) : bInitialized_(false) |
---|
58 | { |
---|
59 | RegisterRootObject(BaseObject); |
---|
60 | |
---|
61 | this->bInitialized_ = true; |
---|
62 | |
---|
63 | this->bActive_ = true; |
---|
64 | this->bVisible_ = true; |
---|
65 | this->oldGametype_ = 0; |
---|
66 | this->bRegisteredEventStates_ = false; |
---|
67 | |
---|
68 | this->lastLoadedXMLElement_ = 0; |
---|
69 | |
---|
70 | this->mainStateFunctor_ = 0; |
---|
71 | |
---|
72 | this->setCreator(creator); |
---|
73 | if (this->creator_) |
---|
74 | { |
---|
75 | this->setFile(this->creator_->getFile()); |
---|
76 | this->setNamespace(this->creator_->getNamespace()); |
---|
77 | this->setScene(this->creator_->getScene(), this->creator_->getSceneID()); |
---|
78 | this->setGametype(this->creator_->getGametype()); |
---|
79 | this->setLevel(this->creator_->getLevel()); |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | this->file_ = 0; |
---|
84 | this->namespace_ = 0; |
---|
85 | this->scene_ = 0; |
---|
86 | this->sceneID_ = OBJECTID_UNKNOWN; |
---|
87 | this->gametype_ = 0; |
---|
88 | this->level_ = 0; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | @brief Destructor |
---|
94 | */ |
---|
95 | BaseObject::~BaseObject() |
---|
96 | { |
---|
97 | if (this->isInitialized()) |
---|
98 | { |
---|
99 | for (std::map<BaseObject*, std::string>::const_iterator it = this->eventSources_.begin(); it != this->eventSources_.end(); ) |
---|
100 | this->removeEventSource((it++)->first); |
---|
101 | |
---|
102 | for (std::set<BaseObject*>::const_iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ) |
---|
103 | (*(it++))->removeEventSource(this); |
---|
104 | |
---|
105 | for (std::map<std::string, EventState*>::const_iterator it = this->eventStates_.begin(); it != this->eventStates_.end(); ++it) |
---|
106 | delete it->second; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | /** @brief Adds an object which listens to the events of this object. */ |
---|
111 | void BaseObject::registerEventListener(BaseObject* object) |
---|
112 | { |
---|
113 | orxout(verbose, context::events) << "New EventListener: " << object->getIdentifier()->getName() << " &(" << object << ")." << endl; |
---|
114 | this->eventListeners_.insert(object); |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | @brief XML loading and saving. |
---|
119 | @param xmlelement The XML-element |
---|
120 | @param mode The mode defines the operation that is being executed: loading or saving the object (from or to XML respectively) |
---|
121 | */ |
---|
122 | void BaseObject::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
123 | { |
---|
124 | XMLPortParam(BaseObject, "name", setXMLName, getName, xmlelement, mode); |
---|
125 | XMLPortParam(BaseObject, "visible", setVisible, isVisible, xmlelement, mode); |
---|
126 | XMLPortParam(BaseObject, "active", setActive, isActive, xmlelement, mode); |
---|
127 | XMLPortParam(BaseObject, "mainstate", setMainStateName, getMainStateName, xmlelement, mode); |
---|
128 | XMLPortParamTemplate(BaseObject, "template", addTemplate, getSingleTemplate, xmlelement, mode, const std::string&); |
---|
129 | |
---|
130 | XMLPortObjectTemplate(BaseObject, Template, "templates", addTemplate, getTemplate, xmlelement, mode, Template*); |
---|
131 | XMLPortObject(BaseObject, BaseObject, "eventlisteners", addEventListener, getEventListener, xmlelement, mode); |
---|
132 | |
---|
133 | Element* events = 0; |
---|
134 | if (mode == XMLPort::LoadObject || mode == XMLPort::ExpandObject) |
---|
135 | events = xmlelement.FirstChildElement("events", false); |
---|
136 | else if (mode == XMLPort::SaveObject) |
---|
137 | {} |
---|
138 | if (events) |
---|
139 | this->XMLEventPort(*events, mode); |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | @brief Defines the possible event states of this object and parses eventsources from an XML file. |
---|
144 | @param xmlelement The XML-element |
---|
145 | @param mode The mode defines the operation that is being executed: loading or saving the object (from or to XML respectively) |
---|
146 | */ |
---|
147 | void BaseObject::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
148 | { |
---|
149 | XMLPortEventState(BaseObject, BaseObject, "activity", setActive, xmlelement, mode); |
---|
150 | XMLPortEventState(BaseObject, BaseObject, "visibility", setVisible, xmlelement, mode); |
---|
151 | XMLPortEventState(BaseObject, BaseObject, "mainstate", setMainState, xmlelement, mode); |
---|
152 | |
---|
153 | this->bRegisteredEventStates_ = true; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | @brief Loads the name of the object through XML and calls all XMLNameListener. |
---|
158 | @param name The name of the object |
---|
159 | */ |
---|
160 | void BaseObject::setXMLName(const std::string& name) |
---|
161 | { |
---|
162 | this->setName(name); |
---|
163 | |
---|
164 | for (ObjectList<XMLNameListener>::iterator it = ObjectList<XMLNameListener>::begin(); it != ObjectList<XMLNameListener>::end(); ++it) |
---|
165 | it->loadedNewXMLName(this); |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | @brief Returns the levelfile that loaded this object. |
---|
170 | @return The levelfile |
---|
171 | */ |
---|
172 | const std::string& BaseObject::getFilename() const |
---|
173 | { |
---|
174 | if (this->file_) |
---|
175 | return this->file_->getFilename(); |
---|
176 | else |
---|
177 | return BLANKSTRING; |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | @brief Adds a Template to the object. |
---|
182 | @param name The name of the Template |
---|
183 | */ |
---|
184 | void BaseObject::addTemplate(const std::string& name) |
---|
185 | { |
---|
186 | Template* temp = Template::getTemplate(name); |
---|
187 | if (temp) |
---|
188 | this->addTemplate(temp); |
---|
189 | else |
---|
190 | orxout(internal_error) << "\"" << name << "\" is not a valid Template name (in class: " << this->getIdentifier()->getName() << ", name: " << this->getName() << ")." << endl; |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | @brief Adds a Template to the object. |
---|
195 | @param temp The Template |
---|
196 | */ |
---|
197 | void BaseObject::addTemplate(Template* temp) |
---|
198 | { |
---|
199 | // network |
---|
200 | if (temp->isLink()) |
---|
201 | { |
---|
202 | this->networkTemplateNames_.insert(temp->getLink()); |
---|
203 | |
---|
204 | Template* link; |
---|
205 | assert(!(link = Template::getTemplate(temp->getLink())) || !link->isLink()); |
---|
206 | link = NULL; |
---|
207 | } |
---|
208 | else |
---|
209 | this->networkTemplateNames_.insert(temp->getName()); |
---|
210 | |
---|
211 | // add template |
---|
212 | this->templates_.insert(temp); |
---|
213 | temp->applyOn(this); |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | @brief Returns the name of the first Template. |
---|
218 | @return The name as string. |
---|
219 | */ |
---|
220 | const std::string& BaseObject::getSingleTemplate(void) const |
---|
221 | { |
---|
222 | if(this->templates_.empty()) |
---|
223 | return BLANKSTRING; |
---|
224 | |
---|
225 | return (*this->templates_.begin())->getName(); |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | @brief Returns the Template with the given index. |
---|
230 | @param index The index |
---|
231 | */ |
---|
232 | Template* BaseObject::getTemplate(unsigned int index) const |
---|
233 | { |
---|
234 | unsigned int i = 0; |
---|
235 | for (std::set<Template*>::const_iterator it = this->templates_.begin(); it != this->templates_.end(); ++it) |
---|
236 | { |
---|
237 | if (i == index) |
---|
238 | return (*it); |
---|
239 | i++; |
---|
240 | } |
---|
241 | return 0; |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | @brief Adds a new event source for a specific state. |
---|
246 | @param source The object which sends events to this object |
---|
247 | @param state The state of this object which will be affected by the events |
---|
248 | */ |
---|
249 | void BaseObject::addEventSource(BaseObject* source, const std::string& state) |
---|
250 | { |
---|
251 | this->eventSources_[source] = state; |
---|
252 | source->registerEventListener(this); |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | @brief Removes an eventsource (but doesn't unregister itself at the source). |
---|
257 | */ |
---|
258 | void BaseObject::removeEventSource(BaseObject* source) |
---|
259 | { |
---|
260 | this->eventSources_.erase(source); |
---|
261 | source->unregisterEventListener(this); |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | @brief Returns an eventsource with a given index. |
---|
266 | */ |
---|
267 | BaseObject* BaseObject::getEventSource(unsigned int index, const std::string& state) const |
---|
268 | { |
---|
269 | unsigned int i = 0; |
---|
270 | for (std::map<BaseObject*, std::string>::const_iterator it = this->eventSources_.begin(); it != this->eventSources_.end(); ++it) |
---|
271 | { |
---|
272 | if (it->second != state) |
---|
273 | continue; |
---|
274 | |
---|
275 | if (i == index) |
---|
276 | return it->first; |
---|
277 | ++i; |
---|
278 | } |
---|
279 | return 0; |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | @brief Adds an object which listens to the events of this object. The events are sent to the other objects mainstate. |
---|
284 | */ |
---|
285 | void BaseObject::addEventListener(BaseObject* listener) |
---|
286 | { |
---|
287 | this->eventListenersXML_.insert(listener); |
---|
288 | listener->addEventSource(this, "mainstate"); |
---|
289 | } |
---|
290 | |
---|
291 | /** |
---|
292 | @brief Returns an event listener with a given index. |
---|
293 | */ |
---|
294 | BaseObject* BaseObject::getEventListener(unsigned int index) const |
---|
295 | { |
---|
296 | unsigned int i = 0; |
---|
297 | for (std::set<BaseObject*>::const_iterator it = this->eventListenersXML_.begin(); it != this->eventListenersXML_.end(); ++it) |
---|
298 | { |
---|
299 | if (i == index) |
---|
300 | return *it; |
---|
301 | ++i; |
---|
302 | } |
---|
303 | return 0; |
---|
304 | } |
---|
305 | |
---|
306 | /** |
---|
307 | @brief Adds a new event-state to the object. Event-states are states which can be changed by events. |
---|
308 | @param name The name of the event |
---|
309 | @param state The object containing information about the event-state |
---|
310 | */ |
---|
311 | void BaseObject::addEventState(const std::string& name, EventState* state) |
---|
312 | { |
---|
313 | std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(name); |
---|
314 | if (it != this->eventStates_.end()) |
---|
315 | { |
---|
316 | orxout(internal_warning, context::events) << "Overwriting EventState in class " << this->getIdentifier()->getName() << '.' << endl; |
---|
317 | delete (it->second); |
---|
318 | } |
---|
319 | |
---|
320 | this->eventStates_[name] = state; |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | @brief Returns the event-state with the given name. |
---|
325 | */ |
---|
326 | EventState* BaseObject::getEventState(const std::string& name) const |
---|
327 | { |
---|
328 | std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(name); |
---|
329 | if (it != this->eventStates_.end()) |
---|
330 | return (it->second); |
---|
331 | else |
---|
332 | return 0; |
---|
333 | } |
---|
334 | |
---|
335 | /** |
---|
336 | @brief Fires an event (without a state). |
---|
337 | */ |
---|
338 | void BaseObject::fireEvent(const std::string& name) |
---|
339 | { |
---|
340 | this->fireEvent(true, name); |
---|
341 | this->fireEvent(false, name); |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | @brief Fires an event which activates or deactivates a state. |
---|
346 | */ |
---|
347 | void BaseObject::fireEvent(bool activate, const std::string& name) |
---|
348 | { |
---|
349 | this->fireEvent(activate, this, name); |
---|
350 | } |
---|
351 | |
---|
352 | /** |
---|
353 | @brief Fires an event which activates or deactivates a state with agiven originator (the object which triggered the event). |
---|
354 | */ |
---|
355 | void BaseObject::fireEvent(bool activate, BaseObject* originator, const std::string& name) |
---|
356 | { |
---|
357 | Event event(activate, originator, name); |
---|
358 | |
---|
359 | for (std::set<BaseObject*>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it) |
---|
360 | { |
---|
361 | event.statename_ = (*it)->eventSources_[this]; |
---|
362 | (*it)->processEvent(event); |
---|
363 | } |
---|
364 | } |
---|
365 | |
---|
366 | /** |
---|
367 | @brief Fires an event, using the Event struct. |
---|
368 | */ |
---|
369 | void BaseObject::fireEvent(Event& event) |
---|
370 | { |
---|
371 | for (std::set<BaseObject*>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it) |
---|
372 | (*it)->processEvent(event); |
---|
373 | } |
---|
374 | |
---|
375 | /** |
---|
376 | @brief Processing an event by calling the right main state. |
---|
377 | @param event The event struct which contains the information about the event |
---|
378 | */ |
---|
379 | void BaseObject::processEvent(Event& event) |
---|
380 | { |
---|
381 | this->registerEventStates(); |
---|
382 | |
---|
383 | orxout(verbose, context::events) << this->getIdentifier()->getName() << " (&" << this << ") processing event. originator: " << event.originator_->getIdentifier()->getName() << " (&" << event.originator_ << "), activate: " << event.activate_ << ", name: " << event.name_ << ", statename: " << event.statename_ << "." << endl; |
---|
384 | |
---|
385 | std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(event.statename_); |
---|
386 | if (it != this->eventStates_.end()) |
---|
387 | it->second->process(event, this); |
---|
388 | else if (!event.statename_.empty()) |
---|
389 | orxout(internal_warning, context::events) << "\"" << event.statename_ << "\" is not a valid state in object \"" << this->getName() << "\" of class " << this->getIdentifier()->getName() << "." << endl; |
---|
390 | else |
---|
391 | orxout(internal_warning, context::events) << "Event with invalid source sent to object \"" << this->getName() << "\" of class " << this->getIdentifier()->getName() << "." << endl; |
---|
392 | } |
---|
393 | |
---|
394 | /** |
---|
395 | @brief Sets the main state of the object to a given boolean value. |
---|
396 | |
---|
397 | Note: The main state of an object can be set with the @ref setMainStateName function. |
---|
398 | It's part of the eventsystem and used for event forwarding (when the target object can't specify a specific state, |
---|
399 | the main state is used by default). |
---|
400 | */ |
---|
401 | void BaseObject::setMainState(bool state) |
---|
402 | { |
---|
403 | if (this->mainStateFunctor_) |
---|
404 | { |
---|
405 | if (this->mainStateFunctor_->getParamCount() == 0) |
---|
406 | { |
---|
407 | if (state) |
---|
408 | (*this->mainStateFunctor_)(); |
---|
409 | } |
---|
410 | else |
---|
411 | { |
---|
412 | (*this->mainStateFunctor_)(state); |
---|
413 | } |
---|
414 | } |
---|
415 | else |
---|
416 | orxout(internal_warning, context::events) << "No MainState defined in object \"" << this->getName() << "\" (" << this->getIdentifier()->getName() << ")" << endl; |
---|
417 | } |
---|
418 | |
---|
419 | /** |
---|
420 | @brief This function gets called if the main state name of the object changes. |
---|
421 | */ |
---|
422 | void BaseObject::changedMainStateName() |
---|
423 | { |
---|
424 | this->mainStateFunctor_ = 0; |
---|
425 | |
---|
426 | if (!this->mainStateName_.empty()) |
---|
427 | { |
---|
428 | this->registerEventStates(); |
---|
429 | |
---|
430 | std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(this->mainStateName_); |
---|
431 | if (it != this->eventStates_.end() && it->second->getFunctor()) |
---|
432 | { |
---|
433 | if (it->second->getFunctor()->getParamCount() <= 1) |
---|
434 | this->mainStateFunctor_ = it->second->getFunctor(); |
---|
435 | else |
---|
436 | orxout(internal_warning, context::events) << "Can't use \"" << this->mainStateName_ << "\" as MainState because it needs a second argument." << endl; |
---|
437 | } |
---|
438 | else |
---|
439 | orxout(internal_warning, context::events) << "\"" << this->mainStateName_ << "\" is not a valid MainState." << endl; |
---|
440 | } |
---|
441 | } |
---|
442 | |
---|
443 | /** |
---|
444 | @brief Calls XMLEventPort with an empty XML-element to register the event states if necessary. |
---|
445 | */ |
---|
446 | void BaseObject::registerEventStates() |
---|
447 | { |
---|
448 | if (!this->bRegisteredEventStates_) |
---|
449 | { |
---|
450 | Element xmlelement; |
---|
451 | this->XMLEventPort(xmlelement, XMLPort::NOP); |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | /** |
---|
456 | @brief Manually loads all event states, even if the class doesn't officially support them. This is needed by some classes like @ref EventDispatcher or @ref EventTarget. |
---|
457 | */ |
---|
458 | void BaseObject::loadAllEventStates(Element& xmlelement, XMLPort::Mode mode, BaseObject* object, Identifier* identifier) |
---|
459 | { |
---|
460 | Element* events = xmlelement.FirstChildElement("events", false); |
---|
461 | if (events) |
---|
462 | { |
---|
463 | // get the list of all states present |
---|
464 | std::list<std::string> eventnames; |
---|
465 | if (mode == XMLPort::LoadObject || mode == XMLPort::ExpandObject) |
---|
466 | { |
---|
467 | for (ticpp::Iterator<ticpp::Element> child = events->FirstChildElement(false); child != child.end(); child++) |
---|
468 | eventnames.push_back(child->Value()); |
---|
469 | } |
---|
470 | else if (mode == XMLPort::SaveObject) |
---|
471 | { |
---|
472 | } |
---|
473 | |
---|
474 | // iterate through all states and get the event sources |
---|
475 | for (std::list<std::string>::iterator it = eventnames.begin(); it != eventnames.end(); ++it) |
---|
476 | { |
---|
477 | const std::string& statename = (*it); |
---|
478 | |
---|
479 | // if the event state is already known, continue with the next state |
---|
480 | orxonox::EventState* eventstate = object->getEventState(statename); |
---|
481 | if (eventstate) |
---|
482 | continue; |
---|
483 | |
---|
484 | XMLPortClassObjectContainer<BaseObject, BaseObject>* container = (XMLPortClassObjectContainer<BaseObject, BaseObject>*)(identifier->getXMLPortObjectContainer(statename)); |
---|
485 | if (!container) |
---|
486 | { |
---|
487 | const ExecutorMemberPtr<BaseObject>& setfunctor = createExecutor(createFunctor(&BaseObject::addEventSource), std::string( "BaseObject" ) + "::" + "addEventSource" + '(' + statename + ')'); |
---|
488 | const ExecutorMemberPtr<BaseObject>& getfunctor = createExecutor(createFunctor(&BaseObject::getEventSource), std::string( "BaseObject" ) + "::" + "getEventSource" + '(' + statename + ')'); |
---|
489 | setfunctor->setDefaultValue(1, statename); |
---|
490 | getfunctor->setDefaultValue(1, statename); |
---|
491 | |
---|
492 | container = new XMLPortClassObjectContainer<BaseObject, BaseObject>(statename, identifier, setfunctor, getfunctor, false, true); |
---|
493 | identifier->addXMLPortObjectContainer(statename, container); |
---|
494 | } |
---|
495 | container->port(object, *events, mode); |
---|
496 | } |
---|
497 | } |
---|
498 | } |
---|
499 | } |
---|