| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 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 |
|
| 34 |
#include "librocketinputprocessor.h" |
| 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) { |
| 43 |
populateKeyMap(); |
| 44 |
} |
| 45 |
|
| 46 |
LibRocketInputProcessor::~LibRocketInputProcessor() { |
| 47 |
} |
| 48 |
|
| 49 |
void LibRocketInputProcessor::updateKeyModState() { |
| 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 |
// KMOD_META is gone so we change it to KMOD_GUI |
| 69 |
if((modState & KMOD_GUI) == KMOD_GUI) { |
| 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 |
|
| 89 |
updateKeyModState(); |
| 90 |
|
| 91 |
switch(event.type) { |
| 92 |
|
| 93 |
case SDL_KEYUP: |
| 94 |
case SDL_KEYDOWN: |
| 95 |
consumed = processKeyInput(event); |
| 96 |
break; |
| 97 |
|
| 98 |
case SDL_TEXTINPUT: |
| 99 |
consumed = processTextInput(event); |
| 100 |
break; |
| 101 |
|
| 102 |
case SDL_MOUSEWHEEL: |
| 103 |
case SDL_MOUSEBUTTONUP: |
| 104 |
case SDL_MOUSEBUTTONDOWN: |
| 105 |
consumed = processMouseInput(event); |
| 106 |
break; |
| 107 |
|
| 108 |
case SDL_MOUSEMOTION: |
| 109 |
consumed = processMouseMotion(event); |
| 110 |
break; |
| 111 |
|
| 112 |
default: |
| 113 |
break; |
| 114 |
} |
| 115 |
|
| 116 |
return consumed; |
| 117 |
} |
| 118 |
|
| 119 |
void LibRocketInputProcessor::turn() { |
| 120 |
if(m_wheelCounter != 0) { |
| 121 |
m_context->ProcessMouseWheel(m_wheelCounter, m_keyModState); |
| 122 |
m_wheelCounter = 0; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
bool LibRocketInputProcessor::processMouseMotion(SDL_Event& event) { |
| 127 |
|
| 128 |
int x = static_cast<int>(event.motion.x); |
| 129 |
int y = static_cast<int>(event.motion.y); |
| 130 |
|
| 131 |
m_context->ProcessMouseMove(x, y, m_keyModState); |
| 132 |
|
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
bool LibRocketInputProcessor::processMouseInput(SDL_Event& event) { |
| 137 |
|
| 138 |
int index = (event.button.button == SDL_BUTTON_LEFT) ? 0 : |
| 139 |
(event.button.button == SDL_BUTTON_RIGHT) ? 1 : |
| 140 |
(event.button.button == SDL_BUTTON_MIDDLE) ? 2 : 3; |
| 141 |
|
| 142 |
|
| 143 |
if(event.type == SDL_MOUSEBUTTONDOWN) { |
| 144 |
m_context->ProcessMouseButtonDown(index, m_keyModState); |
| 145 |
} else if (event.type == SDL_MOUSEBUTTONUP) { |
| 146 |
m_context->ProcessMouseButtonUp(index, m_keyModState); |
| 147 |
} else if (event.type == SDL_MOUSEWHEEL) { |
| 148 |
processMouseWheelMotion(event); |
| 149 |
} |
| 150 |
|
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
bool LibRocketInputProcessor::processMouseWheelMotion(SDL_Event& event) { |
| 155 |
// mousewheel up |
| 156 |
#if SDL_VERSION_ATLEAST(2,0,4) |
| 157 |
if (event.wheel.y > 0 || (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED && event.wheel.y < 0)) { |
| 158 |
#else |
| 159 |
if (event.wheel.y > 0) { |
| 160 |
#endif |
| 161 |
if(m_wheelCounter <= 0) { |
| 162 |
m_wheelCounter--; |
| 163 |
} else { |
| 164 |
//the wheel had been moving downwards so sent those movements before reseting the counter |
| 165 |
m_context->ProcessMouseWheel(m_wheelCounter, m_keyModState); |
| 166 |
m_wheelCounter = -1; |
| 167 |
} |
| 168 |
} |
| 169 |
// mousewheel down |
| 170 |
#if SDL_VERSION_ATLEAST(2,0,4) |
| 171 |
else if (event.wheel.y < 0 || (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED && event.wheel.y > 0)) { |
| 172 |
#else |
| 173 |
else if (event.wheel.y < 0) { |
| 174 |
#endif |
| 175 |
if(m_wheelCounter >= 0) { |
| 176 |
m_wheelCounter++; |
| 177 |
} else { |
| 178 |
//the wheel had been moving upwards so sent those movements before reseting the counter |
| 179 |
m_context->ProcessMouseWheel(m_wheelCounter, m_keyModState); |
| 180 |
m_wheelCounter = 1; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
bool LibRocketInputProcessor::processKeyInput(SDL_Event& event) { |
| 188 |
|
| 189 |
Rocket::Core::Input::KeyIdentifier key = m_keyMap[event.key.keysym.sym]; |
| 190 |
|
| 191 |
if(event.type == SDL_KEYDOWN) { |
| 192 |
|
| 193 |
m_context->ProcessKeyDown(key, m_keyModState); |
| 194 |
|
| 195 |
if(key == Rocket::Core::Input::KI_RETURN) { |
| 196 |
m_context->ProcessTextInput((Rocket::Core::word) '\n'); |
| 197 |
} |
| 198 |
|
| 199 |
} else { |
| 200 |
m_context->ProcessKeyUp(m_keyMap[event.key.keysym.sym], m_keyModState); |
| 201 |
} |
| 202 |
|
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
bool LibRocketInputProcessor::processTextInput(SDL_Event& event) { |
| 207 |
Rocket::Core::String text(event.text.text); |
| 208 |
m_context->ProcessTextInput(text); |
| 209 |
|
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
void LibRocketInputProcessor::populateKeyMap() { |
| 214 |
|
| 215 |
m_keyMap[SDLK_UNKNOWN] = Rocket::Core::Input::KI_UNKNOWN; |
| 216 |
m_keyMap[SDLK_SPACE] = Rocket::Core::Input::KI_SPACE; |
| 217 |
m_keyMap[SDLK_0] = Rocket::Core::Input::KI_0; |
| 218 |
m_keyMap[SDLK_1] = Rocket::Core::Input::KI_1; |
| 219 |
m_keyMap[SDLK_2] = Rocket::Core::Input::KI_2; |
| 220 |
m_keyMap[SDLK_3] = Rocket::Core::Input::KI_3; |
| 221 |
m_keyMap[SDLK_4] = Rocket::Core::Input::KI_4; |
| 222 |
m_keyMap[SDLK_5] = Rocket::Core::Input::KI_5; |
| 223 |
m_keyMap[SDLK_6] = Rocket::Core::Input::KI_6; |
| 224 |
m_keyMap[SDLK_7] = Rocket::Core::Input::KI_7; |
| 225 |
m_keyMap[SDLK_8] = Rocket::Core::Input::KI_8; |
| 226 |
m_keyMap[SDLK_9] = Rocket::Core::Input::KI_9; |
| 227 |
m_keyMap[SDLK_a] = Rocket::Core::Input::KI_A; |
| 228 |
m_keyMap[SDLK_b] = Rocket::Core::Input::KI_B; |
| 229 |
m_keyMap[SDLK_c] = Rocket::Core::Input::KI_C; |
| 230 |
m_keyMap[SDLK_d] = Rocket::Core::Input::KI_D; |
| 231 |
m_keyMap[SDLK_e] = Rocket::Core::Input::KI_E; |
| 232 |
m_keyMap[SDLK_f] = Rocket::Core::Input::KI_F; |
| 233 |
m_keyMap[SDLK_g] = Rocket::Core::Input::KI_G; |
| 234 |
m_keyMap[SDLK_h] = Rocket::Core::Input::KI_H; |
| 235 |
m_keyMap[SDLK_i] = Rocket::Core::Input::KI_I; |
| 236 |
m_keyMap[SDLK_j] = Rocket::Core::Input::KI_J; |
| 237 |
m_keyMap[SDLK_k] = Rocket::Core::Input::KI_K; |
| 238 |
m_keyMap[SDLK_l] = Rocket::Core::Input::KI_L; |
| 239 |
m_keyMap[SDLK_m] = Rocket::Core::Input::KI_M; |
| 240 |
m_keyMap[SDLK_n] = Rocket::Core::Input::KI_N; |
| 241 |
m_keyMap[SDLK_o] = Rocket::Core::Input::KI_O; |
| 242 |
m_keyMap[SDLK_p] = Rocket::Core::Input::KI_P; |
| 243 |
m_keyMap[SDLK_q] = Rocket::Core::Input::KI_Q; |
| 244 |
m_keyMap[SDLK_r] = Rocket::Core::Input::KI_R; |
| 245 |
m_keyMap[SDLK_s] = Rocket::Core::Input::KI_S; |
| 246 |
m_keyMap[SDLK_t] = Rocket::Core::Input::KI_T; |
| 247 |
m_keyMap[SDLK_u] = Rocket::Core::Input::KI_U; |
| 248 |
m_keyMap[SDLK_v] = Rocket::Core::Input::KI_V; |
| 249 |
m_keyMap[SDLK_w] = Rocket::Core::Input::KI_W; |
| 250 |
m_keyMap[SDLK_x] = Rocket::Core::Input::KI_X; |
| 251 |
m_keyMap[SDLK_y] = Rocket::Core::Input::KI_Y; |
| 252 |
m_keyMap[SDLK_z] = Rocket::Core::Input::KI_Z; |
| 253 |
m_keyMap[SDLK_SEMICOLON] = Rocket::Core::Input::KI_OEM_1; |
| 254 |
m_keyMap[SDLK_PLUS] = Rocket::Core::Input::KI_OEM_PLUS; |
| 255 |
m_keyMap[SDLK_COMMA] = Rocket::Core::Input::KI_OEM_COMMA; |
| 256 |
m_keyMap[SDLK_MINUS] = Rocket::Core::Input::KI_OEM_MINUS; |
| 257 |
m_keyMap[SDLK_PERIOD] = Rocket::Core::Input::KI_OEM_PERIOD; |
| 258 |
m_keyMap[SDLK_SLASH] = Rocket::Core::Input::KI_OEM_2; |
| 259 |
m_keyMap[SDLK_BACKQUOTE] = Rocket::Core::Input::KI_OEM_3; |
| 260 |
m_keyMap[SDLK_LEFTBRACKET] = Rocket::Core::Input::KI_OEM_4; |
| 261 |
m_keyMap[SDLK_BACKSLASH] = Rocket::Core::Input::KI_OEM_5; |
| 262 |
m_keyMap[SDLK_RIGHTBRACKET] = Rocket::Core::Input::KI_OEM_6; |
| 263 |
m_keyMap[SDLK_QUOTEDBL] = Rocket::Core::Input::KI_OEM_7; |
| 264 |
m_keyMap[SDLK_KP_0] = Rocket::Core::Input::KI_NUMPAD0; |
| 265 |
m_keyMap[SDLK_KP_1] = Rocket::Core::Input::KI_NUMPAD1; |
| 266 |
m_keyMap[SDLK_KP_2] = Rocket::Core::Input::KI_NUMPAD2; |
| 267 |
m_keyMap[SDLK_KP_3] = Rocket::Core::Input::KI_NUMPAD3; |
| 268 |
m_keyMap[SDLK_KP_4] = Rocket::Core::Input::KI_NUMPAD4; |
| 269 |
m_keyMap[SDLK_KP_5] = Rocket::Core::Input::KI_NUMPAD5; |
| 270 |
m_keyMap[SDLK_KP_6] = Rocket::Core::Input::KI_NUMPAD6; |
| 271 |
m_keyMap[SDLK_KP_7] = Rocket::Core::Input::KI_NUMPAD7; |
| 272 |
m_keyMap[SDLK_KP_8] = Rocket::Core::Input::KI_NUMPAD8; |
| 273 |
m_keyMap[SDLK_KP_9] = Rocket::Core::Input::KI_NUMPAD9; |
| 274 |
m_keyMap[SDLK_KP_ENTER] = Rocket::Core::Input::KI_NUMPADENTER; |
| 275 |
m_keyMap[SDLK_KP_MULTIPLY] = Rocket::Core::Input::KI_MULTIPLY; |
| 276 |
m_keyMap[SDLK_KP_PLUS] = Rocket::Core::Input::KI_ADD; |
| 277 |
m_keyMap[SDLK_KP_MINUS] = Rocket::Core::Input::KI_SUBTRACT; |
| 278 |
m_keyMap[SDLK_KP_PERIOD] = Rocket::Core::Input::KI_DECIMAL; |
| 279 |
m_keyMap[SDLK_KP_DIVIDE] = Rocket::Core::Input::KI_DIVIDE; |
| 280 |
m_keyMap[SDLK_KP_EQUALS] = Rocket::Core::Input::KI_OEM_NEC_EQUAL; |
| 281 |
m_keyMap[SDLK_BACKSPACE] = Rocket::Core::Input::KI_BACK; |
| 282 |
m_keyMap[SDLK_TAB] = Rocket::Core::Input::KI_TAB; |
| 283 |
m_keyMap[SDLK_CLEAR] = Rocket::Core::Input::KI_CLEAR; |
| 284 |
m_keyMap[SDLK_RETURN] = Rocket::Core::Input::KI_RETURN; |
| 285 |
m_keyMap[SDLK_PAUSE] = Rocket::Core::Input::KI_PAUSE; |
| 286 |
m_keyMap[SDLK_CAPSLOCK] = Rocket::Core::Input::KI_CAPITAL; |
| 287 |
m_keyMap[SDLK_PAGEUP] = Rocket::Core::Input::KI_PRIOR; |
| 288 |
m_keyMap[SDLK_PAGEDOWN] = Rocket::Core::Input::KI_NEXT; |
| 289 |
m_keyMap[SDLK_END] = Rocket::Core::Input::KI_END; |
| 290 |
m_keyMap[SDLK_HOME] = Rocket::Core::Input::KI_HOME; |
| 291 |
m_keyMap[SDLK_LEFT] = Rocket::Core::Input::KI_LEFT; |
| 292 |
m_keyMap[SDLK_UP] = Rocket::Core::Input::KI_UP; |
| 293 |
m_keyMap[SDLK_RIGHT] = Rocket::Core::Input::KI_RIGHT; |
| 294 |
m_keyMap[SDLK_DOWN] = Rocket::Core::Input::KI_DOWN; |
| 295 |
m_keyMap[SDLK_INSERT] = Rocket::Core::Input::KI_INSERT; |
| 296 |
m_keyMap[SDLK_DELETE] = Rocket::Core::Input::KI_DELETE; |
| 297 |
m_keyMap[SDLK_HELP] = Rocket::Core::Input::KI_HELP; |
| 298 |
m_keyMap[SDLK_LGUI] = Rocket::Core::Input::KI_LWIN; |
| 299 |
m_keyMap[SDLK_RGUI] = Rocket::Core::Input::KI_RWIN; |
| 300 |
m_keyMap[SDLK_F1] = Rocket::Core::Input::KI_F1; |
| 301 |
m_keyMap[SDLK_F2] = Rocket::Core::Input::KI_F2; |
| 302 |
m_keyMap[SDLK_F3] = Rocket::Core::Input::KI_F3; |
| 303 |
m_keyMap[SDLK_F4] = Rocket::Core::Input::KI_F4; |
| 304 |
m_keyMap[SDLK_F5] = Rocket::Core::Input::KI_F5; |
| 305 |
m_keyMap[SDLK_F6] = Rocket::Core::Input::KI_F6; |
| 306 |
m_keyMap[SDLK_F7] = Rocket::Core::Input::KI_F7; |
| 307 |
m_keyMap[SDLK_F8] = Rocket::Core::Input::KI_F8; |
| 308 |
m_keyMap[SDLK_F9] = Rocket::Core::Input::KI_F9; |
| 309 |
m_keyMap[SDLK_F10] = Rocket::Core::Input::KI_F10; |
| 310 |
m_keyMap[SDLK_F11] = Rocket::Core::Input::KI_F11; |
| 311 |
m_keyMap[SDLK_F12] = Rocket::Core::Input::KI_F12; |
| 312 |
m_keyMap[SDLK_F13] = Rocket::Core::Input::KI_F13; |
| 313 |
m_keyMap[SDLK_F14] = Rocket::Core::Input::KI_F14; |
| 314 |
m_keyMap[SDLK_F15] = Rocket::Core::Input::KI_F15; |
| 315 |
m_keyMap[SDLK_NUMLOCKCLEAR] = Rocket::Core::Input::KI_NUMLOCK; |
| 316 |
m_keyMap[SDLK_SCROLLLOCK] = Rocket::Core::Input::KI_SCROLL; |
| 317 |
m_keyMap[SDLK_LSHIFT] = Rocket::Core::Input::KI_LSHIFT; |
| 318 |
m_keyMap[SDLK_RSHIFT] = Rocket::Core::Input::KI_RSHIFT; |
| 319 |
m_keyMap[SDLK_LCTRL] = Rocket::Core::Input::KI_LCONTROL; |
| 320 |
m_keyMap[SDLK_RCTRL] = Rocket::Core::Input::KI_RCONTROL; |
| 321 |
m_keyMap[SDLK_LALT] = Rocket::Core::Input::KI_LMENU; |
| 322 |
m_keyMap[SDLK_RALT] = Rocket::Core::Input::KI_RMENU; |
| 323 |
} |
| 324 |
}; |