Input.cpp 28 KB

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