Input.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. HandleSDLEvent(&evt);
  151. }
  152. // Poll SDL window activation state
  153. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  154. unsigned flags = SDL_GetWindowFlags(window);
  155. if ((flags & (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS)) == (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS))
  156. {
  157. if (!active_)
  158. activated_ = true;
  159. }
  160. else
  161. {
  162. if (active_)
  163. MakeInactive();
  164. }
  165. #endif
  166. // Activate input now if necessary
  167. if (activated_)
  168. MakeActive();
  169. // Finally send the mouse move event if motion has been accumulated
  170. if (active_)
  171. {
  172. IntVector2 mousePos = GetCursorPosition();
  173. mouseMove_ = mousePos - lastCursorPosition_;
  174. // Recenter the mouse cursor manually if it moved
  175. if (mouseMove_ != IntVector2::ZERO)
  176. {
  177. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  178. SetCursorPosition(center);
  179. lastCursorPosition_ = center;
  180. }
  181. else
  182. lastCursorPosition_ = mousePos;
  183. if (mouseMove_ != IntVector2::ZERO && suppressNextMouseMove_)
  184. {
  185. mouseMove_ = IntVector2::ZERO;
  186. suppressNextMouseMove_ = false;
  187. }
  188. if (mouseMove_ != IntVector2::ZERO)
  189. {
  190. using namespace MouseMove;
  191. VariantMap eventData;
  192. eventData[P_DX] = mouseMove_.x_;
  193. eventData[P_DY] = mouseMove_.y_;
  194. eventData[P_BUTTONS] = mouseButtonDown_;
  195. eventData[P_QUALIFIERS] = GetQualifiers();
  196. SendEvent(E_MOUSEMOVE, eventData);
  197. }
  198. }
  199. }
  200. void Input::SetToggleFullscreen(bool enable)
  201. {
  202. toggleFullscreen_ = enable;
  203. }
  204. bool Input::GetKeyDown(int key) const
  205. {
  206. return keyDown_.Contains(key);
  207. }
  208. bool Input::GetKeyPress(int key) const
  209. {
  210. return keyPress_.Contains(key);
  211. }
  212. bool Input::GetMouseButtonDown(int button) const
  213. {
  214. return (mouseButtonDown_ & button) != 0;
  215. }
  216. bool Input::GetMouseButtonPress(int button) const
  217. {
  218. return (mouseButtonPress_ & button) != 0;
  219. }
  220. bool Input::GetQualifierDown(int qualifier) const
  221. {
  222. if (qualifier == QUAL_SHIFT)
  223. return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
  224. if (qualifier == QUAL_CTRL)
  225. return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
  226. if (qualifier == QUAL_ALT)
  227. return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
  228. return false;
  229. }
  230. bool Input::GetQualifierPress(int qualifier) const
  231. {
  232. if (qualifier == QUAL_SHIFT)
  233. return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
  234. if (qualifier == QUAL_CTRL)
  235. return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
  236. if (qualifier == QUAL_ALT)
  237. return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
  238. return false;
  239. }
  240. int Input::GetQualifiers() const
  241. {
  242. int ret = 0;
  243. if (GetQualifierDown(QUAL_SHIFT))
  244. ret |= QUAL_SHIFT;
  245. if (GetQualifierDown(QUAL_CTRL))
  246. ret |= QUAL_CTRL;
  247. if (GetQualifierDown(QUAL_ALT))
  248. ret |= QUAL_ALT;
  249. return ret;
  250. }
  251. void Input::Initialize()
  252. {
  253. Graphics* graphics = GetSubsystem<Graphics>();
  254. if (!graphics || !graphics->IsInitialized())
  255. return;
  256. graphics_ = graphics;
  257. // Set the initial activation
  258. activated_ = true;
  259. initialized_ = true;
  260. #ifdef USE_OPENGL
  261. {
  262. MutexLock lock(GetStaticMutex());
  263. // Store window ID to direct SDL events to the correct instance
  264. windowID_ = SDL_GetWindowID(graphics_->GetImpl()->GetWindow());
  265. inputInstances[windowID_] = this;
  266. }
  267. #endif
  268. LOGINFO("Initialized input");
  269. }
  270. void Input::MakeActive()
  271. {
  272. if (!graphics_ || !graphics_->IsInitialized())
  273. return;
  274. ResetState();
  275. active_ = true;
  276. activated_ = false;
  277. // Re-establish mouse cursor clipping as necessary
  278. #ifdef USE_OPENGL
  279. SDL_ShowCursor(SDL_FALSE);
  280. suppressNextMouseMove_ = true;
  281. #else
  282. SetClipCursor(true);
  283. SetCursorVisible(false);
  284. #endif
  285. using namespace Activation;
  286. VariantMap eventData;
  287. eventData[P_ACTIVE] = active_;
  288. eventData[P_MINIMIZED] = minimized_;
  289. SendEvent(E_ACTIVATION, eventData);
  290. }
  291. void Input::MakeInactive()
  292. {
  293. if (!graphics_ || !graphics_->IsInitialized())
  294. return;
  295. ResetState();
  296. active_ = false;
  297. activated_ = false;
  298. // Free and show the mouse cursor
  299. #ifndef USE_OPENGL
  300. ReleaseCapture();
  301. ClipCursor(0);
  302. SetCursorVisible(true);
  303. #else
  304. SDL_ShowCursor(SDL_TRUE);
  305. #endif
  306. using namespace Activation;
  307. VariantMap eventData;
  308. eventData[P_ACTIVE] = active_;
  309. eventData[P_MINIMIZED] = minimized_;
  310. SendEvent(E_ACTIVATION, eventData);
  311. }
  312. void Input::ResetState()
  313. {
  314. keyDown_.Clear();
  315. keyPress_.Clear();
  316. // Use SetMouseButton() to reset the state so that mouse events will be sent properly
  317. SetMouseButton(MOUSEB_LEFT, false);
  318. SetMouseButton(MOUSEB_RIGHT, false);
  319. SetMouseButton(MOUSEB_MIDDLE, false);
  320. mouseMove_ = IntVector2::ZERO;
  321. mouseMoveWheel_ = 0;
  322. mouseButtonPress_ = 0;
  323. }
  324. void Input::SetMouseButton(int button, bool newState)
  325. {
  326. // If we are not active yet, do not react to the mouse button down
  327. if (newState && !active_)
  328. return;
  329. if (newState)
  330. {
  331. if (!(mouseButtonDown_ & button))
  332. mouseButtonPress_ |= button;
  333. mouseButtonDown_ |= button;
  334. }
  335. else
  336. {
  337. if (!(mouseButtonDown_ & button))
  338. return;
  339. mouseButtonDown_ &= ~button;
  340. }
  341. using namespace MouseButtonDown;
  342. VariantMap eventData;
  343. eventData[P_BUTTON] = button;
  344. eventData[P_BUTTONS] = mouseButtonDown_;
  345. eventData[P_QUALIFIERS] = GetQualifiers();
  346. SendEvent(newState ? E_MOUSEBUTTONDOWN : E_MOUSEBUTTONUP, eventData);
  347. }
  348. void Input::SetKey(int key, bool newState)
  349. {
  350. // If we are not active yet, do not react to the key down
  351. if (newState && !active_)
  352. return;
  353. bool repeat = false;
  354. if (newState)
  355. {
  356. if (!keyDown_.Contains(key))
  357. {
  358. keyDown_.Insert(key);
  359. keyPress_.Insert(key);
  360. }
  361. else
  362. repeat = true;
  363. }
  364. else
  365. {
  366. if (!keyDown_.Erase(key))
  367. return;
  368. }
  369. using namespace KeyDown;
  370. VariantMap eventData;
  371. eventData[P_KEY] = key;
  372. eventData[P_BUTTONS] = mouseButtonDown_;
  373. eventData[P_QUALIFIERS] = GetQualifiers();
  374. if (newState)
  375. eventData[P_REPEAT] = repeat;
  376. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  377. if (key == KEY_RETURN && newState && !repeat && toggleFullscreen_ && (GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT)))
  378. graphics_->ToggleFullscreen();
  379. }
  380. void Input::SetMouseWheel(int delta)
  381. {
  382. // If we are not active yet, do not react to the wheel
  383. if (!active_)
  384. return;
  385. if (delta)
  386. {
  387. mouseMoveWheel_ += delta;
  388. using namespace MouseWheel;
  389. VariantMap eventData;
  390. eventData[P_WHEEL] = delta;
  391. eventData[P_BUTTONS] = mouseButtonDown_;
  392. eventData[P_QUALIFIERS] = GetQualifiers();
  393. SendEvent(E_MOUSEWHEEL, eventData);
  394. }
  395. }
  396. void Input::SetCursorPosition(const IntVector2& position)
  397. {
  398. if (!graphics_)
  399. return;
  400. #ifdef USE_OPENGL
  401. SDL_WarpMouseInWindow(graphics_->GetImpl()->GetWindow(), position.x_, position.y_);
  402. #else
  403. POINT point;
  404. point.x = position.x_;
  405. point.y = position.y_;
  406. ClientToScreen((HWND)graphics_->GetWindowHandle(), &point);
  407. SetCursorPos(point.x, point.y);
  408. #endif
  409. }
  410. IntVector2 Input::GetCursorPosition() const
  411. {
  412. IntVector2 ret = lastCursorPosition_;
  413. if (!graphics_ || !graphics_->IsInitialized())
  414. return ret;
  415. #ifdef USE_OPENGL
  416. SDL_GetMouseState(&ret.x_, &ret.y_);
  417. #else
  418. POINT mouse;
  419. GetCursorPos(&mouse);
  420. ScreenToClient((HWND)graphics_->GetWindowHandle(), &mouse);
  421. ret.x_ = mouse.x;
  422. ret.y_ = mouse.y;
  423. #endif
  424. return ret;
  425. }
  426. #ifndef USE_OPENGL
  427. void Input::SetClipCursor(bool enable)
  428. {
  429. if (!graphics_)
  430. return;
  431. if (!graphics_->GetFullscreen() && active_ && enable)
  432. {
  433. SetCursorPosition(IntVector2(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2));
  434. lastCursorPosition_ = GetCursorPosition();
  435. RECT clipRect;
  436. GetWindowRect((HWND)graphics_->GetWindowHandle(), &clipRect);
  437. ClipCursor(&clipRect);
  438. }
  439. else
  440. {
  441. if (graphics_->GetFullscreen() && active_ && enable)
  442. {
  443. SetCursorPosition(IntVector2(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2));
  444. lastCursorPosition_ = GetCursorPosition();
  445. }
  446. ClipCursor(0);
  447. }
  448. }
  449. void Input::SetCursorVisible(bool enable)
  450. {
  451. if (!graphics_)
  452. return;
  453. // When inactive, always show the cursor
  454. if (!active_)
  455. enable = true;
  456. if (showCursor_ == enable)
  457. return;
  458. ShowCursor(enable ? TRUE : FALSE);
  459. showCursor_ = enable;
  460. }
  461. void Input::HandleWindowMessage(StringHash eventType, VariantMap& eventData)
  462. {
  463. if (!initialized_)
  464. Initialize();
  465. using namespace WindowMessage;
  466. int msg = eventData[P_MSG].GetInt();
  467. int wParam = eventData[P_WPARAM].GetInt();
  468. int lParam = eventData[P_LPARAM].GetInt();
  469. int keyCode;
  470. switch (msg)
  471. {
  472. case WM_LBUTTONDOWN:
  473. SetMouseButton(MOUSEB_LEFT, true);
  474. eventData[P_HANDLED] = true;
  475. break;
  476. case WM_NCLBUTTONUP:
  477. case WM_LBUTTONUP:
  478. SetMouseButton(MOUSEB_LEFT, false);
  479. eventData[P_HANDLED] = true;
  480. break;
  481. case WM_RBUTTONDOWN:
  482. SetMouseButton(MOUSEB_RIGHT, true);
  483. eventData[P_HANDLED] = true;
  484. break;
  485. case WM_NCRBUTTONUP:
  486. case WM_RBUTTONUP:
  487. SetMouseButton(MOUSEB_RIGHT, false);
  488. eventData[P_HANDLED] = true;
  489. break;
  490. case WM_MBUTTONDOWN:
  491. SetMouseButton(MOUSEB_MIDDLE, true);
  492. eventData[P_HANDLED] = true;
  493. break;
  494. case WM_NCMBUTTONUP:
  495. case WM_MBUTTONUP:
  496. SetMouseButton(MOUSEB_MIDDLE, false);
  497. eventData[P_HANDLED] = true;
  498. break;
  499. case WM_MOUSEWHEEL:
  500. SetMouseWheel(wParam >> 16);
  501. eventData[P_HANDLED] = true;
  502. break;
  503. case WM_ACTIVATE:
  504. minimized_ = HIWORD(wParam) != 0;
  505. if (LOWORD(wParam) == WA_INACTIVE)
  506. MakeInactive();
  507. else
  508. {
  509. if (!minimized_)
  510. activated_ = true;
  511. }
  512. eventData[P_HANDLED] = true;
  513. break;
  514. case WM_KEYDOWN:
  515. keyCode = ConvertKeyCode(wParam, lParam);
  516. if (keyCode)
  517. SetKey(keyCode, true);
  518. eventData[P_HANDLED] = true;
  519. break;
  520. case WM_SYSKEYDOWN:
  521. keyCode = ConvertKeyCode(wParam, lParam);
  522. if (keyCode)
  523. SetKey(keyCode, true);
  524. if (keyCode != KEY_F4)
  525. eventData[P_HANDLED] = true;
  526. break;
  527. case WM_KEYUP:
  528. case WM_SYSKEYUP:
  529. keyCode = ConvertKeyCode(wParam, lParam);
  530. if (keyCode)
  531. SetKey(keyCode, false);
  532. eventData[P_HANDLED] = true;
  533. break;
  534. case WM_CHAR:
  535. {
  536. using namespace Char;
  537. VariantMap keyEventData;
  538. keyEventData[P_CHAR] = wParam;
  539. keyEventData[P_BUTTONS] = mouseButtonDown_;
  540. keyEventData[P_QUALIFIERS] = GetQualifiers();
  541. SendEvent(E_CHAR, keyEventData);
  542. }
  543. eventData[P_HANDLED] = true;
  544. break;
  545. }
  546. }
  547. #else
  548. void Input::HandleSDLEvent(void* sdlEvent)
  549. {
  550. SDL_Event& evt = *static_cast<SDL_Event*>(sdlEvent);
  551. Input* input = 0;
  552. switch (evt.type)
  553. {
  554. case SDL_KEYDOWN:
  555. // Convert to uppercase to match Win32 virtual key codes
  556. input = GetInputInstance(evt.key.windowID);
  557. if (input)
  558. input->SetKey(SDL_toupper(evt.key.keysym.sym), true);
  559. break;
  560. case SDL_KEYUP:
  561. input = GetInputInstance(evt.key.windowID);
  562. if (input)
  563. input->SetKey(SDL_toupper(evt.key.keysym.sym), false);
  564. break;
  565. case SDL_TEXTINPUT:
  566. input = GetInputInstance(evt.text.windowID);
  567. if (input && evt.text.text[0])
  568. {
  569. String text(&evt.text.text[0]);
  570. unsigned unicode = text.AtUTF8(0);
  571. if (unicode)
  572. {
  573. using namespace Char;
  574. VariantMap keyEventData;
  575. keyEventData[P_CHAR] = unicode;
  576. keyEventData[P_BUTTONS] = input->mouseButtonDown_;
  577. keyEventData[P_QUALIFIERS] = input->GetQualifiers();
  578. input->SendEvent(E_CHAR, keyEventData);
  579. }
  580. }
  581. break;
  582. case SDL_MOUSEBUTTONDOWN:
  583. input = GetInputInstance(evt.button.windowID);
  584. if (input)
  585. input->SetMouseButton(1 << (evt.button.button - 1), true);
  586. break;
  587. case SDL_MOUSEBUTTONUP:
  588. input = GetInputInstance(evt.button.windowID);
  589. if (input)
  590. input->SetMouseButton(1 << (evt.button.button - 1), false);
  591. break;
  592. case SDL_MOUSEWHEEL:
  593. input = GetInputInstance(evt.wheel.windowID);
  594. if (input)
  595. input->SetMouseWheel(evt.wheel.y);
  596. break;
  597. case SDL_WINDOWEVENT:
  598. if (evt.window.event == SDL_WINDOWEVENT_CLOSE)
  599. {
  600. input = GetInputInstance(evt.window.windowID);
  601. if (input)
  602. input->GetSubsystem<Graphics>()->Close();
  603. }
  604. break;
  605. }
  606. }
  607. #endif
  608. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  609. {
  610. // Reset input state on subsequent initializations
  611. if (!initialized_)
  612. Initialize();
  613. else
  614. ResetState();
  615. // Re-enable cursor clipping, and re-center the cursor (if needed) to the new screen size, so that there is no erroneous
  616. // mouse move event
  617. #ifdef USE_OPENGL
  618. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  619. SetCursorPosition(center);
  620. lastCursorPosition_ = center;
  621. activated_ = true;
  622. #else
  623. SetClipCursor(true);
  624. #endif
  625. }
  626. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  627. {
  628. // Update input right at the beginning of the frame
  629. if (initialized_)
  630. Update();
  631. }