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 "OgreInput.h"
|
---|
28 | #include "OgreCursor.h"
|
---|
29 | #include "OgreEventQueue.h"
|
---|
30 | #include "OgreKeyEvent.h"
|
---|
31 | #include "OgreEventListeners.h"
|
---|
32 | #include "OgreLogManager.h"
|
---|
33 |
|
---|
34 |
|
---|
35 | namespace Ogre {
|
---|
36 | //-----------------------------------------------------------------------
|
---|
37 | InputReader::InputReader()
|
---|
38 | {
|
---|
39 | mCursor = 0;
|
---|
40 | mModifiers = 0;
|
---|
41 | mEventQueue = 0;
|
---|
42 | mUseBufferedKeys = false;
|
---|
43 | mUseBufferedMouse = false;
|
---|
44 | }
|
---|
45 |
|
---|
46 | //-----------------------------------------------------------------------
|
---|
47 | void InputReader::useBufferedInput(EventQueue* pEventQueue, bool keys, bool mouse)
|
---|
48 | {
|
---|
49 | mEventQueue = pEventQueue;
|
---|
50 |
|
---|
51 | if (mCursor)
|
---|
52 | delete mCursor;
|
---|
53 |
|
---|
54 | mCursor = new Cursor();
|
---|
55 |
|
---|
56 | // initial states of buffered don't call setBufferedInput
|
---|
57 | // because that can be overriden (in the future) to save releasing and acquiring unchanged inputs
|
---|
58 | // if we ever decide to release and acquire devices
|
---|
59 | mUseBufferedKeys = keys;
|
---|
60 | mUseBufferedMouse = mouse;
|
---|
61 | }
|
---|
62 |
|
---|
63 | //-----------------------------------------------------------------------
|
---|
64 | void InputReader::setBufferedInput(bool keys, bool mouse)
|
---|
65 | {
|
---|
66 | mUseBufferedKeys = keys;
|
---|
67 | mUseBufferedMouse = mouse;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | //-----------------------------------------------------------------------
|
---|
72 | InputReader::~InputReader()
|
---|
73 | {
|
---|
74 | if (mCursor)
|
---|
75 | {
|
---|
76 | delete mCursor;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | //-----------------------------------------------------------------------
|
---|
81 | void InputReader::triggerMouseButton(int nMouseCode, bool mousePressed)
|
---|
82 | {
|
---|
83 | if (mousePressed)
|
---|
84 | {
|
---|
85 | mModifiers |= nMouseCode;
|
---|
86 | createMouseEvent(MouseEvent::ME_MOUSE_PRESSED, nMouseCode);
|
---|
87 | // Update immediate-mode mouse button state
|
---|
88 | switch(nMouseCode)
|
---|
89 | {
|
---|
90 | case InputEvent::BUTTON0_MASK:
|
---|
91 | mMouseState.Buttons |= 0x1;
|
---|
92 | break;
|
---|
93 | case InputEvent::BUTTON1_MASK:
|
---|
94 | mMouseState.Buttons |= 0x2;
|
---|
95 | break;
|
---|
96 | case InputEvent::BUTTON2_MASK:
|
---|
97 | mMouseState.Buttons |= 0x4;
|
---|
98 | break;
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|
102 | else
|
---|
103 | { // button up... trigger MouseReleased, and MouseClicked
|
---|
104 | mModifiers &= ~nMouseCode;
|
---|
105 | createMouseEvent(MouseEvent::ME_MOUSE_RELEASED, nMouseCode);
|
---|
106 | //createMouseEvent(MouseEvent::ME_MOUSE_CLICKED, nMouseCode); JCA - moved to EventDispatcher
|
---|
107 | // Update immediate-mode mouse button state
|
---|
108 | switch(nMouseCode)
|
---|
109 | {
|
---|
110 | case InputEvent::BUTTON0_MASK:
|
---|
111 | mMouseState.Buttons &= 0xFE;
|
---|
112 | break;
|
---|
113 | case InputEvent::BUTTON1_MASK:
|
---|
114 | mMouseState.Buttons &= 0xFD;
|
---|
115 | break;
|
---|
116 | case InputEvent::BUTTON2_MASK:
|
---|
117 | mMouseState.Buttons &= 0xFB;
|
---|
118 | break;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | }
|
---|
123 |
|
---|
124 | //-----------------------------------------------------------------------
|
---|
125 | void InputReader::createMouseEvent(int id, int button)
|
---|
126 | {
|
---|
127 | MouseEvent* me =
|
---|
128 | new MouseEvent(
|
---|
129 | NULL, id, button, 0, // hack fix time
|
---|
130 | mModifiers,
|
---|
131 | mCursor->getX(), mCursor->getY(), mCursor->getZ(),
|
---|
132 | mCursor->getRelX(), mCursor->getRelY(), mCursor->getRelZ(),
|
---|
133 | 0
|
---|
134 | ); // hack fix click count
|
---|
135 |
|
---|
136 |
|
---|
137 | mCursor->processEvent(me);
|
---|
138 | mEventQueue->push(me);
|
---|
139 |
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 |
|
---|
144 | //-----------------------------------------------------------------------
|
---|
145 | void InputReader::createKeyEvent(int id, int key)
|
---|
146 | {
|
---|
147 | KeyEvent* ke = new KeyEvent(NULL, id, key, 0, // hack fix time
|
---|
148 | mModifiers); // hack fix click count
|
---|
149 | mEventQueue->push(ke);
|
---|
150 |
|
---|
151 | }
|
---|
152 |
|
---|
153 | //-----------------------------------------------------------------------
|
---|
154 | void InputReader::mouseMoved()
|
---|
155 | {
|
---|
156 |
|
---|
157 | if (mModifiers & InputEvent::BUTTON_ANY_MASK) // don't need to know which button. you can get that from the modifiers
|
---|
158 | {
|
---|
159 | createMouseEvent(MouseEvent::ME_MOUSE_DRAGGED, 0);
|
---|
160 | }
|
---|
161 | else
|
---|
162 | {
|
---|
163 | createMouseEvent(MouseEvent::ME_MOUSE_MOVED, 0);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | //-----------------------------------------------------------------------
|
---|
167 | void InputReader::addCursorMoveListener(MouseMotionListener* c)
|
---|
168 | {
|
---|
169 | mCursor->addMouseMotionListener(c);
|
---|
170 | }
|
---|
171 | //-----------------------------------------------------------------------
|
---|
172 | void InputReader::removeCursorMoveListener(MouseMotionListener* c)
|
---|
173 | {
|
---|
174 | mCursor->removeMouseMotionListener(c);
|
---|
175 | }
|
---|
176 | //-----------------------------------------------------------------------
|
---|
177 |
|
---|
178 | void InputReader::keyChanged(int key, bool down)
|
---|
179 | {
|
---|
180 | if (down)
|
---|
181 | {
|
---|
182 | switch (key) {
|
---|
183 | case KC_LMENU :
|
---|
184 | case KC_RMENU :
|
---|
185 | mModifiers |= InputEvent::ALT_MASK;
|
---|
186 | break;
|
---|
187 |
|
---|
188 | case KC_LSHIFT :
|
---|
189 | case KC_RSHIFT :
|
---|
190 | mModifiers |= InputEvent::SHIFT_MASK;
|
---|
191 | break;
|
---|
192 |
|
---|
193 | case KC_LCONTROL :
|
---|
194 | case KC_RCONTROL :
|
---|
195 | mModifiers |= InputEvent::CTRL_MASK;
|
---|
196 | break;
|
---|
197 | }
|
---|
198 |
|
---|
199 | createKeyEvent(KeyEvent::KE_KEY_PRESSED, key);
|
---|
200 |
|
---|
201 | // Update keydown map
|
---|
202 | mBufferedKeysDown.insert(static_cast<KeyCode>(key));
|
---|
203 | }
|
---|
204 | else
|
---|
205 | {
|
---|
206 | switch (key) {
|
---|
207 | case KC_LMENU :
|
---|
208 | case KC_RMENU :
|
---|
209 | mModifiers &= ~InputEvent::ALT_MASK;
|
---|
210 | break;
|
---|
211 |
|
---|
212 | case KC_LSHIFT :
|
---|
213 | case KC_RSHIFT :
|
---|
214 | mModifiers &= ~InputEvent::SHIFT_MASK;
|
---|
215 | break;
|
---|
216 |
|
---|
217 | case KC_LCONTROL :
|
---|
218 | case KC_RCONTROL :
|
---|
219 | mModifiers &= ~InputEvent::CTRL_MASK;
|
---|
220 | break;
|
---|
221 | }
|
---|
222 |
|
---|
223 | createKeyEvent(KeyEvent::KE_KEY_RELEASED, key);
|
---|
224 | createKeyEvent(KeyEvent::KE_KEY_CLICKED, key);
|
---|
225 | // Update keydown map
|
---|
226 | mBufferedKeysDown.erase(static_cast<KeyCode>(key));
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | //-----------------------------------------------------------------------
|
---|
231 | char InputReader::getKeyChar(int keyCode, long modifiers)
|
---|
232 | {
|
---|
233 | if (modifiers == 0)
|
---|
234 | {
|
---|
235 | switch (keyCode) {
|
---|
236 | case KC_1: return '1';
|
---|
237 | case KC_2: return '2';
|
---|
238 | case KC_3: return '3';
|
---|
239 | case KC_4: return '4';
|
---|
240 | case KC_5: return '5';
|
---|
241 | case KC_6: return '6';
|
---|
242 | case KC_7: return '7';
|
---|
243 | case KC_8: return '8';
|
---|
244 | case KC_9: return '9';
|
---|
245 | case KC_0: return '0';
|
---|
246 | case KC_MINUS: return '-'; /* - on main keyboard */
|
---|
247 | case KC_EQUALS: return '=';
|
---|
248 | case KC_Q: return 'q';
|
---|
249 | case KC_W: return 'w';
|
---|
250 | case KC_E: return 'e';
|
---|
251 | case KC_R: return 'r';
|
---|
252 | case KC_T: return 't';
|
---|
253 | case KC_Y: return 'y';
|
---|
254 | case KC_U: return 'u';
|
---|
255 | case KC_I: return 'i';
|
---|
256 | case KC_O: return 'o';
|
---|
257 | case KC_P: return 'p';
|
---|
258 | case KC_LBRACKET: return '[';
|
---|
259 | case KC_RBRACKET: return ']';
|
---|
260 | case KC_A: return 'a';
|
---|
261 | case KC_S: return 's';
|
---|
262 | case KC_D: return 'd';
|
---|
263 | case KC_F: return 'f';
|
---|
264 | case KC_G: return 'g';
|
---|
265 | case KC_H: return 'h';
|
---|
266 | case KC_J: return 'j';
|
---|
267 | case KC_K: return 'k';
|
---|
268 | case KC_L: return 'l';
|
---|
269 | case KC_SEMICOLON: return ';';
|
---|
270 | case KC_APOSTROPHE: return '\'';
|
---|
271 | case KC_GRAVE: return '`'; /* accent grave */
|
---|
272 | case KC_BACKSLASH: return '\\';
|
---|
273 | case KC_Z: return 'z';
|
---|
274 | case KC_X: return 'x';
|
---|
275 | case KC_C: return 'c';
|
---|
276 | case KC_V: return 'v';
|
---|
277 | case KC_B: return 'b';
|
---|
278 | case KC_N: return 'n';
|
---|
279 | case KC_M: return 'm';
|
---|
280 | case KC_COMMA: return ',';
|
---|
281 | case KC_PERIOD: return '.'; /* . on main keyboard */
|
---|
282 | case KC_SLASH: return '/'; /* '/' on main keyboard */
|
---|
283 | case KC_MULTIPLY: return '*'; /* * on numeric keypad */
|
---|
284 | case KC_SPACE: return ' ';
|
---|
285 | case KC_NUMPAD7: return '7';
|
---|
286 | case KC_NUMPAD8: return '8';
|
---|
287 | case KC_NUMPAD9: return '9';
|
---|
288 | case KC_SUBTRACT: return '-'; /* - on numeric keypad */
|
---|
289 | case KC_NUMPAD4: return '4';
|
---|
290 | case KC_NUMPAD5: return '5';
|
---|
291 | case KC_NUMPAD6: return '6';
|
---|
292 | case KC_ADD: return '+'; /* + on numeric keypad */
|
---|
293 | case KC_NUMPAD1: return '1';
|
---|
294 | case KC_NUMPAD2: return '2';
|
---|
295 | case KC_NUMPAD3: return '3';
|
---|
296 | case KC_NUMPAD0: return '0';
|
---|
297 | case KC_DECIMAL: return '.'; /* . on numeric keypad */
|
---|
298 | case KC_NUMPADEQUALS: return '='; /* = on numeric keypad (NEC PC98) */
|
---|
299 | case KC_AT: return '@'; /* (NEC PC98) */
|
---|
300 | case KC_COLON: return ':'; /* (NEC PC98) */
|
---|
301 | case KC_UNDERLINE: return '_'; /* (NEC PC98) */
|
---|
302 | case KC_NUMPADCOMMA: return ','; /* , on numeric keypad (NEC PC98) */
|
---|
303 | case KC_DIVIDE: return '/'; /* / on numeric keypad */
|
---|
304 | }
|
---|
305 | }
|
---|
306 | else if (modifiers == InputEvent::SHIFT_MASK)
|
---|
307 | {
|
---|
308 | switch (keyCode) {
|
---|
309 | case KC_1: return '!';
|
---|
310 | case KC_2: return '@';
|
---|
311 | case KC_3: return '#';
|
---|
312 | case KC_4: return '$';
|
---|
313 | case KC_5: return '%';
|
---|
314 | case KC_6: return '^';
|
---|
315 | case KC_7: return '&';
|
---|
316 | case KC_8: return '*';
|
---|
317 | case KC_9: return '(';
|
---|
318 | case KC_0: return ')';
|
---|
319 | case KC_MINUS: return '_';
|
---|
320 | case KC_EQUALS: return '+';
|
---|
321 | case KC_Q: return 'Q';
|
---|
322 | case KC_W: return 'W';
|
---|
323 | case KC_E: return 'E';
|
---|
324 | case KC_R: return 'R';
|
---|
325 | case KC_T: return 'T';
|
---|
326 | case KC_Y: return 'Y';
|
---|
327 | case KC_U: return 'U';
|
---|
328 | case KC_I: return 'I';
|
---|
329 | case KC_O: return 'O';
|
---|
330 | case KC_P: return 'P';
|
---|
331 | case KC_LBRACKET: return '{';
|
---|
332 | case KC_RBRACKET: return '}';
|
---|
333 | case KC_A: return 'A';
|
---|
334 | case KC_S: return 'S';
|
---|
335 | case KC_D: return 'D';
|
---|
336 | case KC_F: return 'F';
|
---|
337 | case KC_G: return 'G';
|
---|
338 | case KC_H: return 'H';
|
---|
339 | case KC_J: return 'J';
|
---|
340 | case KC_K: return 'K';
|
---|
341 | case KC_L: return 'L';
|
---|
342 | case KC_SEMICOLON: return ':';
|
---|
343 | case KC_APOSTROPHE: return '"';
|
---|
344 | case KC_GRAVE: return '~'; /* accent grave */
|
---|
345 | case KC_BACKSLASH: return '|';
|
---|
346 | case KC_Z: return 'Z';
|
---|
347 | case KC_X: return 'X';
|
---|
348 | case KC_C: return 'C';
|
---|
349 | case KC_V: return 'V';
|
---|
350 | case KC_B: return 'B';
|
---|
351 | case KC_N: return 'N';
|
---|
352 | case KC_M: return 'M';
|
---|
353 | case KC_COMMA: return '<';
|
---|
354 | case KC_PERIOD: return '>'; /* . on main keyboard */
|
---|
355 | case KC_SLASH: return '?'; /* '/' on main keyboard */
|
---|
356 | case KC_MULTIPLY: return '*'; /* * on numeric keypad */
|
---|
357 | case KC_SPACE: return ' ';
|
---|
358 | }
|
---|
359 | }
|
---|
360 | return 0;
|
---|
361 | }
|
---|
362 | //-----------------------------------------------------------------------
|
---|
363 | bool InputReader::isKeyDown( KeyCode kc ) const
|
---|
364 | {
|
---|
365 | if (mUseBufferedKeys)
|
---|
366 | {
|
---|
367 | return mBufferedKeysDown.find(kc) != mBufferedKeysDown.end();
|
---|
368 | }
|
---|
369 | else
|
---|
370 | {
|
---|
371 | return isKeyDownImmediate(kc);
|
---|
372 | }
|
---|
373 | }
|
---|
374 | }
|
---|