Input.cpp 28 KB

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