Input.cpp 26 KB

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