Input.cpp 61 KB

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