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