Input.cpp 26 KB

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