Input.cpp 29 KB

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