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 |
|
---|
26 | #include "OgreSDLInput.h"
|
---|
27 | #include "OgreLogManager.h"
|
---|
28 | #include "OgreMouseEvent.h"
|
---|
29 | #include "OgreCursor.h"
|
---|
30 |
|
---|
31 | namespace Ogre {
|
---|
32 |
|
---|
33 | SDLInput::SDLInput()
|
---|
34 | : InputReader(), mKeyboardBuffer(0), mMouseX(0), mMouseY(0),
|
---|
35 | mMouseRelativeX(0), mMouseRelativeY(0), mMouseRelativeZ(0),
|
---|
36 | _visible(true)
|
---|
37 | {
|
---|
38 | mMouseScale = 0.002f;
|
---|
39 | mMouseGrabbed = false;
|
---|
40 | mGrabMouse = false;
|
---|
41 | mMouseLeft = false;
|
---|
42 | mGrabMode = GRAB_NONE;
|
---|
43 | mEventQueue = 0;
|
---|
44 |
|
---|
45 | _key_map.insert(InputKeyMap::value_type(SDLK_ESCAPE,KC_ESCAPE));
|
---|
46 | _key_map.insert(InputKeyMap::value_type(SDLK_1, KC_1));
|
---|
47 | _key_map.insert(InputKeyMap::value_type(SDLK_2, KC_2));
|
---|
48 | _key_map.insert(InputKeyMap::value_type(SDLK_3, KC_3));
|
---|
49 | _key_map.insert(InputKeyMap::value_type(SDLK_4, KC_4));
|
---|
50 | _key_map.insert(InputKeyMap::value_type(SDLK_5, KC_5));
|
---|
51 | _key_map.insert(InputKeyMap::value_type(SDLK_6, KC_6));
|
---|
52 | _key_map.insert(InputKeyMap::value_type(SDLK_7, KC_7));
|
---|
53 | _key_map.insert(InputKeyMap::value_type(SDLK_8, KC_8));
|
---|
54 | _key_map.insert(InputKeyMap::value_type(SDLK_9, KC_9));
|
---|
55 | _key_map.insert(InputKeyMap::value_type(SDLK_0, KC_0));
|
---|
56 | _key_map.insert(InputKeyMap::value_type(SDLK_MINUS, KC_MINUS));
|
---|
57 | _key_map.insert(InputKeyMap::value_type(SDLK_EQUALS, KC_EQUALS));
|
---|
58 | _key_map.insert(InputKeyMap::value_type(SDLK_BACKSPACE, KC_BACK));
|
---|
59 | _key_map.insert(InputKeyMap::value_type(SDLK_TAB, KC_TAB));
|
---|
60 | _key_map.insert(InputKeyMap::value_type(SDLK_q, KC_Q));
|
---|
61 | _key_map.insert(InputKeyMap::value_type(SDLK_w, KC_W));
|
---|
62 | _key_map.insert(InputKeyMap::value_type(SDLK_e, KC_E));
|
---|
63 | _key_map.insert(InputKeyMap::value_type(SDLK_r, KC_R));
|
---|
64 | _key_map.insert(InputKeyMap::value_type(SDLK_t, KC_T));
|
---|
65 | _key_map.insert(InputKeyMap::value_type(SDLK_y, KC_Y));
|
---|
66 | _key_map.insert(InputKeyMap::value_type(SDLK_u, KC_U));
|
---|
67 | _key_map.insert(InputKeyMap::value_type(SDLK_i, KC_I));
|
---|
68 | _key_map.insert(InputKeyMap::value_type(SDLK_o, KC_O));
|
---|
69 | _key_map.insert(InputKeyMap::value_type(SDLK_p, KC_P));
|
---|
70 | _key_map.insert(InputKeyMap::value_type(SDLK_RETURN, KC_RETURN));
|
---|
71 | _key_map.insert(InputKeyMap::value_type(SDLK_LCTRL, KC_LCONTROL));
|
---|
72 | _key_map.insert(InputKeyMap::value_type(SDLK_a, KC_A));
|
---|
73 | _key_map.insert(InputKeyMap::value_type(SDLK_s, KC_S));
|
---|
74 | _key_map.insert(InputKeyMap::value_type(SDLK_d, KC_D));
|
---|
75 | _key_map.insert(InputKeyMap::value_type(SDLK_f, KC_F));
|
---|
76 | _key_map.insert(InputKeyMap::value_type(SDLK_g, KC_G));
|
---|
77 | _key_map.insert(InputKeyMap::value_type(SDLK_h, KC_H));
|
---|
78 | _key_map.insert(InputKeyMap::value_type(SDLK_j, KC_J));
|
---|
79 | _key_map.insert(InputKeyMap::value_type(SDLK_k, KC_K));
|
---|
80 | _key_map.insert(InputKeyMap::value_type(SDLK_l, KC_L));
|
---|
81 | _key_map.insert(InputKeyMap::value_type(SDLK_SEMICOLON, KC_SEMICOLON));
|
---|
82 | _key_map.insert(InputKeyMap::value_type(SDLK_COLON, KC_COLON));
|
---|
83 | _key_map.insert(InputKeyMap::value_type(SDLK_QUOTE, KC_APOSTROPHE));
|
---|
84 | _key_map.insert(InputKeyMap::value_type(SDLK_BACKQUOTE, KC_GRAVE));
|
---|
85 | _key_map.insert(InputKeyMap::value_type(SDLK_LSHIFT, KC_LSHIFT));
|
---|
86 | _key_map.insert(InputKeyMap::value_type(SDLK_BACKSLASH, KC_BACKSLASH));
|
---|
87 | _key_map.insert(InputKeyMap::value_type(SDLK_z, KC_Z));
|
---|
88 | _key_map.insert(InputKeyMap::value_type(SDLK_x, KC_X));
|
---|
89 | _key_map.insert(InputKeyMap::value_type(SDLK_c, KC_C));
|
---|
90 | _key_map.insert(InputKeyMap::value_type(SDLK_v, KC_V));
|
---|
91 | _key_map.insert(InputKeyMap::value_type(SDLK_b, KC_B));
|
---|
92 | _key_map.insert(InputKeyMap::value_type(SDLK_n, KC_N));
|
---|
93 | _key_map.insert(InputKeyMap::value_type(SDLK_m, KC_M));
|
---|
94 | _key_map.insert(InputKeyMap::value_type(SDLK_COMMA, KC_COMMA));
|
---|
95 | _key_map.insert(InputKeyMap::value_type(SDLK_PERIOD, KC_PERIOD));
|
---|
96 | _key_map.insert(InputKeyMap::value_type(SDLK_RSHIFT, KC_RSHIFT));
|
---|
97 | _key_map.insert(InputKeyMap::value_type(SDLK_KP_MULTIPLY, KC_MULTIPLY));
|
---|
98 | _key_map.insert(InputKeyMap::value_type(SDLK_LALT, KC_LMENU));
|
---|
99 | _key_map.insert(InputKeyMap::value_type(SDLK_SPACE, KC_SPACE));
|
---|
100 | _key_map.insert(InputKeyMap::value_type(SDLK_CAPSLOCK, KC_CAPITAL));
|
---|
101 | _key_map.insert(InputKeyMap::value_type(SDLK_F1, KC_F1));
|
---|
102 | _key_map.insert(InputKeyMap::value_type(SDLK_F2, KC_F2));
|
---|
103 | _key_map.insert(InputKeyMap::value_type(SDLK_F3, KC_F3));
|
---|
104 | _key_map.insert(InputKeyMap::value_type(SDLK_F4, KC_F4));
|
---|
105 | _key_map.insert(InputKeyMap::value_type(SDLK_F5, KC_F5));
|
---|
106 | _key_map.insert(InputKeyMap::value_type(SDLK_F6, KC_F6));
|
---|
107 | _key_map.insert(InputKeyMap::value_type(SDLK_F7, KC_F7));
|
---|
108 | _key_map.insert(InputKeyMap::value_type(SDLK_F8, KC_F8));
|
---|
109 | _key_map.insert(InputKeyMap::value_type(SDLK_F9, KC_F9));
|
---|
110 | _key_map.insert(InputKeyMap::value_type(SDLK_F10, KC_F10));
|
---|
111 | _key_map.insert(InputKeyMap::value_type(SDLK_NUMLOCK, KC_NUMLOCK));
|
---|
112 | _key_map.insert(InputKeyMap::value_type(SDLK_SCROLLOCK, KC_SCROLL));
|
---|
113 | _key_map.insert(InputKeyMap::value_type(SDLK_KP7, KC_NUMPAD7));
|
---|
114 | _key_map.insert(InputKeyMap::value_type(SDLK_KP8, KC_NUMPAD8));
|
---|
115 | _key_map.insert(InputKeyMap::value_type(SDLK_KP9, KC_NUMPAD9));
|
---|
116 | _key_map.insert(InputKeyMap::value_type(SDLK_KP_MINUS, KC_SUBTRACT));
|
---|
117 | _key_map.insert(InputKeyMap::value_type(SDLK_KP4, KC_NUMPAD4));
|
---|
118 | _key_map.insert(InputKeyMap::value_type(SDLK_KP5, KC_NUMPAD5));
|
---|
119 | _key_map.insert(InputKeyMap::value_type(SDLK_KP6, KC_NUMPAD6));
|
---|
120 | _key_map.insert(InputKeyMap::value_type(SDLK_KP_PLUS, KC_ADD));
|
---|
121 | _key_map.insert(InputKeyMap::value_type(SDLK_KP1, KC_NUMPAD1));
|
---|
122 | _key_map.insert(InputKeyMap::value_type(SDLK_KP2, KC_NUMPAD2));
|
---|
123 | _key_map.insert(InputKeyMap::value_type(SDLK_KP3, KC_NUMPAD3));
|
---|
124 | _key_map.insert(InputKeyMap::value_type(SDLK_KP0, KC_NUMPAD0));
|
---|
125 | _key_map.insert(InputKeyMap::value_type(SDLK_KP_PERIOD, KC_DECIMAL));
|
---|
126 | _key_map.insert(InputKeyMap::value_type(SDLK_F11, KC_F11));
|
---|
127 | _key_map.insert(InputKeyMap::value_type(SDLK_F12, KC_F12));
|
---|
128 | _key_map.insert(InputKeyMap::value_type(SDLK_F13, KC_F13));
|
---|
129 | _key_map.insert(InputKeyMap::value_type(SDLK_F14, KC_F14));
|
---|
130 | _key_map.insert(InputKeyMap::value_type(SDLK_F15, KC_F15));
|
---|
131 | _key_map.insert(InputKeyMap::value_type(SDLK_KP_EQUALS, KC_NUMPADEQUALS));
|
---|
132 | _key_map.insert(InputKeyMap::value_type(SDLK_KP_DIVIDE, KC_DIVIDE));
|
---|
133 | _key_map.insert(InputKeyMap::value_type(SDLK_SYSREQ, KC_SYSRQ));
|
---|
134 | _key_map.insert(InputKeyMap::value_type(SDLK_RALT, KC_RMENU));
|
---|
135 | _key_map.insert(InputKeyMap::value_type(SDLK_HOME, KC_HOME));
|
---|
136 | _key_map.insert(InputKeyMap::value_type(SDLK_UP, KC_UP));
|
---|
137 | _key_map.insert(InputKeyMap::value_type(SDLK_PAGEUP, KC_PGUP));
|
---|
138 | _key_map.insert(InputKeyMap::value_type(SDLK_LEFT, KC_LEFT));
|
---|
139 | _key_map.insert(InputKeyMap::value_type(SDLK_RIGHT, KC_RIGHT));
|
---|
140 | _key_map.insert(InputKeyMap::value_type(SDLK_END, KC_END));
|
---|
141 | _key_map.insert(InputKeyMap::value_type(SDLK_DOWN, KC_DOWN));
|
---|
142 | _key_map.insert(InputKeyMap::value_type(SDLK_PAGEDOWN, KC_PGDOWN));
|
---|
143 | _key_map.insert(InputKeyMap::value_type(SDLK_INSERT, KC_INSERT));
|
---|
144 | _key_map.insert(InputKeyMap::value_type(SDLK_DELETE, KC_DELETE));
|
---|
145 | _key_map.insert(InputKeyMap::value_type(SDLK_LSUPER, KC_LWIN));
|
---|
146 | _key_map.insert(InputKeyMap::value_type(SDLK_RSUPER, KC_RWIN));
|
---|
147 | }
|
---|
148 |
|
---|
149 | SDLInput::~SDLInput()
|
---|
150 | {
|
---|
151 | if ( mUseMouse )
|
---|
152 | {
|
---|
153 | SDL_WM_GrabInput(SDL_GRAB_OFF);
|
---|
154 | SDL_ShowCursor(SDL_ENABLE);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | void SDLInput::initialise(RenderWindow* pWindow, bool useKeyboard, bool useMouse, bool useGameController)
|
---|
159 | {
|
---|
160 | mUseMouse = useMouse;
|
---|
161 | if ( useMouse )
|
---|
162 | {
|
---|
163 | mGrabMode = GRAB_MOUSE_CLICK;
|
---|
164 |
|
---|
165 | // is the mouse pointer over the app window?
|
---|
166 | if ( SDL_GetAppState() & SDL_APPMOUSEFOCUS )
|
---|
167 | {
|
---|
168 | _grabMouse();
|
---|
169 | }
|
---|
170 | else
|
---|
171 | {
|
---|
172 | // no, so we have to wait until the app gets
|
---|
173 | // the mouse focus
|
---|
174 | mGrabMouse = true;
|
---|
175 | mMouseLeft = true;
|
---|
176 | }
|
---|
177 | // Get the center and put the mouse there
|
---|
178 | unsigned int width, height, depth;
|
---|
179 | int left, top;
|
---|
180 | pWindow->getMetrics(width, height, depth, left, top);
|
---|
181 |
|
---|
182 | mMouseX = width / 2;
|
---|
183 | mMouseY = height / 2;
|
---|
184 | }
|
---|
185 | else
|
---|
186 | {
|
---|
187 | mGrabMode = GRAB_NONE;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | void SDLInput::_releaseMouse()
|
---|
192 | {
|
---|
193 | SDL_WM_GrabInput(SDL_GRAB_OFF);
|
---|
194 | SDL_ShowCursor(SDL_ENABLE);
|
---|
195 | mMouseGrabbed = false;
|
---|
196 | mMouseLeft = false;
|
---|
197 | mGrabMouse = true;
|
---|
198 | }
|
---|
199 |
|
---|
200 | void SDLInput::_grabMouse()
|
---|
201 | {
|
---|
202 | SDL_ShowCursor(SDL_DISABLE);
|
---|
203 | SDL_WM_GrabInput(SDL_GRAB_ON);
|
---|
204 | mMouseGrabbed = true;
|
---|
205 | mGrabMouse = false;
|
---|
206 | }
|
---|
207 |
|
---|
208 | void SDLInput::capture()
|
---|
209 | {
|
---|
210 | SDL_Event event[16];
|
---|
211 |
|
---|
212 | // Wait until we're visible again
|
---|
213 | // and throw away all SDL_Events
|
---|
214 | if (!_visible)
|
---|
215 | {
|
---|
216 | SDL_Event event;
|
---|
217 | while (SDL_WaitEvent(&event))
|
---|
218 | {
|
---|
219 | if (event.type == SDL_ACTIVEEVENT && event.active.gain == 1)
|
---|
220 | {
|
---|
221 | break;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | SDL_PumpEvents();
|
---|
227 |
|
---|
228 | // look for SDL "system" events
|
---|
229 | int count = SDL_PeepEvents( event, 16 , SDL_GETEVENT
|
---|
230 | , SDL_EVENTMASK(SDL_ACTIVEEVENT)
|
---|
231 | | SDL_EVENTMASK(SDL_VIDEORESIZE)
|
---|
232 | | SDL_EVENTMASK(SDL_VIDEOEXPOSE)
|
---|
233 | | SDL_EVENTMASK(SDL_SYSWMEVENT)
|
---|
234 | );
|
---|
235 | if ( count )
|
---|
236 | {
|
---|
237 | for (int i = 0; i < count; i++)
|
---|
238 | {
|
---|
239 | switch (event[i].type)
|
---|
240 | {
|
---|
241 | case SDL_ACTIVEEVENT:
|
---|
242 | if ( mGrabMouse && (mGrabMode == GRAB_MOUSE_OVER ) )
|
---|
243 | {
|
---|
244 | if ( event[i].active.gain )
|
---|
245 | {
|
---|
246 | if ( mMouseLeft )
|
---|
247 | {
|
---|
248 | // mouse is over the application window
|
---|
249 | _grabMouse();
|
---|
250 | }
|
---|
251 | }
|
---|
252 | else
|
---|
253 | {
|
---|
254 | mMouseLeft = true;
|
---|
255 | }
|
---|
256 | }
|
---|
257 | break;
|
---|
258 | case SDL_VIDEORESIZE:
|
---|
259 | case SDL_VIDEOEXPOSE:
|
---|
260 | case SDL_SYSWMEVENT:
|
---|
261 | break;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | // Keyboard input
|
---|
267 | if (mUseBufferedKeys)
|
---|
268 | {
|
---|
269 | processBufferedKeyboard();
|
---|
270 | }
|
---|
271 | // check for ALT + TAB to mimic windoze behaviour (useful to get the mouse
|
---|
272 | // pointer back ;-)
|
---|
273 |
|
---|
274 | // we have to call GetKeyState() even if buffered keyboard input is selected
|
---|
275 | mKeyboardBuffer = SDL_GetKeyState(NULL);
|
---|
276 | if ( mKeyboardBuffer[SDLK_LALT] && mKeyboardBuffer[SDLK_TAB] )
|
---|
277 | {
|
---|
278 | _releaseMouse();
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | if (mUseBufferedMouse)
|
---|
283 | {
|
---|
284 | processBufferedMouse();
|
---|
285 | }
|
---|
286 | else
|
---|
287 | {
|
---|
288 | mMouseKeys = 0;
|
---|
289 | mMouseRelativeX = 0; mMouseRelativeY = 0; mMouseRelativeZ = 0;
|
---|
290 |
|
---|
291 | // Get mouse info
|
---|
292 | if( 1 ) // SDL_GetAppState() & SDL_APPMOUSEFOCUS )
|
---|
293 | {
|
---|
294 | mMouseKeys = SDL_GetMouseState( &mMouseX, &mMouseY );
|
---|
295 | SDL_GetRelativeMouseState( &mMouseRelativeX, &mMouseRelativeY );
|
---|
296 |
|
---|
297 |
|
---|
298 | // get mouse wheel movement
|
---|
299 | // fetch all mouse wheel related events
|
---|
300 | int count = SDL_PeepEvents( event, 16 , SDL_GETEVENT
|
---|
301 | ,(SDL_EVENTMASK(SDL_MOUSEBUTTONDOWN)
|
---|
302 | | SDL_EVENTMASK(SDL_MOUSEBUTTONUP)
|
---|
303 | | SDL_EVENTMASK(SDL_MOUSEMOTION)
|
---|
304 | ));
|
---|
305 | if ( count )
|
---|
306 | {
|
---|
307 | for (int i = 0; i < count; i++)
|
---|
308 | {
|
---|
309 | switch (event[i].type)
|
---|
310 | {
|
---|
311 | case SDL_MOUSEBUTTONDOWN:
|
---|
312 | case SDL_MOUSEBUTTONUP:
|
---|
313 | // grab the mouse if the user presses a mouse button
|
---|
314 | if ( !mMouseGrabbed && mGrabMouse && ( mGrabMode == GRAB_MOUSE_CLICK ) )
|
---|
315 | {
|
---|
316 | _grabMouse();
|
---|
317 | }
|
---|
318 | switch(event[i].button.button)
|
---|
319 | {
|
---|
320 | case SDL_BUTTON_WHEELUP:
|
---|
321 | mMouseRelativeZ += mWheelStep;
|
---|
322 | break;
|
---|
323 | case SDL_BUTTON_WHEELDOWN:
|
---|
324 | mMouseRelativeZ -= mWheelStep;
|
---|
325 | break;
|
---|
326 | }
|
---|
327 | break;
|
---|
328 | }
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | mMouseState.Xabs = mMouseX;
|
---|
334 | mMouseState.Yabs = mMouseY;
|
---|
335 | mMouseState.Zabs = 0;
|
---|
336 |
|
---|
337 | mMouseState.Xrel = mMouseRelativeX;
|
---|
338 | mMouseState.Yrel = mMouseRelativeY;
|
---|
339 | mMouseState.Zrel = mMouseRelativeZ;
|
---|
340 |
|
---|
341 | mMouseState.Buttons = (mMouseKeys & SDL_BUTTON_LMASK) ? 1 : 0; // left
|
---|
342 | mMouseState.Buttons |= (mMouseKeys & SDL_BUTTON_RMASK) ? 2 : 0; // right
|
---|
343 | mMouseState.Buttons |= (mMouseKeys & SDL_BUTTON_MMASK) ? 4 : 0; // middle
|
---|
344 |
|
---|
345 | }
|
---|
346 |
|
---|
347 | // XXX Fix me up
|
---|
348 | // Game controller state
|
---|
349 | }
|
---|
350 |
|
---|
351 | bool SDLInput::isKeyDownImmediate(KeyCode kc) const
|
---|
352 | {
|
---|
353 | assert(mKeyboardBuffer && "SDLInput::isKeyDownImmediate called before SDLInput::capture has initialized it");
|
---|
354 |
|
---|
355 | switch(kc)
|
---|
356 | {
|
---|
357 | case KC_ESCAPE:
|
---|
358 | return mKeyboardBuffer[SDLK_ESCAPE];
|
---|
359 | case KC_1:
|
---|
360 | return mKeyboardBuffer[SDLK_1];
|
---|
361 | case KC_2:
|
---|
362 | return mKeyboardBuffer[SDLK_2];
|
---|
363 | case KC_3:
|
---|
364 | return mKeyboardBuffer[SDLK_3];
|
---|
365 | case KC_4:
|
---|
366 | return mKeyboardBuffer[SDLK_4];
|
---|
367 | case KC_5:
|
---|
368 | return mKeyboardBuffer[SDLK_5];
|
---|
369 | case KC_6:
|
---|
370 | return mKeyboardBuffer[SDLK_6];
|
---|
371 | case KC_7:
|
---|
372 | return mKeyboardBuffer[SDLK_7];
|
---|
373 | case KC_8:
|
---|
374 | return mKeyboardBuffer[SDLK_8];
|
---|
375 | case KC_9:
|
---|
376 | return mKeyboardBuffer[SDLK_9];
|
---|
377 | case KC_0:
|
---|
378 | return mKeyboardBuffer[SDLK_0];
|
---|
379 | case KC_MINUS: /* - on main keyboard */
|
---|
380 | return mKeyboardBuffer[SDLK_MINUS];
|
---|
381 | case KC_EQUALS:
|
---|
382 | return mKeyboardBuffer[SDLK_EQUALS];
|
---|
383 | case KC_BACK: /* backspace */
|
---|
384 | return mKeyboardBuffer[SDLK_BACKSPACE];
|
---|
385 | case KC_TAB:
|
---|
386 | return mKeyboardBuffer[SDLK_TAB];
|
---|
387 | case KC_Q:
|
---|
388 | return mKeyboardBuffer[SDLK_q];
|
---|
389 | case KC_W:
|
---|
390 | return mKeyboardBuffer[SDLK_w];
|
---|
391 | case KC_E:
|
---|
392 | return mKeyboardBuffer[SDLK_e];
|
---|
393 | case KC_R:
|
---|
394 | return mKeyboardBuffer[SDLK_r];
|
---|
395 | case KC_T:
|
---|
396 | return mKeyboardBuffer[SDLK_t];
|
---|
397 | case KC_Y:
|
---|
398 | return mKeyboardBuffer[SDLK_y];
|
---|
399 | case KC_U:
|
---|
400 | return mKeyboardBuffer[SDLK_u];
|
---|
401 | case KC_I:
|
---|
402 | return mKeyboardBuffer[SDLK_i];
|
---|
403 | case KC_O:
|
---|
404 | return mKeyboardBuffer[SDLK_o];
|
---|
405 | case KC_P:
|
---|
406 | return mKeyboardBuffer[SDLK_p];
|
---|
407 | case KC_LBRACKET:
|
---|
408 | case KC_RBRACKET:
|
---|
409 | break;
|
---|
410 | case KC_RETURN: /* Enter on main keyboard */
|
---|
411 | return mKeyboardBuffer[SDLK_RETURN];
|
---|
412 | case KC_LCONTROL:
|
---|
413 | return mKeyboardBuffer[SDLK_LCTRL];
|
---|
414 | case KC_A:
|
---|
415 | return mKeyboardBuffer[SDLK_a];
|
---|
416 | case KC_S:
|
---|
417 | return mKeyboardBuffer[SDLK_s];
|
---|
418 | case KC_D:
|
---|
419 | return mKeyboardBuffer[SDLK_d];
|
---|
420 | case KC_F:
|
---|
421 | return mKeyboardBuffer[SDLK_f];
|
---|
422 | case KC_G:
|
---|
423 | return mKeyboardBuffer[SDLK_g];
|
---|
424 | case KC_H:
|
---|
425 | return mKeyboardBuffer[SDLK_h];
|
---|
426 | case KC_J:
|
---|
427 | return mKeyboardBuffer[SDLK_j];
|
---|
428 | case KC_K:
|
---|
429 | return mKeyboardBuffer[SDLK_k];
|
---|
430 | case KC_L:
|
---|
431 | return mKeyboardBuffer[SDLK_l];
|
---|
432 | case KC_SEMICOLON:
|
---|
433 | return mKeyboardBuffer[SDLK_SEMICOLON];
|
---|
434 | case KC_APOSTROPHE:
|
---|
435 | return mKeyboardBuffer[SDLK_QUOTE];
|
---|
436 | case KC_GRAVE: /* accent grave */
|
---|
437 | return mKeyboardBuffer[SDLK_BACKQUOTE];
|
---|
438 | case KC_LSHIFT:
|
---|
439 | return mKeyboardBuffer[SDLK_LSHIFT];
|
---|
440 | case KC_BACKSLASH:
|
---|
441 | return mKeyboardBuffer[SDLK_BACKSLASH];
|
---|
442 | case KC_Z:
|
---|
443 | return mKeyboardBuffer[SDLK_z];
|
---|
444 | case KC_X:
|
---|
445 | return mKeyboardBuffer[SDLK_x];
|
---|
446 | case KC_C:
|
---|
447 | return mKeyboardBuffer[SDLK_c];
|
---|
448 | case KC_V:
|
---|
449 | return mKeyboardBuffer[SDLK_v];
|
---|
450 | case KC_B:
|
---|
451 | return mKeyboardBuffer[SDLK_b];
|
---|
452 | case KC_N:
|
---|
453 | return mKeyboardBuffer[SDLK_n];
|
---|
454 | case KC_M:
|
---|
455 | return mKeyboardBuffer[SDLK_m];
|
---|
456 | case KC_COMMA:
|
---|
457 | return mKeyboardBuffer[SDLK_COMMA];
|
---|
458 | case KC_PERIOD: /* . on main keyboard */
|
---|
459 | return mKeyboardBuffer[SDLK_PERIOD];
|
---|
460 | case KC_SLASH: /* '/' on main keyboard */
|
---|
461 | break;
|
---|
462 | case KC_RSHIFT:
|
---|
463 | return mKeyboardBuffer[SDLK_RSHIFT];
|
---|
464 | case KC_MULTIPLY: /* * on numeric keypad */
|
---|
465 | return mKeyboardBuffer[SDLK_KP_MULTIPLY];
|
---|
466 | case KC_LMENU: /* left Alt */
|
---|
467 | return mKeyboardBuffer[SDLK_LALT];
|
---|
468 | case KC_SPACE:
|
---|
469 | return mKeyboardBuffer[SDLK_SPACE];
|
---|
470 | case KC_CAPITAL:
|
---|
471 | return mKeyboardBuffer[SDLK_CAPSLOCK];
|
---|
472 | case KC_F1:
|
---|
473 | return mKeyboardBuffer[SDLK_F1];
|
---|
474 | case KC_F2:
|
---|
475 | return mKeyboardBuffer[SDLK_F2];
|
---|
476 | case KC_F3:
|
---|
477 | return mKeyboardBuffer[SDLK_F3];
|
---|
478 | case KC_F4:
|
---|
479 | return mKeyboardBuffer[SDLK_F4];
|
---|
480 | case KC_F5:
|
---|
481 | return mKeyboardBuffer[SDLK_F5];
|
---|
482 | case KC_F6:
|
---|
483 | return mKeyboardBuffer[SDLK_F6];
|
---|
484 | case KC_F7:
|
---|
485 | return mKeyboardBuffer[SDLK_F7];
|
---|
486 | case KC_F8:
|
---|
487 | return mKeyboardBuffer[SDLK_F8];
|
---|
488 | case KC_F9:
|
---|
489 | return mKeyboardBuffer[SDLK_F9];
|
---|
490 | case KC_F10:
|
---|
491 | return mKeyboardBuffer[SDLK_F10];
|
---|
492 | case KC_NUMLOCK:
|
---|
493 | return mKeyboardBuffer[SDLK_NUMLOCK];
|
---|
494 | case KC_SCROLL: /* Scroll Lock */
|
---|
495 | return mKeyboardBuffer[SDLK_SCROLLOCK];
|
---|
496 | case KC_NUMPAD7:
|
---|
497 | return mKeyboardBuffer[SDLK_KP7];
|
---|
498 | case KC_NUMPAD8:
|
---|
499 | return mKeyboardBuffer[SDLK_KP8];
|
---|
500 | case KC_NUMPAD9:
|
---|
501 | return mKeyboardBuffer[SDLK_KP9];
|
---|
502 | case KC_SUBTRACT: /* - on numeric keypad */
|
---|
503 | return mKeyboardBuffer[SDLK_KP_MINUS];
|
---|
504 | case KC_NUMPAD4:
|
---|
505 | return mKeyboardBuffer[SDLK_KP4];
|
---|
506 | case KC_NUMPAD5:
|
---|
507 | return mKeyboardBuffer[SDLK_KP5];
|
---|
508 | case KC_NUMPAD6:
|
---|
509 | return mKeyboardBuffer[SDLK_KP6];
|
---|
510 | case KC_ADD: /* + on numeric keypad */
|
---|
511 | return mKeyboardBuffer[SDLK_KP_PLUS];
|
---|
512 | case KC_NUMPAD1:
|
---|
513 | return mKeyboardBuffer[SDLK_KP1];
|
---|
514 | case KC_NUMPAD2:
|
---|
515 | return mKeyboardBuffer[SDLK_KP2];
|
---|
516 | case KC_NUMPAD3:
|
---|
517 | return mKeyboardBuffer[SDLK_KP3];
|
---|
518 | case KC_NUMPAD0:
|
---|
519 | return mKeyboardBuffer[SDLK_KP0];
|
---|
520 | case KC_DECIMAL: /* . on numeric keypad */
|
---|
521 | return mKeyboardBuffer[SDLK_KP_PERIOD];
|
---|
522 | case KC_OEM_102: /* < > | on UK/Germany keyboards */
|
---|
523 | break;
|
---|
524 | case KC_F11:
|
---|
525 | return mKeyboardBuffer[SDLK_F11];
|
---|
526 | case KC_F12:
|
---|
527 | return mKeyboardBuffer[SDLK_F12];
|
---|
528 | case KC_F13: /* (NEC PC98) */
|
---|
529 | return mKeyboardBuffer[SDLK_F13];
|
---|
530 | case KC_F14: /* (NEC PC98) */
|
---|
531 | return mKeyboardBuffer[SDLK_F14];
|
---|
532 | case KC_F15: /* (NEC PC98) */
|
---|
533 | return mKeyboardBuffer[SDLK_F15];
|
---|
534 | case KC_KANA: /* (Japanese keyboard) */
|
---|
535 | case KC_ABNT_C1: /* / ? on Portugese (Brazilian) keyboards */
|
---|
536 | case KC_CONVERT: /* (Japanese keyboard) */
|
---|
537 | case KC_NOCONVERT: /* (Japanese keyboard) */
|
---|
538 | case KC_YEN: /* (Japanese keyboard) */
|
---|
539 | case KC_ABNT_C2: /* Numpad . on Portugese (Brazilian) keyboards */
|
---|
540 | break;
|
---|
541 | case KC_NUMPADEQUALS: /* = on numeric keypad (NEC PC98) */
|
---|
542 | return mKeyboardBuffer[SDLK_KP_EQUALS];
|
---|
543 | case KC_PREVTRACK: /* Previous Track (KC_CIRCUMFLEX on Japanese keyboard) */
|
---|
544 | case KC_AT: /* (NEC PC98) */
|
---|
545 | case KC_COLON: /* (NEC PC98) */
|
---|
546 | return mKeyboardBuffer[SDLK_COLON];
|
---|
547 | case KC_UNDERLINE: /* (NEC PC98) */
|
---|
548 | case KC_KANJI: /* (Japanese keyboard) */
|
---|
549 | case KC_STOP: /* (NEC PC98) */
|
---|
550 | case KC_AX: /* (Japan AX) */
|
---|
551 | case KC_UNLABELED: /* (J3100) */
|
---|
552 | case KC_NEXTTRACK: /* Next Track */
|
---|
553 | case KC_NUMPADENTER: /* Enter on numeric keypad */
|
---|
554 | case KC_RCONTROL:
|
---|
555 | case KC_MUTE: /* Mute */
|
---|
556 | case KC_CALCULATOR: /* Calculator */
|
---|
557 | case KC_PLAYPAUSE: /* Play / Pause */
|
---|
558 | case KC_MEDIASTOP: /* Media Stop */
|
---|
559 | case KC_VOLUMEDOWN: /* Volume - */
|
---|
560 | case KC_VOLUMEUP: /* Volume + */
|
---|
561 | case KC_WEBHOME: /* Web home */
|
---|
562 | case KC_NUMPADCOMMA: /* , on numeric keypad (NEC PC98) */
|
---|
563 | break;
|
---|
564 | case KC_DIVIDE: /* / on numeric keypad */
|
---|
565 | return mKeyboardBuffer[SDLK_KP_DIVIDE];
|
---|
566 | case KC_SYSRQ:
|
---|
567 | return mKeyboardBuffer[SDLK_SYSREQ] | mKeyboardBuffer[SDLK_PRINT];
|
---|
568 | case KC_RMENU: /* right Alt */
|
---|
569 | return mKeyboardBuffer[SDLK_RALT];
|
---|
570 | case KC_PAUSE: /* Pause */
|
---|
571 | break;
|
---|
572 | case KC_HOME: /* Home on arrow keypad */
|
---|
573 | return mKeyboardBuffer[SDLK_HOME];
|
---|
574 | case KC_UP: /* UpArrow on arrow keypad */
|
---|
575 | return mKeyboardBuffer[SDLK_UP];
|
---|
576 | case KC_PGUP: /* PgUp on arrow keypad */
|
---|
577 | return mKeyboardBuffer[SDLK_PAGEUP];
|
---|
578 | case KC_LEFT: /* LeftArrow on arrow keypad */
|
---|
579 | return mKeyboardBuffer[SDLK_LEFT];
|
---|
580 | case KC_RIGHT: /* RightArrow on arrow keypad */
|
---|
581 | return mKeyboardBuffer[SDLK_RIGHT];
|
---|
582 | case KC_END: /* End on arrow keypad */
|
---|
583 | return mKeyboardBuffer[SDLK_END];
|
---|
584 | case KC_DOWN: /* DownArrow on arrow keypad */
|
---|
585 | return mKeyboardBuffer[SDLK_DOWN];
|
---|
586 | case KC_PGDOWN: /* PgDn on arrow keypad */
|
---|
587 | return mKeyboardBuffer[SDLK_PAGEDOWN];
|
---|
588 | case KC_INSERT: /* Insert on arrow keypad */
|
---|
589 | return mKeyboardBuffer[SDLK_INSERT];
|
---|
590 | case KC_DELETE: /* Delete on arrow keypad */
|
---|
591 | return mKeyboardBuffer[SDLK_DELETE];
|
---|
592 | case KC_LWIN: /* Left Windows key */
|
---|
593 | return mKeyboardBuffer[SDLK_LSUPER];
|
---|
594 | case KC_RWIN: /* Right Windows key */
|
---|
595 | return mKeyboardBuffer[SDLK_RSUPER];
|
---|
596 | case KC_APPS: /* AppMenu key */
|
---|
597 | case KC_POWER: /* System Power */
|
---|
598 | case KC_SLEEP: /* System Sleep */
|
---|
599 | case KC_WAKE: /* System Wake */
|
---|
600 | case KC_WEBSEARCH: /* Web Search */
|
---|
601 | case KC_WEBFAVORITES: /* Web Favorites */
|
---|
602 | case KC_WEBREFRESH: /* Web Refresh */
|
---|
603 | case KC_WEBSTOP: /* Web Stop */
|
---|
604 | case KC_WEBFORWARD: /* Web Forward */
|
---|
605 | case KC_WEBBACK: /* Web Back */
|
---|
606 | case KC_MYCOMPUTER: /* My Computer */
|
---|
607 | case KC_MAIL: /* Mail */
|
---|
608 | case KC_MEDIASELECT: /* Media Select */
|
---|
609 | break;
|
---|
610 | default:
|
---|
611 | break;
|
---|
612 | };
|
---|
613 |
|
---|
614 | return 0;
|
---|
615 | }
|
---|
616 |
|
---|
617 | long SDLInput::getMouseRelX() const
|
---|
618 | {
|
---|
619 | return mMouseState.Xrel;
|
---|
620 | }
|
---|
621 |
|
---|
622 | long SDLInput::getMouseRelY() const
|
---|
623 | {
|
---|
624 | return mMouseState.Yrel;
|
---|
625 | }
|
---|
626 |
|
---|
627 | long SDLInput::getMouseRelZ() const
|
---|
628 | {
|
---|
629 | return mMouseState.Zrel;
|
---|
630 | }
|
---|
631 |
|
---|
632 | long SDLInput::getMouseAbsX() const
|
---|
633 | {
|
---|
634 | return mMouseX;
|
---|
635 | }
|
---|
636 |
|
---|
637 | long SDLInput::getMouseAbsY() const
|
---|
638 | {
|
---|
639 | return mMouseY;
|
---|
640 | }
|
---|
641 |
|
---|
642 | long SDLInput::getMouseAbsZ() const
|
---|
643 | {
|
---|
644 | return 0;
|
---|
645 | }
|
---|
646 |
|
---|
647 | bool SDLInput::getMouseButton( uchar button ) const
|
---|
648 | {
|
---|
649 | return mMouseState.isButtonDown( button );
|
---|
650 | }
|
---|
651 |
|
---|
652 | void SDLInput::processBufferedKeyboard()
|
---|
653 | {
|
---|
654 | // XXX Arbitrarily picked 16
|
---|
655 | SDL_Event events[16];
|
---|
656 |
|
---|
657 | int count = SDL_PeepEvents(events, 16, SDL_GETEVENT,
|
---|
658 | (SDL_KEYDOWNMASK | SDL_KEYUPMASK));
|
---|
659 | if (!count)
|
---|
660 | {
|
---|
661 | return;
|
---|
662 | }
|
---|
663 |
|
---|
664 | for (int i = 0; i < count; i++)
|
---|
665 | {
|
---|
666 | bool down = false;
|
---|
667 | if (events[i].type == SDL_KEYDOWN)
|
---|
668 | down = true;
|
---|
669 | keyChanged(_key_map[events[i].key.keysym.sym], down);
|
---|
670 | }
|
---|
671 | }
|
---|
672 |
|
---|
673 | void SDLInput::processBufferedMouse()
|
---|
674 | {
|
---|
675 | // XXX Arbitrarily picked 16
|
---|
676 | SDL_Event events[16];
|
---|
677 |
|
---|
678 | int count = SDL_PeepEvents(events, 16, SDL_GETEVENT,
|
---|
679 | (SDL_MOUSEMOTIONMASK | SDL_MOUSEBUTTONDOWNMASK |
|
---|
680 | SDL_MOUSEBUTTONUPMASK | SDL_ACTIVEEVENTMASK));
|
---|
681 | if (!count)
|
---|
682 | {
|
---|
683 | return;
|
---|
684 | }
|
---|
685 |
|
---|
686 | bool Xset, Yset, Zset;
|
---|
687 | Xset = Yset = Zset = false;
|
---|
688 | for (int i = 0; i < count; i++)
|
---|
689 | {
|
---|
690 | int button_mask = -1;
|
---|
691 | bool button_down = false;
|
---|
692 | switch (events[i].type)
|
---|
693 | {
|
---|
694 | case SDL_ACTIVEEVENT:
|
---|
695 | _visible = events[i].active.gain ? true : false;
|
---|
696 | break;
|
---|
697 | case SDL_MOUSEMOTION:
|
---|
698 | if (events[i].motion.xrel)
|
---|
699 | {
|
---|
700 | if (Xset)
|
---|
701 | {
|
---|
702 | mouseMoved();
|
---|
703 | }
|
---|
704 | mCursor->addToX(events[i].motion.xrel * mMouseScale);
|
---|
705 | Xset = true;
|
---|
706 | }
|
---|
707 | if (events[i].motion.yrel)
|
---|
708 | {
|
---|
709 | if (Yset)
|
---|
710 | {
|
---|
711 | mouseMoved();
|
---|
712 | }
|
---|
713 | mCursor->addToY(events[i].motion.yrel * mMouseScale);
|
---|
714 | Yset = true;
|
---|
715 | }
|
---|
716 | break;
|
---|
717 | case SDL_MOUSEBUTTONDOWN:
|
---|
718 | button_down = true;
|
---|
719 | case SDL_MOUSEBUTTONUP:
|
---|
720 | // grab the mouse if the user presses a mouse button
|
---|
721 | if ( !mMouseGrabbed && mGrabMouse && ( mGrabMode == GRAB_MOUSE_CLICK ) )
|
---|
722 | {
|
---|
723 | _grabMouse();
|
---|
724 | }
|
---|
725 | switch(events[i].button.button)
|
---|
726 | {
|
---|
727 | case SDL_BUTTON_LEFT:
|
---|
728 | button_mask = InputEvent::BUTTON0_MASK;
|
---|
729 | break;
|
---|
730 | case SDL_BUTTON_RIGHT:
|
---|
731 | button_mask = InputEvent::BUTTON1_MASK;
|
---|
732 | break;
|
---|
733 | case SDL_BUTTON_MIDDLE:
|
---|
734 | button_mask = InputEvent::BUTTON2_MASK;
|
---|
735 | break;
|
---|
736 | };
|
---|
737 | triggerMouseButton(button_mask, button_down);
|
---|
738 | break;
|
---|
739 | };
|
---|
740 |
|
---|
741 | if (Xset && Yset)
|
---|
742 | {
|
---|
743 | mouseMoved();
|
---|
744 | Xset = Yset = false;
|
---|
745 | }
|
---|
746 | }
|
---|
747 |
|
---|
748 | if (Xset || Yset)
|
---|
749 | {
|
---|
750 | mouseMoved();
|
---|
751 | }
|
---|
752 |
|
---|
753 | }
|
---|
754 |
|
---|
755 | void SDLInput::getMouseState( MouseState& state ) const
|
---|
756 | {
|
---|
757 | memcpy( &state, &mMouseState, sizeof( MouseState ) );
|
---|
758 | }
|
---|
759 |
|
---|
760 | }
|
---|