FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
librocketinputprocessor.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <iostream>
24 
25 // 3rd party library includes
26 #include <Rocket/Core/Context.h>
27 #include <Rocket/Core/Input.h>
28 
29 // FIFE includes
30 // These includes are split up in two parts, separated by one empty line
31 // First block: files included from the FIFE root src directory
32 // Second block: files included from the same folder
33 
35 
36 namespace FIFE {
37 
38  LibRocketInputProcessor::LibRocketInputProcessor(Rocket::Core::Context* context)
39  :
40  m_context(context),
41  m_keyModState(0),
42  m_wheelCounter(0) {
44  }
45 
47  }
48 
50 
51  SDLMod modState = SDL_GetModState();
52 
53  m_keyModState = 0;
54 
55  if((modState & KMOD_NONE) != KMOD_NONE) {
56 
57  if((modState & KMOD_SHIFT) == KMOD_SHIFT) {
58  m_keyModState |= Rocket::Core::Input::KM_SHIFT;
59  }
60 
61  if((modState & KMOD_CTRL) == KMOD_CTRL) {
62  m_keyModState |= Rocket::Core::Input::KM_CTRL;
63  }
64 
65  if((modState & KMOD_ALT) == KMOD_ALT) {
66  m_keyModState |= Rocket::Core::Input::KM_ALT;
67  }
68 
69  if((modState & KMOD_META) == KMOD_META) {
70  m_keyModState |= Rocket::Core::Input::KM_META;
71  }
72 
73  if((modState & KMOD_NUM) == KMOD_NUM) {
74  m_keyModState |= Rocket::Core::Input::KM_NUMLOCK;
75  }
76 
77  if((modState & KMOD_CAPS) == KMOD_CAPS) {
78  m_keyModState |= Rocket::Core::Input::KM_CAPSLOCK;
79  }
80 
81  }
82 
83  }
84 
85  bool LibRocketInputProcessor::onSdlEvent(SDL_Event& event) {
86 
87  bool consumed = false;
88 
90 
91  switch(event.type) {
92 
93  case SDL_KEYUP:
94  case SDL_KEYDOWN:
95  consumed = processKeyInput(event);
96  break;
97 
98  case SDL_MOUSEBUTTONUP:
99  case SDL_MOUSEBUTTONDOWN:
100  consumed = processMouseInput(event);
101  break;
102 
103  case SDL_MOUSEMOTION:
104  consumed = processMouseMotion(event);
105  break;
106 
107  default:
108  break;
109  }
110 
111  return consumed;
112  }
113 
115  if(m_wheelCounter != 0) {
116  m_context->ProcessMouseWheel(m_wheelCounter, m_keyModState);
117  m_wheelCounter = 0;
118  }
119  }
120 
122 
123  int x = static_cast<int>(event.motion.x);
124  int y = static_cast<int>(event.motion.y);
125 
126  m_context->ProcessMouseMove(x, y, m_keyModState);
127 
128  return false;
129  }
130 
132 
133  int index = (event.button.button == SDL_BUTTON_LEFT) ? 0 :
134  (event.button.button == SDL_BUTTON_RIGHT) ? 1 :
135  (event.button.button == SDL_BUTTON_MIDDLE) ? 2 : 3;
136 
137 
138  if(event.type == SDL_MOUSEBUTTONDOWN) {
139  m_context->ProcessMouseButtonDown(index, m_keyModState);
140  }
141  else if (event.type == SDL_MOUSEBUTTONUP) {
142 
143  if(event.button.button == SDL_BUTTON_WHEELUP || event.button.button == SDL_BUTTON_WHEELDOWN) {
145  }
146  else {
147  m_context->ProcessMouseButtonUp(index, m_keyModState);
148  }
149  }
150 
151  return false;
152  }
153 
155 
156  if(event.button.button == SDL_BUTTON_WHEELUP) {
157  if(m_wheelCounter <= 0) {
158  m_wheelCounter--;
159  } else {
160  //the wheel had been moving downwards so sent those movements before reseting the counter
161  m_context->ProcessMouseWheel(m_wheelCounter, m_keyModState);
162  m_wheelCounter = -1;
163  }
164  }
165  else if(event.button.button == SDL_BUTTON_WHEELDOWN) {
166  if(m_wheelCounter >= 0) {
167  m_wheelCounter++;
168  } else {
169  //the wheel had been moving upwards so sent those movements before reseting the counter
170  m_context->ProcessMouseWheel(m_wheelCounter, m_keyModState);
171  m_wheelCounter = 1;
172  }
173  }
174 
175  return false;
176  }
177 
179 
180  uint16_t unicode = event.key.keysym.unicode;
181 
182  Rocket::Core::Input::KeyIdentifier key = m_keyMap[event.key.keysym.sym];
183 
184  if(event.type == SDL_KEYDOWN) {
185 
186  m_context->ProcessKeyDown(key, m_keyModState);
187 
188  if(unicode >= 32) {
189  m_context->ProcessTextInput(unicode);
190  }
191 
192  if(key == Rocket::Core::Input::KI_RETURN) {
193  m_context->ProcessTextInput((Rocket::Core::word) '\n');
194  }
195 
196  } else {
197  m_context->ProcessKeyUp(m_keyMap[event.key.keysym.sym], m_keyModState);
198  }
199 
200  return false;
201  }
202 
204 
205  m_keyMap[SDLK_UNKNOWN] = Rocket::Core::Input::KI_UNKNOWN;
206  m_keyMap[SDLK_SPACE] = Rocket::Core::Input::KI_SPACE;
207  m_keyMap[SDLK_0] = Rocket::Core::Input::KI_0;
208  m_keyMap[SDLK_1] = Rocket::Core::Input::KI_1;
209  m_keyMap[SDLK_2] = Rocket::Core::Input::KI_2;
210  m_keyMap[SDLK_3] = Rocket::Core::Input::KI_3;
211  m_keyMap[SDLK_4] = Rocket::Core::Input::KI_4;
212  m_keyMap[SDLK_5] = Rocket::Core::Input::KI_5;
213  m_keyMap[SDLK_6] = Rocket::Core::Input::KI_6;
214  m_keyMap[SDLK_7] = Rocket::Core::Input::KI_7;
215  m_keyMap[SDLK_8] = Rocket::Core::Input::KI_8;
216  m_keyMap[SDLK_9] = Rocket::Core::Input::KI_9;
217  m_keyMap[SDLK_a] = Rocket::Core::Input::KI_A;
218  m_keyMap[SDLK_b] = Rocket::Core::Input::KI_B;
219  m_keyMap[SDLK_c] = Rocket::Core::Input::KI_C;
220  m_keyMap[SDLK_d] = Rocket::Core::Input::KI_D;
221  m_keyMap[SDLK_e] = Rocket::Core::Input::KI_E;
222  m_keyMap[SDLK_f] = Rocket::Core::Input::KI_F;
223  m_keyMap[SDLK_g] = Rocket::Core::Input::KI_G;
224  m_keyMap[SDLK_h] = Rocket::Core::Input::KI_H;
225  m_keyMap[SDLK_i] = Rocket::Core::Input::KI_I;
226  m_keyMap[SDLK_j] = Rocket::Core::Input::KI_J;
227  m_keyMap[SDLK_k] = Rocket::Core::Input::KI_K;
228  m_keyMap[SDLK_l] = Rocket::Core::Input::KI_L;
229  m_keyMap[SDLK_m] = Rocket::Core::Input::KI_M;
230  m_keyMap[SDLK_n] = Rocket::Core::Input::KI_N;
231  m_keyMap[SDLK_o] = Rocket::Core::Input::KI_O;
232  m_keyMap[SDLK_p] = Rocket::Core::Input::KI_P;
233  m_keyMap[SDLK_q] = Rocket::Core::Input::KI_Q;
234  m_keyMap[SDLK_r] = Rocket::Core::Input::KI_R;
235  m_keyMap[SDLK_s] = Rocket::Core::Input::KI_S;
236  m_keyMap[SDLK_t] = Rocket::Core::Input::KI_T;
237  m_keyMap[SDLK_u] = Rocket::Core::Input::KI_U;
238  m_keyMap[SDLK_v] = Rocket::Core::Input::KI_V;
239  m_keyMap[SDLK_w] = Rocket::Core::Input::KI_W;
240  m_keyMap[SDLK_x] = Rocket::Core::Input::KI_X;
241  m_keyMap[SDLK_y] = Rocket::Core::Input::KI_Y;
242  m_keyMap[SDLK_z] = Rocket::Core::Input::KI_Z;
243  m_keyMap[SDLK_SEMICOLON] = Rocket::Core::Input::KI_OEM_1;
244  m_keyMap[SDLK_PLUS] = Rocket::Core::Input::KI_OEM_PLUS;
245  m_keyMap[SDLK_COMMA] = Rocket::Core::Input::KI_OEM_COMMA;
246  m_keyMap[SDLK_MINUS] = Rocket::Core::Input::KI_OEM_MINUS;
247  m_keyMap[SDLK_PERIOD] = Rocket::Core::Input::KI_OEM_PERIOD;
248  m_keyMap[SDLK_SLASH] = Rocket::Core::Input::KI_OEM_2;
249  m_keyMap[SDLK_BACKQUOTE] = Rocket::Core::Input::KI_OEM_3;
250  m_keyMap[SDLK_LEFTBRACKET] = Rocket::Core::Input::KI_OEM_4;
251  m_keyMap[SDLK_BACKSLASH] = Rocket::Core::Input::KI_OEM_5;
252  m_keyMap[SDLK_RIGHTBRACKET] = Rocket::Core::Input::KI_OEM_6;
253  m_keyMap[SDLK_QUOTEDBL] = Rocket::Core::Input::KI_OEM_7;
254  m_keyMap[SDLK_KP0] = Rocket::Core::Input::KI_NUMPAD0;
255  m_keyMap[SDLK_KP1] = Rocket::Core::Input::KI_NUMPAD1;
256  m_keyMap[SDLK_KP2] = Rocket::Core::Input::KI_NUMPAD2;
257  m_keyMap[SDLK_KP3] = Rocket::Core::Input::KI_NUMPAD3;
258  m_keyMap[SDLK_KP4] = Rocket::Core::Input::KI_NUMPAD4;
259  m_keyMap[SDLK_KP5] = Rocket::Core::Input::KI_NUMPAD5;
260  m_keyMap[SDLK_KP6] = Rocket::Core::Input::KI_NUMPAD6;
261  m_keyMap[SDLK_KP7] = Rocket::Core::Input::KI_NUMPAD7;
262  m_keyMap[SDLK_KP8] = Rocket::Core::Input::KI_NUMPAD8;
263  m_keyMap[SDLK_KP9] = Rocket::Core::Input::KI_NUMPAD9;
264  m_keyMap[SDLK_KP_ENTER] = Rocket::Core::Input::KI_NUMPADENTER;
265  m_keyMap[SDLK_KP_MULTIPLY] = Rocket::Core::Input::KI_MULTIPLY;
266  m_keyMap[SDLK_KP_PLUS] = Rocket::Core::Input::KI_ADD;
267  m_keyMap[SDLK_KP_MINUS] = Rocket::Core::Input::KI_SUBTRACT;
268  m_keyMap[SDLK_KP_PERIOD] = Rocket::Core::Input::KI_DECIMAL;
269  m_keyMap[SDLK_KP_DIVIDE] = Rocket::Core::Input::KI_DIVIDE;
270  m_keyMap[SDLK_KP_EQUALS] = Rocket::Core::Input::KI_OEM_NEC_EQUAL;
271  m_keyMap[SDLK_BACKSPACE] = Rocket::Core::Input::KI_BACK;
272  m_keyMap[SDLK_TAB] = Rocket::Core::Input::KI_TAB;
273  m_keyMap[SDLK_CLEAR] = Rocket::Core::Input::KI_CLEAR;
274  m_keyMap[SDLK_RETURN] = Rocket::Core::Input::KI_RETURN;
275  m_keyMap[SDLK_PAUSE] = Rocket::Core::Input::KI_PAUSE;
276  m_keyMap[SDLK_CAPSLOCK] = Rocket::Core::Input::KI_CAPITAL;
277  m_keyMap[SDLK_PAGEUP] = Rocket::Core::Input::KI_PRIOR;
278  m_keyMap[SDLK_PAGEDOWN] = Rocket::Core::Input::KI_NEXT;
279  m_keyMap[SDLK_END] = Rocket::Core::Input::KI_END;
280  m_keyMap[SDLK_HOME] = Rocket::Core::Input::KI_HOME;
281  m_keyMap[SDLK_LEFT] = Rocket::Core::Input::KI_LEFT;
282  m_keyMap[SDLK_UP] = Rocket::Core::Input::KI_UP;
283  m_keyMap[SDLK_RIGHT] = Rocket::Core::Input::KI_RIGHT;
284  m_keyMap[SDLK_DOWN] = Rocket::Core::Input::KI_DOWN;
285  m_keyMap[SDLK_INSERT] = Rocket::Core::Input::KI_INSERT;
286  m_keyMap[SDLK_DELETE] = Rocket::Core::Input::KI_DELETE;
287  m_keyMap[SDLK_HELP] = Rocket::Core::Input::KI_HELP;
288  m_keyMap[SDLK_LSUPER] = Rocket::Core::Input::KI_LWIN;
289  m_keyMap[SDLK_RSUPER] = Rocket::Core::Input::KI_RWIN;
290  m_keyMap[SDLK_F1] = Rocket::Core::Input::KI_F1;
291  m_keyMap[SDLK_F2] = Rocket::Core::Input::KI_F2;
292  m_keyMap[SDLK_F3] = Rocket::Core::Input::KI_F3;
293  m_keyMap[SDLK_F4] = Rocket::Core::Input::KI_F4;
294  m_keyMap[SDLK_F5] = Rocket::Core::Input::KI_F5;
295  m_keyMap[SDLK_F6] = Rocket::Core::Input::KI_F6;
296  m_keyMap[SDLK_F7] = Rocket::Core::Input::KI_F7;
297  m_keyMap[SDLK_F8] = Rocket::Core::Input::KI_F8;
298  m_keyMap[SDLK_F9] = Rocket::Core::Input::KI_F9;
299  m_keyMap[SDLK_F10] = Rocket::Core::Input::KI_F10;
300  m_keyMap[SDLK_F11] = Rocket::Core::Input::KI_F11;
301  m_keyMap[SDLK_F12] = Rocket::Core::Input::KI_F12;
302  m_keyMap[SDLK_F13] = Rocket::Core::Input::KI_F13;
303  m_keyMap[SDLK_F14] = Rocket::Core::Input::KI_F14;
304  m_keyMap[SDLK_F15] = Rocket::Core::Input::KI_F15;
305  m_keyMap[SDLK_NUMLOCK] = Rocket::Core::Input::KI_NUMLOCK;
306  m_keyMap[SDLK_SCROLLOCK] = Rocket::Core::Input::KI_SCROLL;
307  m_keyMap[SDLK_LSHIFT] = Rocket::Core::Input::KI_LSHIFT;
308  m_keyMap[SDLK_RSHIFT] = Rocket::Core::Input::KI_RSHIFT;
309  m_keyMap[SDLK_LCTRL] = Rocket::Core::Input::KI_LCONTROL;
310  m_keyMap[SDLK_RCTRL] = Rocket::Core::Input::KI_RCONTROL;
311  m_keyMap[SDLK_LALT] = Rocket::Core::Input::KI_LMENU;
312  m_keyMap[SDLK_RALT] = Rocket::Core::Input::KI_RMENU;
313  m_keyMap[SDLK_LMETA] = Rocket::Core::Input::KI_LMETA;
314  m_keyMap[SDLK_RMETA] = Rocket::Core::Input::KI_RMETA;
315  }
316 };
bool onSdlEvent(SDL_Event &evt)
Processes SDL input and converts it to librocket input, then forwards it to the librocket context...
bool processMouseWheelMotion(SDL_Event &event)
Process a mouse wheel motion event.
bool processKeyInput(SDL_Event &event)
Process a key input event.
bool processMouseInput(SDL_Event &event)
Process a mouse input event.
LibRocketInputProcessor(Rocket::Core::Context *context)
Constructor.
void populateKeyMap()
Creates the key map.
void updateKeyModState()
Updates the key mod state bitmask.
uint32_t m_keyModState
Bitmask that stores key modifiers.
bool processMouseMotion(SDL_Event &event)
Process a mouse motion event.
unsigned short uint16_t
Definition: core.h:39
int32_t m_wheelCounter
Counts how many times the wheel has been moved.
void turn()
Called each frame to perform update operations.
std::map< SDLKey, Rocket::Core::Input::KeyIdentifier > m_keyMap
Keymap to convert SDL key to Librocket key.
Rocket::Core::Context * m_context
Reference to librocket's context.