Input.cpp 61 KB

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