Input.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Context.h"
  24. #include "CoreEvents.h"
  25. #include "Graphics.h"
  26. #include "GraphicsEvents.h"
  27. #include "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. #ifdef USE_OPENGL
  35. #include <GraphicsImpl.h>
  36. #else
  37. #include <windows.h>
  38. #endif
  39. #include "DebugNew.h"
  40. #ifdef USE_OPENGL
  41. static HashMap<unsigned, Input*> inputInstances;
  42. /// Return the Input subsystem instance corresponding to an SDL window ID.
  43. Input* GetInputInstance(unsigned windowID)
  44. {
  45. return windowID ? inputInstances[windowID] : 0;
  46. }
  47. #else
  48. /// Convert the virtual key code & scan code if necessary. Return non-zero if key should be posted
  49. int ConvertKeyCode(unsigned wParam, unsigned lParam)
  50. {
  51. unsigned scanCode = (lParam >> 16) & 0x1ff;
  52. // Recognize left/right qualifier key from the scan code
  53. switch (wParam)
  54. {
  55. case VK_SHIFT:
  56. if (scanCode == 54)
  57. return KEY_RSHIFT;
  58. else
  59. return KEY_LSHIFT;
  60. break;
  61. case VK_CONTROL:
  62. // Control might not be a real key, as Windows posts it whenever Alt-Gr is pressed (inspired by GLFW)
  63. {
  64. MSG nextMsg;
  65. DWORD msgTime = GetMessageTime();
  66. if (PeekMessage(&nextMsg, NULL, 0, 0, PM_NOREMOVE))
  67. {
  68. if ((nextMsg.message == WM_KEYDOWN || nextMsg.message == WM_SYSKEYDOWN) && nextMsg.wParam == VK_MENU &&
  69. (nextMsg.lParam & 0x01000000) != 0 && nextMsg.time == msgTime)
  70. return 0;
  71. }
  72. if (scanCode & 0x100)
  73. return KEY_RCTRL;
  74. else
  75. return KEY_LCTRL;
  76. }
  77. break;
  78. case VK_MENU:
  79. if (scanCode & 0x100)
  80. return KEY_RALT;
  81. else
  82. return KEY_LALT;
  83. default:
  84. return wParam;
  85. }
  86. }
  87. #endif
  88. OBJECTTYPESTATIC(Input);
  89. Input::Input(Context* context) :
  90. Object(context),
  91. #ifdef USE_OPENGL
  92. windowID_(0),
  93. #else
  94. showCursor_(true),
  95. #endif
  96. toggleFullscreen_(true),
  97. active_(false),
  98. minimized_(false),
  99. activated_(false),
  100. suppressNextMouseMove_(false),
  101. initialized_(false)
  102. {
  103. // Zero the initial state
  104. mouseButtonDown_ = 0;
  105. ResetState();
  106. #ifndef USE_OPENGL
  107. SubscribeToEvent(E_WINDOWMESSAGE, HANDLER(Input, HandleWindowMessage));
  108. #endif
  109. SubscribeToEvent(E_SCREENMODE, HANDLER(Input, HandleScreenMode));
  110. SubscribeToEvent(E_BEGINFRAME, HANDLER(Input, HandleBeginFrame));
  111. // Try to initialize right now, but skip if screen mode is not yet set
  112. Initialize();
  113. }
  114. Input::~Input()
  115. {
  116. #ifdef USE_OPENGL
  117. // Remove input instance mapping
  118. if (initialized_)
  119. {
  120. MutexLock lock(GetStaticMutex());
  121. inputInstances.Erase(windowID_);
  122. }
  123. #endif
  124. }
  125. void Input::Update()
  126. {
  127. PROFILE(UpdateInput);
  128. if (!graphics_ || !graphics_->IsInitialized())
  129. return;
  130. // Reset input accumulation for this frame
  131. keyPress_.Clear();
  132. mouseButtonPress_ = 0;
  133. mouseMove_ = IntVector2::ZERO;
  134. mouseMoveWheel_ = 0;
  135. #ifndef USE_OPENGL
  136. // Pump Win32 events
  137. MSG msg;
  138. while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
  139. {
  140. TranslateMessage(&msg);
  141. DispatchMessageW(&msg);
  142. }
  143. #else
  144. {
  145. MutexLock lock(GetStaticMutex());
  146. // Pump SDL events
  147. SDL_Event evt;
  148. SDL_PumpEvents();
  149. while (SDL_PollEvent(&evt))
  150. {
  151. // Dispatch event to the appropriate Input instance. However SDL_QUIT can not at the moment be handled for multiple
  152. // instances properly (OpenGL contexts running in other threads can not be closed from this thread), so we handle
  153. // it only for the own instance
  154. if (evt.type != SDL_QUIT)
  155. HandleSDLEvent(&evt);
  156. else
  157. graphics_->Close();
  158. }
  159. }
  160. // Poll SDL window activation state
  161. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  162. unsigned flags = SDL_GetWindowFlags(window);
  163. if ((flags & (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS)) == (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS))
  164. {
  165. if (!active_)
  166. activated_ = true;
  167. }
  168. else
  169. {
  170. if (active_)
  171. MakeInactive();
  172. }
  173. #endif
  174. // Activate input now if necessary
  175. if (activated_)
  176. MakeActive();
  177. // Finally send the mouse move event if motion has been accumulated
  178. if (active_)
  179. {
  180. IntVector2 mousePos = GetCursorPosition();
  181. mouseMove_ = mousePos - lastCursorPosition_;
  182. // Recenter the mouse cursor manually if it moved
  183. if (mouseMove_ != IntVector2::ZERO)
  184. {
  185. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  186. SetCursorPosition(center);
  187. lastCursorPosition_ = center;
  188. }
  189. else
  190. lastCursorPosition_ = mousePos;
  191. if (mouseMove_ != IntVector2::ZERO && suppressNextMouseMove_)
  192. {
  193. mouseMove_ = IntVector2::ZERO;
  194. suppressNextMouseMove_ = false;
  195. }
  196. if (mouseMove_ != IntVector2::ZERO)
  197. {
  198. using namespace MouseMove;
  199. VariantMap eventData;
  200. eventData[P_DX] = mouseMove_.x_;
  201. eventData[P_DY] = mouseMove_.y_;
  202. eventData[P_BUTTONS] = mouseButtonDown_;
  203. eventData[P_QUALIFIERS] = GetQualifiers();
  204. SendEvent(E_MOUSEMOVE, eventData);
  205. }
  206. }
  207. }
  208. void Input::SetToggleFullscreen(bool enable)
  209. {
  210. toggleFullscreen_ = enable;
  211. }
  212. bool Input::GetKeyDown(int key) const
  213. {
  214. return keyDown_.Contains(key);
  215. }
  216. bool Input::GetKeyPress(int key) const
  217. {
  218. return keyPress_.Contains(key);
  219. }
  220. bool Input::GetMouseButtonDown(int button) const
  221. {
  222. return (mouseButtonDown_ & button) != 0;
  223. }
  224. bool Input::GetMouseButtonPress(int button) const
  225. {
  226. return (mouseButtonPress_ & button) != 0;
  227. }
  228. bool Input::GetQualifierDown(int qualifier) const
  229. {
  230. if (qualifier == QUAL_SHIFT)
  231. return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
  232. if (qualifier == QUAL_CTRL)
  233. return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
  234. if (qualifier == QUAL_ALT)
  235. return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
  236. return false;
  237. }
  238. bool Input::GetQualifierPress(int qualifier) const
  239. {
  240. if (qualifier == QUAL_SHIFT)
  241. return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
  242. if (qualifier == QUAL_CTRL)
  243. return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
  244. if (qualifier == QUAL_ALT)
  245. return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
  246. return false;
  247. }
  248. int Input::GetQualifiers() const
  249. {
  250. int ret = 0;
  251. if (GetQualifierDown(QUAL_SHIFT))
  252. ret |= QUAL_SHIFT;
  253. if (GetQualifierDown(QUAL_CTRL))
  254. ret |= QUAL_CTRL;
  255. if (GetQualifierDown(QUAL_ALT))
  256. ret |= QUAL_ALT;
  257. return ret;
  258. }
  259. void Input::Initialize()
  260. {
  261. Graphics* graphics = GetSubsystem<Graphics>();
  262. if (!graphics || !graphics->IsInitialized())
  263. return;
  264. graphics_ = graphics;
  265. // Set the initial activation
  266. activated_ = true;
  267. initialized_ = true;
  268. #ifdef USE_OPENGL
  269. {
  270. MutexLock lock(GetStaticMutex());
  271. // Store window ID to direct SDL events to the correct instance
  272. windowID_ = SDL_GetWindowID(graphics_->GetImpl()->GetWindow());
  273. inputInstances[windowID_] = this;
  274. }
  275. #endif
  276. LOGINFO("Initialized input");
  277. }
  278. void Input::MakeActive()
  279. {
  280. if (!graphics_ || !graphics_->IsInitialized())
  281. return;
  282. ResetState();
  283. active_ = true;
  284. activated_ = false;
  285. // Re-establish mouse cursor clipping as necessary
  286. #ifdef USE_OPENGL
  287. SDL_ShowCursor(SDL_FALSE);
  288. suppressNextMouseMove_ = true;
  289. #else
  290. SetClipCursor(true);
  291. SetCursorVisible(false);
  292. #endif
  293. using namespace Activation;
  294. VariantMap eventData;
  295. eventData[P_ACTIVE] = active_;
  296. eventData[P_MINIMIZED] = minimized_;
  297. SendEvent(E_ACTIVATION, eventData);
  298. }
  299. void Input::MakeInactive()
  300. {
  301. if (!graphics_ || !graphics_->IsInitialized())
  302. return;
  303. ResetState();
  304. active_ = false;
  305. activated_ = false;
  306. // Free and show the mouse cursor
  307. #ifndef USE_OPENGL
  308. ReleaseCapture();
  309. ClipCursor(0);
  310. SetCursorVisible(true);
  311. #else
  312. SDL_ShowCursor(SDL_TRUE);
  313. #endif
  314. using namespace Activation;
  315. VariantMap eventData;
  316. eventData[P_ACTIVE] = active_;
  317. eventData[P_MINIMIZED] = minimized_;
  318. SendEvent(E_ACTIVATION, eventData);
  319. }
  320. void Input::ResetState()
  321. {
  322. keyDown_.Clear();
  323. keyPress_.Clear();
  324. // Use SetMouseButton() to reset the state so that mouse events will be sent properly
  325. SetMouseButton(MOUSEB_LEFT, false);
  326. SetMouseButton(MOUSEB_RIGHT, false);
  327. SetMouseButton(MOUSEB_MIDDLE, false);
  328. mouseMove_ = IntVector2::ZERO;
  329. mouseMoveWheel_ = 0;
  330. mouseButtonPress_ = 0;
  331. }
  332. void Input::SetMouseButton(int button, bool newState)
  333. {
  334. // If we are not active yet, do not react to the mouse button down
  335. if (newState && !active_)
  336. return;
  337. if (newState)
  338. {
  339. if (!(mouseButtonDown_ & button))
  340. mouseButtonPress_ |= button;
  341. mouseButtonDown_ |= button;
  342. }
  343. else
  344. {
  345. if (!(mouseButtonDown_ & button))
  346. return;
  347. mouseButtonDown_ &= ~button;
  348. }
  349. using namespace MouseButtonDown;
  350. VariantMap eventData;
  351. eventData[P_BUTTON] = button;
  352. eventData[P_BUTTONS] = mouseButtonDown_;
  353. eventData[P_QUALIFIERS] = GetQualifiers();
  354. SendEvent(newState ? E_MOUSEBUTTONDOWN : E_MOUSEBUTTONUP, eventData);
  355. }
  356. void Input::SetKey(int key, bool newState)
  357. {
  358. // If we are not active yet, do not react to the key down
  359. if (newState && !active_)
  360. return;
  361. bool repeat = false;
  362. if (newState)
  363. {
  364. if (!keyDown_.Contains(key))
  365. {
  366. keyDown_.Insert(key);
  367. keyPress_.Insert(key);
  368. }
  369. else
  370. repeat = true;
  371. }
  372. else
  373. {
  374. if (!keyDown_.Erase(key))
  375. return;
  376. }
  377. using namespace KeyDown;
  378. VariantMap eventData;
  379. eventData[P_KEY] = key;
  380. eventData[P_BUTTONS] = mouseButtonDown_;
  381. eventData[P_QUALIFIERS] = GetQualifiers();
  382. if (newState)
  383. eventData[P_REPEAT] = repeat;
  384. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  385. if (key == KEY_RETURN && newState && !repeat && toggleFullscreen_ && (GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT)))
  386. graphics_->ToggleFullscreen();
  387. }
  388. void Input::SetMouseWheel(int delta)
  389. {
  390. // If we are not active yet, do not react to the wheel
  391. if (!active_)
  392. return;
  393. if (delta)
  394. {
  395. mouseMoveWheel_ += delta;
  396. using namespace MouseWheel;
  397. VariantMap eventData;
  398. eventData[P_WHEEL] = delta;
  399. eventData[P_BUTTONS] = mouseButtonDown_;
  400. eventData[P_QUALIFIERS] = GetQualifiers();
  401. SendEvent(E_MOUSEWHEEL, eventData);
  402. }
  403. }
  404. void Input::SetCursorPosition(const IntVector2& position)
  405. {
  406. if (!graphics_)
  407. return;
  408. #ifdef USE_OPENGL
  409. SDL_WarpMouseInWindow(graphics_->GetImpl()->GetWindow(), position.x_, position.y_);
  410. #else
  411. POINT point;
  412. point.x = position.x_;
  413. point.y = position.y_;
  414. ClientToScreen((HWND)graphics_->GetWindowHandle(), &point);
  415. SetCursorPos(point.x, point.y);
  416. #endif
  417. }
  418. IntVector2 Input::GetCursorPosition() const
  419. {
  420. IntVector2 ret = lastCursorPosition_;
  421. if (!graphics_ || !graphics_->IsInitialized())
  422. return ret;
  423. #ifdef USE_OPENGL
  424. SDL_GetMouseState(&ret.x_, &ret.y_);
  425. #else
  426. POINT mouse;
  427. GetCursorPos(&mouse);
  428. ScreenToClient((HWND)graphics_->GetWindowHandle(), &mouse);
  429. ret.x_ = mouse.x;
  430. ret.y_ = mouse.y;
  431. #endif
  432. return ret;
  433. }
  434. #ifndef USE_OPENGL
  435. void Input::SetClipCursor(bool enable)
  436. {
  437. if (!graphics_)
  438. return;
  439. if (!graphics_->GetFullscreen() && active_ && enable)
  440. {
  441. SetCursorPosition(IntVector2(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2));
  442. lastCursorPosition_ = GetCursorPosition();
  443. RECT clipRect;
  444. GetWindowRect((HWND)graphics_->GetWindowHandle(), &clipRect);
  445. ClipCursor(&clipRect);
  446. }
  447. else
  448. {
  449. if (graphics_->GetFullscreen() && active_ && enable)
  450. {
  451. SetCursorPosition(IntVector2(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2));
  452. lastCursorPosition_ = GetCursorPosition();
  453. }
  454. ClipCursor(0);
  455. }
  456. }
  457. void Input::SetCursorVisible(bool enable)
  458. {
  459. if (!graphics_)
  460. return;
  461. // When inactive, always show the cursor
  462. if (!active_)
  463. enable = true;
  464. if (showCursor_ == enable)
  465. return;
  466. ShowCursor(enable ? TRUE : FALSE);
  467. showCursor_ = enable;
  468. }
  469. void Input::HandleWindowMessage(StringHash eventType, VariantMap& eventData)
  470. {
  471. if (!initialized_)
  472. Initialize();
  473. using namespace WindowMessage;
  474. int msg = eventData[P_MSG].GetInt();
  475. int wParam = eventData[P_WPARAM].GetInt();
  476. int lParam = eventData[P_LPARAM].GetInt();
  477. int keyCode;
  478. switch (msg)
  479. {
  480. case WM_LBUTTONDOWN:
  481. SetMouseButton(MOUSEB_LEFT, true);
  482. eventData[P_HANDLED] = true;
  483. break;
  484. case WM_NCLBUTTONUP:
  485. case WM_LBUTTONUP:
  486. SetMouseButton(MOUSEB_LEFT, false);
  487. eventData[P_HANDLED] = true;
  488. break;
  489. case WM_RBUTTONDOWN:
  490. SetMouseButton(MOUSEB_RIGHT, true);
  491. eventData[P_HANDLED] = true;
  492. break;
  493. case WM_NCRBUTTONUP:
  494. case WM_RBUTTONUP:
  495. SetMouseButton(MOUSEB_RIGHT, false);
  496. eventData[P_HANDLED] = true;
  497. break;
  498. case WM_MBUTTONDOWN:
  499. SetMouseButton(MOUSEB_MIDDLE, true);
  500. eventData[P_HANDLED] = true;
  501. break;
  502. case WM_NCMBUTTONUP:
  503. case WM_MBUTTONUP:
  504. SetMouseButton(MOUSEB_MIDDLE, false);
  505. eventData[P_HANDLED] = true;
  506. break;
  507. case WM_MOUSEWHEEL:
  508. SetMouseWheel(wParam >> 16);
  509. eventData[P_HANDLED] = true;
  510. break;
  511. case WM_ACTIVATE:
  512. minimized_ = HIWORD(wParam) != 0;
  513. if (LOWORD(wParam) == WA_INACTIVE)
  514. MakeInactive();
  515. else
  516. {
  517. if (!minimized_)
  518. activated_ = true;
  519. }
  520. eventData[P_HANDLED] = true;
  521. break;
  522. case WM_KEYDOWN:
  523. keyCode = ConvertKeyCode(wParam, lParam);
  524. if (keyCode)
  525. SetKey(keyCode, true);
  526. eventData[P_HANDLED] = true;
  527. break;
  528. case WM_SYSKEYDOWN:
  529. keyCode = ConvertKeyCode(wParam, lParam);
  530. if (keyCode)
  531. SetKey(keyCode, true);
  532. if (keyCode != KEY_F4)
  533. eventData[P_HANDLED] = true;
  534. break;
  535. case WM_KEYUP:
  536. case WM_SYSKEYUP:
  537. keyCode = ConvertKeyCode(wParam, lParam);
  538. if (keyCode)
  539. SetKey(keyCode, false);
  540. eventData[P_HANDLED] = true;
  541. break;
  542. case WM_CHAR:
  543. {
  544. using namespace Char;
  545. VariantMap keyEventData;
  546. keyEventData[P_CHAR] = wParam;
  547. keyEventData[P_BUTTONS] = mouseButtonDown_;
  548. keyEventData[P_QUALIFIERS] = GetQualifiers();
  549. SendEvent(E_CHAR, keyEventData);
  550. }
  551. eventData[P_HANDLED] = true;
  552. break;
  553. }
  554. }
  555. #else
  556. void Input::HandleSDLEvent(void* sdlEvent)
  557. {
  558. SDL_Event& evt = *static_cast<SDL_Event*>(sdlEvent);
  559. Input* input = 0;
  560. switch (evt.type)
  561. {
  562. case SDL_KEYDOWN:
  563. // Convert to uppercase to match Win32 virtual key codes
  564. input = GetInputInstance(evt.key.windowID);
  565. if (input)
  566. input->SetKey(SDL_toupper(evt.key.keysym.sym), true);
  567. break;
  568. case SDL_KEYUP:
  569. input = GetInputInstance(evt.key.windowID);
  570. if (input)
  571. input->SetKey(SDL_toupper(evt.key.keysym.sym), false);
  572. break;
  573. case SDL_TEXTINPUT:
  574. input = GetInputInstance(evt.text.windowID);
  575. if (input && evt.text.text[0])
  576. {
  577. String text(&evt.text.text[0]);
  578. unsigned unicode = text.AtUTF8(0);
  579. if (unicode)
  580. {
  581. using namespace Char;
  582. VariantMap keyEventData;
  583. keyEventData[P_CHAR] = unicode;
  584. keyEventData[P_BUTTONS] = input->mouseButtonDown_;
  585. keyEventData[P_QUALIFIERS] = input->GetQualifiers();
  586. input->SendEvent(E_CHAR, keyEventData);
  587. }
  588. }
  589. break;
  590. case SDL_MOUSEBUTTONDOWN:
  591. input = GetInputInstance(evt.button.windowID);
  592. if (input)
  593. input->SetMouseButton(1 << (evt.button.button - 1), true);
  594. break;
  595. case SDL_MOUSEBUTTONUP:
  596. input = GetInputInstance(evt.button.windowID);
  597. if (input)
  598. input->SetMouseButton(1 << (evt.button.button - 1), false);
  599. break;
  600. case SDL_MOUSEWHEEL:
  601. input = GetInputInstance(evt.wheel.windowID);
  602. if (input)
  603. input->SetMouseWheel(evt.wheel.y);
  604. break;
  605. case SDL_WINDOWEVENT:
  606. if (evt.window.event == SDL_WINDOWEVENT_CLOSE)
  607. {
  608. input = GetInputInstance(evt.window.windowID);
  609. if (input)
  610. input->GetSubsystem<Graphics>()->Close();
  611. }
  612. break;
  613. }
  614. }
  615. #endif
  616. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  617. {
  618. // Reset input state on subsequent initializations
  619. if (!initialized_)
  620. Initialize();
  621. else
  622. ResetState();
  623. // Re-enable cursor clipping, and re-center the cursor (if needed) to the new screen size, so that there is no erroneous
  624. // mouse move event. Also, in SDL mode reset the window ID
  625. #ifdef USE_OPENGL
  626. unsigned newWindowID = SDL_GetWindowID(graphics_->GetImpl()->GetWindow());
  627. if (newWindowID != windowID_)
  628. {
  629. MutexLock lock(GetStaticMutex());
  630. inputInstances.Erase(windowID_);
  631. inputInstances[newWindowID] = this;
  632. windowID_ = newWindowID;
  633. }
  634. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  635. SetCursorPosition(center);
  636. lastCursorPosition_ = center;
  637. activated_ = true;
  638. #else
  639. SetClipCursor(true);
  640. #endif
  641. }
  642. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  643. {
  644. // Update input right at the beginning of the frame
  645. if (initialized_)
  646. Update();
  647. }