Input.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. 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. windowID_(0),
  62. toggleFullscreen_(true),
  63. mouseVisible_(false),
  64. active_(false),
  65. minimized_(false),
  66. activated_(false),
  67. suppressNextMouseMove_(false),
  68. initialized_(false)
  69. {
  70. // Zero the initial state
  71. mouseButtonDown_ = 0;
  72. ResetState();
  73. SubscribeToEvent(E_SCREENMODE, HANDLER(Input, HandleScreenMode));
  74. SubscribeToEvent(E_BEGINFRAME, HANDLER(Input, HandleBeginFrame));
  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. PROFILE(UpdateInput);
  90. if (!graphics_ || !graphics_->IsInitialized())
  91. return;
  92. // Reset input accumulation for this frame
  93. keyPress_.Clear();
  94. mouseButtonPress_ = 0;
  95. mouseMove_ = IntVector2::ZERO;
  96. mouseMoveWheel_ = 0;
  97. for (Vector<JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  98. {
  99. for (unsigned j = 0; j < i->buttonPress_.Size(); ++j)
  100. i->buttonPress_[j] = false;
  101. }
  102. // Reset touch delta movement
  103. for (HashMap<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  104. {
  105. TouchState& state = i->second_;
  106. state.lastPosition_ = state.position_;
  107. state.delta_ = IntVector2::ZERO;
  108. }
  109. {
  110. MutexLock lock(GetStaticMutex());
  111. // Pump SDL events
  112. SDL_Event evt;
  113. SDL_PumpEvents();
  114. while (SDL_PollEvent(&evt))
  115. {
  116. // Dispatch event to the appropriate Input instance. However SDL_QUIT can not at the moment be handled for multiple
  117. // instances properly (other threads' graphics devices can not be closed from this thread), so we handle it only
  118. // for own instance
  119. if (evt.type != SDL_QUIT)
  120. HandleSDLEvent(&evt);
  121. else
  122. graphics_->Close();
  123. }
  124. }
  125. // Check for activation and inactivation from SDL window flags. Must nullcheck the window pointer because it may have
  126. // been closed due to input events
  127. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  128. if (window)
  129. {
  130. unsigned flags = SDL_GetWindowFlags(window) & (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS);
  131. if (!active_ && (graphics_->GetFullscreen() || mouseVisible_) && flags == (SDL_WINDOW_INPUT_FOCUS |
  132. SDL_WINDOW_MOUSE_FOCUS))
  133. activated_ = true;
  134. else if (active_ && (flags & SDL_WINDOW_INPUT_FOCUS) == 0)
  135. MakeInactive();
  136. // Activate input now if necessary
  137. if (activated_)
  138. MakeActive();
  139. }
  140. else
  141. return;
  142. // Check for mouse move
  143. if (active_)
  144. {
  145. IntVector2 mousePosition = GetMousePosition();
  146. mouseMove_ = mousePosition - lastMousePosition_;
  147. // Recenter the mouse cursor manually
  148. if (!mouseVisible_)
  149. {
  150. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  151. SetMousePosition(center);
  152. lastMousePosition_ = center;
  153. }
  154. else
  155. lastMousePosition_ = mousePosition;
  156. if (mouseMove_ != IntVector2::ZERO && suppressNextMouseMove_)
  157. {
  158. mouseMove_ = IntVector2::ZERO;
  159. suppressNextMouseMove_ = false;
  160. }
  161. // Send mouse move event if necessary
  162. if (mouseMove_ != IntVector2::ZERO)
  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. void Input::SetMouseVisible(bool enable)
  180. {
  181. // External windows can only support visible mouse cursor
  182. if (graphics_ && graphics_->GetExternalWindow())
  183. enable = true;
  184. if (enable != mouseVisible_)
  185. {
  186. mouseVisible_ = enable;
  187. if (initialized_)
  188. {
  189. if (!mouseVisible_ && active_)
  190. SDL_ShowCursor(SDL_FALSE);
  191. else
  192. SDL_ShowCursor(SDL_TRUE);
  193. }
  194. }
  195. }
  196. void Input::SetToggleFullscreen(bool enable)
  197. {
  198. toggleFullscreen_ = enable;
  199. }
  200. bool Input::DetectJoysticks()
  201. {
  202. if (inputInstances.Size() > 1)
  203. {
  204. LOGERROR("Can not redetect joysticks with multiple application instances");
  205. return false;
  206. }
  207. else
  208. {
  209. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  210. SDL_InitSubSystem(SDL_INIT_JOYSTICK);
  211. ResetJoysticks();
  212. return true;
  213. }
  214. }
  215. bool Input::OpenJoystick(unsigned index)
  216. {
  217. if (index >= joysticks_.Size())
  218. return false;
  219. // Check if already opened
  220. if (joysticks_[index].joystick_)
  221. return true;
  222. MutexLock lock(GetStaticMutex());
  223. SDL_Joystick* joystick = SDL_JoystickOpen(index);
  224. if (joystick)
  225. {
  226. JoystickState& state = joysticks_[index];
  227. state.joystick_ = joystick;
  228. state.buttons_.Resize(SDL_JoystickNumButtons(joystick));
  229. state.buttonPress_.Resize(state.buttons_.Size());
  230. state.axes_.Resize(SDL_JoystickNumAxes(joystick));
  231. state.hats_.Resize(SDL_JoystickNumHats(joystick));
  232. for (unsigned i = 0; i < state.buttons_.Size(); ++i)
  233. {
  234. state.buttons_[i] = false;
  235. state.buttonPress_[i] = false;
  236. }
  237. for (unsigned i = 0; i < state.axes_.Size(); ++i)
  238. state.axes_[i] = 0.0f;
  239. for (unsigned i = 0; i < state.hats_.Size(); ++i)
  240. state.hats_[i] = HAT_CENTER;
  241. return true;
  242. }
  243. else
  244. return false;
  245. }
  246. void Input::CloseJoystick(unsigned index)
  247. {
  248. MutexLock lock(GetStaticMutex());
  249. if (index < joysticks_.Size() && joysticks_[index].joystick_)
  250. {
  251. JoystickState& state = joysticks_[index];
  252. SDL_JoystickClose(state.joystick_);
  253. state.joystick_ = 0;
  254. state.buttons_.Clear();
  255. state.axes_.Clear();
  256. state.hats_.Clear();
  257. }
  258. }
  259. bool Input::GetKeyDown(int key) const
  260. {
  261. return keyDown_.Contains(key);
  262. }
  263. bool Input::GetKeyPress(int key) const
  264. {
  265. return keyPress_.Contains(key);
  266. }
  267. bool Input::GetMouseButtonDown(int button) const
  268. {
  269. return (mouseButtonDown_ & button) != 0;
  270. }
  271. bool Input::GetMouseButtonPress(int button) const
  272. {
  273. return (mouseButtonPress_ & button) != 0;
  274. }
  275. bool Input::GetQualifierDown(int qualifier) const
  276. {
  277. if (qualifier == QUAL_SHIFT)
  278. return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
  279. if (qualifier == QUAL_CTRL)
  280. return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
  281. if (qualifier == QUAL_ALT)
  282. return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
  283. return false;
  284. }
  285. bool Input::GetQualifierPress(int qualifier) const
  286. {
  287. if (qualifier == QUAL_SHIFT)
  288. return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
  289. if (qualifier == QUAL_CTRL)
  290. return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
  291. if (qualifier == QUAL_ALT)
  292. return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
  293. return false;
  294. }
  295. int Input::GetQualifiers() const
  296. {
  297. int ret = 0;
  298. if (GetQualifierDown(QUAL_SHIFT))
  299. ret |= QUAL_SHIFT;
  300. if (GetQualifierDown(QUAL_CTRL))
  301. ret |= QUAL_CTRL;
  302. if (GetQualifierDown(QUAL_ALT))
  303. ret |= QUAL_ALT;
  304. return ret;
  305. }
  306. IntVector2 Input::GetMousePosition() const
  307. {
  308. IntVector2 ret = IntVector2::ZERO;
  309. if (!graphics_ || !graphics_->IsInitialized())
  310. return ret;
  311. SDL_GetMouseState(&ret.x_, &ret.y_);
  312. return ret;
  313. }
  314. TouchState* Input::GetTouch(unsigned index) const
  315. {
  316. unsigned cmpIndex = 0;
  317. for (HashMap<int, TouchState>::ConstIterator i = touches_.Begin(); i != touches_.End(); ++i)
  318. {
  319. if (cmpIndex == index)
  320. return const_cast<TouchState*>(&i->second_);
  321. else
  322. ++cmpIndex;
  323. }
  324. return 0;
  325. }
  326. const String& Input::GetJoystickName(unsigned index) const
  327. {
  328. if (index < joysticks_.Size())
  329. return joysticks_[index].name_;
  330. else
  331. return String::EMPTY;
  332. }
  333. JoystickState* Input::GetJoystick(unsigned index)
  334. {
  335. if (index < joysticks_.Size())
  336. {
  337. // If necessary, automatically open the joystick first
  338. if (!joysticks_[index].joystick_)
  339. OpenJoystick(index);
  340. return const_cast<JoystickState*>(&joysticks_[index]);
  341. }
  342. else
  343. return 0;
  344. }
  345. bool Input::IsMinimized() const
  346. {
  347. // Return minimized state also when inactive in fullscreen
  348. if (!active_ && graphics_ && graphics_->GetFullscreen())
  349. return true;
  350. else
  351. return minimized_;
  352. }
  353. void Input::Initialize()
  354. {
  355. Graphics* graphics = GetSubsystem<Graphics>();
  356. if (!graphics || !graphics->IsInitialized())
  357. return;
  358. graphics_ = graphics;
  359. // In external window mode only visible mouse is supported
  360. if (graphics_->GetExternalWindow())
  361. mouseVisible_ = true;
  362. // Set the initial activation
  363. activated_ = true;
  364. initialized_ = true;
  365. {
  366. MutexLock lock(GetStaticMutex());
  367. // Store window ID to direct SDL events to the correct instance
  368. windowID_ = SDL_GetWindowID(graphics_->GetImpl()->GetWindow());
  369. inputInstances[windowID_] = this;
  370. }
  371. ResetJoysticks();
  372. LOGINFO("Initialized input");
  373. }
  374. void Input::ResetJoysticks()
  375. {
  376. joysticks_.Clear();
  377. joysticks_.Resize(SDL_NumJoysticks());
  378. for (unsigned i = 0; i < joysticks_.Size(); ++i)
  379. joysticks_[i].name_ = SDL_JoystickName(i);
  380. }
  381. void Input::MakeActive()
  382. {
  383. if (!graphics_ || !graphics_->IsInitialized())
  384. return;
  385. ResetState();
  386. active_ = true;
  387. activated_ = false;
  388. // Re-establish mouse cursor hiding as necessary
  389. if (!mouseVisible_)
  390. {
  391. SDL_ShowCursor(SDL_FALSE);
  392. suppressNextMouseMove_ = true;
  393. }
  394. else
  395. lastMousePosition_ = GetMousePosition();
  396. SendActivationEvent();
  397. }
  398. void Input::MakeInactive()
  399. {
  400. if (!graphics_ || !graphics_->IsInitialized())
  401. return;
  402. ResetState();
  403. active_ = false;
  404. activated_ = false;
  405. // Show the mouse cursor when inactive
  406. SDL_ShowCursor(SDL_TRUE);
  407. SendActivationEvent();
  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::SendActivationEvent()
  441. {
  442. using namespace Activation;
  443. VariantMap eventData;
  444. eventData[P_ACTIVE] = IsActive();
  445. eventData[P_MINIMIZED] = IsMinimized();
  446. SendEvent(E_ACTIVATION, eventData);
  447. }
  448. void Input::SetMouseButton(int button, bool newState)
  449. {
  450. // After deactivation in windowed hidden mouse mode, activate only after a left-click inside the window
  451. // This allows glitchfree window dragging on all operating systems
  452. if (initialized_ && !graphics_->GetFullscreen() && !mouseVisible_)
  453. {
  454. if (!active_ && newState && button == MOUSEB_LEFT)
  455. activated_ = true;
  456. }
  457. // If we are not active yet, do not react to the mouse button down
  458. if (newState && !active_)
  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 are not active yet, do not react to the key down
  482. if (newState && !active_)
  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 && newState && !repeat && toggleFullscreen_ && (GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT)))
  509. graphics_->ToggleFullscreen();
  510. }
  511. void Input::SetMouseWheel(int delta)
  512. {
  513. // If we are not active yet, do not react to the wheel
  514. if (!active_)
  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->SendActivationEvent();
  718. break;
  719. case SDL_WINDOWEVENT_MAXIMIZED:
  720. case SDL_WINDOWEVENT_RESTORED:
  721. input->minimized_ = false;
  722. input->SendActivationEvent();
  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. }
  740. }
  741. break;
  742. }
  743. }
  744. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  745. {
  746. // Reset input state on subsequent initializations
  747. if (!initialized_)
  748. Initialize();
  749. else
  750. ResetState();
  751. // Re-enable cursor clipping, and re-center the cursor (if needed) to the new screen size, so that there is no erroneous
  752. // mouse move event. Also get the new window ID in case it changed
  753. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  754. unsigned newWindowID = SDL_GetWindowID(window);
  755. if (newWindowID != windowID_)
  756. {
  757. MutexLock lock(GetStaticMutex());
  758. inputInstances.Erase(windowID_);
  759. inputInstances[newWindowID] = this;
  760. windowID_ = newWindowID;
  761. }
  762. if (!mouseVisible_)
  763. {
  764. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  765. SetMousePosition(center);
  766. lastMousePosition_ = center;
  767. }
  768. activated_ = true;
  769. // After setting a new screen mode we should not be minimized
  770. minimized_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0;
  771. }
  772. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  773. {
  774. // Update input right at the beginning of the frame
  775. if (initialized_)
  776. Update();
  777. }
  778. }