Input.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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 "StringUtils.h"
  35. #include <cstring>
  36. #include <SDL.h>
  37. #include "DebugNew.h"
  38. // Require a click inside window before re-hiding mouse cursor on OSX, otherwise dragging the window
  39. // can be incorrectly interpreted as mouse movement inside the window
  40. #if defined(__APPLE__) && !defined(IOS)
  41. #define REQUIRE_CLICK_TO_FOCUS
  42. #endif
  43. namespace Urho3D
  44. {
  45. /// Convert SDL keycode if necessary
  46. int ConvertSDLKeyCode(int keySym, int scanCode)
  47. {
  48. if (scanCode == SDL_SCANCODE_AC_BACK)
  49. return KEY_ESC;
  50. else
  51. return SDL_toupper(keySym);
  52. }
  53. Input::Input(Context* context) :
  54. Object(context),
  55. mouseButtonDown_(0),
  56. mouseButtonPress_(0),
  57. mouseMoveWheel_(0),
  58. windowID_(0),
  59. toggleFullscreen_(true),
  60. mouseVisible_(false),
  61. inputFocus_(false),
  62. minimized_(false),
  63. focusedThisFrame_(false),
  64. suppressNextMouseMove_(false),
  65. initialized_(false)
  66. {
  67. SubscribeToEvent(E_SCREENMODE, HANDLER(Input, HandleScreenMode));
  68. // Try to initialize right now, but skip if screen mode is not yet set
  69. Initialize();
  70. }
  71. Input::~Input()
  72. {
  73. }
  74. void Input::Update()
  75. {
  76. assert(initialized_);
  77. PROFILE(UpdateInput);
  78. // Reset input accumulation for this frame
  79. keyPress_.Clear();
  80. mouseButtonPress_ = 0;
  81. mouseMove_ = IntVector2::ZERO;
  82. mouseMoveWheel_ = 0;
  83. for (Vector<JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  84. {
  85. for (unsigned j = 0; j < i->buttonPress_.Size(); ++j)
  86. i->buttonPress_[j] = false;
  87. }
  88. // Reset touch delta movement
  89. for (HashMap<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  90. {
  91. TouchState& state = i->second_;
  92. state.lastPosition_ = state.position_;
  93. state.delta_ = IntVector2::ZERO;
  94. }
  95. // Check and handle SDL events
  96. SDL_PumpEvents();
  97. SDL_Event evt;
  98. while (SDL_PeepEvents(&evt, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0)
  99. HandleSDLEvent(&evt);
  100. // Check for activation and inactivation from SDL window flags. Must nullcheck the window pointer because it may have
  101. // been closed due to input events
  102. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  103. unsigned flags = window ? SDL_GetWindowFlags(window) & (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS) : 0;
  104. if (window)
  105. {
  106. #ifdef REQUIRE_CLICK_TO_FOCUS
  107. if (!inputFocus_ && (graphics_->GetFullscreen() || mouseVisible_) && flags == (SDL_WINDOW_INPUT_FOCUS |
  108. SDL_WINDOW_MOUSE_FOCUS))
  109. #else
  110. if (!inputFocus_ && (flags & SDL_WINDOW_INPUT_FOCUS))
  111. #endif
  112. focusedThisFrame_ = true;
  113. if (focusedThisFrame_)
  114. GainFocus();
  115. if (inputFocus_ && (flags & SDL_WINDOW_INPUT_FOCUS) == 0)
  116. LoseFocus();
  117. }
  118. else
  119. return;
  120. // Check for relative mode mouse move
  121. if (graphics_->GetExternalWindow() || (!mouseVisible_ && inputFocus_ && (flags & SDL_WINDOW_MOUSE_FOCUS)))
  122. {
  123. IntVector2 mousePosition = GetMousePosition();
  124. mouseMove_ = mousePosition - lastMousePosition_;
  125. if (graphics_->GetExternalWindow())
  126. lastMousePosition_ = mousePosition;
  127. else
  128. {
  129. // Recenter the mouse cursor manually
  130. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  131. if (mousePosition != center)
  132. {
  133. SetMousePosition(center);
  134. lastMousePosition_ = center;
  135. }
  136. }
  137. // Send mouse move event if necessary
  138. if (mouseMove_ != IntVector2::ZERO)
  139. {
  140. if (suppressNextMouseMove_)
  141. {
  142. mouseMove_ = IntVector2::ZERO;
  143. suppressNextMouseMove_ = false;
  144. }
  145. else
  146. {
  147. using namespace MouseMove;
  148. VariantMap& eventData = GetEventDataMap();
  149. if (mouseVisible_)
  150. {
  151. eventData[P_X] = mousePosition.x_;
  152. eventData[P_Y] = mousePosition.y_;
  153. }
  154. eventData[P_DX] = mouseMove_.x_;
  155. eventData[P_DY] = mouseMove_.y_;
  156. eventData[P_BUTTONS] = mouseButtonDown_;
  157. eventData[P_QUALIFIERS] = GetQualifiers();
  158. SendEvent(E_MOUSEMOVE, eventData);
  159. }
  160. }
  161. }
  162. }
  163. void Input::SetMouseVisible(bool enable)
  164. {
  165. // SDL Raspberry Pi "video driver" does not have proper OS mouse support yet, so no-op for now
  166. #ifndef RASPI
  167. if (enable != mouseVisible_)
  168. {
  169. mouseVisible_ = enable;
  170. if (initialized_)
  171. {
  172. // External windows can only support visible mouse cursor
  173. if (graphics_->GetExternalWindow())
  174. {
  175. mouseVisible_ = true;
  176. return;
  177. }
  178. if (!mouseVisible_ && inputFocus_)
  179. SDL_ShowCursor(SDL_FALSE);
  180. else
  181. SDL_ShowCursor(SDL_TRUE);
  182. }
  183. using namespace MouseVisibleChanged;
  184. VariantMap& eventData = GetEventDataMap();
  185. eventData[P_VISIBLE] = mouseVisible_;
  186. SendEvent(E_MOUSEVISIBLECHANGED, eventData);
  187. }
  188. #endif
  189. }
  190. void Input::SetToggleFullscreen(bool enable)
  191. {
  192. toggleFullscreen_ = enable;
  193. }
  194. bool Input::DetectJoysticks()
  195. {
  196. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  197. SDL_InitSubSystem(SDL_INIT_JOYSTICK);
  198. ResetJoysticks();
  199. return true;
  200. }
  201. void Input::SetScreenKeyboardVisible(bool enable)
  202. {
  203. if (!graphics_)
  204. return;
  205. if (enable != IsScreenKeyboardVisible())
  206. {
  207. if (enable)
  208. SDL_StartTextInput();
  209. else
  210. SDL_StopTextInput();
  211. }
  212. }
  213. bool Input::OpenJoystick(unsigned index)
  214. {
  215. if (index >= joysticks_.Size())
  216. return false;
  217. // Check if already opened
  218. if (joysticks_[index].joystick_)
  219. return true;
  220. SDL_Joystick* joystick = SDL_JoystickOpen(index);
  221. if (joystick)
  222. {
  223. // Map SDL joystick index to internal index (which starts at 0)
  224. int sdl_joy_instance_id = SDL_JoystickInstanceID(joystick);
  225. joystickIDMap_[sdl_joy_instance_id] = index;
  226. JoystickState& state = joysticks_[index];
  227. state.joystick_ = joystick;
  228. if (SDL_IsGameController(index))
  229. state.controller_ = SDL_GameControllerOpen(index);
  230. state.buttons_.Resize(SDL_JoystickNumButtons(joystick));
  231. state.buttonPress_.Resize(state.buttons_.Size());
  232. state.axes_.Resize(SDL_JoystickNumAxes(joystick));
  233. state.hats_.Resize(SDL_JoystickNumHats(joystick));
  234. for (unsigned i = 0; i < state.buttons_.Size(); ++i)
  235. {
  236. state.buttons_[i] = false;
  237. state.buttonPress_[i] = false;
  238. }
  239. for (unsigned i = 0; i < state.axes_.Size(); ++i)
  240. state.axes_[i] = 0.0f;
  241. for (unsigned i = 0; i < state.hats_.Size(); ++i)
  242. state.hats_[i] = HAT_CENTER;
  243. return true;
  244. }
  245. else
  246. return false;
  247. }
  248. void Input::CloseJoystick(unsigned index)
  249. {
  250. if (index < joysticks_.Size() && joysticks_[index].joystick_)
  251. {
  252. JoystickState& state = joysticks_[index];
  253. SDL_JoystickClose(state.joystick_);
  254. state.joystick_ = 0;
  255. state.buttons_.Clear();
  256. state.axes_.Clear();
  257. state.hats_.Clear();
  258. }
  259. }
  260. bool Input::GetKeyDown(int key) const
  261. {
  262. return keyDown_.Contains(key);
  263. }
  264. bool Input::GetKeyPress(int key) const
  265. {
  266. return keyPress_.Contains(key);
  267. }
  268. bool Input::GetMouseButtonDown(int button) const
  269. {
  270. return (mouseButtonDown_ & button) != 0;
  271. }
  272. bool Input::GetMouseButtonPress(int button) const
  273. {
  274. return (mouseButtonPress_ & button) != 0;
  275. }
  276. bool Input::GetQualifierDown(int qualifier) const
  277. {
  278. if (qualifier == QUAL_SHIFT)
  279. return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
  280. if (qualifier == QUAL_CTRL)
  281. return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
  282. if (qualifier == QUAL_ALT)
  283. return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
  284. return false;
  285. }
  286. bool Input::GetQualifierPress(int qualifier) const
  287. {
  288. if (qualifier == QUAL_SHIFT)
  289. return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
  290. if (qualifier == QUAL_CTRL)
  291. return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
  292. if (qualifier == QUAL_ALT)
  293. return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
  294. return false;
  295. }
  296. int Input::GetQualifiers() const
  297. {
  298. int ret = 0;
  299. if (GetQualifierDown(QUAL_SHIFT))
  300. ret |= QUAL_SHIFT;
  301. if (GetQualifierDown(QUAL_CTRL))
  302. ret |= QUAL_CTRL;
  303. if (GetQualifierDown(QUAL_ALT))
  304. ret |= QUAL_ALT;
  305. return ret;
  306. }
  307. IntVector2 Input::GetMousePosition() const
  308. {
  309. IntVector2 ret = IntVector2::ZERO;
  310. if (!initialized_)
  311. return ret;
  312. SDL_GetMouseState(&ret.x_, &ret.y_);
  313. return ret;
  314. }
  315. TouchState* Input::GetTouch(unsigned index) const
  316. {
  317. if (index >= touches_.Size())
  318. return 0;
  319. HashMap<int, TouchState>::ConstIterator i = touches_.Begin();
  320. while (index--)
  321. ++i;
  322. return const_cast<TouchState*>(&i->second_);
  323. }
  324. const String& Input::GetJoystickName(unsigned index) const
  325. {
  326. if (index < joysticks_.Size())
  327. return joysticks_[index].name_;
  328. else
  329. return String::EMPTY;
  330. }
  331. JoystickState* Input::GetJoystick(unsigned index)
  332. {
  333. if (index < joysticks_.Size())
  334. {
  335. // If necessary, automatically open the joystick first
  336. if (!joysticks_[index].joystick_)
  337. OpenJoystick(index);
  338. return const_cast<JoystickState*>(&joysticks_[index]);
  339. }
  340. else
  341. return 0;
  342. }
  343. bool Input::GetScreenKeyboardSupport() const
  344. {
  345. return graphics_ ? SDL_HasScreenKeyboardSupport() : false;
  346. }
  347. bool Input::IsScreenKeyboardVisible() const
  348. {
  349. if (graphics_)
  350. {
  351. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  352. return SDL_IsScreenKeyboardShown(window);
  353. }
  354. else
  355. return false;
  356. }
  357. bool Input::IsMinimized() const
  358. {
  359. // Return minimized state also when unfocused in fullscreen
  360. if (!inputFocus_ && graphics_ && graphics_->GetFullscreen())
  361. return true;
  362. else
  363. return minimized_;
  364. }
  365. void Input::Initialize()
  366. {
  367. Graphics* graphics = GetSubsystem<Graphics>();
  368. if (!graphics || !graphics->IsInitialized())
  369. return;
  370. graphics_ = graphics;
  371. // In external window mode only visible mouse is supported
  372. if (graphics_->GetExternalWindow())
  373. mouseVisible_ = true;
  374. // Set the initial activation
  375. focusedThisFrame_ = true;
  376. initialized_ = true;
  377. ResetJoysticks();
  378. ResetState();
  379. SubscribeToEvent(E_BEGINFRAME, HANDLER(Input, HandleBeginFrame));
  380. LOGINFO("Initialized input");
  381. }
  382. void Input::ResetJoysticks()
  383. {
  384. joysticks_.Clear();
  385. joysticks_.Resize(SDL_NumJoysticks());
  386. for (unsigned i = 0; i < joysticks_.Size(); ++i)
  387. joysticks_[i].name_ = SDL_JoystickNameForIndex(i);
  388. }
  389. void Input::GainFocus()
  390. {
  391. ResetState();
  392. inputFocus_ = true;
  393. focusedThisFrame_ = false;
  394. // Re-establish mouse cursor hiding as necessary
  395. if (!mouseVisible_)
  396. {
  397. SDL_ShowCursor(SDL_FALSE);
  398. suppressNextMouseMove_ = true;
  399. }
  400. else
  401. lastMousePosition_ = GetMousePosition();
  402. SendInputFocusEvent();
  403. }
  404. void Input::LoseFocus()
  405. {
  406. ResetState();
  407. inputFocus_ = false;
  408. focusedThisFrame_ = false;
  409. // Show the mouse cursor when inactive
  410. SDL_ShowCursor(SDL_TRUE);
  411. SendInputFocusEvent();
  412. }
  413. void Input::ResetState()
  414. {
  415. keyDown_.Clear();
  416. keyPress_.Clear();
  417. /// \todo Check if this is necessary
  418. for (Vector<JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  419. {
  420. for (unsigned j = 0; j < i->buttons_.Size(); ++j)
  421. i->buttons_[j] = false;
  422. for (unsigned j = 0; j < i->hats_.Size(); ++j)
  423. i->hats_[j] = HAT_CENTER;
  424. }
  425. // When clearing touch states, send the corresponding touch end events
  426. for (HashMap<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  427. {
  428. TouchState& state = i->second_;
  429. using namespace TouchEnd;
  430. VariantMap& eventData = GetEventDataMap();
  431. eventData[P_TOUCHID] = state.touchID_;
  432. eventData[P_X] = state.position_.x_;
  433. eventData[P_Y] = state.position_.y_;
  434. SendEvent(E_TOUCHEND, eventData);
  435. }
  436. // Use SetMouseButton() to reset the state so that mouse events will be sent properly
  437. SetMouseButton(MOUSEB_LEFT, false);
  438. SetMouseButton(MOUSEB_RIGHT, false);
  439. SetMouseButton(MOUSEB_MIDDLE, false);
  440. mouseMove_ = IntVector2::ZERO;
  441. mouseMoveWheel_ = 0;
  442. mouseButtonPress_ = 0;
  443. }
  444. void Input::SendInputFocusEvent()
  445. {
  446. using namespace InputFocus;
  447. VariantMap& eventData = GetEventDataMap();
  448. eventData[P_FOCUS] = HasFocus();
  449. eventData[P_MINIMIZED] = IsMinimized();
  450. SendEvent(E_INPUTFOCUS, eventData);
  451. }
  452. void Input::SetMouseButton(int button, bool newState)
  453. {
  454. #ifdef REQUIRE_CLICK_TO_FOCUS
  455. if (!mouseVisible_ && !graphics_->GetFullscreen())
  456. {
  457. if (!inputFocus_ && newState && button == MOUSEB_LEFT)
  458. focusedThisFrame_ = true;
  459. }
  460. #endif
  461. // If we do not have focus yet, do not react to the mouse button down
  462. if (!graphics_->GetExternalWindow() && newState && !inputFocus_)
  463. return;
  464. if (newState)
  465. {
  466. if (!(mouseButtonDown_ & button))
  467. mouseButtonPress_ |= button;
  468. mouseButtonDown_ |= button;
  469. }
  470. else
  471. {
  472. if (!(mouseButtonDown_ & button))
  473. return;
  474. mouseButtonDown_ &= ~button;
  475. }
  476. using namespace MouseButtonDown;
  477. VariantMap& eventData = GetEventDataMap();
  478. eventData[P_BUTTON] = button;
  479. eventData[P_BUTTONS] = mouseButtonDown_;
  480. eventData[P_QUALIFIERS] = GetQualifiers();
  481. SendEvent(newState ? E_MOUSEBUTTONDOWN : E_MOUSEBUTTONUP, eventData);
  482. }
  483. void Input::SetKey(int key, bool newState)
  484. {
  485. // If we do not have focus yet, do not react to the key down
  486. if (!graphics_->GetExternalWindow() && newState && !inputFocus_)
  487. return;
  488. bool repeat = false;
  489. if (newState)
  490. {
  491. if (!keyDown_.Contains(key))
  492. {
  493. keyDown_.Insert(key);
  494. keyPress_.Insert(key);
  495. }
  496. else
  497. repeat = true;
  498. }
  499. else
  500. {
  501. if (!keyDown_.Erase(key))
  502. return;
  503. }
  504. using namespace KeyDown;
  505. VariantMap& eventData = GetEventDataMap();
  506. eventData[P_KEY] = key;
  507. eventData[P_BUTTONS] = mouseButtonDown_;
  508. eventData[P_QUALIFIERS] = GetQualifiers();
  509. if (newState)
  510. eventData[P_REPEAT] = repeat;
  511. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  512. if ((key == KEY_RETURN || key == KEY_RETURN2 || key == KEY_KP_ENTER) && newState && !repeat && toggleFullscreen_ && (GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT)))
  513. graphics_->ToggleFullscreen();
  514. }
  515. void Input::SetMouseWheel(int delta)
  516. {
  517. // If we do not have focus yet, do not react to the wheel
  518. if (!graphics_->GetExternalWindow() && !inputFocus_)
  519. return;
  520. if (delta)
  521. {
  522. mouseMoveWheel_ += delta;
  523. using namespace MouseWheel;
  524. VariantMap& eventData = GetEventDataMap();
  525. eventData[P_WHEEL] = delta;
  526. eventData[P_BUTTONS] = mouseButtonDown_;
  527. eventData[P_QUALIFIERS] = GetQualifiers();
  528. SendEvent(E_MOUSEWHEEL, eventData);
  529. }
  530. }
  531. void Input::SetMousePosition(const IntVector2& position)
  532. {
  533. if (!graphics_)
  534. return;
  535. SDL_WarpMouseInWindow(graphics_->GetImpl()->GetWindow(), position.x_, position.y_);
  536. }
  537. void Input::HandleSDLEvent(void* sdlEvent)
  538. {
  539. SDL_Event& evt = *static_cast<SDL_Event*>(sdlEvent);
  540. switch (evt.type)
  541. {
  542. case SDL_KEYDOWN:
  543. // Convert to uppercase to match Win32 virtual key codes
  544. SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), true);
  545. break;
  546. case SDL_KEYUP:
  547. SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), false);
  548. break;
  549. case SDL_TEXTINPUT:
  550. {
  551. textInput_ = &evt.text.text[0];
  552. unsigned unicode = textInput_.AtUTF8(0);
  553. if (unicode)
  554. {
  555. using namespace Char;
  556. VariantMap keyEventData;
  557. keyEventData[P_CHAR] = unicode;
  558. keyEventData[P_BUTTONS] = mouseButtonDown_;
  559. keyEventData[P_QUALIFIERS] = GetQualifiers();
  560. SendEvent(E_CHAR, keyEventData);
  561. }
  562. }
  563. break;
  564. case SDL_MOUSEBUTTONDOWN:
  565. SetMouseButton(1 << (evt.button.button - 1), true);
  566. break;
  567. case SDL_MOUSEBUTTONUP:
  568. SetMouseButton(1 << (evt.button.button - 1), false);
  569. break;
  570. case SDL_MOUSEMOTION:
  571. if (mouseVisible_)
  572. {
  573. mouseMove_.x_ += evt.motion.xrel;
  574. mouseMove_.y_ += evt.motion.yrel;
  575. using namespace MouseMove;
  576. VariantMap& eventData = GetEventDataMap();
  577. if (mouseVisible_)
  578. {
  579. eventData[P_X] = evt.motion.x;
  580. eventData[P_Y] = evt.motion.y;
  581. }
  582. eventData[P_DX] = evt.motion.xrel;
  583. eventData[P_DY] = evt.motion.yrel;
  584. eventData[P_BUTTONS] = mouseButtonDown_;
  585. eventData[P_QUALIFIERS] = GetQualifiers();
  586. SendEvent(E_MOUSEMOVE, eventData);
  587. }
  588. break;
  589. case SDL_MOUSEWHEEL:
  590. SetMouseWheel(evt.wheel.y);
  591. break;
  592. case SDL_FINGERDOWN:
  593. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  594. {
  595. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  596. TouchState& state = touches_[touchID];
  597. state.touchID_ = touchID;
  598. state.lastPosition_ = state.position_ = IntVector2((int)(evt.tfinger.x * graphics_->GetWidth()),
  599. (int)(evt.tfinger.y * graphics_->GetHeight()));
  600. state.delta_ = IntVector2::ZERO;
  601. state.pressure_ = evt.tfinger.pressure;
  602. using namespace TouchBegin;
  603. VariantMap& eventData = GetEventDataMap();
  604. eventData[P_TOUCHID] = touchID;
  605. eventData[P_X] = state.position_.x_;
  606. eventData[P_Y] = state.position_.y_;
  607. eventData[P_PRESSURE] = state.pressure_;
  608. SendEvent(E_TOUCHBEGIN, eventData);
  609. }
  610. break;
  611. case SDL_FINGERUP:
  612. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  613. {
  614. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  615. TouchState& state = touches_[touchID];
  616. using namespace TouchEnd;
  617. VariantMap& eventData = GetEventDataMap();
  618. // Do not trust the position in the finger up event. Instead use the last position stored in the
  619. // touch structure
  620. eventData[P_TOUCHID] = touchID;
  621. eventData[P_X] = state.position_.x_;
  622. eventData[P_Y] = state.position_.y_;
  623. touches_.Erase(touchID);
  624. SendEvent(E_TOUCHEND, eventData);
  625. }
  626. break;
  627. case SDL_FINGERMOTION:
  628. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  629. {
  630. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  631. TouchState& state = touches_[touchID];
  632. state.touchID_ = touchID;
  633. state.position_ = IntVector2((int)(evt.tfinger.x * graphics_->GetWidth()),
  634. (int)(evt.tfinger.y * graphics_->GetHeight()));
  635. state.delta_ = state.position_ - state.lastPosition_;
  636. state.pressure_ = evt.tfinger.pressure;
  637. using namespace TouchMove;
  638. VariantMap& eventData = GetEventDataMap();
  639. eventData[P_TOUCHID] = touchID;
  640. eventData[P_X] = state.position_.x_;
  641. eventData[P_Y] = state.position_.y_;
  642. eventData[P_DX] = (int)(evt.tfinger.dx * graphics_->GetWidth());
  643. eventData[P_DY] = (int)(evt.tfinger.dy * graphics_->GetHeight());
  644. eventData[P_PRESSURE] = state.pressure_;
  645. SendEvent(E_TOUCHMOVE, eventData);
  646. }
  647. break;
  648. case SDL_JOYBUTTONDOWN:
  649. {
  650. using namespace JoystickButtonDown;
  651. unsigned button = evt.jbutton.button;
  652. unsigned joystickIndex = joystickIDMap_[evt.jbutton.which];
  653. VariantMap& eventData = GetEventDataMap();
  654. eventData[P_JOYSTICK] = joystickIndex;
  655. eventData[P_BUTTON] = button;
  656. if (joystickIndex < joysticks_.Size() && button < joysticks_[joystickIndex].buttons_.Size()) {
  657. joysticks_[joystickIndex].buttons_[button] = true;
  658. joysticks_[joystickIndex].buttonPress_[button] = true;
  659. SendEvent(E_JOYSTICKBUTTONDOWN, eventData);
  660. }
  661. }
  662. break;
  663. case SDL_JOYBUTTONUP:
  664. {
  665. using namespace JoystickButtonUp;
  666. unsigned button = evt.jbutton.button;
  667. unsigned joystickIndex = joystickIDMap_[evt.jbutton.which];
  668. VariantMap& eventData = GetEventDataMap();
  669. eventData[P_JOYSTICK] = joystickIndex;
  670. eventData[P_BUTTON] = button;
  671. if (joystickIndex < joysticks_.Size() && button < joysticks_[joystickIndex].buttons_.Size()) {
  672. joysticks_[joystickIndex].buttons_[button] = false;
  673. SendEvent(E_JOYSTICKBUTTONUP, eventData);
  674. }
  675. }
  676. break;
  677. case SDL_JOYAXISMOTION:
  678. {
  679. using namespace JoystickAxisMove;
  680. unsigned joystickIndex = joystickIDMap_[evt.jaxis.which];
  681. VariantMap& eventData = GetEventDataMap();
  682. eventData[P_JOYSTICK] = joystickIndex;
  683. eventData[P_AXIS] = evt.jaxis.axis;
  684. eventData[P_POSITION] = Clamp((float)evt.jaxis.value / 32767.0f, -1.0f, 1.0f);
  685. if (joystickIndex < joysticks_.Size() && evt.jaxis.axis <
  686. joysticks_[joystickIndex].axes_.Size())
  687. {
  688. joysticks_[joystickIndex].axes_[evt.jaxis.axis] = eventData[P_POSITION].GetFloat();
  689. SendEvent(E_JOYSTICKAXISMOVE, eventData);
  690. }
  691. }
  692. break;
  693. case SDL_JOYHATMOTION:
  694. {
  695. using namespace JoystickHatMove;
  696. unsigned joystickIndex = joystickIDMap_[evt.jaxis.which];
  697. VariantMap& eventData = GetEventDataMap();
  698. eventData[P_JOYSTICK] = joystickIndex;
  699. eventData[P_HAT] = evt.jhat.hat;
  700. eventData[P_POSITION] = evt.jhat.value;
  701. if (joystickIndex < joysticks_.Size() && evt.jhat.hat <
  702. joysticks_[joystickIndex].hats_.Size())
  703. {
  704. joysticks_[joystickIndex].hats_[evt.jhat.hat] = evt.jhat.value;
  705. SendEvent(E_JOYSTICKHATMOVE, eventData);
  706. }
  707. }
  708. break;
  709. case SDL_CONTROLLERBUTTONDOWN:
  710. {
  711. using namespace ControllerButtonDown;
  712. unsigned button = evt.cbutton.button;
  713. unsigned joystickIndex = joystickIDMap_[evt.cbutton.which];
  714. VariantMap& eventData = GetEventDataMap();
  715. eventData[P_JOYSTICK] = joystickIndex;
  716. eventData[P_BUTTON] = button;
  717. if (joystickIndex < joysticks_.Size() && button < joysticks_[joystickIndex].buttons_.Size()) {
  718. joysticks_[joystickIndex].buttons_[button] = true;
  719. joysticks_[joystickIndex].buttonPress_[button] = true;
  720. SendEvent(E_CONTROLLERBUTTONDOWN, eventData);
  721. }
  722. }
  723. break;
  724. case SDL_CONTROLLERBUTTONUP:
  725. {
  726. using namespace ControllerButtonUp;
  727. unsigned button = evt.cbutton.button;
  728. unsigned joystickIndex = joystickIDMap_[evt.cbutton.which];
  729. VariantMap& eventData = GetEventDataMap();
  730. eventData[P_JOYSTICK] = joystickIndex;
  731. eventData[P_BUTTON] = button;
  732. if (joystickIndex < joysticks_.Size() && button < joysticks_[joystickIndex].buttons_.Size()) {
  733. joysticks_[joystickIndex].buttons_[button] = false;
  734. SendEvent(E_CONTROLLERBUTTONUP, eventData);
  735. }
  736. }
  737. break;
  738. case SDL_CONTROLLERAXISMOTION:
  739. {
  740. using namespace ControllerAxisMove;
  741. unsigned joystickIndex = joystickIDMap_[evt.caxis.which];
  742. VariantMap& eventData = GetEventDataMap();
  743. eventData[P_JOYSTICK] = joystickIndex;
  744. eventData[P_AXIS] = evt.caxis.axis;
  745. eventData[P_POSITION] = Clamp((float)evt.caxis.value / 32767.0f, -1.0f, 1.0f);
  746. if (joystickIndex < joysticks_.Size() && evt.caxis.axis <
  747. joysticks_[joystickIndex].axes_.Size())
  748. {
  749. joysticks_[joystickIndex].axes_[evt.caxis.axis] = eventData[P_POSITION].GetFloat();
  750. SendEvent(E_CONTROLLERAXISMOVE, eventData);
  751. }
  752. }
  753. break;
  754. case SDL_WINDOWEVENT:
  755. {
  756. switch (evt.window.event)
  757. {
  758. case SDL_WINDOWEVENT_MINIMIZED:
  759. minimized_ = true;
  760. SendInputFocusEvent();
  761. break;
  762. case SDL_WINDOWEVENT_MAXIMIZED:
  763. case SDL_WINDOWEVENT_RESTORED:
  764. minimized_ = false;
  765. SendInputFocusEvent();
  766. #ifdef IOS
  767. // On iOS we never lose the GL context, but may have done GPU object changes that could not be applied yet.
  768. // Apply them now
  769. graphics_->Restore();
  770. #endif
  771. break;
  772. #ifdef ANDROID
  773. case SDL_WINDOWEVENT_FOCUS_GAINED:
  774. // Restore GPU objects to the new GL context
  775. graphics_->Restore();
  776. break;
  777. #endif
  778. case SDL_WINDOWEVENT_RESIZED:
  779. graphics_->WindowResized();
  780. break;
  781. }
  782. }
  783. break;
  784. case SDL_DROPFILE:
  785. {
  786. using namespace DropFile;
  787. VariantMap& eventData = GetEventDataMap();
  788. eventData[P_FILENAME] = GetInternalPath(String(evt.drop.file));
  789. SDL_free(evt.drop.file);
  790. SendEvent(E_DROPFILE, eventData);
  791. }
  792. break;
  793. case SDL_QUIT:
  794. SendEvent(E_EXITREQUESTED);
  795. break;
  796. }
  797. }
  798. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  799. {
  800. // Reset input state on subsequent initializations
  801. if (!initialized_)
  802. Initialize();
  803. else
  804. ResetState();
  805. // Re-enable cursor clipping, and re-center the cursor (if needed) to the new screen size, so that there is no erroneous
  806. // mouse move event. Also get new window ID if it changed
  807. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  808. windowID_ = SDL_GetWindowID(window);
  809. if (!mouseVisible_)
  810. {
  811. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  812. SetMousePosition(center);
  813. lastMousePosition_ = center;
  814. }
  815. focusedThisFrame_ = true;
  816. // After setting a new screen mode we should not be minimized
  817. minimized_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0;
  818. }
  819. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  820. {
  821. // Update input right at the beginning of the frame
  822. Update();
  823. }
  824. }