Input.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "CoreEvents.h"
  25. #include "FileSystem.h"
  26. #include "Graphics.h"
  27. #include "GraphicsEvents.h"
  28. #include "GraphicsImpl.h"
  29. #include "Input.h"
  30. #include "Log.h"
  31. #include "Mutex.h"
  32. #include "ProcessUtils.h"
  33. #include "Profiler.h"
  34. #include "ResourceCache.h"
  35. #include "RWOpsWrapper.h"
  36. #include "StringUtils.h"
  37. #include "Text.h"
  38. #include "UI.h"
  39. #include <cstring>
  40. #include <SDL.h>
  41. #include "DebugNew.h"
  42. extern "C" int SDL_AddTouch(SDL_TouchID touchID, const char *name);
  43. // Require a click inside window before re-hiding mouse cursor on OSX, otherwise dragging the window
  44. // can be incorrectly interpreted as mouse movement inside the window
  45. #if defined(__APPLE__) && !defined(IOS)
  46. #define REQUIRE_CLICK_TO_FOCUS
  47. #endif
  48. namespace Urho3D
  49. {
  50. const int SCREEN_JOYSTICK_START_ID = 0x40000000;
  51. const StringHash VAR_BUTTON_KEY_BINDING("VAR_BUTTON_KEY_BINDING");
  52. const StringHash VAR_BUTTON_MOUSE_BUTTON_BINDING("VAR_BUTTON_MOUSE_BUTTON_BINDING");
  53. const StringHash VAR_LAST_KEYSYM("VAR_LAST_KEYSYM");
  54. const StringHash VAR_SCREEN_JOYSTICK_ID("VAR_SCREEN_JOYSTICK_ID");
  55. /// Convert SDL keycode if necessary.
  56. int ConvertSDLKeyCode(int keySym, int scanCode)
  57. {
  58. if (scanCode == SCANCODE_AC_BACK)
  59. return KEY_ESC;
  60. else
  61. return SDL_toupper(keySym);
  62. }
  63. UIElement* TouchState::GetTouchedElement()
  64. {
  65. return touchedElement_.Get();
  66. }
  67. void JoystickState::Initialize(unsigned numButtons, unsigned numAxes, unsigned numHats)
  68. {
  69. buttons_.Resize(numButtons);
  70. buttonPress_.Resize(numButtons);
  71. axes_.Resize(numAxes);
  72. hats_.Resize(numHats);
  73. Reset();
  74. }
  75. void JoystickState::Reset()
  76. {
  77. for (unsigned i = 0; i < buttons_.Size(); ++i)
  78. {
  79. buttons_[i] = false;
  80. buttonPress_[i] = false;
  81. }
  82. for (unsigned i = 0; i < axes_.Size(); ++i)
  83. axes_[i] = 0.0f;
  84. for (unsigned i = 0; i < hats_.Size(); ++i)
  85. hats_[i] = HAT_CENTER;
  86. }
  87. Input::Input(Context* context) :
  88. Object(context),
  89. mouseButtonDown_(0),
  90. mouseButtonPress_(0),
  91. mouseMoveWheel_(0),
  92. windowID_(0),
  93. toggleFullscreen_(true),
  94. mouseVisible_(false),
  95. mouseGrabbed_(false),
  96. mouseMode_(MM_ABSOLUTE),
  97. lastVisibleMousePosition_(MOUSE_POSITION_OFFSCREEN),
  98. supressNextVisibleChangeEvent_(false),
  99. touchEmulation_(false),
  100. inputFocus_(false),
  101. minimized_(false),
  102. focusedThisFrame_(false),
  103. suppressNextMouseMove_(false),
  104. initialized_(false)
  105. {
  106. SubscribeToEvent(E_SCREENMODE, HANDLER(Input, HandleScreenMode));
  107. // Try to initialize right now, but skip if screen mode is not yet set
  108. Initialize();
  109. }
  110. Input::~Input()
  111. {
  112. }
  113. void Input::Update()
  114. {
  115. assert(initialized_);
  116. PROFILE(UpdateInput);
  117. // Reset input accumulation for this frame
  118. keyPress_.Clear();
  119. scancodePress_.Clear();
  120. mouseButtonPress_ = 0;
  121. mouseMove_ = IntVector2::ZERO;
  122. mouseMoveWheel_ = 0;
  123. for (HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  124. {
  125. for (unsigned j = 0; j < i->second_.buttonPress_.Size(); ++j)
  126. i->second_.buttonPress_[j] = false;
  127. }
  128. // Reset touch delta movement
  129. for (HashMap<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  130. {
  131. TouchState& state = i->second_;
  132. state.lastPosition_ = state.position_;
  133. state.delta_ = IntVector2::ZERO;
  134. }
  135. // Check and handle SDL events
  136. SDL_PumpEvents();
  137. SDL_Event evt;
  138. while (SDL_PeepEvents(&evt, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0)
  139. HandleSDLEvent(&evt);
  140. if (mouseVisible_ && mouseMode_ == MM_WRAP)
  141. {
  142. IntVector2 mpos;
  143. SDL_GetMouseState(&mpos.x_, &mpos.y_);
  144. int buffer = 5;
  145. int width = graphics_->GetWidth() - buffer * 2;
  146. int height = graphics_->GetHeight() - buffer * 2;
  147. bool warp = false;
  148. if (mpos.x_ < buffer)
  149. {
  150. warp = true;
  151. mpos.x_ += width;
  152. }
  153. if (mpos.x_ > width)
  154. {
  155. warp = true;
  156. mpos.x_ -= width;
  157. }
  158. if (mpos.y_ < buffer)
  159. {
  160. warp = true;
  161. mpos.y_ += height;
  162. }
  163. if (mpos.y_ > height)
  164. {
  165. warp = true;
  166. mpos.y_ -= height;
  167. }
  168. if (warp)
  169. {
  170. SetMousePosition(mpos);
  171. SDL_FlushEvent(SDL_MOUSEMOTION);
  172. }
  173. }
  174. // Check for activation and inactivation from SDL window flags. Must nullcheck the window pointer because it may have
  175. // been closed due to input events
  176. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  177. unsigned flags = window ? SDL_GetWindowFlags(window) & (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS) : 0;
  178. if (window)
  179. {
  180. #ifdef REQUIRE_CLICK_TO_FOCUS
  181. if (!inputFocus_ && (graphics_->GetFullscreen() || mouseVisible_) && flags == (SDL_WINDOW_INPUT_FOCUS |
  182. SDL_WINDOW_MOUSE_FOCUS))
  183. #else
  184. if (!inputFocus_ && (flags & SDL_WINDOW_INPUT_FOCUS))
  185. #endif
  186. focusedThisFrame_ = true;
  187. if (focusedThisFrame_)
  188. GainFocus();
  189. if (inputFocus_ && (flags & SDL_WINDOW_INPUT_FOCUS) == 0)
  190. LoseFocus();
  191. }
  192. else
  193. return;
  194. // Check for relative mode mouse move
  195. if (!touchEmulation_ && (graphics_->GetExternalWindow() || (!mouseVisible_ && inputFocus_ && (flags & SDL_WINDOW_MOUSE_FOCUS))))
  196. {
  197. IntVector2 mousePosition = GetMousePosition();
  198. mouseMove_ = mousePosition - lastMousePosition_;
  199. if (graphics_->GetExternalWindow())
  200. lastMousePosition_ = mousePosition;
  201. else
  202. {
  203. // Recenter the mouse cursor manually after move
  204. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  205. if (mousePosition != center)
  206. {
  207. SetMousePosition(center);
  208. lastMousePosition_ = center;
  209. }
  210. }
  211. // Send mouse move event if necessary
  212. if (mouseMove_ != IntVector2::ZERO)
  213. {
  214. if (suppressNextMouseMove_)
  215. {
  216. mouseMove_ = IntVector2::ZERO;
  217. suppressNextMouseMove_ = false;
  218. }
  219. else
  220. {
  221. using namespace MouseMove;
  222. VariantMap& eventData = GetEventDataMap();
  223. if (mouseVisible_)
  224. {
  225. eventData[P_X] = mousePosition.x_;
  226. eventData[P_Y] = mousePosition.y_;
  227. }
  228. eventData[P_DX] = mouseMove_.x_;
  229. eventData[P_DY] = mouseMove_.y_;
  230. eventData[P_BUTTONS] = mouseButtonDown_;
  231. eventData[P_QUALIFIERS] = GetQualifiers();
  232. SendEvent(E_MOUSEMOVE, eventData);
  233. }
  234. }
  235. }
  236. }
  237. void Input::SetMouseVisible(bool enable)
  238. {
  239. // In touch emulation mode only enabled mouse is allowed
  240. if (touchEmulation_)
  241. enable = true;
  242. // SDL Raspberry Pi "video driver" does not have proper OS mouse support yet, so no-op for now
  243. #ifndef RASPI
  244. if (enable != mouseVisible_)
  245. {
  246. mouseVisible_ = enable;
  247. if (initialized_)
  248. {
  249. // External windows can only support visible mouse cursor
  250. if (graphics_->GetExternalWindow())
  251. {
  252. mouseVisible_ = true;
  253. return;
  254. }
  255. if (!mouseVisible_ && inputFocus_)
  256. {
  257. SDL_ShowCursor(SDL_FALSE);
  258. // Recenter the mouse cursor manually when hiding it to avoid erratic mouse move for one frame
  259. lastVisibleMousePosition_ = GetMousePosition();
  260. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  261. SetMousePosition(center);
  262. lastMousePosition_ = center;
  263. }
  264. else
  265. {
  266. SDL_ShowCursor(SDL_TRUE);
  267. if (lastVisibleMousePosition_.x_ != MOUSE_POSITION_OFFSCREEN.x_ && lastVisibleMousePosition_.y_ != MOUSE_POSITION_OFFSCREEN.y_)
  268. SetMousePosition(lastVisibleMousePosition_);
  269. }
  270. }
  271. if (supressNextVisibleChangeEvent_)
  272. {
  273. supressNextVisibleChangeEvent_ = false;
  274. using namespace MouseVisibleChanged;
  275. VariantMap& eventData = GetEventDataMap();
  276. eventData[P_VISIBLE] = mouseVisible_;
  277. SendEvent(E_MOUSEVISIBLECHANGED, eventData);
  278. }
  279. }
  280. #endif
  281. }
  282. void Input::SetMouseGrabbed(bool grab)
  283. {
  284. mouseGrabbed_ = grab;
  285. }
  286. void Input::SetMouseMode(MouseMode mode)
  287. {
  288. if (mode != mouseMode_)
  289. {
  290. if (mouseMode_ == MM_RELATIVE)
  291. {
  292. // Todo: Use SDL_SetRelativeMouseMode()
  293. supressNextVisibleChangeEvent_ = true;
  294. SetMouseVisible(true);
  295. // Send updated mouse position:
  296. {
  297. using namespace MouseMove;
  298. VariantMap& eventData = GetEventDataMap();
  299. eventData[P_X] = lastVisibleMousePosition_.x_;
  300. eventData[P_Y] = lastVisibleMousePosition_.y_;
  301. eventData[P_DX] = mouseMove_.x_;
  302. eventData[P_DY] = mouseMove_.y_;
  303. eventData[P_BUTTONS] = mouseButtonDown_;
  304. eventData[P_QUALIFIERS] = GetQualifiers();
  305. SendEvent(E_MOUSEMOVE, eventData);
  306. }
  307. }
  308. else if (mouseMode_ == MM_WRAP)
  309. {
  310. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  311. SDL_SetWindowGrab(window, SDL_FALSE);
  312. }
  313. if (mode == MM_ABSOLUTE)
  314. SetMouseGrabbed(false);
  315. else
  316. {
  317. SetMouseGrabbed(true);
  318. if (mode == MM_RELATIVE)
  319. {
  320. supressNextVisibleChangeEvent_ = true;
  321. SetMouseVisible(false);
  322. }
  323. else if (mode == MM_WRAP)
  324. {
  325. // Todo: When SDL 2.0.4 is integrated, use SDL_CaptureMouse() and global mouse functions.
  326. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  327. SDL_SetWindowGrab(window, SDL_TRUE);
  328. }
  329. }
  330. mouseMode_ = mode;
  331. }
  332. }
  333. void Input::SetToggleFullscreen(bool enable)
  334. {
  335. toggleFullscreen_ = enable;
  336. }
  337. static void PopulateKeyBindingMap(HashMap<String, int>& keyBindingMap)
  338. {
  339. if (keyBindingMap.Empty())
  340. {
  341. keyBindingMap.Insert(MakePair<String, int>("SPACE", KEY_SPACE));
  342. keyBindingMap.Insert(MakePair<String, int>("LCTRL", KEY_LCTRL));
  343. keyBindingMap.Insert(MakePair<String, int>("RCTRL", KEY_RCTRL));
  344. keyBindingMap.Insert(MakePair<String, int>("LSHIFT", KEY_LSHIFT));
  345. keyBindingMap.Insert(MakePair<String, int>("RSHIFT", KEY_RSHIFT));
  346. keyBindingMap.Insert(MakePair<String, int>("LALT", KEY_LALT));
  347. keyBindingMap.Insert(MakePair<String, int>("RALT", KEY_RALT));
  348. keyBindingMap.Insert(MakePair<String, int>("LGUI", KEY_LGUI));
  349. keyBindingMap.Insert(MakePair<String, int>("RGUI", KEY_RGUI));
  350. keyBindingMap.Insert(MakePair<String, int>("TAB", KEY_TAB));
  351. keyBindingMap.Insert(MakePair<String, int>("RETURN", KEY_RETURN));
  352. keyBindingMap.Insert(MakePair<String, int>("RETURN2", KEY_RETURN2));
  353. keyBindingMap.Insert(MakePair<String, int>("ENTER", KEY_KP_ENTER));
  354. keyBindingMap.Insert(MakePair<String, int>("SELECT", KEY_SELECT));
  355. keyBindingMap.Insert(MakePair<String, int>("LEFT", KEY_LEFT));
  356. keyBindingMap.Insert(MakePair<String, int>("RIGHT", KEY_RIGHT));
  357. keyBindingMap.Insert(MakePair<String, int>("UP", KEY_UP));
  358. keyBindingMap.Insert(MakePair<String, int>("DOWN", KEY_DOWN));
  359. keyBindingMap.Insert(MakePair<String, int>("PAGEUP", KEY_PAGEUP));
  360. keyBindingMap.Insert(MakePair<String, int>("PAGEDOWN", KEY_PAGEDOWN));
  361. keyBindingMap.Insert(MakePair<String, int>("F1", KEY_F1));
  362. keyBindingMap.Insert(MakePair<String, int>("F2", KEY_F2));
  363. keyBindingMap.Insert(MakePair<String, int>("F3", KEY_F3));
  364. keyBindingMap.Insert(MakePair<String, int>("F4", KEY_F4));
  365. keyBindingMap.Insert(MakePair<String, int>("F5", KEY_F5));
  366. keyBindingMap.Insert(MakePair<String, int>("F6", KEY_F6));
  367. keyBindingMap.Insert(MakePair<String, int>("F7", KEY_F7));
  368. keyBindingMap.Insert(MakePair<String, int>("F8", KEY_F8));
  369. keyBindingMap.Insert(MakePair<String, int>("F9", KEY_F9));
  370. keyBindingMap.Insert(MakePair<String, int>("F10", KEY_F10));
  371. keyBindingMap.Insert(MakePair<String, int>("F11", KEY_F11));
  372. keyBindingMap.Insert(MakePair<String, int>("F12", KEY_F12));
  373. }
  374. }
  375. static void PopulateMouseButtonBindingMap(HashMap<String, int>& mouseButtonBindingMap)
  376. {
  377. if (mouseButtonBindingMap.Empty())
  378. {
  379. mouseButtonBindingMap.Insert(MakePair<String, int>("LEFT", SDL_BUTTON_LEFT));
  380. mouseButtonBindingMap.Insert(MakePair<String, int>("MIDDLE", SDL_BUTTON_MIDDLE));
  381. mouseButtonBindingMap.Insert(MakePair<String, int>("RIGHT", SDL_BUTTON_RIGHT));
  382. mouseButtonBindingMap.Insert(MakePair<String, int>("X1", SDL_BUTTON_X1));
  383. mouseButtonBindingMap.Insert(MakePair<String, int>("X2", SDL_BUTTON_X2));
  384. }
  385. }
  386. SDL_JoystickID Input::AddScreenJoystick(XMLFile* layoutFile, XMLFile* styleFile)
  387. {
  388. static HashMap<String, int> keyBindingMap;
  389. static HashMap<String, int> mouseButtonBindingMap;
  390. if (!graphics_)
  391. {
  392. LOGWARNING("Cannot add screen joystick in headless mode");
  393. return -1;
  394. }
  395. // If layout file is not given, use the default screen joystick layout
  396. if (!layoutFile)
  397. {
  398. ResourceCache* cache = GetSubsystem<ResourceCache>();
  399. layoutFile = cache->GetResource<XMLFile>("UI/ScreenJoystick.xml");
  400. if (!layoutFile) // Error is already logged
  401. return -1;
  402. }
  403. UI* ui = GetSubsystem<UI>();
  404. SharedPtr<UIElement> screenJoystick = ui->LoadLayout(layoutFile, styleFile);
  405. if (!screenJoystick) // Error is already logged
  406. return -1;
  407. screenJoystick->SetSize(ui->GetRoot()->GetSize());
  408. ui->GetRoot()->AddChild(screenJoystick);
  409. // Get an unused ID for the screen joystick
  410. /// \todo After a real joystick has been plugged in 1073741824 times, the ranges will overlap
  411. SDL_JoystickID joystickID = SCREEN_JOYSTICK_START_ID;
  412. while (joysticks_.Contains(joystickID))
  413. ++joystickID;
  414. JoystickState& state = joysticks_[joystickID];
  415. state.joystickID_ = joystickID;
  416. state.name_ = screenJoystick->GetName();
  417. state.screenJoystick_ = screenJoystick;
  418. unsigned numButtons = 0;
  419. unsigned numAxes = 0;
  420. unsigned numHats = 0;
  421. const Vector<SharedPtr<UIElement> >& children = state.screenJoystick_->GetChildren();
  422. for (Vector<SharedPtr<UIElement> >::ConstIterator iter = children.Begin(); iter != children.End(); ++iter)
  423. {
  424. UIElement* element = iter->Get();
  425. String name = element->GetName();
  426. if (name.StartsWith("Button"))
  427. {
  428. ++numButtons;
  429. // Check whether the button has key binding
  430. Text* text = dynamic_cast<Text*>(element->GetChild("KeyBinding", false));
  431. if (text)
  432. {
  433. text->SetVisible(false);
  434. const String& key = text->GetText();
  435. int keyBinding;
  436. if (key.Length() == 1)
  437. keyBinding = key[0];
  438. else
  439. {
  440. PopulateKeyBindingMap(keyBindingMap);
  441. HashMap<String, int>::Iterator i = keyBindingMap.Find(key);
  442. if (i != keyBindingMap.End())
  443. keyBinding = i->second_;
  444. else
  445. {
  446. LOGERRORF("Unsupported key binding: %s", key.CString());
  447. keyBinding = M_MAX_INT;
  448. }
  449. }
  450. if (keyBinding != M_MAX_INT)
  451. element->SetVar(VAR_BUTTON_KEY_BINDING, keyBinding);
  452. }
  453. // Check whether the button has mouse button binding
  454. text = dynamic_cast<Text*>(element->GetChild("MouseButtonBinding", false));
  455. if (text)
  456. {
  457. text->SetVisible(false);
  458. const String& mouseButton = text->GetText();
  459. PopulateMouseButtonBindingMap(mouseButtonBindingMap);
  460. HashMap<String, int>::Iterator i = mouseButtonBindingMap.Find(mouseButton);
  461. if (i != mouseButtonBindingMap.End())
  462. element->SetVar(VAR_BUTTON_MOUSE_BUTTON_BINDING, i->second_);
  463. else
  464. LOGERRORF("Unsupported mouse button binding: %s", mouseButton.CString());
  465. }
  466. }
  467. else if (name.StartsWith("Axis"))
  468. {
  469. ++numAxes;
  470. ///\todo Axis emulation for screen joystick is not fully supported yet.
  471. LOGWARNING("Axis emulation for screen joystick is not fully supported yet");
  472. }
  473. else if (name.StartsWith("Hat"))
  474. {
  475. ++numHats;
  476. Text* text = dynamic_cast<Text*>(element->GetChild("KeyBinding", false));
  477. if (text)
  478. {
  479. text->SetVisible(false);
  480. String keyBinding = text->GetText();
  481. if (keyBinding.Contains(' ')) // e.g.: "UP DOWN LEFT RIGHT"
  482. {
  483. // Attempt to split the text using ' ' as separator
  484. Vector<String>keyBindings(keyBinding.Split(' '));
  485. String mappedKeyBinding;
  486. if (keyBindings.Size() == 4)
  487. {
  488. PopulateKeyBindingMap(keyBindingMap);
  489. for (unsigned j = 0; j < 4; ++j)
  490. {
  491. if (keyBindings[j].Length() == 1)
  492. mappedKeyBinding.Append(keyBindings[j][0]);
  493. else
  494. {
  495. HashMap<String, int>::Iterator i = keyBindingMap.Find(keyBindings[j]);
  496. if (i != keyBindingMap.End())
  497. mappedKeyBinding.Append(i->second_);
  498. else
  499. break;
  500. }
  501. }
  502. }
  503. if (mappedKeyBinding.Length() != 4)
  504. {
  505. LOGERRORF("%s has invalid key binding %s, fallback to WSAD", name.CString(), keyBinding.CString());
  506. keyBinding = "WSAD";
  507. }
  508. else
  509. keyBinding = mappedKeyBinding;
  510. }
  511. else if (keyBinding.Length() != 4)
  512. {
  513. LOGERRORF("%s has invalid key binding %s, fallback to WSAD", name.CString(), keyBinding.CString());
  514. keyBinding = "WSAD";
  515. }
  516. element->SetVar(VAR_BUTTON_KEY_BINDING, keyBinding);
  517. }
  518. }
  519. element->SetVar(VAR_SCREEN_JOYSTICK_ID, joystickID);
  520. }
  521. // Make sure all the children are non-focusable so they do not mistakenly to be considered as active UI input controls by application
  522. PODVector<UIElement*> allChildren;
  523. state.screenJoystick_->GetChildren(allChildren, true);
  524. for (PODVector<UIElement*>::Iterator iter = allChildren.Begin(); iter != allChildren.End(); ++iter)
  525. (*iter)->SetFocusMode(FM_NOTFOCUSABLE);
  526. state.Initialize(numButtons, numAxes, numHats);
  527. // There could be potentially more than one screen joystick, however they all will be handled by a same handler method
  528. // So there is no harm to replace the old handler with the new handler in each call to SubscribeToEvent()
  529. SubscribeToEvent(E_TOUCHBEGIN, HANDLER(Input, HandleScreenJoystickTouch));
  530. SubscribeToEvent(E_TOUCHMOVE, HANDLER(Input, HandleScreenJoystickTouch));
  531. SubscribeToEvent(E_TOUCHEND, HANDLER(Input, HandleScreenJoystickTouch));
  532. return joystickID;
  533. }
  534. bool Input::RemoveScreenJoystick(SDL_JoystickID id)
  535. {
  536. if (!joysticks_.Contains(id))
  537. {
  538. LOGERRORF("Failed to remove non-existing screen joystick ID #%d", id);
  539. return false;
  540. }
  541. JoystickState& state = joysticks_[id];
  542. if (!state.screenJoystick_)
  543. {
  544. LOGERRORF("Failed to remove joystick with ID #%d which is not a screen joystick", id);
  545. return false;
  546. }
  547. state.screenJoystick_->Remove();
  548. joysticks_.Erase(id);
  549. return true;
  550. }
  551. void Input::SetScreenJoystickVisible(SDL_JoystickID id, bool enable)
  552. {
  553. if (joysticks_.Contains(id))
  554. {
  555. JoystickState& state = joysticks_[id];
  556. if (state.screenJoystick_)
  557. state.screenJoystick_->SetVisible(enable);
  558. }
  559. }
  560. void Input::SetScreenKeyboardVisible(bool enable)
  561. {
  562. if (!graphics_)
  563. return;
  564. if (enable != IsScreenKeyboardVisible())
  565. {
  566. if (enable)
  567. SDL_StartTextInput();
  568. else
  569. SDL_StopTextInput();
  570. }
  571. }
  572. void Input::SetTouchEmulation(bool enable)
  573. {
  574. #if !defined(ANDROID) && !defined(IOS)
  575. if (enable != touchEmulation_)
  576. {
  577. if (enable)
  578. {
  579. // Touch emulation needs the mouse visible
  580. if (!mouseVisible_)
  581. SetMouseVisible(true);
  582. // Add a virtual touch device the first time we are enabling emulated touch
  583. if (!SDL_GetNumTouchDevices())
  584. SDL_AddTouch(0, "Emulated Touch");
  585. }
  586. else
  587. ResetTouches();
  588. touchEmulation_ = enable;
  589. }
  590. #endif
  591. }
  592. bool Input::RecordGesture()
  593. {
  594. // If have no touch devices, fail
  595. if (!SDL_GetNumTouchDevices())
  596. {
  597. LOGERROR("Can not record gesture: no touch devices");
  598. return false;
  599. }
  600. return SDL_RecordGesture(-1) != 0;
  601. }
  602. bool Input::SaveGestures(Serializer& dest)
  603. {
  604. RWOpsWrapper<Serializer> wrapper(dest);
  605. return SDL_SaveAllDollarTemplates(wrapper.GetRWOps()) != 0;
  606. }
  607. bool Input::SaveGesture(Serializer& dest, unsigned gestureID)
  608. {
  609. RWOpsWrapper<Serializer> wrapper(dest);
  610. return SDL_SaveDollarTemplate(gestureID, wrapper.GetRWOps()) != 0;
  611. }
  612. unsigned Input::LoadGestures(Deserializer& source)
  613. {
  614. // If have no touch devices, fail
  615. if (!SDL_GetNumTouchDevices())
  616. {
  617. LOGERROR("Can not load gestures: no touch devices");
  618. return 0;
  619. }
  620. RWOpsWrapper<Deserializer> wrapper(source);
  621. return SDL_LoadDollarTemplates(-1, wrapper.GetRWOps());
  622. }
  623. bool Input::RemoveGesture(unsigned gestureID)
  624. {
  625. return SDL_RemoveDollarTemplate(gestureID) != 0;
  626. }
  627. void Input::RemoveAllGestures()
  628. {
  629. SDL_RemoveAllDollarTemplates();
  630. }
  631. SDL_JoystickID Input::OpenJoystick(unsigned index)
  632. {
  633. SDL_Joystick* joystick = SDL_JoystickOpen(index);
  634. if (!joystick)
  635. {
  636. LOGERRORF("Cannot open joystick #%d", index);
  637. return -1;
  638. }
  639. // Create joystick state for the new joystick
  640. int joystickID = SDL_JoystickInstanceID(joystick);
  641. JoystickState& state = joysticks_[joystickID];
  642. state.joystick_ = joystick;
  643. state.joystickID_ = joystickID;
  644. state.name_ = SDL_JoystickName(joystick);
  645. if (SDL_IsGameController(index))
  646. state.controller_ = SDL_GameControllerOpen(index);
  647. unsigned numButtons = SDL_JoystickNumButtons(joystick);
  648. unsigned numAxes = SDL_JoystickNumAxes(joystick);
  649. unsigned numHats = SDL_JoystickNumHats(joystick);
  650. // When the joystick is a controller, make sure there's enough axes & buttons for the standard controller mappings
  651. if (state.controller_)
  652. {
  653. if (numButtons < SDL_CONTROLLER_BUTTON_MAX)
  654. numButtons = SDL_CONTROLLER_BUTTON_MAX;
  655. if (numAxes < SDL_CONTROLLER_AXIS_MAX)
  656. numAxes = SDL_CONTROLLER_AXIS_MAX;
  657. }
  658. state.Initialize(numButtons, numAxes, numHats);
  659. return joystickID;
  660. }
  661. int Input::GetKeyFromName(const String& name) const
  662. {
  663. return SDL_GetKeyFromName(name.CString());
  664. }
  665. int Input::GetKeyFromScancode(int scancode) const
  666. {
  667. return SDL_GetKeyFromScancode((SDL_Scancode)scancode);
  668. }
  669. String Input::GetKeyName(int key) const
  670. {
  671. return String(SDL_GetKeyName(key));
  672. }
  673. int Input::GetScancodeFromKey(int key) const
  674. {
  675. return SDL_GetScancodeFromKey(key);
  676. }
  677. int Input::GetScancodeFromName(const String& name) const
  678. {
  679. return SDL_GetScancodeFromName(name.CString());
  680. }
  681. String Input::GetScancodeName(int scancode) const
  682. {
  683. return SDL_GetScancodeName((SDL_Scancode)scancode);
  684. }
  685. bool Input::GetKeyDown(int key) const
  686. {
  687. return keyDown_.Contains(SDL_toupper(key));
  688. }
  689. bool Input::GetKeyPress(int key) const
  690. {
  691. return keyPress_.Contains(SDL_toupper(key));
  692. }
  693. bool Input::GetScancodeDown(int scancode) const
  694. {
  695. return scancodeDown_.Contains(scancode);
  696. }
  697. bool Input::GetScancodePress(int scancode) const
  698. {
  699. return scancodePress_.Contains(scancode);
  700. }
  701. bool Input::GetMouseButtonDown(int button) const
  702. {
  703. return (mouseButtonDown_ & button) != 0;
  704. }
  705. bool Input::GetMouseButtonPress(int button) const
  706. {
  707. return (mouseButtonPress_ & button) != 0;
  708. }
  709. bool Input::GetQualifierDown(int qualifier) const
  710. {
  711. if (qualifier == QUAL_SHIFT)
  712. return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
  713. if (qualifier == QUAL_CTRL)
  714. return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
  715. if (qualifier == QUAL_ALT)
  716. return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
  717. return false;
  718. }
  719. bool Input::GetQualifierPress(int qualifier) const
  720. {
  721. if (qualifier == QUAL_SHIFT)
  722. return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
  723. if (qualifier == QUAL_CTRL)
  724. return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
  725. if (qualifier == QUAL_ALT)
  726. return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
  727. return false;
  728. }
  729. int Input::GetQualifiers() const
  730. {
  731. int ret = 0;
  732. if (GetQualifierDown(QUAL_SHIFT))
  733. ret |= QUAL_SHIFT;
  734. if (GetQualifierDown(QUAL_CTRL))
  735. ret |= QUAL_CTRL;
  736. if (GetQualifierDown(QUAL_ALT))
  737. ret |= QUAL_ALT;
  738. return ret;
  739. }
  740. IntVector2 Input::GetMousePosition() const
  741. {
  742. IntVector2 ret = IntVector2::ZERO;
  743. if (!initialized_)
  744. return ret;
  745. SDL_GetMouseState(&ret.x_, &ret.y_);
  746. return ret;
  747. }
  748. TouchState* Input::GetTouch(unsigned index) const
  749. {
  750. if (index >= touches_.Size())
  751. return 0;
  752. HashMap<int, TouchState>::ConstIterator i = touches_.Begin();
  753. while (index--)
  754. ++i;
  755. return const_cast<TouchState*>(&i->second_);
  756. }
  757. JoystickState* Input::GetJoystickByIndex(unsigned index)
  758. {
  759. unsigned compare = 0;
  760. for (HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  761. {
  762. if (compare++ == index)
  763. return &(i->second_);
  764. }
  765. return 0;
  766. }
  767. JoystickState* Input::GetJoystick(SDL_JoystickID id)
  768. {
  769. HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Find(id);
  770. return i != joysticks_.End() ? &(i->second_) : 0;
  771. }
  772. bool Input::IsScreenJoystickVisible(SDL_JoystickID id) const
  773. {
  774. HashMap<SDL_JoystickID, JoystickState>::ConstIterator i = joysticks_.Find(id);
  775. return i != joysticks_.End() && i->second_.screenJoystick_ && i->second_.screenJoystick_->IsVisible();
  776. }
  777. bool Input::GetScreenKeyboardSupport() const
  778. {
  779. return graphics_ ? SDL_HasScreenKeyboardSupport() != 0 : false;
  780. }
  781. bool Input::IsScreenKeyboardVisible() const
  782. {
  783. if (graphics_)
  784. {
  785. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  786. return SDL_IsScreenKeyboardShown(window) != SDL_FALSE;
  787. }
  788. else
  789. return false;
  790. }
  791. bool Input::IsMinimized() const
  792. {
  793. // Return minimized state also when unfocused in fullscreen
  794. if (!inputFocus_ && graphics_ && graphics_->GetFullscreen())
  795. return true;
  796. else
  797. return minimized_;
  798. }
  799. void Input::Initialize()
  800. {
  801. Graphics* graphics = GetSubsystem<Graphics>();
  802. if (!graphics || !graphics->IsInitialized())
  803. return;
  804. graphics_ = graphics;
  805. // In external window mode only visible mouse is supported
  806. if (graphics_->GetExternalWindow())
  807. mouseVisible_ = true;
  808. // Set the initial activation
  809. focusedThisFrame_ = true;
  810. initialized_ = true;
  811. ResetJoysticks();
  812. ResetState();
  813. SubscribeToEvent(E_BEGINFRAME, HANDLER(Input, HandleBeginFrame));
  814. LOGINFO("Initialized input");
  815. }
  816. void Input::ResetJoysticks()
  817. {
  818. joysticks_.Clear();
  819. // Open each detected joystick automatically on startup
  820. int size = SDL_NumJoysticks();
  821. for (int i = 0; i < size; ++i)
  822. OpenJoystick(i);
  823. }
  824. void Input::GainFocus()
  825. {
  826. ResetState();
  827. inputFocus_ = true;
  828. focusedThisFrame_ = false;
  829. // Re-establish mouse cursor hiding as necessary
  830. if (!mouseVisible_)
  831. {
  832. SDL_ShowCursor(SDL_FALSE);
  833. suppressNextMouseMove_ = true;
  834. }
  835. else
  836. lastMousePosition_ = GetMousePosition();
  837. SendInputFocusEvent();
  838. }
  839. void Input::LoseFocus()
  840. {
  841. ResetState();
  842. inputFocus_ = false;
  843. focusedThisFrame_ = false;
  844. // Show the mouse cursor when inactive
  845. SDL_ShowCursor(SDL_TRUE);
  846. SetMouseMode(MM_ABSOLUTE);
  847. SendInputFocusEvent();
  848. }
  849. void Input::ResetState()
  850. {
  851. keyDown_.Clear();
  852. keyPress_.Clear();
  853. scancodeDown_.Clear();
  854. scancodePress_.Clear();
  855. /// \todo Check if resetting joystick state on input focus loss is even necessary
  856. for (HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  857. i->second_.Reset();
  858. ResetTouches();
  859. // Use SetMouseButton() to reset the state so that mouse events will be sent properly
  860. SetMouseButton(MOUSEB_LEFT, false);
  861. SetMouseButton(MOUSEB_RIGHT, false);
  862. SetMouseButton(MOUSEB_MIDDLE, false);
  863. mouseMove_ = IntVector2::ZERO;
  864. mouseMoveWheel_ = 0;
  865. mouseButtonPress_ = 0;
  866. }
  867. void Input::ResetTouches()
  868. {
  869. for (HashMap<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  870. {
  871. TouchState& state = i->second_;
  872. using namespace TouchEnd;
  873. VariantMap& eventData = GetEventDataMap();
  874. eventData[P_TOUCHID] = state.touchID_;
  875. eventData[P_X] = state.position_.x_;
  876. eventData[P_Y] = state.position_.y_;
  877. SendEvent(E_TOUCHEND, eventData);
  878. }
  879. touches_.Clear();
  880. }
  881. void Input::SendInputFocusEvent()
  882. {
  883. using namespace InputFocus;
  884. VariantMap& eventData = GetEventDataMap();
  885. eventData[P_FOCUS] = HasFocus();
  886. eventData[P_MINIMIZED] = IsMinimized();
  887. SendEvent(E_INPUTFOCUS, eventData);
  888. }
  889. void Input::SetMouseButton(int button, bool newState)
  890. {
  891. #ifdef REQUIRE_CLICK_TO_FOCUS
  892. if (!mouseVisible_ && !graphics_->GetFullscreen())
  893. {
  894. if (!inputFocus_ && newState && button == MOUSEB_LEFT)
  895. focusedThisFrame_ = true;
  896. }
  897. #endif
  898. // If we do not have focus yet, do not react to the mouse button down
  899. if (!graphics_->GetExternalWindow() && newState && !inputFocus_)
  900. return;
  901. if (newState)
  902. {
  903. if (!(mouseButtonDown_ & button))
  904. mouseButtonPress_ |= button;
  905. mouseButtonDown_ |= button;
  906. }
  907. else
  908. {
  909. if (!(mouseButtonDown_ & button))
  910. return;
  911. mouseButtonDown_ &= ~button;
  912. }
  913. using namespace MouseButtonDown;
  914. VariantMap& eventData = GetEventDataMap();
  915. eventData[P_BUTTON] = button;
  916. eventData[P_BUTTONS] = mouseButtonDown_;
  917. eventData[P_QUALIFIERS] = GetQualifiers();
  918. SendEvent(newState ? E_MOUSEBUTTONDOWN : E_MOUSEBUTTONUP, eventData);
  919. }
  920. void Input::SetKey(int key, int scancode, unsigned raw, bool newState)
  921. {
  922. // If we do not have focus yet, do not react to the key down
  923. if (!graphics_->GetExternalWindow() && newState && !inputFocus_)
  924. return;
  925. bool repeat = false;
  926. if (newState)
  927. {
  928. scancodeDown_.Insert(scancode);
  929. scancodePress_.Insert(scancode);
  930. if (!keyDown_.Contains(key))
  931. {
  932. keyDown_.Insert(key);
  933. keyPress_.Insert(key);
  934. }
  935. else
  936. repeat = true;
  937. }
  938. else
  939. {
  940. scancodeDown_.Erase(scancode);
  941. if (!keyDown_.Erase(key))
  942. return;
  943. }
  944. using namespace KeyDown;
  945. VariantMap& eventData = GetEventDataMap();
  946. eventData[P_KEY] = key;
  947. eventData[P_SCANCODE] = scancode;
  948. eventData[P_RAW] = raw;
  949. eventData[P_BUTTONS] = mouseButtonDown_;
  950. eventData[P_QUALIFIERS] = GetQualifiers();
  951. if (newState)
  952. eventData[P_REPEAT] = repeat;
  953. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  954. if ((key == KEY_RETURN || key == KEY_RETURN2 || key == KEY_KP_ENTER) && newState && !repeat && toggleFullscreen_ &&
  955. (GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT)))
  956. graphics_->ToggleFullscreen();
  957. }
  958. void Input::SetMouseWheel(int delta)
  959. {
  960. // If we do not have focus yet, do not react to the wheel
  961. if (!graphics_->GetExternalWindow() && !inputFocus_)
  962. return;
  963. if (delta)
  964. {
  965. mouseMoveWheel_ += delta;
  966. using namespace MouseWheel;
  967. VariantMap& eventData = GetEventDataMap();
  968. eventData[P_WHEEL] = delta;
  969. eventData[P_BUTTONS] = mouseButtonDown_;
  970. eventData[P_QUALIFIERS] = GetQualifiers();
  971. SendEvent(E_MOUSEWHEEL, eventData);
  972. }
  973. }
  974. void Input::SetMousePosition(const IntVector2& position)
  975. {
  976. if (!graphics_)
  977. return;
  978. SDL_WarpMouseInWindow(graphics_->GetImpl()->GetWindow(), position.x_, position.y_);
  979. }
  980. void Input::HandleSDLEvent(void* sdlEvent)
  981. {
  982. SDL_Event& evt = *static_cast<SDL_Event*>(sdlEvent);
  983. switch (evt.type)
  984. {
  985. case SDL_KEYDOWN:
  986. // Convert to uppercase to match Win32 virtual key codes
  987. SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), evt.key.keysym.scancode, evt.key.keysym.raw, true);
  988. break;
  989. case SDL_KEYUP:
  990. SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), evt.key.keysym.scancode, evt.key.keysym.raw, false);
  991. break;
  992. case SDL_TEXTINPUT:
  993. {
  994. textInput_ = &evt.text.text[0];
  995. unsigned unicode = textInput_.AtUTF8(0);
  996. if (unicode)
  997. {
  998. using namespace TextInput;
  999. VariantMap textInputEventData;
  1000. textInputEventData[P_TEXT] = textInput_;
  1001. textInputEventData[P_BUTTONS] = mouseButtonDown_;
  1002. textInputEventData[P_QUALIFIERS] = GetQualifiers();
  1003. SendEvent(E_TEXTINPUT, textInputEventData);
  1004. }
  1005. }
  1006. break;
  1007. case SDL_MOUSEBUTTONDOWN:
  1008. if (!touchEmulation_)
  1009. SetMouseButton(1 << (evt.button.button - 1), true);
  1010. else
  1011. {
  1012. int x, y;
  1013. SDL_GetMouseState(&x, &y);
  1014. SDL_Event event;
  1015. event.type = SDL_FINGERDOWN;
  1016. event.tfinger.touchId = 0;
  1017. event.tfinger.fingerId = evt.button.button - 1;
  1018. event.tfinger.pressure = 1.0f;
  1019. event.tfinger.x = (float)x / (float)graphics_->GetWidth();
  1020. event.tfinger.y = (float)y / (float)graphics_->GetHeight();
  1021. event.tfinger.dx = 0;
  1022. event.tfinger.dy = 0;
  1023. SDL_PushEvent(&event);
  1024. }
  1025. break;
  1026. case SDL_MOUSEBUTTONUP:
  1027. if (!touchEmulation_)
  1028. SetMouseButton(1 << (evt.button.button - 1), false);
  1029. else
  1030. {
  1031. int x, y;
  1032. SDL_GetMouseState(&x, &y);
  1033. SDL_Event event;
  1034. event.type = SDL_FINGERUP;
  1035. event.tfinger.touchId = 0;
  1036. event.tfinger.fingerId = evt.button.button - 1;
  1037. event.tfinger.pressure = 0.0f;
  1038. event.tfinger.x = (float)x / (float)graphics_->GetWidth();
  1039. event.tfinger.y = (float)y / (float)graphics_->GetHeight();
  1040. event.tfinger.dx = 0;
  1041. event.tfinger.dy = 0;
  1042. SDL_PushEvent(&event);
  1043. }
  1044. break;
  1045. case SDL_MOUSEMOTION:
  1046. if ((mouseVisible_ || mouseMode_ == MM_RELATIVE) && !touchEmulation_)
  1047. {
  1048. mouseMove_.x_ += evt.motion.xrel;
  1049. mouseMove_.y_ += evt.motion.yrel;
  1050. using namespace MouseMove;
  1051. VariantMap& eventData = GetEventDataMap();
  1052. if (mouseVisible_)
  1053. {
  1054. eventData[P_X] = evt.motion.x;
  1055. eventData[P_Y] = evt.motion.y;
  1056. }
  1057. eventData[P_DX] = evt.motion.xrel;
  1058. eventData[P_DY] = evt.motion.yrel;
  1059. eventData[P_BUTTONS] = mouseButtonDown_;
  1060. eventData[P_QUALIFIERS] = GetQualifiers();
  1061. SendEvent(E_MOUSEMOVE, eventData);
  1062. }
  1063. // Only the left mouse button "finger" moves along with the mouse movement
  1064. else if (touchEmulation_ && touches_.Contains(0))
  1065. {
  1066. int x, y;
  1067. SDL_GetMouseState(&x, &y);
  1068. SDL_Event event;
  1069. event.type = SDL_FINGERMOTION;
  1070. event.tfinger.touchId = 0;
  1071. event.tfinger.fingerId = 0;
  1072. event.tfinger.pressure = 1.0f;
  1073. event.tfinger.x = (float)x / (float)graphics_->GetWidth();
  1074. event.tfinger.y = (float)y / (float)graphics_->GetHeight();
  1075. event.tfinger.dx = (float)evt.motion.xrel / (float)graphics_->GetWidth();
  1076. event.tfinger.dy = (float)evt.motion.yrel / (float)graphics_->GetHeight();
  1077. SDL_PushEvent(&event);
  1078. }
  1079. break;
  1080. case SDL_MOUSEWHEEL:
  1081. if (!touchEmulation_)
  1082. SetMouseWheel(evt.wheel.y);
  1083. break;
  1084. case SDL_FINGERDOWN:
  1085. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  1086. {
  1087. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  1088. TouchState& state = touches_[touchID];
  1089. state.touchID_ = touchID;
  1090. state.lastPosition_ = state.position_ = IntVector2((int)(evt.tfinger.x * graphics_->GetWidth()),
  1091. (int)(evt.tfinger.y * graphics_->GetHeight()));
  1092. state.delta_ = IntVector2::ZERO;
  1093. state.pressure_ = evt.tfinger.pressure;
  1094. using namespace TouchBegin;
  1095. VariantMap& eventData = GetEventDataMap();
  1096. eventData[P_TOUCHID] = touchID;
  1097. eventData[P_X] = state.position_.x_;
  1098. eventData[P_Y] = state.position_.y_;
  1099. eventData[P_PRESSURE] = state.pressure_;
  1100. SendEvent(E_TOUCHBEGIN, eventData);
  1101. }
  1102. break;
  1103. case SDL_FINGERUP:
  1104. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  1105. {
  1106. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  1107. TouchState& state = touches_[touchID];
  1108. using namespace TouchEnd;
  1109. VariantMap& eventData = GetEventDataMap();
  1110. // Do not trust the position in the finger up event. Instead use the last position stored in the
  1111. // touch structure
  1112. eventData[P_TOUCHID] = touchID;
  1113. eventData[P_X] = state.position_.x_;
  1114. eventData[P_Y] = state.position_.y_;
  1115. SendEvent(E_TOUCHEND, eventData);
  1116. touches_.Erase(touchID);
  1117. }
  1118. break;
  1119. case SDL_FINGERMOTION:
  1120. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  1121. {
  1122. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  1123. // We don't want this event to create a new touches_ event if it doesn't exist (touchEmulation)
  1124. if (touchEmulation_ && !touches_.Contains(touchID))
  1125. break;
  1126. TouchState& state = touches_[touchID];
  1127. state.touchID_ = touchID;
  1128. state.position_ = IntVector2((int)(evt.tfinger.x * graphics_->GetWidth()),
  1129. (int)(evt.tfinger.y * graphics_->GetHeight()));
  1130. state.delta_ = state.position_ - state.lastPosition_;
  1131. state.pressure_ = evt.tfinger.pressure;
  1132. using namespace TouchMove;
  1133. VariantMap& eventData = GetEventDataMap();
  1134. eventData[P_TOUCHID] = touchID;
  1135. eventData[P_X] = state.position_.x_;
  1136. eventData[P_Y] = state.position_.y_;
  1137. eventData[P_DX] = (int)(evt.tfinger.dx * graphics_->GetWidth());
  1138. eventData[P_DY] = (int)(evt.tfinger.dy * graphics_->GetHeight());
  1139. eventData[P_PRESSURE] = state.pressure_;
  1140. SendEvent(E_TOUCHMOVE, eventData);
  1141. }
  1142. break;
  1143. case SDL_DOLLARRECORD:
  1144. {
  1145. using namespace GestureRecorded;
  1146. VariantMap& eventData = GetEventDataMap();
  1147. eventData[P_GESTUREID] = (int)evt.dgesture.gestureId;
  1148. SendEvent(E_GESTURERECORDED, eventData);
  1149. }
  1150. break;
  1151. case SDL_DOLLARGESTURE:
  1152. {
  1153. using namespace GestureInput;
  1154. VariantMap& eventData = GetEventDataMap();
  1155. eventData[P_GESTUREID] = (int)evt.dgesture.gestureId;
  1156. eventData[P_CENTERX] = (int)(evt.dgesture.x * graphics_->GetWidth());
  1157. eventData[P_CENTERY] = (int)(evt.dgesture.y * graphics_->GetHeight());
  1158. eventData[P_NUMFINGERS] = (int)evt.dgesture.numFingers;
  1159. eventData[P_ERROR] = evt.dgesture.error;
  1160. SendEvent(E_GESTUREINPUT, eventData);
  1161. }
  1162. break;
  1163. case SDL_MULTIGESTURE:
  1164. {
  1165. using namespace MultiGesture;
  1166. VariantMap& eventData = GetEventDataMap();
  1167. eventData[P_CENTERX] = (int)(evt.mgesture.x * graphics_->GetWidth());
  1168. eventData[P_CENTERY] = (int)(evt.mgesture.y * graphics_->GetHeight());
  1169. eventData[P_NUMFINGERS] = (int)evt.mgesture.numFingers;
  1170. eventData[P_DTHETA] = M_RADTODEG * evt.mgesture.dTheta;
  1171. eventData[P_DDIST] = evt.mgesture.dDist;
  1172. SendEvent(E_MULTIGESTURE, eventData);
  1173. }
  1174. break;
  1175. case SDL_JOYDEVICEADDED:
  1176. {
  1177. using namespace JoystickConnected;
  1178. SDL_JoystickID joystickID = OpenJoystick(evt.jdevice.which);
  1179. VariantMap& eventData = GetEventDataMap();
  1180. eventData[P_JOYSTICKID] = joystickID;
  1181. SendEvent(E_JOYSTICKCONNECTED, eventData);
  1182. }
  1183. break;
  1184. case SDL_JOYDEVICEREMOVED:
  1185. {
  1186. using namespace JoystickDisconnected;
  1187. joysticks_.Erase(evt.jdevice.which);
  1188. VariantMap& eventData = GetEventDataMap();
  1189. eventData[P_JOYSTICKID] = evt.jdevice.which;
  1190. SendEvent(E_JOYSTICKDISCONNECTED, eventData);
  1191. }
  1192. break;
  1193. case SDL_JOYBUTTONDOWN:
  1194. {
  1195. using namespace JoystickButtonDown;
  1196. unsigned button = evt.jbutton.button;
  1197. SDL_JoystickID joystickID = evt.jbutton.which;
  1198. JoystickState& state = joysticks_[joystickID];
  1199. // Skip ordinary joystick event for a controller
  1200. if (!state.controller_)
  1201. {
  1202. VariantMap& eventData = GetEventDataMap();
  1203. eventData[P_JOYSTICKID] = joystickID;
  1204. eventData[P_BUTTON] = button;
  1205. if (button < state.buttons_.Size())
  1206. {
  1207. state.buttons_[button] = true;
  1208. state.buttonPress_[button] = true;
  1209. SendEvent(E_JOYSTICKBUTTONDOWN, eventData);
  1210. }
  1211. }
  1212. }
  1213. break;
  1214. case SDL_JOYBUTTONUP:
  1215. {
  1216. using namespace JoystickButtonUp;
  1217. unsigned button = evt.jbutton.button;
  1218. SDL_JoystickID joystickID = evt.jbutton.which;
  1219. JoystickState& state = joysticks_[joystickID];
  1220. if (!state.controller_)
  1221. {
  1222. VariantMap& eventData = GetEventDataMap();
  1223. eventData[P_JOYSTICKID] = joystickID;
  1224. eventData[P_BUTTON] = button;
  1225. if (button < state.buttons_.Size())
  1226. {
  1227. if (!state.controller_)
  1228. state.buttons_[button] = false;
  1229. SendEvent(E_JOYSTICKBUTTONUP, eventData);
  1230. }
  1231. }
  1232. }
  1233. break;
  1234. case SDL_JOYAXISMOTION:
  1235. {
  1236. using namespace JoystickAxisMove;
  1237. SDL_JoystickID joystickID = evt.jaxis.which;
  1238. JoystickState& state = joysticks_[joystickID];
  1239. if (!state.controller_)
  1240. {
  1241. VariantMap& eventData = GetEventDataMap();
  1242. eventData[P_JOYSTICKID] = joystickID;
  1243. eventData[P_AXIS] = evt.jaxis.axis;
  1244. eventData[P_POSITION] = Clamp((float)evt.jaxis.value / 32767.0f, -1.0f, 1.0f);
  1245. if (evt.jaxis.axis < state.axes_.Size())
  1246. {
  1247. // If the joystick is a controller, only use the controller axis mappings
  1248. // (we'll also get the controller event)
  1249. if (!state.controller_)
  1250. state.axes_[evt.jaxis.axis] = eventData[P_POSITION].GetFloat();
  1251. SendEvent(E_JOYSTICKAXISMOVE, eventData);
  1252. }
  1253. }
  1254. }
  1255. break;
  1256. case SDL_JOYHATMOTION:
  1257. {
  1258. using namespace JoystickHatMove;
  1259. SDL_JoystickID joystickID = evt.jaxis.which;
  1260. JoystickState& state = joysticks_[joystickID];
  1261. VariantMap& eventData = GetEventDataMap();
  1262. eventData[P_JOYSTICKID] = joystickID;
  1263. eventData[P_HAT] = evt.jhat.hat;
  1264. eventData[P_POSITION] = evt.jhat.value;
  1265. if (evt.jhat.hat < state.hats_.Size())
  1266. {
  1267. state.hats_[evt.jhat.hat] = evt.jhat.value;
  1268. SendEvent(E_JOYSTICKHATMOVE, eventData);
  1269. }
  1270. }
  1271. break;
  1272. case SDL_CONTROLLERBUTTONDOWN:
  1273. {
  1274. using namespace JoystickButtonDown;
  1275. unsigned button = evt.cbutton.button;
  1276. SDL_JoystickID joystickID = evt.cbutton.which;
  1277. JoystickState& state = joysticks_[joystickID];
  1278. VariantMap& eventData = GetEventDataMap();
  1279. eventData[P_JOYSTICKID] = joystickID;
  1280. eventData[P_BUTTON] = button;
  1281. if (button < state.buttons_.Size())
  1282. {
  1283. state.buttons_[button] = true;
  1284. state.buttonPress_[button] = true;
  1285. SendEvent(E_JOYSTICKBUTTONDOWN, eventData);
  1286. }
  1287. }
  1288. break;
  1289. case SDL_CONTROLLERBUTTONUP:
  1290. {
  1291. using namespace JoystickButtonUp;
  1292. unsigned button = evt.cbutton.button;
  1293. SDL_JoystickID joystickID = evt.cbutton.which;
  1294. JoystickState& state = joysticks_[joystickID];
  1295. VariantMap& eventData = GetEventDataMap();
  1296. eventData[P_JOYSTICKID] = joystickID;
  1297. eventData[P_BUTTON] = button;
  1298. if (button < state.buttons_.Size())
  1299. {
  1300. state.buttons_[button] = false;
  1301. SendEvent(E_JOYSTICKBUTTONUP, eventData);
  1302. }
  1303. }
  1304. break;
  1305. case SDL_CONTROLLERAXISMOTION:
  1306. {
  1307. using namespace JoystickAxisMove;
  1308. SDL_JoystickID joystickID = evt.caxis.which;
  1309. JoystickState& state = joysticks_[joystickID];
  1310. VariantMap& eventData = GetEventDataMap();
  1311. eventData[P_JOYSTICKID] = joystickID;
  1312. eventData[P_AXIS] = evt.caxis.axis;
  1313. eventData[P_POSITION] = Clamp((float)evt.caxis.value / 32767.0f, -1.0f, 1.0f);
  1314. if (evt.caxis.axis < state.axes_.Size())
  1315. {
  1316. state.axes_[evt.caxis.axis] = eventData[P_POSITION].GetFloat();
  1317. SendEvent(E_JOYSTICKAXISMOVE, eventData);
  1318. }
  1319. }
  1320. break;
  1321. case SDL_WINDOWEVENT:
  1322. {
  1323. switch (evt.window.event)
  1324. {
  1325. case SDL_WINDOWEVENT_MINIMIZED:
  1326. minimized_ = true;
  1327. SendInputFocusEvent();
  1328. break;
  1329. case SDL_WINDOWEVENT_MAXIMIZED:
  1330. case SDL_WINDOWEVENT_RESTORED:
  1331. minimized_ = false;
  1332. SendInputFocusEvent();
  1333. #ifdef IOS
  1334. // On iOS we never lose the GL context, but may have done GPU object changes that could not be applied yet.
  1335. // Apply them now
  1336. graphics_->Restore();
  1337. #endif
  1338. break;
  1339. #ifdef ANDROID
  1340. case SDL_WINDOWEVENT_FOCUS_GAINED:
  1341. // Restore GPU objects to the new GL context
  1342. graphics_->Restore();
  1343. break;
  1344. #endif
  1345. case SDL_WINDOWEVENT_RESIZED:
  1346. graphics_->WindowResized();
  1347. break;
  1348. case SDL_WINDOWEVENT_MOVED:
  1349. graphics_->WindowMoved();
  1350. break;
  1351. }
  1352. }
  1353. break;
  1354. case SDL_DROPFILE:
  1355. {
  1356. using namespace DropFile;
  1357. VariantMap& eventData = GetEventDataMap();
  1358. eventData[P_FILENAME] = GetInternalPath(String(evt.drop.file));
  1359. SDL_free(evt.drop.file);
  1360. SendEvent(E_DROPFILE, eventData);
  1361. }
  1362. break;
  1363. case SDL_QUIT:
  1364. SendEvent(E_EXITREQUESTED);
  1365. break;
  1366. }
  1367. }
  1368. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  1369. {
  1370. // Reset input state on subsequent initializations
  1371. if (!initialized_)
  1372. Initialize();
  1373. else
  1374. ResetState();
  1375. // Re-enable cursor clipping, and re-center the cursor (if needed) to the new screen size, so that there is no erroneous
  1376. // mouse move event. Also get new window ID if it changed
  1377. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  1378. windowID_ = SDL_GetWindowID(window);
  1379. if (!mouseVisible_)
  1380. {
  1381. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  1382. SetMousePosition(center);
  1383. lastMousePosition_ = center;
  1384. }
  1385. focusedThisFrame_ = true;
  1386. // After setting a new screen mode we should not be minimized
  1387. minimized_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0;
  1388. }
  1389. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  1390. {
  1391. // Update input right at the beginning of the frame
  1392. Update();
  1393. }
  1394. void Input::HandleScreenJoystickTouch(StringHash eventType, VariantMap& eventData)
  1395. {
  1396. using namespace TouchBegin;
  1397. // Only interested in events from screen joystick(s)
  1398. TouchState& state = touches_[eventData[P_TOUCHID].GetInt()];
  1399. IntVector2 position(state.position_.x_, state.position_.y_);
  1400. UIElement* element = eventType == E_TOUCHBEGIN ? GetSubsystem<UI>()->GetElementAt(position) : state.touchedElement_;
  1401. if (!element)
  1402. return;
  1403. Variant variant = element->GetVar(VAR_SCREEN_JOYSTICK_ID);
  1404. if (variant.IsEmpty())
  1405. return;
  1406. SDL_JoystickID joystickID = variant.GetInt();
  1407. if (eventType == E_TOUCHEND)
  1408. state.touchedElement_.Reset();
  1409. else
  1410. state.touchedElement_ = element;
  1411. // Prepare a fake SDL event
  1412. SDL_Event evt;
  1413. const String& name = element->GetName();
  1414. if (name.StartsWith("Button"))
  1415. {
  1416. if (eventType == E_TOUCHMOVE)
  1417. return;
  1418. // Determine whether to inject a joystick event or keyboard/mouse event
  1419. Variant keyBindingVar = element->GetVar(VAR_BUTTON_KEY_BINDING);
  1420. Variant mouseButtonBindingVar = element->GetVar(VAR_BUTTON_MOUSE_BUTTON_BINDING);
  1421. if (keyBindingVar.IsEmpty() && mouseButtonBindingVar.IsEmpty())
  1422. {
  1423. evt.type = eventType == E_TOUCHBEGIN ? SDL_JOYBUTTONDOWN : SDL_JOYBUTTONUP;
  1424. evt.jbutton.which = joystickID;
  1425. evt.jbutton.button = ToUInt(name.Substring(6));
  1426. }
  1427. else
  1428. {
  1429. if (!keyBindingVar.IsEmpty())
  1430. {
  1431. evt.type = eventType == E_TOUCHBEGIN ? SDL_KEYDOWN : SDL_KEYUP;
  1432. evt.key.keysym.sym = keyBindingVar.GetInt();
  1433. evt.key.keysym.scancode = SDL_SCANCODE_UNKNOWN;
  1434. }
  1435. if (!mouseButtonBindingVar.IsEmpty())
  1436. {
  1437. // Mouse button are sent as extra events besides key events
  1438. // Disable touch emulation handling during this to prevent endless loop
  1439. bool oldTouchEmulation = touchEmulation_;
  1440. touchEmulation_ = false;
  1441. SDL_Event evt;
  1442. evt.type = eventType == E_TOUCHBEGIN ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP;
  1443. evt.button.button = mouseButtonBindingVar.GetInt();
  1444. HandleSDLEvent(&evt);
  1445. touchEmulation_ = oldTouchEmulation;
  1446. }
  1447. }
  1448. }
  1449. else if (name.StartsWith("Hat"))
  1450. {
  1451. Variant keyBindingVar = element->GetVar(VAR_BUTTON_KEY_BINDING);
  1452. if (keyBindingVar.IsEmpty())
  1453. {
  1454. evt.type = SDL_JOYHATMOTION;
  1455. evt.jaxis.which = joystickID;
  1456. evt.jhat.hat = ToUInt(name.Substring(3));
  1457. evt.jhat.value = HAT_CENTER;
  1458. if (eventType != E_TOUCHEND)
  1459. {
  1460. IntVector2 relPosition = position - element->GetScreenPosition() - element->GetSize() / 2;
  1461. if (relPosition.y_ < 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1462. evt.jhat.value |= HAT_UP;
  1463. if (relPosition.y_ > 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1464. evt.jhat.value |= HAT_DOWN;
  1465. if (relPosition.x_ < 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1466. evt.jhat.value |= HAT_LEFT;
  1467. if (relPosition.x_ > 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1468. evt.jhat.value |= HAT_RIGHT;
  1469. }
  1470. }
  1471. else
  1472. {
  1473. // Hat is binded by 4 keys, like 'WASD'
  1474. String keyBinding = keyBindingVar.GetString();
  1475. if (eventType == E_TOUCHEND)
  1476. {
  1477. evt.type = SDL_KEYUP;
  1478. evt.key.keysym.sym = element->GetVar(VAR_LAST_KEYSYM).GetInt();
  1479. if (!evt.key.keysym.sym)
  1480. return;
  1481. element->SetVar(VAR_LAST_KEYSYM, 0);
  1482. }
  1483. else
  1484. {
  1485. evt.type = SDL_KEYDOWN;
  1486. IntVector2 relPosition = position - element->GetScreenPosition() - element->GetSize() / 2;
  1487. if (relPosition.y_ < 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1488. evt.key.keysym.sym = keyBinding[0];
  1489. else if (relPosition.y_ > 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1490. evt.key.keysym.sym = keyBinding[1];
  1491. else if (relPosition.x_ < 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1492. evt.key.keysym.sym = keyBinding[2];
  1493. else if (relPosition.x_ > 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1494. evt.key.keysym.sym = keyBinding[3];
  1495. else
  1496. return;
  1497. if (eventType == E_TOUCHMOVE && evt.key.keysym.sym != element->GetVar(VAR_LAST_KEYSYM).GetInt())
  1498. {
  1499. // Dragging past the directional boundary will cause an additional key up event for previous key symbol
  1500. SDL_Event evt;
  1501. evt.type = SDL_KEYUP;
  1502. evt.key.keysym.sym = element->GetVar(VAR_LAST_KEYSYM).GetInt();
  1503. if (evt.key.keysym.sym)
  1504. {
  1505. evt.key.keysym.scancode = SDL_SCANCODE_UNKNOWN;
  1506. HandleSDLEvent(&evt);
  1507. }
  1508. element->SetVar(VAR_LAST_KEYSYM, 0);
  1509. }
  1510. evt.key.keysym.scancode = SDL_SCANCODE_UNKNOWN;
  1511. element->SetVar(VAR_LAST_KEYSYM, evt.key.keysym.sym);
  1512. }
  1513. }
  1514. }
  1515. else
  1516. return;
  1517. // Handle the fake SDL event to turn it into Urho3D genuine event
  1518. HandleSDLEvent(&evt);
  1519. }
  1520. }