Input.cpp 27 KB

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