FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
ceguiinputprocessor.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 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 
24 // 3rd party library includes
25 #include <CEGUI/CEGUI.h>
26 
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 
32 #include "ceguiinputprocessor.h"
33 
34 namespace FIFE {
37  }
38 
40  }
41 
42  bool CEGuiInputProcessor::onSdlEvent(SDL_Event& event) {
43  bool consumed = false;
44 
45  switch(event.type) {
46 
47  case SDL_KEYUP:
48  case SDL_KEYDOWN:
49  consumed = processKeyInput(event);
50  break;
51 
52  case SDL_TEXTINPUT:
53  consumed = processTextInput(event);
54  break;
55 
56  case SDL_MOUSEWHEEL:
57  case SDL_MOUSEBUTTONUP:
58  case SDL_MOUSEBUTTONDOWN:
59  consumed = processMouseInput(event);
60  break;
61 
62  case SDL_MOUSEMOTION:
63  consumed = processMouseMotion(event);
64  break;
65 
66  default:
67  ;
68  }
69 
70  return consumed;
71  }
72 
73  bool CEGuiInputProcessor::processKeyInput(SDL_Event& event) {
74  bool consumed = false;
75 
76  switch(event.type) {
77  case SDL_KEYDOWN:
78  if (m_keymap.find(event.key.keysym.sym) != m_keymap.end())
79  consumed |= CEGUI::System::getSingleton().getDefaultGUIContext().injectKeyDown(m_keymap[event.key.keysym.sym]);
80  break;
81 
82  case SDL_KEYUP:
83  if (m_keymap.find(event.key.keysym.sym) != m_keymap.end())
84  consumed = CEGUI::System::getSingleton().getDefaultGUIContext().injectKeyUp(m_keymap[event.key.keysym.sym]);
85  break;
86 
87  default:
88  ;
89  }
90 
91  return consumed;
92  }
93 
94  bool CEGuiInputProcessor::processTextInput(SDL_Event& event) {
95  CEGUI::String character(event.text.text);
96  bool consumed = CEGUI::System::getSingleton().getDefaultGUIContext().injectChar(character[0]);
97 
98  return consumed;
99  }
100 
101  bool CEGuiInputProcessor::processMouseInput(SDL_Event& event) {
102  bool consumed = false;
103 
104  switch(event.type) {
105  case SDL_MOUSEBUTTONDOWN:
106  {
107  switch(event.button.button) {
108  case SDL_BUTTON_LEFT:
109  consumed = CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonDown(CEGUI::LeftButton);
110  break;
111 
112  case SDL_BUTTON_RIGHT:
113  consumed = CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonDown(CEGUI::RightButton);
114  break;
115 
116  case SDL_BUTTON_MIDDLE:
117  consumed = CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonDown(CEGUI::MiddleButton) ;
118  break;
119 
120  default:
121  ;
122  }
123  break;
124  }
125 
126  case SDL_MOUSEBUTTONUP:
127  {
128  switch(event.button.button) {
129  case SDL_BUTTON_LEFT:
130  consumed = CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonUp(CEGUI::LeftButton);
131  break;
132 
133  case SDL_BUTTON_RIGHT:
134  consumed = CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonUp(CEGUI::RightButton);
135  break;
136 
137  case SDL_BUTTON_MIDDLE:
138  consumed = CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonUp(CEGUI::MiddleButton) ;
139  break;
140 
141  default:
142  ;
143  }
144  break;
145  }
146 
147  case SDL_MOUSEWHEEL:
148  {
149  // mousewheel up or down
150  int32_t wheelChange = event.wheel.y;
151  if (wheelChange != 0) {
152 #if SDL_VERSION_ATLEAST(2,0,4)
153  if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) {
154  wheelChange *= -1;
155  }
156 #endif
157  consumed = CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseWheelChange(wheelChange);
158  }
159  break;
160  }
161 
162  default:
163  ;
164  }
165 
166  return consumed;
167  }
168 
170  return CEGUI::System::getSingleton().getDefaultGUIContext().injectMousePosition(static_cast<float>(event.motion.x), static_cast<float>(event.motion.y));
171  }
172 
174  m_keymap[SDLK_1] = CEGUI::Key::One;
175  m_keymap[SDLK_2] = CEGUI::Key::Two;
176  m_keymap[SDLK_3] = CEGUI::Key::Three;
177  m_keymap[SDLK_4] = CEGUI::Key::Four;
178  m_keymap[SDLK_5] = CEGUI::Key::Five;
179  m_keymap[SDLK_6] = CEGUI::Key::Six;
180  m_keymap[SDLK_7] = CEGUI::Key::Seven;
181  m_keymap[SDLK_8] = CEGUI::Key::Eight;
182  m_keymap[SDLK_9] = CEGUI::Key::Nine;
183  m_keymap[SDLK_0] = CEGUI::Key::Zero;
184 
185  m_keymap[SDLK_q] = CEGUI::Key::Q;
186  m_keymap[SDLK_w] = CEGUI::Key::W;
187  m_keymap[SDLK_e] = CEGUI::Key::E;
188  m_keymap[SDLK_r] = CEGUI::Key::R;
189  m_keymap[SDLK_t] = CEGUI::Key::T;
190  m_keymap[SDLK_y] = CEGUI::Key::Y;
191  m_keymap[SDLK_u] = CEGUI::Key::U;
192  m_keymap[SDLK_i] = CEGUI::Key::I;
193  m_keymap[SDLK_o] = CEGUI::Key::O;
194  m_keymap[SDLK_p] = CEGUI::Key::P;
195  m_keymap[SDLK_a] = CEGUI::Key::A;
196  m_keymap[SDLK_s] = CEGUI::Key::S;
197  m_keymap[SDLK_d] = CEGUI::Key::D;
198  m_keymap[SDLK_f] = CEGUI::Key::F;
199  m_keymap[SDLK_g] = CEGUI::Key::G;
200  m_keymap[SDLK_h] = CEGUI::Key::H;
201  m_keymap[SDLK_j] = CEGUI::Key::J;
202  m_keymap[SDLK_k] = CEGUI::Key::K;
203  m_keymap[SDLK_l] = CEGUI::Key::L;
204  m_keymap[SDLK_z] = CEGUI::Key::Z;
205  m_keymap[SDLK_x] = CEGUI::Key::X;
206  m_keymap[SDLK_c] = CEGUI::Key::C;
207  m_keymap[SDLK_v] = CEGUI::Key::V;
208  m_keymap[SDLK_b] = CEGUI::Key::B;
209  m_keymap[SDLK_n] = CEGUI::Key::N;
210  m_keymap[SDLK_m] = CEGUI::Key::M;
211 
212  m_keymap[SDLK_COMMA] = CEGUI::Key::Comma;
213  m_keymap[SDLK_PERIOD] = CEGUI::Key::Period;
214  m_keymap[SDLK_SLASH] = CEGUI::Key::Slash;
215  m_keymap[SDLK_BACKSLASH] = CEGUI::Key::Backslash;
216  m_keymap[SDLK_MINUS] = CEGUI::Key::Minus;
217  m_keymap[SDLK_EQUALS] = CEGUI::Key::Equals;
218  m_keymap[SDLK_SEMICOLON] = CEGUI::Key::Semicolon;
219  m_keymap[SDLK_COLON] = CEGUI::Key::Colon;
220  m_keymap[SDLK_LEFTBRACKET] = CEGUI::Key::LeftBracket;
221  m_keymap[SDLK_RIGHTBRACKET] = CEGUI::Key::RightBracket;
222  m_keymap[SDLK_QUOTE] = CEGUI::Key::Apostrophe;
223  m_keymap[SDLK_BACKQUOTE] = CEGUI::Key::Grave;
224  m_keymap[SDLK_AT] = CEGUI::Key::At;
225  m_keymap[SDLK_UNDERSCORE] = CEGUI::Key::Underline;
226 
227  m_keymap[SDLK_RETURN] = CEGUI::Key::Return;
228  m_keymap[SDLK_SPACE] = CEGUI::Key::Space;
229  m_keymap[SDLK_BACKSPACE] = CEGUI::Key::Backspace;
230  m_keymap[SDLK_TAB] = CEGUI::Key::Tab;
231 
232  m_keymap[SDLK_ESCAPE] = CEGUI::Key::Escape;
233  m_keymap[SDLK_PAUSE] = CEGUI::Key::Pause;
234  m_keymap[SDLK_SYSREQ] = CEGUI::Key::SysRq;
235  m_keymap[SDLK_POWER] = CEGUI::Key::Power;
236  m_keymap[SDLK_SLEEP] = CEGUI::Key::Sleep;
237 
238  m_keymap[SDLK_CALCULATOR] = CEGUI::Key::Calculator;
239  m_keymap[SDLK_MAIL] = CEGUI::Key::Mail;
240  m_keymap[SDLK_COMPUTER] = CEGUI::Key::MyComputer;
241  m_keymap[SDLK_MEDIASELECT] = CEGUI::Key::MediaSelect;
242  m_keymap[SDLK_AC_STOP] = CEGUI::Key::Stop;
243 
244  m_keymap[SDLK_AUDIOPLAY] = CEGUI::Key::PlayPause;
245  m_keymap[SDLK_AUDIOSTOP] = CEGUI::Key::MediaStop;
246  m_keymap[SDLK_AUDIOPREV] = CEGUI::Key::PrevTrack;
247  m_keymap[SDLK_AUDIONEXT] = CEGUI::Key::NextTrack;
248  m_keymap[SDLK_AUDIOMUTE] = CEGUI::Key::Mute;
249  m_keymap[SDLK_VOLUMEUP] = CEGUI::Key::VolumeUp;
250  m_keymap[SDLK_VOLUMEDOWN] = CEGUI::Key::VolumeDown;
251 
252  m_keymap[SDLK_AC_BACK] = CEGUI::Key::WebBack;
253  m_keymap[SDLK_AC_FORWARD] = CEGUI::Key::WebForward;
254  m_keymap[SDLK_AC_HOME] = CEGUI::Key::WebHome;
255  m_keymap[SDLK_AC_BOOKMARKS] = CEGUI::Key::WebFavorites;
256  m_keymap[SDLK_AC_SEARCH] = CEGUI::Key::WebSearch;
257  m_keymap[SDLK_AC_REFRESH] = CEGUI::Key::WebRefresh;
258  m_keymap[SDLK_AC_STOP] = CEGUI::Key::WebStop;
259 
260  m_keymap[SDLK_NUMLOCKCLEAR] = CEGUI::Key::NumLock;
261  m_keymap[SDLK_SCROLLLOCK] = CEGUI::Key::ScrollLock;
262  m_keymap[SDLK_CAPSLOCK] = CEGUI::Key::Capital;
263 
264  m_keymap[SDLK_F1] = CEGUI::Key::F1;
265  m_keymap[SDLK_F2] = CEGUI::Key::F2;
266  m_keymap[SDLK_F3] = CEGUI::Key::F3;
267  m_keymap[SDLK_F4] = CEGUI::Key::F4;
268  m_keymap[SDLK_F5] = CEGUI::Key::F5;
269  m_keymap[SDLK_F6] = CEGUI::Key::F6;
270  m_keymap[SDLK_F7] = CEGUI::Key::F7;
271  m_keymap[SDLK_F8] = CEGUI::Key::F8;
272  m_keymap[SDLK_F9] = CEGUI::Key::F9;
273  m_keymap[SDLK_F10] = CEGUI::Key::F10;
274  m_keymap[SDLK_F11] = CEGUI::Key::F11;
275  m_keymap[SDLK_F12] = CEGUI::Key::F12;
276  m_keymap[SDLK_F13] = CEGUI::Key::F13;
277  m_keymap[SDLK_F14] = CEGUI::Key::F14;
278  m_keymap[SDLK_F15] = CEGUI::Key::F15;
279 
280  m_keymap[SDLK_LCTRL] = CEGUI::Key::LeftControl;
281  m_keymap[SDLK_LALT] = CEGUI::Key::LeftAlt;
282  m_keymap[SDLK_LSHIFT] = CEGUI::Key::LeftShift;
283  m_keymap[SDLK_LGUI] = CEGUI::Key::LeftWindows;
284  m_keymap[SDLK_RCTRL] = CEGUI::Key::RightControl;
285  m_keymap[SDLK_RALT] = CEGUI::Key::RightAlt;
286  m_keymap[SDLK_RSHIFT] = CEGUI::Key::RightShift;
287  m_keymap[SDLK_RGUI] = CEGUI::Key::RightWindows;
288  m_keymap[SDLK_MENU] = CEGUI::Key::AppMenu;
289 
290  m_keymap[SDLK_KP_0] = CEGUI::Key::Numpad0;
291  m_keymap[SDLK_KP_1] = CEGUI::Key::Numpad1;
292  m_keymap[SDLK_KP_2] = CEGUI::Key::Numpad2;
293  m_keymap[SDLK_KP_3] = CEGUI::Key::Numpad3;
294  m_keymap[SDLK_KP_4] = CEGUI::Key::Numpad4;
295  m_keymap[SDLK_KP_5] = CEGUI::Key::Numpad5;
296  m_keymap[SDLK_KP_6] = CEGUI::Key::Numpad6;
297  m_keymap[SDLK_KP_7] = CEGUI::Key::Numpad7;
298  m_keymap[SDLK_KP_8] = CEGUI::Key::Numpad8;
299  m_keymap[SDLK_KP_9] = CEGUI::Key::Numpad9;
300  m_keymap[SDLK_KP_PERIOD] = CEGUI::Key::Decimal;
301  m_keymap[SDLK_KP_PLUS] = CEGUI::Key::Add;
302  m_keymap[SDLK_KP_MINUS] = CEGUI::Key::Subtract;
303  m_keymap[SDLK_KP_MULTIPLY] = CEGUI::Key::Multiply;
304  m_keymap[SDLK_KP_DIVIDE] = CEGUI::Key::Divide;
305  m_keymap[SDLK_KP_ENTER] = CEGUI::Key::NumpadEnter;
306  m_keymap[SDLK_KP_COMMA] = CEGUI::Key::NumpadComma;
307  m_keymap[SDLK_KP_EQUALS] = CEGUI::Key::NumpadEquals;
308 
309  m_keymap[SDLK_UP] = CEGUI::Key::ArrowUp;
310  m_keymap[SDLK_LEFT] = CEGUI::Key::ArrowLeft;
311  m_keymap[SDLK_RIGHT] = CEGUI::Key::ArrowRight;
312  m_keymap[SDLK_DOWN] = CEGUI::Key::ArrowDown;
313 
314  m_keymap[SDLK_HOME] = CEGUI::Key::Home;
315  m_keymap[SDLK_END] = CEGUI::Key::End;
316  m_keymap[SDLK_PAGEUP] = CEGUI::Key::PageUp;
317  m_keymap[SDLK_PAGEDOWN] = CEGUI::Key::PageDown;
318  m_keymap[SDLK_INSERT] = CEGUI::Key::Insert;
319  m_keymap[SDLK_DELETE] = CEGUI::Key::Delete;
320  }
321 }
std::map< SDL_Keycode, CEGUI::Key::Scan > m_keymap
Holds translation of key scancodes from SDL to CEGUI.
void initializeKeyMap()
Initialize the key translation map.
bool processTextInput(SDL_Event &event)
Process a text input event.
bool processMouseInput(SDL_Event &event)
Process a mouse input event.
bool processKeyInput(SDL_Event &event)
Process a key input event.
bool onSdlEvent(SDL_Event &event)
Injects input to the CEGUI system.
bool processMouseMotion(SDL_Event &event)
Process a mouse motion event.