Input.cpp 26 KB

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