Input.cpp 25 KB

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