Input.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. using namespace MouseVisibleChanged;
  175. VariantMap eventData;
  176. eventData[P_VISIBLE] = mouseVisible_;
  177. SendEvent(E_MOUSEVISIBLECHANGED, eventData);
  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. case SDL_MOUSEBUTTONDOWN:
  524. SetMouseButton(1 << (evt.button.button - 1), true);
  525. break;
  526. case SDL_MOUSEBUTTONUP:
  527. SetMouseButton(1 << (evt.button.button - 1), false);
  528. break;
  529. case SDL_MOUSEMOTION:
  530. if (mouseVisible_)
  531. {
  532. mouseMove_.x_ += evt.motion.xrel;
  533. mouseMove_.y_ += evt.motion.yrel;
  534. using namespace MouseMove;
  535. VariantMap eventData;
  536. if (mouseVisible_)
  537. {
  538. eventData[P_X] = evt.motion.x;
  539. eventData[P_Y] = evt.motion.y;
  540. }
  541. eventData[P_DX] = evt.motion.xrel;
  542. eventData[P_DY] = evt.motion.yrel;
  543. eventData[P_BUTTONS] = mouseButtonDown_;
  544. eventData[P_QUALIFIERS] = GetQualifiers();
  545. SendEvent(E_MOUSEMOVE, eventData);
  546. }
  547. break;
  548. case SDL_MOUSEWHEEL:
  549. SetMouseWheel(evt.wheel.y);
  550. break;
  551. case SDL_FINGERDOWN:
  552. {
  553. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  554. TouchState& state = touches_[touchID];
  555. state.touchID_ = touchID;
  556. state.lastPosition_ = state.position_ = IntVector2((int)(evt.tfinger.x * graphics_->GetWidth()),
  557. (int)(evt.tfinger.y * graphics_->GetHeight()));
  558. state.delta_ = IntVector2::ZERO;
  559. state.pressure_ = evt.tfinger.pressure;
  560. using namespace TouchBegin;
  561. VariantMap eventData;
  562. eventData[P_TOUCHID] = touchID;
  563. eventData[P_X] = state.position_.x_;
  564. eventData[P_Y] = state.position_.y_;
  565. eventData[P_PRESSURE] = state.pressure_;
  566. SendEvent(E_TOUCHBEGIN, eventData);
  567. }
  568. break;
  569. case SDL_FINGERUP:
  570. {
  571. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  572. TouchState& state = touches_[touchID];
  573. using namespace TouchEnd;
  574. VariantMap eventData;
  575. // Do not trust the position in the finger up event. Instead use the last position stored in the
  576. // touch structure
  577. eventData[P_TOUCHID] = touchID;
  578. eventData[P_X] = state.position_.x_;
  579. eventData[P_Y] = state.position_.y_;
  580. touches_.Erase(touchID);
  581. SendEvent(E_TOUCHEND, eventData);
  582. }
  583. break;
  584. case SDL_FINGERMOTION:
  585. {
  586. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  587. TouchState& state = touches_[touchID];
  588. state.touchID_ = touchID;
  589. state.position_ = IntVector2((int)(evt.tfinger.x * graphics_->GetWidth()),
  590. (int)(evt.tfinger.y * graphics_->GetHeight()));
  591. state.delta_ = state.position_ - state.lastPosition_;
  592. state.pressure_ = evt.tfinger.pressure;
  593. using namespace TouchMove;
  594. VariantMap eventData;
  595. eventData[P_TOUCHID] = touchID;
  596. eventData[P_X] = state.position_.x_;
  597. eventData[P_Y] = state.position_.y_;
  598. eventData[P_DX] = (int)(evt.tfinger.dx * graphics_->GetWidth());
  599. eventData[P_DY] = (int)(evt.tfinger.dy * graphics_->GetHeight());
  600. eventData[P_PRESSURE] = state.pressure_;
  601. SendEvent(E_TOUCHMOVE, eventData);
  602. }
  603. break;
  604. case SDL_JOYBUTTONDOWN:
  605. {
  606. using namespace JoystickButtonDown;
  607. VariantMap eventData;
  608. eventData[P_JOYSTICK] = evt.jbutton.which;
  609. eventData[P_BUTTON] = evt.jbutton.button;
  610. if (evt.jbutton.which < joysticks_.Size() && evt.jbutton.button <
  611. joysticks_[evt.jbutton.which].buttons_.Size())
  612. {
  613. joysticks_[evt.jbutton.which].buttons_[evt.jbutton.button] = true;
  614. joysticks_[evt.jbutton.which].buttonPress_[evt.jbutton.button] = true;
  615. SendEvent(E_JOYSTICKBUTTONDOWN, eventData);
  616. }
  617. }
  618. break;
  619. case SDL_JOYBUTTONUP:
  620. {
  621. using namespace JoystickButtonUp;
  622. VariantMap eventData;
  623. eventData[P_JOYSTICK] = evt.jbutton.which;
  624. eventData[P_BUTTON] = evt.jbutton.button;
  625. if (evt.jbutton.which < joysticks_.Size() && evt.jbutton.button <
  626. joysticks_[evt.jbutton.which].buttons_.Size())
  627. {
  628. joysticks_[evt.jbutton.which].buttons_[evt.jbutton.button] = false;
  629. SendEvent(E_JOYSTICKBUTTONUP, eventData);
  630. }
  631. }
  632. break;
  633. case SDL_JOYAXISMOTION:
  634. {
  635. using namespace JoystickAxisMove;
  636. VariantMap eventData;
  637. eventData[P_JOYSTICK] = evt.jaxis.which;
  638. eventData[P_AXIS] = evt.jaxis.axis;
  639. eventData[P_POSITION] = Clamp((float)evt.jaxis.value / 32767.0f, -1.0f, 1.0f);
  640. if (evt.jaxis.which < joysticks_.Size() && evt.jaxis.axis <
  641. joysticks_[evt.jaxis.which].axes_.Size())
  642. {
  643. joysticks_[evt.jaxis.which].axes_[evt.jaxis.axis] = eventData[P_POSITION].GetFloat();
  644. SendEvent(E_JOYSTICKAXISMOVE, eventData);
  645. }
  646. }
  647. break;
  648. case SDL_JOYHATMOTION:
  649. {
  650. using namespace JoystickHatMove;
  651. VariantMap eventData;
  652. eventData[P_JOYSTICK] = evt.jhat.which;
  653. eventData[P_HAT] = evt.jhat.hat;
  654. eventData[P_POSITION] = evt.jhat.value;
  655. if (evt.jhat.which < joysticks_.Size() && evt.jhat.hat <
  656. joysticks_[evt.jhat.which].hats_.Size())
  657. {
  658. joysticks_[evt.jhat.which].hats_[evt.jhat.hat] = evt.jhat.value;
  659. SendEvent(E_JOYSTICKHATMOVE, eventData);
  660. }
  661. }
  662. break;
  663. case SDL_WINDOWEVENT:
  664. {
  665. switch (evt.window.event)
  666. {
  667. case SDL_WINDOWEVENT_MINIMIZED:
  668. minimized_ = true;
  669. SendInputFocusEvent();
  670. break;
  671. case SDL_WINDOWEVENT_MAXIMIZED:
  672. case SDL_WINDOWEVENT_RESTORED:
  673. minimized_ = false;
  674. SendInputFocusEvent();
  675. #ifdef IOS
  676. // On iOS we never lose the GL context, but may have done GPU object changes that could not be applied yet.
  677. // Apply them now
  678. graphics_->Restore();
  679. #endif
  680. break;
  681. #ifdef ANDROID
  682. case SDL_WINDOWEVENT_FOCUS_LOST:
  683. // Mark GPU objects lost
  684. graphics_->Release(false, false);
  685. break;
  686. case SDL_WINDOWEVENT_FOCUS_GAINED:
  687. // Restore GPU objects
  688. graphics_->Restore();
  689. break;
  690. #endif
  691. #if !defined(IOS) && !defined(ANDROID)
  692. case SDL_WINDOWEVENT_RESIZED:
  693. graphics_->WindowResized(evt.window.data1, evt.window.data2);
  694. break;
  695. #endif
  696. }
  697. }
  698. break;
  699. case SDL_QUIT:
  700. SendEvent(E_EXITREQUESTED);
  701. break;
  702. }
  703. }
  704. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  705. {
  706. // Reset input state on subsequent initializations
  707. if (!initialized_)
  708. Initialize();
  709. else
  710. ResetState();
  711. // Re-enable cursor clipping, and re-center the cursor (if needed) to the new screen size, so that there is no erroneous
  712. // mouse move event. Also get new window ID if it changed
  713. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  714. windowID_ = SDL_GetWindowID(window);
  715. if (!mouseVisible_)
  716. {
  717. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  718. SetMousePosition(center);
  719. lastMousePosition_ = center;
  720. }
  721. focusedThisFrame_ = true;
  722. // After setting a new screen mode we should not be minimized
  723. minimized_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0;
  724. }
  725. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  726. {
  727. // Update input right at the beginning of the frame
  728. Update();
  729. }
  730. }