Input.cpp 25 KB

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