Input.cpp 25 KB

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