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