Input.cpp 25 KB

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