comparison engine/core/eventchannel/eventmanager.cpp @ 428:a1884665aa95

Added the ability to add an event listener to the front of the event listener deque in the event manager. fixes[t:454]
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 22 Feb 2010 21:23:01 +0000
parents bb9902910067
children b312d170ab0c
comparison
equal deleted inserted replaced
427:68aa65e5bd9c 428:a1884665aa95
54 54
55 EventManager::~EventManager() { 55 EventManager::~EventManager() {
56 } 56 }
57 57
58 template<typename T> 58 template<typename T>
59 void removeListener(std::vector<T>& vec, T& listener) { 59 void removeListener(std::deque<T>& vec, T& listener) {
60 vec.push_back(listener); 60 vec.push_back(listener);
61 } 61 }
62 62
63 template<typename T> 63 template<typename T>
64 void addListener(std::vector<T>& vec, T& listener) { 64 void addListener(std::deque<T>& vec, T& listener) {
65 vec.push_back(listener); 65 vec.push_back(listener);
66 } 66 }
67 67
68 void EventManager::addCommandListener(ICommandListener* listener) { 68 void EventManager::addCommandListener(ICommandListener* listener) {
69 addListener<ICommandListener*>(m_pending_commandlisteners, listener); 69 addListener<ICommandListener*>(m_pending_commandlisteners, listener);
70 } 70 }
71 71
72 void EventManager::addCommandListenerFront(ICommandListener* listener) {
73 addListener<ICommandListener*>(m_pending_commandlisteners, listener);
74 }
75
72 void EventManager::removeCommandListener(ICommandListener* listener) { 76 void EventManager::removeCommandListener(ICommandListener* listener) {
73 removeListener<ICommandListener*>(m_pending_cldeletions, listener); 77 removeListener<ICommandListener*>(m_pending_cldeletions, listener);
74 } 78 }
75 79
76 void EventManager::addKeyListener(IKeyListener* listener) { 80 void EventManager::addKeyListener(IKeyListener* listener) {
77 addListener<IKeyListener*>(m_pending_keylisteners, listener); 81 addListener<IKeyListener*>(m_pending_keylisteners, listener);
78 } 82 }
79 83
84 void EventManager::addKeyListenerFront(IKeyListener* listener) {
85 addListener<IKeyListener*>(m_pending_keylisteners_front, listener);
86 }
87
80 void EventManager::removeKeyListener(IKeyListener* listener) { 88 void EventManager::removeKeyListener(IKeyListener* listener) {
81 removeListener<IKeyListener*>(m_pending_kldeletions, listener); 89 removeListener<IKeyListener*>(m_pending_kldeletions, listener);
82 } 90 }
83 91
84 void EventManager::addMouseListener(IMouseListener* listener) { 92 void EventManager::addMouseListener(IMouseListener* listener) {
85 addListener<IMouseListener*>(m_pending_mouselisteners, listener); 93 addListener<IMouseListener*>(m_pending_mouselisteners, listener);
86 } 94 }
87 95
96 void EventManager::addMouseListenerFront(IMouseListener* listener) {
97 addListener<IMouseListener*>(m_pending_mouselisteners, listener);
98 }
99
88 void EventManager::removeMouseListener(IMouseListener* listener) { 100 void EventManager::removeMouseListener(IMouseListener* listener) {
89 removeListener<IMouseListener*>(m_pending_mldeletions, listener); 101 removeListener<IMouseListener*>(m_pending_mldeletions, listener);
90 } 102 }
91 103
92 void EventManager::addSdlEventListener(ISdlEventListener* listener) { 104 void EventManager::addSdlEventListener(ISdlEventListener* listener) {
93 addListener<ISdlEventListener*>(m_pending_sdleventlisteners, listener); 105 addListener<ISdlEventListener*>(m_pending_sdleventlisteners, listener);
94 } 106 }
95 107
108 void EventManager::addSdlEventListenerFront(ISdlEventListener* listener) {
109 addListener<ISdlEventListener*>(m_pending_sdleventlisteners, listener);
110 }
111
96 void EventManager::removeSdlEventListener(ISdlEventListener* listener) { 112 void EventManager::removeSdlEventListener(ISdlEventListener* listener) {
97 removeListener<ISdlEventListener*>(m_pending_sdldeletions, listener); 113 removeListener<ISdlEventListener*>(m_pending_sdldeletions, listener);
98 } 114 }
99 115
100 void EventManager::dispatchCommand(Command& command) { 116 void EventManager::dispatchCommand(Command& command) {
101 if(!m_pending_commandlisteners.empty()) { 117 if(!m_pending_commandlisteners.empty()) {
102 std::vector<ICommandListener*>::iterator i = m_pending_commandlisteners.begin(); 118 std::deque<ICommandListener*>::iterator i = m_pending_commandlisteners.begin();
103 while (i != m_pending_commandlisteners.end()) { 119 while (i != m_pending_commandlisteners.end()) {
104 m_commandlisteners.push_back(*i); 120 m_commandlisteners.push_back(*i);
105 ++i; 121 ++i;
106 } 122 }
107 m_pending_commandlisteners.clear(); 123 m_pending_commandlisteners.clear();
108 } 124 }
109 125
126 if(!m_pending_commandlisteners_front.empty()) {
127 std::deque<ICommandListener*>::iterator i = m_pending_commandlisteners_front.begin();
128 while (i != m_pending_commandlisteners_front.end()) {
129 m_commandlisteners.push_front(*i);
130 ++i;
131 }
132 m_pending_commandlisteners_front.clear();
133 }
134
110 if (!m_pending_cldeletions.empty()) { 135 if (!m_pending_cldeletions.empty()) {
111 std::vector<ICommandListener*>::iterator i = m_pending_cldeletions.begin(); 136 std::deque<ICommandListener*>::iterator i = m_pending_cldeletions.begin();
112 while (i != m_pending_cldeletions.end()) { 137 while (i != m_pending_cldeletions.end()) {
113 std::vector<ICommandListener*>::iterator j = m_commandlisteners.begin(); 138 std::deque<ICommandListener*>::iterator j = m_commandlisteners.begin();
114 while (j != m_commandlisteners.end()) { 139 while (j != m_commandlisteners.end()) {
115 if(*j == *i) { 140 if(*j == *i) {
116 m_commandlisteners.erase(j); 141 m_commandlisteners.erase(j);
117 break; 142 break;
118 } 143 }
121 ++i; 146 ++i;
122 } 147 }
123 m_pending_cldeletions.clear(); 148 m_pending_cldeletions.clear();
124 } 149 }
125 150
126 std::vector<ICommandListener*>::iterator i = m_commandlisteners.begin(); 151 std::deque<ICommandListener*>::iterator i = m_commandlisteners.begin();
127 while (i != m_commandlisteners.end()) { 152 while (i != m_commandlisteners.end()) {
128 (*i)->onCommand(command); 153 (*i)->onCommand(command);
129 if (command.isConsumed()) { 154 if (command.isConsumed()) {
130 break; 155 break;
131 } 156 }
133 } 158 }
134 } 159 }
135 160
136 void EventManager::dispatchKeyEvent(KeyEvent& evt) { 161 void EventManager::dispatchKeyEvent(KeyEvent& evt) {
137 if(!m_pending_keylisteners.empty()) { 162 if(!m_pending_keylisteners.empty()) {
138 std::vector<IKeyListener*>::iterator i = m_pending_keylisteners.begin(); 163 std::deque<IKeyListener*>::iterator i = m_pending_keylisteners.begin();
139 while (i != m_pending_keylisteners.end()) { 164 while (i != m_pending_keylisteners.end()) {
140 m_keylisteners.push_back(*i); 165 m_keylisteners.push_back(*i);
141 ++i; 166 ++i;
142 } 167 }
143 m_pending_keylisteners.clear(); 168 m_pending_keylisteners.clear();
144 } 169 }
145 170
171 if(!m_pending_keylisteners_front.empty()) {
172 std::deque<IKeyListener*>::iterator i = m_pending_keylisteners_front.begin();
173 while (i != m_pending_keylisteners_front.end()) {
174 m_keylisteners.push_front(*i);
175 ++i;
176 }
177 m_pending_keylisteners_front.clear();
178 }
179
146 if (!m_pending_kldeletions.empty()) { 180 if (!m_pending_kldeletions.empty()) {
147 std::vector<IKeyListener*>::iterator i = m_pending_kldeletions.begin(); 181 std::deque<IKeyListener*>::iterator i = m_pending_kldeletions.begin();
148 while (i != m_pending_kldeletions.end()) { 182 while (i != m_pending_kldeletions.end()) {
149 std::vector<IKeyListener*>::iterator j = m_keylisteners.begin(); 183 std::deque<IKeyListener*>::iterator j = m_keylisteners.begin();
150 while (j != m_keylisteners.end()) { 184 while (j != m_keylisteners.end()) {
151 if(*j == *i) { 185 if(*j == *i) {
152 m_keylisteners.erase(j); 186 m_keylisteners.erase(j);
153 break; 187 break;
154 } 188 }
157 ++i; 191 ++i;
158 } 192 }
159 m_pending_kldeletions.clear(); 193 m_pending_kldeletions.clear();
160 } 194 }
161 195
162 std::vector<IKeyListener*>::iterator i = m_keylisteners.begin(); 196 std::deque<IKeyListener*>::iterator i = m_keylisteners.begin();
163 while (i != m_keylisteners.end()) { 197 while (i != m_keylisteners.end()) {
164 switch (evt.getType()) { 198 switch (evt.getType()) {
165 case KeyEvent::PRESSED: 199 case KeyEvent::PRESSED:
166 (*i)->keyPressed(evt); 200 (*i)->keyPressed(evt);
167 break; 201 break;
175 } 209 }
176 } 210 }
177 211
178 void EventManager::dispatchMouseEvent(MouseEvent& evt) { 212 void EventManager::dispatchMouseEvent(MouseEvent& evt) {
179 if(!m_pending_mouselisteners.empty()) { 213 if(!m_pending_mouselisteners.empty()) {
180 std::vector<IMouseListener*>::iterator i = m_pending_mouselisteners.begin(); 214 std::deque<IMouseListener*>::iterator i = m_pending_mouselisteners.begin();
181 while (i != m_pending_mouselisteners.end()) { 215 while (i != m_pending_mouselisteners.end()) {
182 m_mouselisteners.push_back(*i); 216 m_mouselisteners.push_back(*i);
183 ++i; 217 ++i;
184 } 218 }
185 m_pending_mouselisteners.clear(); 219 m_pending_mouselisteners.clear();
186 } 220 }
187 221
222 if(!m_pending_mouselisteners_front.empty()) {
223 std::deque<IMouseListener*>::iterator i = m_pending_mouselisteners_front.begin();
224 while (i != m_pending_mouselisteners_front.end()) {
225 m_mouselisteners.push_front(*i);
226 ++i;
227 }
228 m_pending_mouselisteners_front.clear();
229 }
230
188 if (!m_pending_mldeletions.empty()) { 231 if (!m_pending_mldeletions.empty()) {
189 std::vector<IMouseListener*>::iterator i = m_pending_mldeletions.begin(); 232 std::deque<IMouseListener*>::iterator i = m_pending_mldeletions.begin();
190 while (i != m_pending_mldeletions.end()) { 233 while (i != m_pending_mldeletions.end()) {
191 std::vector<IMouseListener*>::iterator j = m_mouselisteners.begin(); 234 std::deque<IMouseListener*>::iterator j = m_mouselisteners.begin();
192 while (j != m_mouselisteners.end()) { 235 while (j != m_mouselisteners.end()) {
193 if(*j == *i) { 236 if(*j == *i) {
194 m_mouselisteners.erase(j); 237 m_mouselisteners.erase(j);
195 break; 238 break;
196 } 239 }
199 ++i; 242 ++i;
200 } 243 }
201 m_pending_mldeletions.clear(); 244 m_pending_mldeletions.clear();
202 } 245 }
203 246
204 std::vector<IMouseListener*>::iterator i = m_mouselisteners.begin(); 247 std::deque<IMouseListener*>::iterator i = m_mouselisteners.begin();
205 while (i != m_mouselisteners.end()) { 248 while (i != m_mouselisteners.end()) {
206 switch (evt.getType()) { 249 switch (evt.getType()) {
207 case MouseEvent::MOVED: 250 case MouseEvent::MOVED:
208 (*i)->mouseMoved(evt); 251 (*i)->mouseMoved(evt);
209 break; 252 break;
242 } 285 }
243 286
244 bool EventManager::dispatchSdlEvent(SDL_Event& evt) { 287 bool EventManager::dispatchSdlEvent(SDL_Event& evt) {
245 bool ret = false; 288 bool ret = false;
246 if (!m_pending_sdleventlisteners.empty()) { 289 if (!m_pending_sdleventlisteners.empty()) {
247 std::vector<ISdlEventListener*>::iterator i = m_pending_sdleventlisteners.begin(); 290 std::deque<ISdlEventListener*>::iterator i = m_pending_sdleventlisteners.begin();
248 while(i != m_pending_sdleventlisteners.end()) { 291 while(i != m_pending_sdleventlisteners.end()) {
249 m_sdleventlisteners.push_back(*i); 292 m_sdleventlisteners.push_back(*i);
250 ++i; 293 ++i;
251 } 294 }
252 m_pending_sdleventlisteners.clear(); 295 m_pending_sdleventlisteners.clear();
253 } 296 }
254 297
298 if (!m_pending_sdleventlisteners_front.empty()) {
299 std::deque<ISdlEventListener*>::iterator i = m_pending_sdleventlisteners_front.begin();
300 while(i != m_pending_sdleventlisteners_front.end()) {
301 m_sdleventlisteners.push_front(*i);
302 ++i;
303 }
304 m_pending_sdleventlisteners_front.clear();
305 }
306
255 if (!m_pending_sdldeletions.empty()) { 307 if (!m_pending_sdldeletions.empty()) {
256 std::vector<ISdlEventListener*>::iterator i = m_pending_sdldeletions.begin(); 308 std::deque<ISdlEventListener*>::iterator i = m_pending_sdldeletions.begin();
257 while (i != m_pending_sdldeletions.end()) { 309 while (i != m_pending_sdldeletions.end()) {
258 std::vector<ISdlEventListener*>::iterator j = m_sdleventlisteners.begin(); 310 std::deque<ISdlEventListener*>::iterator j = m_sdleventlisteners.begin();
259 while (j != m_sdleventlisteners.end()) { 311 while (j != m_sdleventlisteners.end()) {
260 if(*j == *i) { 312 if(*j == *i) {
261 m_sdleventlisteners.erase(j); 313 m_sdleventlisteners.erase(j);
262 break; 314 break;
263 } 315 }
266 ++i; 318 ++i;
267 } 319 }
268 m_pending_sdldeletions.clear(); 320 m_pending_sdldeletions.clear();
269 } 321 }
270 322
271 std::vector<ISdlEventListener*>::iterator i = m_sdleventlisteners.begin(); 323 std::deque<ISdlEventListener*>::iterator i = m_sdleventlisteners.begin();
272 while (i != m_sdleventlisteners.end()) { 324 while (i != m_sdleventlisteners.end()) {
273 ret = ret || (*i)->onSdlEvent(evt); 325 ret = ret || (*i)->onSdlEvent(evt);
274 ++i; 326 ++i;
275 } 327 }
276 return ret; 328 return ret;
466 keyevt.setAltPressed(keysym.mod & KMOD_ALT); 518 keyevt.setAltPressed(keysym.mod & KMOD_ALT);
467 keyevt.setMetaPressed(keysym.mod & KMOD_META); 519 keyevt.setMetaPressed(keysym.mod & KMOD_META);
468 keyevt.setNumericPad(keysym.sym >= SDLK_KP0 && keysym.sym <= SDLK_KP_EQUALS); 520 keyevt.setNumericPad(keysym.sym >= SDLK_KP0 && keysym.sym <= SDLK_KP_EQUALS);
469 keyevt.setKey(Key(static_cast<Key::KeyType>(keysym.sym), keysym.unicode)); 521 keyevt.setKey(Key(static_cast<Key::KeyType>(keysym.sym), keysym.unicode));
470 } 522 }
471 523
472 void EventManager::fillModifiers(InputEvent& evt) { 524 void EventManager::fillModifiers(InputEvent& evt) {
473 evt.setAltPressed(m_keystatemap[Key::ALT_GR] | 525 evt.setAltPressed(m_keystatemap[Key::ALT_GR] |
474 m_keystatemap[Key::LEFT_ALT] | 526 m_keystatemap[Key::LEFT_ALT] |
475 m_keystatemap[Key::RIGHT_ALT]); 527 m_keystatemap[Key::RIGHT_ALT]);
476 evt.setControlPressed(m_keystatemap[Key::LEFT_CONTROL] | 528 evt.setControlPressed(m_keystatemap[Key::LEFT_CONTROL] |