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 "EventFilter.h" |
---|
30 | |
---|
31 | #include "core/CoreIncludes.h" |
---|
32 | #include "core/XMLPort.h" |
---|
33 | #include "EventName.h" |
---|
34 | |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | CreateFactory(EventFilter); |
---|
38 | |
---|
39 | EventFilter::EventFilter(BaseObject* creator) : BaseObject(creator) |
---|
40 | { |
---|
41 | RegisterObject(EventFilter); |
---|
42 | |
---|
43 | this->bActive_ = false; |
---|
44 | } |
---|
45 | |
---|
46 | EventFilter::~EventFilter() |
---|
47 | { |
---|
48 | for (std::list<EventName*>::const_iterator it = this->names_.begin(); it != this->names_.end(); ) |
---|
49 | (*(it++))->destroy(); |
---|
50 | } |
---|
51 | |
---|
52 | void EventFilter::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
53 | { |
---|
54 | SUPER(EventFilter, XMLPort, xmlelement, mode); |
---|
55 | |
---|
56 | XMLPortObject(EventFilter, BaseObject, "", addFilterSource, getFilterSource, xmlelement, mode); |
---|
57 | XMLPortObject(EventFilter, EventName, "names", addEventName, getEventName, xmlelement, mode); |
---|
58 | } |
---|
59 | |
---|
60 | void EventFilter::processEvent(Event& event) |
---|
61 | { |
---|
62 | if (this->bActive_) |
---|
63 | { |
---|
64 | COUT(2) << "Warning: Detected Event loop in EventFilter \"" << this->getName() << '"' << std::endl; |
---|
65 | return; |
---|
66 | } |
---|
67 | |
---|
68 | if (this->names_.size() > 0) |
---|
69 | { |
---|
70 | bool success = false; |
---|
71 | for (std::list<EventName*>::const_iterator it = this->names_.begin(); it != this->names_.end(); ++it) |
---|
72 | { |
---|
73 | if ((*it)->getName() == event.name_) |
---|
74 | { |
---|
75 | success = true; |
---|
76 | break; |
---|
77 | } |
---|
78 | } |
---|
79 | if (!success) |
---|
80 | return; |
---|
81 | } |
---|
82 | |
---|
83 | this->bActive_ = true; |
---|
84 | this->fireEvent(event.activate_, event.originator_, event.name_); |
---|
85 | this->bActive_ = false; |
---|
86 | } |
---|
87 | |
---|
88 | void EventFilter::addFilterSource(BaseObject* source) |
---|
89 | { |
---|
90 | this->sources_.push_back(source); |
---|
91 | this->addEventSource(source, ""); |
---|
92 | } |
---|
93 | |
---|
94 | BaseObject* EventFilter::getFilterSource(unsigned int index) const |
---|
95 | { |
---|
96 | unsigned int i = 0; |
---|
97 | for (std::list<BaseObject*>::const_iterator it = this->sources_.begin(); it != this->sources_.end(); ++it) |
---|
98 | { |
---|
99 | if (i == index) |
---|
100 | return (*it); |
---|
101 | ++i; |
---|
102 | } |
---|
103 | return 0; |
---|
104 | } |
---|
105 | |
---|
106 | void EventFilter::addEventName(EventName* eventname) |
---|
107 | { |
---|
108 | this->names_.push_back(eventname); |
---|
109 | } |
---|
110 | |
---|
111 | EventName* EventFilter::getEventName(unsigned int index) const |
---|
112 | { |
---|
113 | unsigned int i = 0; |
---|
114 | for (std::list<EventName*>::const_iterator it = this->names_.begin(); it != this->names_.end(); ++it) |
---|
115 | { |
---|
116 | if (i == index) |
---|
117 | return (*it); |
---|
118 | ++i; |
---|
119 | } |
---|
120 | return 0; |
---|
121 | } |
---|
122 | } |
---|