Input.cpp 28 KB

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