Input.cpp 25 KB

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