Input.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "CoreEvents.h"
  25. #include "FileSystem.h"
  26. #include "Graphics.h"
  27. #include "GraphicsEvents.h"
  28. #include "GraphicsImpl.h"
  29. #include "Input.h"
  30. #include "Log.h"
  31. #include "Mutex.h"
  32. #include "ProcessUtils.h"
  33. #include "Profiler.h"
  34. #include "ResourceCache.h"
  35. #include "StringUtils.h"
  36. #include "Text.h"
  37. #include "UI.h"
  38. #include <cstring>
  39. #include <SDL.h>
  40. #include "DebugNew.h"
  41. // Require a click inside window before re-hiding mouse cursor on OSX, otherwise dragging the window
  42. // can be incorrectly interpreted as mouse movement inside the window
  43. #if defined(__APPLE__) && !defined(IOS)
  44. #define REQUIRE_CLICK_TO_FOCUS
  45. #endif
  46. namespace Urho3D
  47. {
  48. const int SCREEN_JOYSTICK_START_INDEX = 1000;
  49. const ShortStringHash VAR_BUTTON_KEY_BINDING("VAR_BUTTON_KEY_BINDING");
  50. const ShortStringHash VAR_LAST_KEYSYM("VAR_LAST_KEYSYM");
  51. const ShortStringHash VAR_SCREEN_JOYSTICK_INDEX("VAR_SCREEN_JOYSTICK_INDEX");
  52. /// Convert SDL keycode if necessary.
  53. int ConvertSDLKeyCode(int keySym, int scanCode)
  54. {
  55. if (scanCode == SCANCODE_AC_BACK)
  56. return KEY_ESC;
  57. else
  58. return SDL_toupper(keySym);
  59. }
  60. Input::Input(Context* context) :
  61. Object(context),
  62. mouseButtonDown_(0),
  63. mouseButtonPress_(0),
  64. mouseMoveWheel_(0),
  65. windowID_(0),
  66. toggleFullscreen_(true),
  67. mouseVisible_(false),
  68. inputFocus_(false),
  69. minimized_(false),
  70. focusedThisFrame_(false),
  71. suppressNextMouseMove_(false),
  72. initialized_(false)
  73. {
  74. SubscribeToEvent(E_SCREENMODE, HANDLER(Input, HandleScreenMode));
  75. // Try to initialize right now, but skip if screen mode is not yet set
  76. Initialize();
  77. }
  78. Input::~Input()
  79. {
  80. }
  81. void Input::Update()
  82. {
  83. assert(initialized_);
  84. PROFILE(UpdateInput);
  85. // Reset input accumulation for this frame
  86. keyPress_.Clear();
  87. scancodePress_.Clear();
  88. mouseButtonPress_ = 0;
  89. mouseMove_ = IntVector2::ZERO;
  90. mouseMoveWheel_ = 0;
  91. for (Vector<JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  92. {
  93. for (unsigned j = 0; j < i->buttonPress_.Size(); ++j)
  94. i->buttonPress_[j] = false;
  95. }
  96. // Reset touch delta movement
  97. for (HashMap<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  98. {
  99. TouchState& state = i->second_;
  100. state.lastPosition_ = state.position_;
  101. state.delta_ = IntVector2::ZERO;
  102. }
  103. // Check and handle SDL events
  104. SDL_PumpEvents();
  105. SDL_Event evt;
  106. while (SDL_PeepEvents(&evt, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0)
  107. HandleSDLEvent(&evt);
  108. // Check for activation and inactivation from SDL window flags. Must nullcheck the window pointer because it may have
  109. // been closed due to input events
  110. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  111. unsigned flags = window ? SDL_GetWindowFlags(window) & (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS) : 0;
  112. if (window)
  113. {
  114. #ifdef REQUIRE_CLICK_TO_FOCUS
  115. if (!inputFocus_ && (graphics_->GetFullscreen() || mouseVisible_) && flags == (SDL_WINDOW_INPUT_FOCUS |
  116. SDL_WINDOW_MOUSE_FOCUS))
  117. #else
  118. if (!inputFocus_ && (flags & SDL_WINDOW_INPUT_FOCUS))
  119. #endif
  120. focusedThisFrame_ = true;
  121. if (focusedThisFrame_)
  122. GainFocus();
  123. if (inputFocus_ && (flags & SDL_WINDOW_INPUT_FOCUS) == 0)
  124. LoseFocus();
  125. }
  126. else
  127. return;
  128. // Check for relative mode mouse move
  129. if (graphics_->GetExternalWindow() || (!mouseVisible_ && inputFocus_ && (flags & SDL_WINDOW_MOUSE_FOCUS)))
  130. {
  131. IntVector2 mousePosition = GetMousePosition();
  132. mouseMove_ = mousePosition - lastMousePosition_;
  133. if (graphics_->GetExternalWindow())
  134. lastMousePosition_ = mousePosition;
  135. else
  136. {
  137. // Recenter the mouse cursor manually
  138. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  139. if (mousePosition != center)
  140. {
  141. SetMousePosition(center);
  142. lastMousePosition_ = center;
  143. }
  144. }
  145. // Send mouse move event if necessary
  146. if (mouseMove_ != IntVector2::ZERO)
  147. {
  148. if (suppressNextMouseMove_)
  149. {
  150. mouseMove_ = IntVector2::ZERO;
  151. suppressNextMouseMove_ = false;
  152. }
  153. else
  154. {
  155. using namespace MouseMove;
  156. VariantMap& eventData = GetEventDataMap();
  157. if (mouseVisible_)
  158. {
  159. eventData[P_X] = mousePosition.x_;
  160. eventData[P_Y] = mousePosition.y_;
  161. }
  162. eventData[P_DX] = mouseMove_.x_;
  163. eventData[P_DY] = mouseMove_.y_;
  164. eventData[P_BUTTONS] = mouseButtonDown_;
  165. eventData[P_QUALIFIERS] = GetQualifiers();
  166. SendEvent(E_MOUSEMOVE, eventData);
  167. }
  168. }
  169. }
  170. }
  171. void Input::SetMouseVisible(bool enable)
  172. {
  173. // SDL Raspberry Pi "video driver" does not have proper OS mouse support yet, so no-op for now
  174. #ifndef RASPI
  175. if (enable != mouseVisible_)
  176. {
  177. mouseVisible_ = enable;
  178. if (initialized_)
  179. {
  180. // External windows can only support visible mouse cursor
  181. if (graphics_->GetExternalWindow())
  182. {
  183. mouseVisible_ = true;
  184. return;
  185. }
  186. if (!mouseVisible_ && inputFocus_)
  187. SDL_ShowCursor(SDL_FALSE);
  188. else
  189. SDL_ShowCursor(SDL_TRUE);
  190. }
  191. using namespace MouseVisibleChanged;
  192. VariantMap& eventData = GetEventDataMap();
  193. eventData[P_VISIBLE] = mouseVisible_;
  194. SendEvent(E_MOUSEVISIBLECHANGED, eventData);
  195. }
  196. #endif
  197. }
  198. void Input::SetToggleFullscreen(bool enable)
  199. {
  200. toggleFullscreen_ = enable;
  201. }
  202. bool Input::DetectJoysticks()
  203. {
  204. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  205. SDL_InitSubSystem(SDL_INIT_JOYSTICK);
  206. ResetJoysticks();
  207. return true;
  208. }
  209. unsigned Input::AddScreenJoystick(XMLFile* layoutFile, XMLFile* styleFile)
  210. {
  211. static HashMap<String, int> keyBindings;
  212. if (!graphics_)
  213. {
  214. LOGWARNING("Cannot add screen joystick in headless mode");
  215. return M_MAX_UNSIGNED;
  216. }
  217. // If layout file is not given, use the default screen joystick layout
  218. if (!layoutFile)
  219. {
  220. ResourceCache* cache = GetSubsystem<ResourceCache>();
  221. layoutFile = cache->GetResource<XMLFile>("UI/ScreenJoystick.xml");
  222. if (!layoutFile) // Error is already logged
  223. return M_MAX_UNSIGNED;
  224. }
  225. UI* ui = GetSubsystem<UI>();
  226. SharedPtr<UIElement> screenJoystick = ui->LoadLayout(layoutFile, styleFile);
  227. if (!screenJoystick) // Error is already logged
  228. return M_MAX_UNSIGNED;
  229. screenJoystick->SetSize(ui->GetRoot()->GetSize());
  230. screenJoystick->SetVisible(false); // Set to visible when it is opened later
  231. ui->GetRoot()->AddChild(screenJoystick);
  232. unsigned index = joysticks_.Size();
  233. joysticks_.Resize(index + 1);
  234. JoystickState& state = joysticks_[index];
  235. joystickIDMap_[SCREEN_JOYSTICK_START_INDEX + index] = index;
  236. state.name_ = screenJoystick->GetName();
  237. state.screenJoystick_ = screenJoystick;
  238. unsigned numButtons = 0;
  239. unsigned numAxes = 0;
  240. unsigned numHats = 0;
  241. const Vector<SharedPtr<UIElement> >& children = state.screenJoystick_->GetChildren();
  242. for (Vector<SharedPtr<UIElement> >::ConstIterator iter = children.Begin(); iter != children.End(); ++iter)
  243. {
  244. UIElement* element = iter->Get();
  245. String name = element->GetName();
  246. if (name.StartsWith("Button"))
  247. {
  248. ++numButtons;
  249. // Check whether the button has key binding
  250. Text* text = dynamic_cast<Text*>(element->GetChild("KeyBinding", false));
  251. if (text)
  252. {
  253. text->SetVisible(false);
  254. const String& key = text->GetText();
  255. int keyBinding;
  256. if (key.Length() == 1)
  257. keyBinding = key[0];
  258. else
  259. {
  260. if (keyBindings.Empty())
  261. {
  262. keyBindings.Insert(MakePair<String, int>("SPACE", KEY_SPACE));
  263. keyBindings.Insert(MakePair<String, int>("LCTRL", KEY_LCTRL));
  264. keyBindings.Insert(MakePair<String, int>("RCTRL", KEY_RCTRL));
  265. keyBindings.Insert(MakePair<String, int>("LSHIFT", KEY_LSHIFT));
  266. keyBindings.Insert(MakePair<String, int>("RSHIFT", KEY_RSHIFT));
  267. keyBindings.Insert(MakePair<String, int>("LALT", KEY_LALT));
  268. keyBindings.Insert(MakePair<String, int>("RALT", KEY_RALT));
  269. keyBindings.Insert(MakePair<String, int>("LGUI", KEY_LGUI));
  270. keyBindings.Insert(MakePair<String, int>("RGUI", KEY_RGUI));
  271. keyBindings.Insert(MakePair<String, int>("TAB", KEY_TAB));
  272. keyBindings.Insert(MakePair<String, int>("RETURN", KEY_RETURN));
  273. keyBindings.Insert(MakePair<String, int>("RETURN2", KEY_RETURN2));
  274. keyBindings.Insert(MakePair<String, int>("ENTER", KEY_KP_ENTER));
  275. keyBindings.Insert(MakePair<String, int>("SELECT", KEY_SELECT));
  276. keyBindings.Insert(MakePair<String, int>("LEFT", KEY_LEFT));
  277. keyBindings.Insert(MakePair<String, int>("RIGHT", KEY_RIGHT));
  278. keyBindings.Insert(MakePair<String, int>("UP", KEY_UP));
  279. keyBindings.Insert(MakePair<String, int>("DOWN", KEY_DOWN));
  280. keyBindings.Insert(MakePair<String, int>("F1", KEY_F1));
  281. keyBindings.Insert(MakePair<String, int>("F2", KEY_F2));
  282. keyBindings.Insert(MakePair<String, int>("F3", KEY_F3));
  283. keyBindings.Insert(MakePair<String, int>("F4", KEY_F4));
  284. keyBindings.Insert(MakePair<String, int>("F5", KEY_F5));
  285. keyBindings.Insert(MakePair<String, int>("F6", KEY_F6));
  286. keyBindings.Insert(MakePair<String, int>("F7", KEY_F7));
  287. keyBindings.Insert(MakePair<String, int>("F8", KEY_F8));
  288. keyBindings.Insert(MakePair<String, int>("F9", KEY_F9));
  289. keyBindings.Insert(MakePair<String, int>("F10", KEY_F10));
  290. keyBindings.Insert(MakePair<String, int>("F11", KEY_F11));
  291. keyBindings.Insert(MakePair<String, int>("F12", KEY_F12));
  292. }
  293. HashMap<String, int>::Iterator i = keyBindings.Find(key);
  294. if (i != keyBindings.End())
  295. keyBinding = i->second_;
  296. else
  297. {
  298. LOGERRORF("Unsupported key binding: %s", key.CString());
  299. keyBinding = M_MAX_INT;
  300. }
  301. }
  302. if (keyBinding != M_MAX_INT)
  303. element->SetVar(VAR_BUTTON_KEY_BINDING, keyBinding);
  304. }
  305. }
  306. else if (name.StartsWith("Axis"))
  307. {
  308. ++numAxes;
  309. ///\todo Axis emulation for screen joystick is not fully supported yet.
  310. LOGWARNING("Axis emulation for screen joystick is not fully supported yet");
  311. }
  312. else if (name.StartsWith("Hat"))
  313. {
  314. ++numHats;
  315. Text* text = dynamic_cast<Text*>(element->GetChild("KeyBinding", false));
  316. if (text)
  317. {
  318. text->SetVisible(false);
  319. String keyBinding = text->GetText();
  320. if (keyBinding.Length() != 4)
  321. {
  322. LOGERRORF("%s has invalid key binding %s, fallback to WASD", name.CString(), keyBinding.CString());
  323. keyBinding = "WASD";
  324. }
  325. element->SetVar(VAR_BUTTON_KEY_BINDING, keyBinding);
  326. }
  327. }
  328. element->SetVar(VAR_SCREEN_JOYSTICK_INDEX, index);
  329. }
  330. // Make sure all the children are non-focusable so they do not mistakenly to be considered as active UI input controls by application
  331. PODVector<UIElement*> allChildren;
  332. state.screenJoystick_->GetChildren(allChildren, true);
  333. for (PODVector<UIElement*>::Iterator iter = allChildren.Begin(); iter != allChildren.End(); ++iter)
  334. (*iter)->SetFocusMode(FM_NOTFOCUSABLE);
  335. state.buttons_.Resize(numButtons);
  336. state.buttonPress_.Resize(numButtons);
  337. state.axes_.Resize(numAxes);
  338. state.hats_.Resize(numHats);
  339. // There could be potentially more than one screen joystick, however they all will be handled by a same handler method
  340. // So there is no harm to replace the old handler with the new handler in each call to SubscribeToEvent()
  341. SubscribeToEvent(E_TOUCHBEGIN, HANDLER(Input, HandleScreenJoystickTouch));
  342. SubscribeToEvent(E_TOUCHMOVE, HANDLER(Input, HandleScreenJoystickTouch));
  343. SubscribeToEvent(E_TOUCHEND, HANDLER(Input, HandleScreenJoystickTouch));
  344. return index;
  345. }
  346. bool Input::RemoveScreenJoystick(unsigned index)
  347. {
  348. if (index >= joysticks_.Size())
  349. {
  350. LOGERRORF("Joystick index #%d is out of bound", index);
  351. return false;
  352. }
  353. JoystickState& state = joysticks_[index];
  354. if (!state.screenJoystick_)
  355. {
  356. LOGERRORF("Failed to remove joystick at index #%d which is not a screen joystick", index);
  357. return false;
  358. }
  359. state.screenJoystick_->Remove();
  360. joysticks_.Erase(index);
  361. return true;
  362. }
  363. void Input::SetScreenKeyboardVisible(bool enable)
  364. {
  365. if (!graphics_)
  366. return;
  367. if (enable != IsScreenKeyboardVisible())
  368. {
  369. if (enable)
  370. SDL_StartTextInput();
  371. else
  372. SDL_StopTextInput();
  373. }
  374. }
  375. bool Input::OpenJoystick(unsigned index)
  376. {
  377. if (index >= joysticks_.Size())
  378. {
  379. LOGERRORF("Joystick index #%d is out of bound", index);
  380. return false;
  381. }
  382. // Check if already opened
  383. JoystickState& state = joysticks_[index];
  384. if (joysticks_[index].joystick_ || (state.screenJoystick_ && state.screenJoystick_->IsVisible()))
  385. return true;
  386. if (state.screenJoystick_)
  387. state.screenJoystick_->SetVisible(true);
  388. else
  389. {
  390. SDL_Joystick* joystick = SDL_JoystickOpen(index);
  391. if (!joystick)
  392. {
  393. LOGERRORF("Cannot open joystick #%d (%s)", index, joysticks_[index].name_.CString());
  394. return false;
  395. }
  396. // Map SDL joystick index to internal index (which starts at 0)
  397. int sdl_joy_instance_id = SDL_JoystickInstanceID(joystick);
  398. joystickIDMap_[sdl_joy_instance_id] = index;
  399. state.joystick_ = joystick;
  400. if (SDL_IsGameController(index))
  401. state.controller_ = SDL_GameControllerOpen(index);
  402. state.buttons_.Resize(SDL_JoystickNumButtons(joystick));
  403. state.buttonPress_.Resize(state.buttons_.Size());
  404. state.axes_.Resize(SDL_JoystickNumAxes(joystick));
  405. state.hats_.Resize(SDL_JoystickNumHats(joystick));
  406. }
  407. for (unsigned i = 0; i < state.buttons_.Size(); ++i)
  408. {
  409. state.buttons_[i] = false;
  410. state.buttonPress_[i] = false;
  411. }
  412. for (unsigned i = 0; i < state.axes_.Size(); ++i)
  413. state.axes_[i] = 0.0f;
  414. for (unsigned i = 0; i < state.hats_.Size(); ++i)
  415. state.hats_[i] = HAT_CENTER;
  416. return true;
  417. }
  418. void Input::CloseJoystick(unsigned index)
  419. {
  420. if (index >= joysticks_.Size())
  421. return;
  422. JoystickState& state = joysticks_[index];
  423. if (joysticks_[index].joystick_)
  424. {
  425. SDL_JoystickClose(state.joystick_);
  426. state.joystick_ = 0;
  427. state.controller_ = 0;
  428. state.buttons_.Clear();
  429. state.axes_.Clear();
  430. state.hats_.Clear();
  431. }
  432. else if (state.screenJoystick_ && state.screenJoystick_->IsVisible())
  433. state.screenJoystick_->SetVisible(false);
  434. }
  435. int Input::GetKeyFromName(const String& name) const
  436. {
  437. return SDL_GetKeyFromName(name.CString());
  438. }
  439. int Input::GetKeyFromScancode(int scancode) const
  440. {
  441. return SDL_GetKeyFromScancode((SDL_Scancode)scancode);
  442. }
  443. String Input::GetKeyName(int key) const
  444. {
  445. return String(SDL_GetKeyName(key));
  446. }
  447. int Input::GetScancodeFromKey(int key) const
  448. {
  449. return SDL_GetScancodeFromKey(key);
  450. }
  451. int Input::GetScancodeFromName(const String& name) const
  452. {
  453. return SDL_GetScancodeFromName(name.CString());
  454. }
  455. String Input::GetScancodeName(int scancode) const
  456. {
  457. return SDL_GetScancodeName((SDL_Scancode)scancode);
  458. }
  459. bool Input::GetKeyDown(int key) const
  460. {
  461. return keyDown_.Contains(SDL_toupper(key));
  462. }
  463. bool Input::GetKeyPress(int key) const
  464. {
  465. return keyPress_.Contains(SDL_toupper(key));
  466. }
  467. bool Input::GetScancodeDown(int scancode) const
  468. {
  469. return scancodeDown_.Contains(scancode);
  470. }
  471. bool Input::GetScancodePress(int scancode) const
  472. {
  473. return scancodePress_.Contains(scancode);
  474. }
  475. bool Input::GetMouseButtonDown(int button) const
  476. {
  477. return (mouseButtonDown_ & button) != 0;
  478. }
  479. bool Input::GetMouseButtonPress(int button) const
  480. {
  481. return (mouseButtonPress_ & button) != 0;
  482. }
  483. bool Input::GetQualifierDown(int qualifier) const
  484. {
  485. if (qualifier == QUAL_SHIFT)
  486. return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
  487. if (qualifier == QUAL_CTRL)
  488. return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
  489. if (qualifier == QUAL_ALT)
  490. return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
  491. return false;
  492. }
  493. bool Input::GetQualifierPress(int qualifier) const
  494. {
  495. if (qualifier == QUAL_SHIFT)
  496. return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
  497. if (qualifier == QUAL_CTRL)
  498. return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
  499. if (qualifier == QUAL_ALT)
  500. return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
  501. return false;
  502. }
  503. int Input::GetQualifiers() const
  504. {
  505. int ret = 0;
  506. if (GetQualifierDown(QUAL_SHIFT))
  507. ret |= QUAL_SHIFT;
  508. if (GetQualifierDown(QUAL_CTRL))
  509. ret |= QUAL_CTRL;
  510. if (GetQualifierDown(QUAL_ALT))
  511. ret |= QUAL_ALT;
  512. return ret;
  513. }
  514. IntVector2 Input::GetMousePosition() const
  515. {
  516. IntVector2 ret = IntVector2::ZERO;
  517. if (!initialized_)
  518. return ret;
  519. SDL_GetMouseState(&ret.x_, &ret.y_);
  520. return ret;
  521. }
  522. TouchState* Input::GetTouch(unsigned index) const
  523. {
  524. if (index >= touches_.Size())
  525. return 0;
  526. HashMap<int, TouchState>::ConstIterator i = touches_.Begin();
  527. while (index--)
  528. ++i;
  529. return const_cast<TouchState*>(&i->second_);
  530. }
  531. const String& Input::GetJoystickName(unsigned index) const
  532. {
  533. return index < joysticks_.Size() ? joysticks_[index].name_ : String::EMPTY;
  534. }
  535. JoystickState* Input::GetJoystick(unsigned index)
  536. {
  537. if (index < joysticks_.Size())
  538. {
  539. JoystickState& state = joysticks_[index];
  540. // If necessary, automatically open the joystick first
  541. if ((!state.joystick_ && !state.screenJoystick_) || (state.screenJoystick_ && !state.screenJoystick_->IsVisible()))
  542. OpenJoystick(index);
  543. return const_cast<JoystickState*>(&state);
  544. }
  545. else
  546. return 0;
  547. }
  548. bool Input::GetScreenKeyboardSupport() const
  549. {
  550. return graphics_ ? SDL_HasScreenKeyboardSupport() : false;
  551. }
  552. bool Input::IsScreenKeyboardVisible() const
  553. {
  554. if (graphics_)
  555. {
  556. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  557. return SDL_IsScreenKeyboardShown(window);
  558. }
  559. else
  560. return false;
  561. }
  562. bool Input::IsMinimized() const
  563. {
  564. // Return minimized state also when unfocused in fullscreen
  565. if (!inputFocus_ && graphics_ && graphics_->GetFullscreen())
  566. return true;
  567. else
  568. return minimized_;
  569. }
  570. void Input::Initialize()
  571. {
  572. Graphics* graphics = GetSubsystem<Graphics>();
  573. if (!graphics || !graphics->IsInitialized())
  574. return;
  575. graphics_ = graphics;
  576. // In external window mode only visible mouse is supported
  577. if (graphics_->GetExternalWindow())
  578. mouseVisible_ = true;
  579. // Set the initial activation
  580. focusedThisFrame_ = true;
  581. initialized_ = true;
  582. ResetJoysticks();
  583. ResetState();
  584. SubscribeToEvent(E_BEGINFRAME, HANDLER(Input, HandleBeginFrame));
  585. LOGINFO("Initialized input");
  586. }
  587. void Input::ResetJoysticks()
  588. {
  589. joysticks_.Clear();
  590. joysticks_.Resize(SDL_NumJoysticks());
  591. for (unsigned i = 0; i < joysticks_.Size(); ++i)
  592. joysticks_[i].name_ = SDL_JoystickNameForIndex(i);
  593. }
  594. void Input::GainFocus()
  595. {
  596. ResetState();
  597. inputFocus_ = true;
  598. focusedThisFrame_ = false;
  599. // Re-establish mouse cursor hiding as necessary
  600. if (!mouseVisible_)
  601. {
  602. SDL_ShowCursor(SDL_FALSE);
  603. suppressNextMouseMove_ = true;
  604. }
  605. else
  606. lastMousePosition_ = GetMousePosition();
  607. SendInputFocusEvent();
  608. }
  609. void Input::LoseFocus()
  610. {
  611. ResetState();
  612. inputFocus_ = false;
  613. focusedThisFrame_ = false;
  614. // Show the mouse cursor when inactive
  615. SDL_ShowCursor(SDL_TRUE);
  616. SendInputFocusEvent();
  617. }
  618. void Input::ResetState()
  619. {
  620. keyDown_.Clear();
  621. keyPress_.Clear();
  622. scancodeDown_.Clear();
  623. scancodePress_.Clear();
  624. /// \todo Check if this is necessary
  625. for (Vector<JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  626. {
  627. for (unsigned j = 0; j < i->buttons_.Size(); ++j)
  628. i->buttons_[j] = false;
  629. for (unsigned j = 0; j < i->hats_.Size(); ++j)
  630. i->hats_[j] = HAT_CENTER;
  631. }
  632. // When clearing touch states, send the corresponding touch end events
  633. for (HashMap<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  634. {
  635. TouchState& state = i->second_;
  636. using namespace TouchEnd;
  637. VariantMap& eventData = GetEventDataMap();
  638. eventData[P_TOUCHID] = state.touchID_;
  639. eventData[P_X] = state.position_.x_;
  640. eventData[P_Y] = state.position_.y_;
  641. SendEvent(E_TOUCHEND, eventData);
  642. }
  643. // Use SetMouseButton() to reset the state so that mouse events will be sent properly
  644. SetMouseButton(MOUSEB_LEFT, false);
  645. SetMouseButton(MOUSEB_RIGHT, false);
  646. SetMouseButton(MOUSEB_MIDDLE, false);
  647. mouseMove_ = IntVector2::ZERO;
  648. mouseMoveWheel_ = 0;
  649. mouseButtonPress_ = 0;
  650. }
  651. void Input::SendInputFocusEvent()
  652. {
  653. using namespace InputFocus;
  654. VariantMap& eventData = GetEventDataMap();
  655. eventData[P_FOCUS] = HasFocus();
  656. eventData[P_MINIMIZED] = IsMinimized();
  657. SendEvent(E_INPUTFOCUS, eventData);
  658. }
  659. void Input::SetMouseButton(int button, bool newState)
  660. {
  661. #ifdef REQUIRE_CLICK_TO_FOCUS
  662. if (!mouseVisible_ && !graphics_->GetFullscreen())
  663. {
  664. if (!inputFocus_ && newState && button == MOUSEB_LEFT)
  665. focusedThisFrame_ = true;
  666. }
  667. #endif
  668. // If we do not have focus yet, do not react to the mouse button down
  669. if (!graphics_->GetExternalWindow() && newState && !inputFocus_)
  670. return;
  671. if (newState)
  672. {
  673. if (!(mouseButtonDown_ & button))
  674. mouseButtonPress_ |= button;
  675. mouseButtonDown_ |= button;
  676. }
  677. else
  678. {
  679. if (!(mouseButtonDown_ & button))
  680. return;
  681. mouseButtonDown_ &= ~button;
  682. }
  683. using namespace MouseButtonDown;
  684. VariantMap& eventData = GetEventDataMap();
  685. eventData[P_BUTTON] = button;
  686. eventData[P_BUTTONS] = mouseButtonDown_;
  687. eventData[P_QUALIFIERS] = GetQualifiers();
  688. SendEvent(newState ? E_MOUSEBUTTONDOWN : E_MOUSEBUTTONUP, eventData);
  689. }
  690. void Input::SetKey(int key, int scancode, unsigned raw, bool newState)
  691. {
  692. // If we do not have focus yet, do not react to the key down
  693. if (!graphics_->GetExternalWindow() && newState && !inputFocus_)
  694. return;
  695. bool repeat = false;
  696. if (newState)
  697. {
  698. scancodeDown_.Insert(scancode);
  699. scancodePress_.Insert(scancode);
  700. if (!keyDown_.Contains(key))
  701. {
  702. keyDown_.Insert(key);
  703. keyPress_.Insert(key);
  704. }
  705. else
  706. repeat = true;
  707. }
  708. else
  709. {
  710. scancodeDown_.Erase(scancode);
  711. if (!keyDown_.Erase(key))
  712. return;
  713. }
  714. using namespace KeyDown;
  715. VariantMap& eventData = GetEventDataMap();
  716. eventData[P_KEY] = key;
  717. eventData[P_SCANCODE] = scancode;
  718. eventData[P_RAW] = raw;
  719. eventData[P_BUTTONS] = mouseButtonDown_;
  720. eventData[P_QUALIFIERS] = GetQualifiers();
  721. if (newState)
  722. eventData[P_REPEAT] = repeat;
  723. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  724. if ((key == KEY_RETURN || key == KEY_RETURN2 || key == KEY_KP_ENTER) && newState && !repeat && toggleFullscreen_ &&
  725. (GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT)))
  726. graphics_->ToggleFullscreen();
  727. }
  728. void Input::SetMouseWheel(int delta)
  729. {
  730. // If we do not have focus yet, do not react to the wheel
  731. if (!graphics_->GetExternalWindow() && !inputFocus_)
  732. return;
  733. if (delta)
  734. {
  735. mouseMoveWheel_ += delta;
  736. using namespace MouseWheel;
  737. VariantMap& eventData = GetEventDataMap();
  738. eventData[P_WHEEL] = delta;
  739. eventData[P_BUTTONS] = mouseButtonDown_;
  740. eventData[P_QUALIFIERS] = GetQualifiers();
  741. SendEvent(E_MOUSEWHEEL, eventData);
  742. }
  743. }
  744. void Input::SetMousePosition(const IntVector2& position)
  745. {
  746. if (!graphics_)
  747. return;
  748. SDL_WarpMouseInWindow(graphics_->GetImpl()->GetWindow(), position.x_, position.y_);
  749. }
  750. void Input::HandleSDLEvent(void* sdlEvent)
  751. {
  752. SDL_Event& evt = *static_cast<SDL_Event*>(sdlEvent);
  753. switch (evt.type)
  754. {
  755. case SDL_KEYDOWN:
  756. // Convert to uppercase to match Win32 virtual key codes
  757. SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), evt.key.keysym.scancode, evt.key.keysym.raw, true);
  758. break;
  759. case SDL_KEYUP:
  760. SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), evt.key.keysym.scancode, evt.key.keysym.raw, false);
  761. break;
  762. case SDL_TEXTINPUT:
  763. {
  764. textInput_ = &evt.text.text[0];
  765. unsigned unicode = textInput_.AtUTF8(0);
  766. if (unicode)
  767. {
  768. using namespace Char;
  769. VariantMap keyEventData;
  770. keyEventData[P_CHAR] = unicode;
  771. keyEventData[P_BUTTONS] = mouseButtonDown_;
  772. keyEventData[P_QUALIFIERS] = GetQualifiers();
  773. SendEvent(E_CHAR, keyEventData);
  774. }
  775. }
  776. break;
  777. case SDL_MOUSEBUTTONDOWN:
  778. SetMouseButton(1 << (evt.button.button - 1), true);
  779. break;
  780. case SDL_MOUSEBUTTONUP:
  781. SetMouseButton(1 << (evt.button.button - 1), false);
  782. break;
  783. case SDL_MOUSEMOTION:
  784. if (mouseVisible_)
  785. {
  786. mouseMove_.x_ += evt.motion.xrel;
  787. mouseMove_.y_ += evt.motion.yrel;
  788. using namespace MouseMove;
  789. VariantMap& eventData = GetEventDataMap();
  790. if (mouseVisible_)
  791. {
  792. eventData[P_X] = evt.motion.x;
  793. eventData[P_Y] = evt.motion.y;
  794. }
  795. eventData[P_DX] = evt.motion.xrel;
  796. eventData[P_DY] = evt.motion.yrel;
  797. eventData[P_BUTTONS] = mouseButtonDown_;
  798. eventData[P_QUALIFIERS] = GetQualifiers();
  799. SendEvent(E_MOUSEMOVE, eventData);
  800. }
  801. break;
  802. case SDL_MOUSEWHEEL:
  803. SetMouseWheel(evt.wheel.y);
  804. break;
  805. case SDL_FINGERDOWN:
  806. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  807. {
  808. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  809. TouchState& state = touches_[touchID];
  810. state.touchID_ = touchID;
  811. state.lastPosition_ = state.position_ = IntVector2((int)(evt.tfinger.x * graphics_->GetWidth()),
  812. (int)(evt.tfinger.y * graphics_->GetHeight()));
  813. state.delta_ = IntVector2::ZERO;
  814. state.pressure_ = evt.tfinger.pressure;
  815. using namespace TouchBegin;
  816. VariantMap& eventData = GetEventDataMap();
  817. eventData[P_TOUCHID] = touchID;
  818. eventData[P_X] = state.position_.x_;
  819. eventData[P_Y] = state.position_.y_;
  820. eventData[P_PRESSURE] = state.pressure_;
  821. SendEvent(E_TOUCHBEGIN, eventData);
  822. }
  823. break;
  824. case SDL_FINGERUP:
  825. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  826. {
  827. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  828. TouchState& state = touches_[touchID];
  829. using namespace TouchEnd;
  830. VariantMap& eventData = GetEventDataMap();
  831. // Do not trust the position in the finger up event. Instead use the last position stored in the
  832. // touch structure
  833. eventData[P_TOUCHID] = touchID;
  834. eventData[P_X] = state.position_.x_;
  835. eventData[P_Y] = state.position_.y_;
  836. SendEvent(E_TOUCHEND, eventData);
  837. touches_.Erase(touchID);
  838. }
  839. break;
  840. case SDL_FINGERMOTION:
  841. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  842. {
  843. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  844. TouchState& state = touches_[touchID];
  845. state.touchID_ = touchID;
  846. state.position_ = IntVector2((int)(evt.tfinger.x * graphics_->GetWidth()),
  847. (int)(evt.tfinger.y * graphics_->GetHeight()));
  848. state.delta_ = state.position_ - state.lastPosition_;
  849. state.pressure_ = evt.tfinger.pressure;
  850. using namespace TouchMove;
  851. VariantMap& eventData = GetEventDataMap();
  852. eventData[P_TOUCHID] = touchID;
  853. eventData[P_X] = state.position_.x_;
  854. eventData[P_Y] = state.position_.y_;
  855. eventData[P_DX] = (int)(evt.tfinger.dx * graphics_->GetWidth());
  856. eventData[P_DY] = (int)(evt.tfinger.dy * graphics_->GetHeight());
  857. eventData[P_PRESSURE] = state.pressure_;
  858. SendEvent(E_TOUCHMOVE, eventData);
  859. }
  860. break;
  861. case SDL_JOYBUTTONDOWN:
  862. {
  863. using namespace JoystickButtonDown;
  864. unsigned button = evt.jbutton.button;
  865. unsigned joystickIndex = joystickIDMap_[evt.jbutton.which];
  866. VariantMap& eventData = GetEventDataMap();
  867. eventData[P_JOYSTICK] = joystickIndex;
  868. eventData[P_BUTTON] = button;
  869. if (joystickIndex < joysticks_.Size() && button < joysticks_[joystickIndex].buttons_.Size()) {
  870. joysticks_[joystickIndex].buttons_[button] = true;
  871. joysticks_[joystickIndex].buttonPress_[button] = true;
  872. SendEvent(E_JOYSTICKBUTTONDOWN, eventData);
  873. }
  874. }
  875. break;
  876. case SDL_JOYBUTTONUP:
  877. {
  878. using namespace JoystickButtonUp;
  879. unsigned button = evt.jbutton.button;
  880. unsigned joystickIndex = joystickIDMap_[evt.jbutton.which];
  881. VariantMap& eventData = GetEventDataMap();
  882. eventData[P_JOYSTICK] = joystickIndex;
  883. eventData[P_BUTTON] = button;
  884. if (joystickIndex < joysticks_.Size() && button < joysticks_[joystickIndex].buttons_.Size()) {
  885. joysticks_[joystickIndex].buttons_[button] = false;
  886. SendEvent(E_JOYSTICKBUTTONUP, eventData);
  887. }
  888. }
  889. break;
  890. case SDL_JOYAXISMOTION:
  891. {
  892. using namespace JoystickAxisMove;
  893. unsigned joystickIndex = joystickIDMap_[evt.jaxis.which];
  894. VariantMap& eventData = GetEventDataMap();
  895. eventData[P_JOYSTICK] = joystickIndex;
  896. eventData[P_AXIS] = evt.jaxis.axis;
  897. eventData[P_POSITION] = Clamp((float)evt.jaxis.value / 32767.0f, -1.0f, 1.0f);
  898. if (joystickIndex < joysticks_.Size() && evt.jaxis.axis <
  899. joysticks_[joystickIndex].axes_.Size())
  900. {
  901. joysticks_[joystickIndex].axes_[evt.jaxis.axis] = eventData[P_POSITION].GetFloat();
  902. SendEvent(E_JOYSTICKAXISMOVE, eventData);
  903. }
  904. }
  905. break;
  906. case SDL_JOYHATMOTION:
  907. {
  908. using namespace JoystickHatMove;
  909. unsigned joystickIndex = joystickIDMap_[evt.jaxis.which];
  910. VariantMap& eventData = GetEventDataMap();
  911. eventData[P_JOYSTICK] = joystickIndex;
  912. eventData[P_HAT] = evt.jhat.hat;
  913. eventData[P_POSITION] = evt.jhat.value;
  914. if (joystickIndex < joysticks_.Size() && evt.jhat.hat <
  915. joysticks_[joystickIndex].hats_.Size())
  916. {
  917. joysticks_[joystickIndex].hats_[evt.jhat.hat] = evt.jhat.value;
  918. SendEvent(E_JOYSTICKHATMOVE, eventData);
  919. }
  920. }
  921. break;
  922. case SDL_CONTROLLERBUTTONDOWN:
  923. {
  924. using namespace ControllerButtonDown;
  925. unsigned button = evt.cbutton.button;
  926. unsigned joystickIndex = joystickIDMap_[evt.cbutton.which];
  927. VariantMap& eventData = GetEventDataMap();
  928. eventData[P_JOYSTICK] = joystickIndex;
  929. eventData[P_BUTTON] = button;
  930. if (joystickIndex < joysticks_.Size() && button < joysticks_[joystickIndex].buttons_.Size()) {
  931. joysticks_[joystickIndex].buttons_[button] = true;
  932. joysticks_[joystickIndex].buttonPress_[button] = true;
  933. SendEvent(E_CONTROLLERBUTTONDOWN, eventData);
  934. }
  935. }
  936. break;
  937. case SDL_CONTROLLERBUTTONUP:
  938. {
  939. using namespace ControllerButtonUp;
  940. unsigned button = evt.cbutton.button;
  941. unsigned joystickIndex = joystickIDMap_[evt.cbutton.which];
  942. VariantMap& eventData = GetEventDataMap();
  943. eventData[P_JOYSTICK] = joystickIndex;
  944. eventData[P_BUTTON] = button;
  945. if (joystickIndex < joysticks_.Size() && button < joysticks_[joystickIndex].buttons_.Size()) {
  946. joysticks_[joystickIndex].buttons_[button] = false;
  947. SendEvent(E_CONTROLLERBUTTONUP, eventData);
  948. }
  949. }
  950. break;
  951. case SDL_CONTROLLERAXISMOTION:
  952. {
  953. using namespace ControllerAxisMove;
  954. unsigned joystickIndex = joystickIDMap_[evt.caxis.which];
  955. VariantMap& eventData = GetEventDataMap();
  956. eventData[P_JOYSTICK] = joystickIndex;
  957. eventData[P_AXIS] = evt.caxis.axis;
  958. eventData[P_POSITION] = Clamp((float)evt.caxis.value / 32767.0f, -1.0f, 1.0f);
  959. if (joystickIndex < joysticks_.Size() && evt.caxis.axis <
  960. joysticks_[joystickIndex].axes_.Size())
  961. {
  962. joysticks_[joystickIndex].axes_[evt.caxis.axis] = eventData[P_POSITION].GetFloat();
  963. SendEvent(E_CONTROLLERAXISMOVE, eventData);
  964. }
  965. }
  966. break;
  967. case SDL_WINDOWEVENT:
  968. {
  969. switch (evt.window.event)
  970. {
  971. case SDL_WINDOWEVENT_MINIMIZED:
  972. minimized_ = true;
  973. SendInputFocusEvent();
  974. break;
  975. case SDL_WINDOWEVENT_MAXIMIZED:
  976. case SDL_WINDOWEVENT_RESTORED:
  977. minimized_ = false;
  978. SendInputFocusEvent();
  979. #ifdef IOS
  980. // On iOS we never lose the GL context, but may have done GPU object changes that could not be applied yet.
  981. // Apply them now
  982. graphics_->Restore();
  983. #endif
  984. break;
  985. #ifdef ANDROID
  986. case SDL_WINDOWEVENT_FOCUS_GAINED:
  987. // Restore GPU objects to the new GL context
  988. graphics_->Restore();
  989. break;
  990. #endif
  991. case SDL_WINDOWEVENT_RESIZED:
  992. graphics_->WindowResized();
  993. break;
  994. }
  995. }
  996. break;
  997. case SDL_DROPFILE:
  998. {
  999. using namespace DropFile;
  1000. VariantMap& eventData = GetEventDataMap();
  1001. eventData[P_FILENAME] = GetInternalPath(String(evt.drop.file));
  1002. SDL_free(evt.drop.file);
  1003. SendEvent(E_DROPFILE, eventData);
  1004. }
  1005. break;
  1006. case SDL_QUIT:
  1007. SendEvent(E_EXITREQUESTED);
  1008. break;
  1009. }
  1010. }
  1011. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  1012. {
  1013. // Reset input state on subsequent initializations
  1014. if (!initialized_)
  1015. Initialize();
  1016. else
  1017. ResetState();
  1018. // Re-enable cursor clipping, and re-center the cursor (if needed) to the new screen size, so that there is no erroneous
  1019. // mouse move event. Also get new window ID if it changed
  1020. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  1021. windowID_ = SDL_GetWindowID(window);
  1022. if (!mouseVisible_)
  1023. {
  1024. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  1025. SetMousePosition(center);
  1026. lastMousePosition_ = center;
  1027. }
  1028. focusedThisFrame_ = true;
  1029. // After setting a new screen mode we should not be minimized
  1030. minimized_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0;
  1031. }
  1032. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  1033. {
  1034. // Update input right at the beginning of the frame
  1035. Update();
  1036. }
  1037. void Input::HandleScreenJoystickTouch(StringHash eventType, VariantMap& eventData)
  1038. {
  1039. using namespace TouchBegin;
  1040. // Only interested in events from screen joystick(s)
  1041. TouchState& state = touches_[eventData[P_TOUCHID].GetInt()];
  1042. IntVector2 position(state.position_.x_, state.position_.y_);
  1043. UIElement* element = eventType == E_TOUCHBEGIN ? GetSubsystem<UI>()->GetElementAt(position) : state.touchedElement_;
  1044. if (!element)
  1045. return;
  1046. Variant variant = element->GetVar(VAR_SCREEN_JOYSTICK_INDEX);
  1047. if (variant.IsEmpty())
  1048. return;
  1049. unsigned index = variant.GetUInt();
  1050. if (eventType == E_TOUCHEND)
  1051. state.touchedElement_.Reset();
  1052. else
  1053. state.touchedElement_ = element;
  1054. // Prepare a fake SDL event
  1055. SDL_Event evt;
  1056. const String& name = element->GetName();
  1057. if (name.StartsWith("Button"))
  1058. {
  1059. if (eventType == E_TOUCHMOVE)
  1060. return;
  1061. // Determine whether to inject a joystick event or keyboard event
  1062. Variant variant = element->GetVar(VAR_BUTTON_KEY_BINDING);
  1063. if (variant.IsEmpty())
  1064. {
  1065. evt.type = eventType == E_TOUCHBEGIN ? SDL_JOYBUTTONDOWN : SDL_JOYBUTTONUP;
  1066. evt.jbutton.which = SCREEN_JOYSTICK_START_INDEX + index;
  1067. evt.jbutton.button = ToUInt(name.Substring(6));
  1068. }
  1069. else
  1070. {
  1071. evt.type = eventType == E_TOUCHBEGIN ? SDL_KEYDOWN : SDL_KEYUP;
  1072. evt.key.keysym.sym = variant.GetInt();
  1073. evt.key.keysym.scancode = SDL_SCANCODE_UNKNOWN;
  1074. }
  1075. }
  1076. else if (name.StartsWith("Hat"))
  1077. {
  1078. Variant variant = element->GetVar(VAR_BUTTON_KEY_BINDING);
  1079. if (variant.IsEmpty())
  1080. {
  1081. evt.type = SDL_JOYHATMOTION;
  1082. evt.jaxis.which = SCREEN_JOYSTICK_START_INDEX + index;
  1083. evt.jhat.hat = ToUInt(name.Substring(3));
  1084. evt.jhat.value = HAT_CENTER;
  1085. if (eventType != E_TOUCHEND)
  1086. {
  1087. IntVector2 relPosition = position - element->GetScreenPosition() - element->GetSize() / 2;
  1088. if (relPosition.y_ < 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1089. evt.jhat.value |= HAT_UP;
  1090. if (relPosition.y_ > 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1091. evt.jhat.value |= HAT_DOWN;
  1092. if (relPosition.x_ < 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1093. evt.jhat.value |= HAT_LEFT;
  1094. if (relPosition.x_ > 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1095. evt.jhat.value |= HAT_RIGHT;
  1096. }
  1097. }
  1098. else
  1099. {
  1100. // Hat is binded by 4 keys, like 'WASD'
  1101. String keyBinding = variant.GetString();
  1102. if (eventType == E_TOUCHEND)
  1103. {
  1104. evt.type = SDL_KEYUP;
  1105. evt.key.keysym.sym = element->GetVar(VAR_LAST_KEYSYM).GetInt();
  1106. if (!evt.key.keysym.sym)
  1107. return;
  1108. element->SetVar(VAR_LAST_KEYSYM, 0);
  1109. }
  1110. else
  1111. {
  1112. evt.type = SDL_KEYDOWN;
  1113. IntVector2 relPosition = position - element->GetScreenPosition() - element->GetSize() / 2;
  1114. if (relPosition.y_ < 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1115. evt.key.keysym.sym = keyBinding[0];
  1116. else if (relPosition.y_ > 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1117. evt.key.keysym.sym = keyBinding[2];
  1118. else if (relPosition.x_ < 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1119. evt.key.keysym.sym = keyBinding[1];
  1120. else if (relPosition.x_ > 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1121. evt.key.keysym.sym = keyBinding[3];
  1122. else
  1123. return;
  1124. if (eventType == E_TOUCHMOVE && evt.key.keysym.sym != element->GetVar(VAR_LAST_KEYSYM).GetInt())
  1125. {
  1126. // Dragging past the directional boundary will cause an additional key up event for previous key symbol
  1127. SDL_Event evt;
  1128. evt.type = SDL_KEYUP;
  1129. evt.key.keysym.sym = element->GetVar(VAR_LAST_KEYSYM).GetInt();
  1130. if (evt.key.keysym.sym)
  1131. {
  1132. evt.key.keysym.scancode = SDL_SCANCODE_UNKNOWN;
  1133. HandleSDLEvent(&evt);
  1134. }
  1135. element->SetVar(VAR_LAST_KEYSYM, 0);
  1136. }
  1137. evt.key.keysym.scancode = SDL_SCANCODE_UNKNOWN;
  1138. element->SetVar(VAR_LAST_KEYSYM, evt.key.keysym.sym);
  1139. }
  1140. }
  1141. }
  1142. else
  1143. return;
  1144. // Handle the fake SDL event to turn it into Urho3D genuine event
  1145. HandleSDLEvent(&evt);
  1146. }
  1147. }