Input.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "Profiler.h"
  30. #include <cstring>
  31. #ifndef USE_OPENGL
  32. #include <Windows.h>
  33. #else
  34. #include <GL/glfw.h>
  35. #endif
  36. #include "DebugNew.h"
  37. #ifndef USE_OPENGL
  38. /// Convert the virtual key code & scan code if necessary. Return non-zero if key should be posted
  39. int ConvertKeyCode(unsigned wParam, unsigned lParam)
  40. {
  41. unsigned scanCode = (lParam >> 16) & 0x1ff;
  42. // Recognize left/right qualifier key from the scan code
  43. switch (wParam)
  44. {
  45. case VK_SHIFT:
  46. if (scanCode == 54)
  47. return KEY_RSHIFT;
  48. else
  49. return KEY_LSHIFT;
  50. break;
  51. case VK_CONTROL:
  52. // Control might not be a real key, as Windows posts it whenever Alt-Gr is pressed (inspired by GLFW)
  53. {
  54. MSG nextMsg;
  55. DWORD msgTime = GetMessageTime();
  56. if (PeekMessage(&nextMsg, NULL, 0, 0, PM_NOREMOVE))
  57. {
  58. if ((nextMsg.message == WM_KEYDOWN || nextMsg.message == WM_SYSKEYDOWN) && nextMsg.wParam == VK_MENU &&
  59. (nextMsg.lParam & 0x01000000) != 0 && nextMsg.time == msgTime)
  60. return 0;
  61. }
  62. if (scanCode & 0x100)
  63. return KEY_RCTRL;
  64. else
  65. return KEY_LCTRL;
  66. }
  67. break;
  68. case VK_MENU:
  69. if (scanCode & 0x100)
  70. return KEY_RALT;
  71. else
  72. return KEY_LALT;
  73. default:
  74. return wParam;
  75. }
  76. }
  77. #else
  78. static Input* inputInstance = 0;
  79. #endif
  80. OBJECTTYPESTATIC(Input);
  81. Input::Input(Context* context) :
  82. Object(context),
  83. showCursor_(true),
  84. toggleFullscreen_(true),
  85. active_(false),
  86. minimized_(false),
  87. activated_(false),
  88. suppressNextChar_(false),
  89. initialized_(false)
  90. {
  91. // Zero the initial state
  92. mouseButtonDown_ = 0;
  93. ResetState();
  94. #ifndef USE_OPENGL
  95. SubscribeToEvent(E_WINDOWMESSAGE, HANDLER(Input, HandleWindowMessage));
  96. #else
  97. // GLFW callbacks do not include userdata, so a static instance pointer is necessary
  98. inputInstance = this;
  99. #endif
  100. SubscribeToEvent(E_SCREENMODE, HANDLER(Input, HandleScreenMode));
  101. SubscribeToEvent(E_BEGINFRAME, HANDLER(Input, HandleBeginFrame));
  102. // Try to initialize right now, but skip if screen mode is not yet set
  103. Initialize();
  104. }
  105. Input::~Input()
  106. {
  107. }
  108. void Input::Update()
  109. {
  110. PROFILE(UpdateInput);
  111. if (!graphics_)
  112. return;
  113. // Reset input accumulation for this frame
  114. keyPress_.Clear();
  115. mouseButtonPress_ = 0;
  116. mouseMove_ = IntVector2::ZERO;
  117. mouseMoveWheel_ = 0;
  118. #ifndef USE_OPENGL
  119. // Pump Win32 events
  120. MSG msg;
  121. while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  122. {
  123. TranslateMessage(&msg);
  124. DispatchMessage(&msg);
  125. }
  126. #else
  127. // Pump GLFW events
  128. glfwPollEvents();
  129. // Check state
  130. if (glfwGetWindowParam(GLFW_ACTIVE))
  131. {
  132. if (!active_)
  133. activated_ = true;
  134. }
  135. else
  136. {
  137. if (active_)
  138. MakeInactive();
  139. }
  140. #endif
  141. // Activate application now if necessary
  142. if (activated_)
  143. MakeActive();
  144. // Finally send the mouse move event if motion has been accumulated
  145. if (active_)
  146. {
  147. #ifndef USE_OPENGL
  148. IntVector2 mousePos = GetCursorPosition();
  149. mouseMove_ = mousePos - lastCursorPosition_;
  150. // Recenter the mouse cursor manually if it moved
  151. if (mouseMove_ != IntVector2::ZERO)
  152. {
  153. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  154. SetCursorPosition(center);
  155. lastCursorPosition_ = GetCursorPosition();
  156. }
  157. else
  158. lastCursorPosition_ = mousePos;
  159. #else
  160. IntVector2 mousePos = GetCursorPosition();
  161. mouseMove_ = mousePos - lastCursorPosition_;
  162. lastCursorPosition_ = mousePos;
  163. #endif
  164. if (mouseMove_ != IntVector2::ZERO)
  165. {
  166. using namespace MouseMove;
  167. VariantMap eventData;
  168. eventData[P_DX] = mouseMove_.x_;
  169. eventData[P_DY] = mouseMove_.y_;
  170. eventData[P_BUTTONS] = mouseButtonDown_;
  171. eventData[P_QUALIFIERS] = GetQualifiers();
  172. SendEvent(E_MOUSEMOVE, eventData);
  173. }
  174. }
  175. }
  176. void Input::SetToggleFullscreen(bool enable)
  177. {
  178. toggleFullscreen_ = enable;
  179. }
  180. void Input::SuppressNextChar()
  181. {
  182. suppressNextChar_ = true;
  183. }
  184. bool Input::GetKeyDown(int key) const
  185. {
  186. return keyDown_.Contains(key);
  187. }
  188. bool Input::GetKeyPress(int key) const
  189. {
  190. return keyPress_.Contains(key);
  191. }
  192. bool Input::GetMouseButtonDown(int button) const
  193. {
  194. return (mouseButtonDown_ & button) != 0;
  195. }
  196. bool Input::GetMouseButtonPress(int button) const
  197. {
  198. return (mouseButtonPress_ & button) != 0;
  199. }
  200. bool Input::GetQualifierDown(int qualifier) const
  201. {
  202. if (qualifier == QUAL_SHIFT)
  203. return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
  204. if (qualifier == QUAL_CTRL)
  205. return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
  206. if (qualifier == QUAL_ALT)
  207. return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
  208. return false;
  209. }
  210. bool Input::GetQualifierPress(int qualifier) const
  211. {
  212. if (qualifier == QUAL_SHIFT)
  213. return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
  214. if (qualifier == QUAL_CTRL)
  215. return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
  216. if (qualifier == QUAL_ALT)
  217. return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
  218. return false;
  219. }
  220. int Input::GetQualifiers() const
  221. {
  222. int ret = 0;
  223. if (GetQualifierDown(QUAL_SHIFT))
  224. ret |= QUAL_SHIFT;
  225. if (GetQualifierDown(QUAL_CTRL))
  226. ret |= QUAL_CTRL;
  227. if (GetQualifierDown(QUAL_ALT))
  228. ret |= QUAL_ALT;
  229. return ret;
  230. }
  231. void Input::Initialize()
  232. {
  233. Graphics* graphics = GetSubsystem<Graphics>();
  234. if (!graphics || !graphics->IsInitialized())
  235. return;
  236. graphics_ = graphics;
  237. // Set the initial activation
  238. MakeActive();
  239. initialized_ = true;
  240. LOGINFO("Initialized input");
  241. }
  242. void Input::MakeActive()
  243. {
  244. if (!graphics_)
  245. return;
  246. ResetState();
  247. active_ = true;
  248. activated_ = false;
  249. // Re-establish mouse cursor clipping as necessary
  250. #ifndef USE_OPENGL
  251. SetClipCursor(true);
  252. SetCursorVisible(false);
  253. #else
  254. // Get the current mouse position as a base for movement calculations
  255. lastCursorPosition_ = GetCursorPosition();
  256. lastWheelPosition_ = glfwGetMouseWheel();
  257. #endif
  258. using namespace Activation;
  259. VariantMap eventData;
  260. eventData[P_ACTIVE] = active_;
  261. eventData[P_MINIMIZED] = minimized_;
  262. SendEvent(E_ACTIVATION, eventData);
  263. }
  264. void Input::MakeInactive()
  265. {
  266. if (!graphics_)
  267. return;
  268. ResetState();
  269. active_ = false;
  270. activated_ = false;
  271. // Free and show the mouse cursor
  272. #ifndef USE_OPENGL
  273. ReleaseCapture();
  274. ClipCursor(0);
  275. SetCursorVisible(true);
  276. #endif
  277. using namespace Activation;
  278. VariantMap eventData;
  279. eventData[P_ACTIVE] = active_;
  280. eventData[P_MINIMIZED] = minimized_;
  281. SendEvent(E_ACTIVATION, eventData);
  282. }
  283. void Input::ResetState()
  284. {
  285. keyDown_.Clear();
  286. keyPress_.Clear();
  287. // Use SetMouseButton() to reset the state so that mouse events will be sent properly
  288. SetMouseButton(MOUSEB_LEFT, false);
  289. SetMouseButton(MOUSEB_RIGHT, false);
  290. SetMouseButton(MOUSEB_MIDDLE, false);
  291. mouseMove_ = IntVector2::ZERO;
  292. mouseMoveWheel_ = 0;
  293. mouseButtonPress_ = 0;
  294. }
  295. void Input::SetMouseButton(int button, bool newState)
  296. {
  297. // If we are not active yet, do not react to the mouse button down
  298. if (newState && !active_)
  299. return;
  300. if (newState)
  301. {
  302. if (!(mouseButtonDown_ & button))
  303. mouseButtonPress_ |= button;
  304. mouseButtonDown_ |= button;
  305. }
  306. else
  307. {
  308. if (!(mouseButtonDown_ & button))
  309. return;
  310. mouseButtonDown_ &= ~button;
  311. }
  312. using namespace MouseButtonDown;
  313. VariantMap eventData;
  314. eventData[P_BUTTON] = button;
  315. eventData[P_BUTTONS] = mouseButtonDown_;
  316. eventData[P_QUALIFIERS] = GetQualifiers();
  317. SendEvent(newState ? E_MOUSEBUTTONDOWN : E_MOUSEBUTTONUP, eventData);
  318. }
  319. void Input::SetKey(int key, bool newState)
  320. {
  321. // If we are not active yet, do not react to the key down
  322. if (newState && !active_)
  323. return;
  324. bool repeat = false;
  325. if (newState)
  326. {
  327. if (!keyDown_.Contains(key))
  328. {
  329. keyDown_.Insert(key);
  330. keyPress_.Insert(key);
  331. }
  332. else
  333. repeat = true;
  334. }
  335. else
  336. {
  337. if (!keyDown_.Erase(key))
  338. return;
  339. }
  340. using namespace KeyDown;
  341. VariantMap eventData;
  342. eventData[P_KEY] = key;
  343. eventData[P_BUTTONS] = mouseButtonDown_;
  344. eventData[P_QUALIFIERS] = GetQualifiers();
  345. if (newState)
  346. eventData[P_REPEAT] = repeat;
  347. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  348. if (key == KEY_RETURN && newState && !repeat && toggleFullscreen_ && (GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT)))
  349. {
  350. graphics_->ToggleFullscreen();
  351. ResetState();
  352. }
  353. }
  354. void Input::SetMouseWheel(int delta)
  355. {
  356. // If we are not active yet, do not react to the wheel
  357. if (!active_)
  358. return;
  359. if (delta)
  360. {
  361. mouseMoveWheel_ += delta;
  362. using namespace MouseWheel;
  363. VariantMap eventData;
  364. eventData[P_WHEEL] = delta;
  365. eventData[P_BUTTONS] = mouseButtonDown_;
  366. eventData[P_QUALIFIERS] = GetQualifiers();
  367. SendEvent(E_MOUSEWHEEL, eventData);
  368. }
  369. }
  370. #ifndef USE_OPENGL
  371. void Input::SetClipCursor(bool enable)
  372. {
  373. if (!graphics_)
  374. return;
  375. if (!graphics_->GetFullscreen() && active_ && enable)
  376. {
  377. SetCursorPosition(IntVector2(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2));
  378. lastCursorPosition_ = GetCursorPosition();
  379. RECT clipRect;
  380. GetWindowRect((HWND)graphics_->GetWindowHandle(), &clipRect);
  381. ClipCursor(&clipRect);
  382. }
  383. else
  384. {
  385. if (graphics_->GetFullscreen() && active_ && enable)
  386. {
  387. SetCursorPosition(IntVector2(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2));
  388. lastCursorPosition_ = GetCursorPosition();
  389. }
  390. ClipCursor(0);
  391. }
  392. }
  393. void Input::SetCursorPosition(const IntVector2& position)
  394. {
  395. if (!graphics_)
  396. return;
  397. POINT point;
  398. point.x = position.x_;
  399. point.y = position.y_;
  400. ClientToScreen((HWND)graphics_->GetWindowHandle(), &point);
  401. SetCursorPos(point.x, point.y);
  402. }
  403. void Input::SetCursorVisible(bool enable)
  404. {
  405. if (!graphics_)
  406. return;
  407. // When inactive, always show the cursor
  408. if (!active_)
  409. enable = true;
  410. if (showCursor_ == enable)
  411. return;
  412. ShowCursor(enable ? TRUE : FALSE);
  413. showCursor_ = enable;
  414. }
  415. void Input::HandleWindowMessage(StringHash eventType, VariantMap& eventData)
  416. {
  417. using namespace WindowMessage;
  418. int msg = eventData[P_MSG].GetInt();
  419. int wParam = eventData[P_WPARAM].GetInt();
  420. int lParam = eventData[P_LPARAM].GetInt();
  421. int keyCode;
  422. switch (msg)
  423. {
  424. case WM_LBUTTONDOWN:
  425. SetMouseButton(MOUSEB_LEFT, true);
  426. eventData[P_HANDLED] = true;
  427. break;
  428. case WM_NCLBUTTONUP:
  429. case WM_LBUTTONUP:
  430. SetMouseButton(MOUSEB_LEFT, false);
  431. eventData[P_HANDLED] = true;
  432. break;
  433. case WM_RBUTTONDOWN:
  434. SetMouseButton(MOUSEB_RIGHT, true);
  435. eventData[P_HANDLED] = true;
  436. break;
  437. case WM_NCRBUTTONUP:
  438. case WM_RBUTTONUP:
  439. SetMouseButton(MOUSEB_RIGHT, false);
  440. eventData[P_HANDLED] = true;
  441. break;
  442. case WM_MBUTTONDOWN:
  443. SetMouseButton(MOUSEB_MIDDLE, true);
  444. eventData[P_HANDLED] = true;
  445. break;
  446. case WM_NCMBUTTONUP:
  447. case WM_MBUTTONUP:
  448. SetMouseButton(MOUSEB_MIDDLE, false);
  449. eventData[P_HANDLED] = true;
  450. break;
  451. case WM_MOUSEWHEEL:
  452. SetMouseWheel(wParam >> 16);
  453. eventData[P_HANDLED] = true;
  454. break;
  455. case WM_ACTIVATE:
  456. minimized_ = HIWORD(wParam) != 0;
  457. if (LOWORD(wParam) == WA_INACTIVE)
  458. MakeInactive();
  459. else
  460. {
  461. if (!minimized_)
  462. activated_ = true;
  463. }
  464. eventData[P_HANDLED] = true;
  465. break;
  466. case WM_KEYDOWN:
  467. keyCode = ConvertKeyCode(wParam, lParam);
  468. if (keyCode)
  469. SetKey(keyCode, true);
  470. eventData[P_HANDLED] = true;
  471. break;
  472. case WM_SYSKEYDOWN:
  473. keyCode = ConvertKeyCode(wParam, lParam);
  474. if (keyCode)
  475. SetKey(keyCode, true);
  476. if (keyCode != KEY_F4)
  477. eventData[P_HANDLED] = true;
  478. break;
  479. case WM_KEYUP:
  480. case WM_SYSKEYUP:
  481. keyCode = ConvertKeyCode(wParam, lParam);
  482. if (keyCode)
  483. SetKey(keyCode, false);
  484. eventData[P_HANDLED] = true;
  485. break;
  486. case WM_CHAR:
  487. if (!suppressNextChar_)
  488. {
  489. using namespace Char;
  490. VariantMap keyEventData;
  491. keyEventData[P_CHAR] = wParam;
  492. keyEventData[P_BUTTONS] = mouseButtonDown_;
  493. keyEventData[P_QUALIFIERS] = GetQualifiers();
  494. SendEvent(E_CHAR, keyEventData);
  495. }
  496. suppressNextChar_ = false;
  497. eventData[P_HANDLED] = true;
  498. break;
  499. }
  500. }
  501. #else
  502. void KeyCallback(int key, int action)
  503. {
  504. inputInstance->SetKey(key, action & GLFW_PRESS);
  505. }
  506. void CharCallback(int key, int action)
  507. {
  508. if (key < 256 && action == GLFW_PRESS)
  509. {
  510. using namespace Char;
  511. VariantMap keyEventData;
  512. keyEventData[P_CHAR] = key;
  513. keyEventData[P_BUTTONS] = inputInstance->mouseButtonDown_;
  514. keyEventData[P_QUALIFIERS] = inputInstance->GetQualifiers();
  515. inputInstance->SendEvent(E_CHAR, keyEventData);
  516. }
  517. }
  518. void MouseButtonCallback(int button, int action)
  519. {
  520. inputInstance->SetMouseButton(1 << button, action == GLFW_PRESS);
  521. }
  522. void MouseWheelCallback(int wheel)
  523. {
  524. inputInstance->SetMouseWheel(wheel - inputInstance->lastWheelPosition_);
  525. inputInstance->lastWheelPosition_ = wheel;
  526. }
  527. #endif
  528. IntVector2 Input::GetCursorPosition() const
  529. {
  530. IntVector2 ret(0, 0);
  531. if (!graphics_)
  532. return ret;
  533. #ifndef USE_OPENGL
  534. POINT mouse;
  535. GetCursorPos(&mouse);
  536. ScreenToClient((HWND)graphics_->GetWindowHandle(), &mouse);
  537. ret.x_ = mouse.x;
  538. ret.y_ = mouse.y;
  539. #else
  540. glfwGetMousePos(&ret.x_, &ret.y_);
  541. #endif
  542. return ret;
  543. }
  544. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  545. {
  546. if (!initialized_)
  547. Initialize();
  548. // Screen mode change may affect the cursor clipping behaviour. Also re-center the cursor (if needed) to the new screen size,
  549. // so that there is no erroneous mouse move event
  550. #ifndef USE_OPENGL
  551. SetClipCursor(true);
  552. #else
  553. // Re-enable GLFW callbacks each time the window has been recreated
  554. glfwDisable(GLFW_MOUSE_CURSOR);
  555. glfwEnable(GLFW_KEY_REPEAT);
  556. glfwSetKeyCallback(&KeyCallback);
  557. glfwSetCharCallback(&CharCallback);
  558. glfwSetMouseButtonCallback(&MouseButtonCallback);
  559. glfwSetMouseWheelCallback(&MouseWheelCallback);
  560. lastCursorPosition_ = GetCursorPosition();
  561. lastWheelPosition_ = glfwGetMouseWheel();
  562. #endif
  563. }
  564. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  565. {
  566. // Update input right at the beginning of the frame
  567. if (initialized_)
  568. Update();
  569. }