Input.cpp 17 KB

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