Input.cpp 25 KB

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