1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 | #include "OgreStableHeaders.h"
|
---|
26 |
|
---|
27 | #include "OgreMouseEvent.h"
|
---|
28 | #include "OgreEventDispatcher.h"
|
---|
29 | #include "OgreTargetManager.h"
|
---|
30 | #include "OgreEventProcessor.h"
|
---|
31 | #include "OgrePositionTarget.h"
|
---|
32 | #include "OgreKeyEvent.h"
|
---|
33 |
|
---|
34 | namespace Ogre {
|
---|
35 |
|
---|
36 | EventDispatcher::EventDispatcher(TargetManager* pTargetManager)
|
---|
37 | :mTargetManager(pTargetManager) // abstract this out TODO
|
---|
38 | {
|
---|
39 | mFocus = 0;
|
---|
40 | mMouseDragSource = 0;
|
---|
41 | mKeyCursorOn = 0;
|
---|
42 | mEventMask = 0;
|
---|
43 | mTargetLastEntered = 0;
|
---|
44 | mMouseX = 0;
|
---|
45 | mMouseY = 0;
|
---|
46 | mDragging = false;
|
---|
47 | mDragDropOn = false;
|
---|
48 | mDragDropActive = false;
|
---|
49 | }
|
---|
50 |
|
---|
51 | //---------------------------------------------------------------------
|
---|
52 | EventDispatcher::~EventDispatcher()
|
---|
53 | {
|
---|
54 | }
|
---|
55 |
|
---|
56 | //---------------------------------------------------------------------
|
---|
57 | bool EventDispatcher::dispatchEvent(InputEvent* e)
|
---|
58 | {
|
---|
59 | bool ret = false;
|
---|
60 | if (e->isEventBetween(MouseEvent::ME_FIRST_EVENT, MouseEvent::ME_LAST_EVENT)) // i am open to suggestions for a better way to do this
|
---|
61 | // maybe a method e->isEvent(InputEvent::MouseEvent) ??
|
---|
62 | {
|
---|
63 | MouseEvent* me = static_cast<MouseEvent*> (e);
|
---|
64 | ret = processMouseEvent(me);
|
---|
65 | }
|
---|
66 | else if (e->isEventBetween(KeyEvent::KE_FIRST_EVENT, KeyEvent::KE_LAST_EVENT))
|
---|
67 | {
|
---|
68 | KeyEvent* ke = static_cast<KeyEvent*> (e);
|
---|
69 | ret = processKeyEvent(ke);
|
---|
70 |
|
---|
71 | }
|
---|
72 | return ret;
|
---|
73 | }
|
---|
74 |
|
---|
75 | //---------------------------------------------------------------------
|
---|
76 | void EventDispatcher::setDragDrop(bool dragDropOn)
|
---|
77 | {
|
---|
78 | mDragDropOn = dragDropOn;
|
---|
79 | }
|
---|
80 |
|
---|
81 | //---------------------------------------------------------------------
|
---|
82 | bool EventDispatcher::processKeyEvent(KeyEvent* e)
|
---|
83 | {
|
---|
84 | if (mKeyCursorOn != 0)
|
---|
85 | {
|
---|
86 | mKeyCursorOn->processEvent(e);
|
---|
87 | }
|
---|
88 | return e->isConsumed();
|
---|
89 | }
|
---|
90 |
|
---|
91 | //---------------------------------------------------------------------
|
---|
92 | bool EventDispatcher::processMouseEvent(MouseEvent* e)
|
---|
93 | {
|
---|
94 | PositionTarget* targetOver;
|
---|
95 |
|
---|
96 | mMouseX = e->getX();
|
---|
97 | mMouseY = e->getY();
|
---|
98 |
|
---|
99 | targetOver = mTargetManager->getPositionTargetAt(e->getX(), e->getY());
|
---|
100 | trackMouseEnterExit(targetOver, e);
|
---|
101 |
|
---|
102 | switch (e->getID())
|
---|
103 | {
|
---|
104 | case MouseEvent::ME_MOUSE_PRESSED:
|
---|
105 | mDragging = true;
|
---|
106 | if (mDragDropOn)
|
---|
107 | mDragDropActive = true;
|
---|
108 | mMouseDragSource = targetOver;
|
---|
109 | retargetMouseEvent(targetOver, e);
|
---|
110 | trackKeyEnterExit(targetOver, e);
|
---|
111 | break;
|
---|
112 |
|
---|
113 | case MouseEvent::ME_MOUSE_RELEASED:
|
---|
114 | if (targetOver != 0)
|
---|
115 | {
|
---|
116 | if (targetOver == mMouseDragSource)
|
---|
117 | {
|
---|
118 | retargetMouseEvent(mMouseDragSource, MouseEvent::ME_MOUSE_CLICKED, e);
|
---|
119 | retargetMouseEvent(mMouseDragSource, e);
|
---|
120 | }
|
---|
121 | else // i.e. targetOver != mMouseDragSource
|
---|
122 | {
|
---|
123 | if (mDragDropActive)
|
---|
124 | retargetMouseEvent(targetOver, MouseEvent::ME_MOUSE_DRAGDROPPED, e);
|
---|
125 | retargetMouseEvent(mMouseDragSource, e);
|
---|
126 | retargetMouseEvent(targetOver, MouseEvent::ME_MOUSE_ENTERED, e);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | else
|
---|
130 | retargetMouseEvent(mMouseDragSource, e);
|
---|
131 |
|
---|
132 | mDragging = false;
|
---|
133 | mDragDropActive = false;
|
---|
134 | mMouseDragSource = 0;
|
---|
135 | break;
|
---|
136 |
|
---|
137 | case MouseEvent::ME_MOUSE_MOVED:
|
---|
138 | case MouseEvent::ME_MOUSE_DRAGGED:
|
---|
139 | if (!mDragging || targetOver == mMouseDragSource)
|
---|
140 | {
|
---|
141 | retargetMouseEvent(targetOver, e);
|
---|
142 | }
|
---|
143 | else // i.e. mDragging && targetOver != mMouseDragSource
|
---|
144 | {
|
---|
145 | retargetMouseEvent(mMouseDragSource, MouseEvent::ME_MOUSE_DRAGGED, e, true);
|
---|
146 | if (mDragDropActive)
|
---|
147 | retargetMouseEvent(targetOver, MouseEvent::ME_MOUSE_DRAGMOVED, e);
|
---|
148 | }
|
---|
149 | break;
|
---|
150 | }
|
---|
151 |
|
---|
152 | return e->isConsumed();
|
---|
153 | }
|
---|
154 |
|
---|
155 | //---------------------------------------------------------------------
|
---|
156 | void EventDispatcher::retargetMouseEvent(PositionTarget* target, MouseEvent* e)
|
---|
157 | {
|
---|
158 | if (target == NULL)
|
---|
159 | {
|
---|
160 | return;
|
---|
161 | }
|
---|
162 |
|
---|
163 | MouseEvent* retargeted = new MouseEvent(target,
|
---|
164 | e->getID(),
|
---|
165 | e->getButtonID(),
|
---|
166 | e->getWhen(),
|
---|
167 | e->getModifiers(),
|
---|
168 | e->getX(),
|
---|
169 | e->getY(),
|
---|
170 | e->getZ(),
|
---|
171 | e->getClickCount());
|
---|
172 |
|
---|
173 | target->processEvent(retargeted);
|
---|
174 | delete retargeted;
|
---|
175 |
|
---|
176 | e->consume();
|
---|
177 | }
|
---|
178 |
|
---|
179 | //---------------------------------------------------------------------
|
---|
180 | void EventDispatcher::retargetMouseEvent(PositionTarget* target, int id, MouseEvent* e, bool consume)
|
---|
181 | {
|
---|
182 | if (target == NULL)
|
---|
183 | {
|
---|
184 | return;
|
---|
185 | }
|
---|
186 |
|
---|
187 | MouseEvent* retargeted = new MouseEvent(target,
|
---|
188 | id,
|
---|
189 | e->getButtonID(),
|
---|
190 | e->getWhen(),
|
---|
191 | e->getModifiers(),
|
---|
192 | e->getX(),
|
---|
193 | e->getY(),
|
---|
194 | e->getZ(),
|
---|
195 | e->getClickCount());
|
---|
196 |
|
---|
197 | target->processEvent(retargeted);
|
---|
198 | delete retargeted;
|
---|
199 |
|
---|
200 | if (consume)
|
---|
201 | e->consume();
|
---|
202 | }
|
---|
203 |
|
---|
204 | //---------------------------------------------------------------------
|
---|
205 | void EventDispatcher::retargetKeyEvent(PositionTarget* target, int id, MouseEvent* e)
|
---|
206 | {
|
---|
207 | if (target == NULL)
|
---|
208 | {
|
---|
209 | return;
|
---|
210 | }
|
---|
211 |
|
---|
212 | KeyEvent* retargeted = new KeyEvent(target,
|
---|
213 | id,
|
---|
214 | 0,
|
---|
215 | e->getWhen(),
|
---|
216 | e->getModifiers());
|
---|
217 |
|
---|
218 | target->processEvent(retargeted);
|
---|
219 | delete retargeted;
|
---|
220 | }
|
---|
221 |
|
---|
222 | //---------------------------------------------------------------------
|
---|
223 | void EventDispatcher::trackMouseEnterExit(PositionTarget* targetOver, MouseEvent* e)
|
---|
224 | {
|
---|
225 | if (mTargetLastEntered == targetOver)
|
---|
226 | {
|
---|
227 | return;
|
---|
228 | }
|
---|
229 |
|
---|
230 | if (mTargetLastEntered != 0)
|
---|
231 | {
|
---|
232 | if (!mDragging || mTargetLastEntered == mMouseDragSource)
|
---|
233 | {
|
---|
234 | retargetMouseEvent(mTargetLastEntered, MouseEvent::ME_MOUSE_EXITED, e);
|
---|
235 | }
|
---|
236 | else if (mDragDropActive) // i.e. mDragging && mTargetLastEntered != mMouseDragSource && mDragDropActive
|
---|
237 | {
|
---|
238 | retargetMouseEvent(mTargetLastEntered, MouseEvent::ME_MOUSE_DRAGEXITED, e);
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | if (targetOver != 0)
|
---|
243 | {
|
---|
244 | if (!mDragging || targetOver == mMouseDragSource)
|
---|
245 | {
|
---|
246 | retargetMouseEvent(targetOver, MouseEvent::ME_MOUSE_ENTERED, e);
|
---|
247 | }
|
---|
248 | else if (mDragDropActive) // i.e. mDragging && targetOver != mMouseDragSource && mDragDropActive
|
---|
249 | {
|
---|
250 | retargetMouseEvent(targetOver, MouseEvent::ME_MOUSE_DRAGENTERED, e);
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | mTargetLastEntered = targetOver;
|
---|
255 | }
|
---|
256 |
|
---|
257 | //---------------------------------------------------------------------
|
---|
258 | void EventDispatcher::trackKeyEnterExit(PositionTarget* targetOver, MouseEvent* e)
|
---|
259 | {
|
---|
260 | if (targetOver != mKeyCursorOn)
|
---|
261 | {
|
---|
262 | if (mKeyCursorOn != 0)
|
---|
263 | {
|
---|
264 | retargetKeyEvent(mKeyCursorOn, KeyEvent::KE_KEY_FOCUSOUT, e);
|
---|
265 | }
|
---|
266 |
|
---|
267 | if (targetOver != 0 && targetOver->isKeyEnabled())
|
---|
268 | {
|
---|
269 | mKeyCursorOn = targetOver;
|
---|
270 | retargetKeyEvent(targetOver, KeyEvent::KE_KEY_FOCUSIN, e);
|
---|
271 | }
|
---|
272 | else
|
---|
273 | {
|
---|
274 | mKeyCursorOn = NULL;
|
---|
275 | }
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 |
|
---|