Input.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. if (!keyDown_.Contains(key))
  327. {
  328. keyDown_.Insert(key);
  329. keyPress_.Insert(key);
  330. }
  331. else
  332. repeat = true;
  333. }
  334. else
  335. {
  336. if (!keyDown_.Erase(key))
  337. return;
  338. }
  339. using namespace KeyDown;
  340. VariantMap eventData;
  341. eventData[P_KEY] = key;
  342. eventData[P_BUTTONS] = mouseButtonDown_;
  343. eventData[P_QUALIFIERS] = GetQualifiers();
  344. if (newState)
  345. eventData[P_REPEAT] = repeat;
  346. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  347. if (key == KEY_RETURN && newState && !repeat && toggleFullscreen_ && (GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT)))
  348. graphics_->ToggleFullscreen();
  349. }
  350. void Input::SetMouseWheel(int delta)
  351. {
  352. // If we are not active yet, do not react to the wheel
  353. if (!active_)
  354. return;
  355. if (delta)
  356. {
  357. mouseMoveWheel_ += delta;
  358. using namespace MouseWheel;
  359. VariantMap eventData;
  360. eventData[P_WHEEL] = delta;
  361. eventData[P_BUTTONS] = mouseButtonDown_;
  362. eventData[P_QUALIFIERS] = GetQualifiers();
  363. SendEvent(E_MOUSEWHEEL, eventData);
  364. }
  365. }
  366. #ifndef USE_OPENGL
  367. void Input::SetClipCursor(bool enable)
  368. {
  369. if (!graphics_)
  370. return;
  371. if (!graphics_->GetFullscreen() && active_ && enable)
  372. {
  373. SetCursorPosition(IntVector2(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2));
  374. lastCursorPosition_ = GetCursorPosition();
  375. RECT clipRect;
  376. GetWindowRect((HWND)graphics_->GetWindowHandle(), &clipRect);
  377. ClipCursor(&clipRect);
  378. }
  379. else
  380. {
  381. if (graphics_->GetFullscreen() && active_ && enable)
  382. {
  383. SetCursorPosition(IntVector2(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2));
  384. lastCursorPosition_ = GetCursorPosition();
  385. }
  386. ClipCursor(0);
  387. }
  388. }
  389. void Input::SetCursorPosition(const IntVector2& position)
  390. {
  391. if (!graphics_)
  392. return;
  393. POINT point;
  394. point.x = position.x_;
  395. point.y = position.y_;
  396. ClientToScreen((HWND)graphics_->GetWindowHandle(), &point);
  397. SetCursorPos(point.x, point.y);
  398. }
  399. void Input::SetCursorVisible(bool enable)
  400. {
  401. if (!graphics_)
  402. return;
  403. // When inactive, always show the cursor
  404. if (!active_)
  405. enable = true;
  406. if (showCursor_ == enable)
  407. return;
  408. ShowCursor(enable ? TRUE : FALSE);
  409. showCursor_ = enable;
  410. }
  411. void Input::HandleWindowMessage(StringHash eventType, VariantMap& eventData)
  412. {
  413. using namespace WindowMessage;
  414. int msg = eventData[P_MSG].GetInt();
  415. int wParam = eventData[P_WPARAM].GetInt();
  416. int lParam = eventData[P_LPARAM].GetInt();
  417. int keyCode;
  418. switch (msg)
  419. {
  420. case WM_LBUTTONDOWN:
  421. SetMouseButton(MOUSEB_LEFT, true);
  422. eventData[P_HANDLED] = true;
  423. break;
  424. case WM_NCLBUTTONUP:
  425. case WM_LBUTTONUP:
  426. SetMouseButton(MOUSEB_LEFT, false);
  427. eventData[P_HANDLED] = true;
  428. break;
  429. case WM_RBUTTONDOWN:
  430. SetMouseButton(MOUSEB_RIGHT, true);
  431. eventData[P_HANDLED] = true;
  432. break;
  433. case WM_NCRBUTTONUP:
  434. case WM_RBUTTONUP:
  435. SetMouseButton(MOUSEB_RIGHT, false);
  436. eventData[P_HANDLED] = true;
  437. break;
  438. case WM_MBUTTONDOWN:
  439. SetMouseButton(MOUSEB_MIDDLE, true);
  440. eventData[P_HANDLED] = true;
  441. break;
  442. case WM_NCMBUTTONUP:
  443. case WM_MBUTTONUP:
  444. SetMouseButton(MOUSEB_MIDDLE, false);
  445. eventData[P_HANDLED] = true;
  446. break;
  447. case WM_MOUSEWHEEL:
  448. SetMouseWheel(wParam >> 16);
  449. eventData[P_HANDLED] = true;
  450. break;
  451. case WM_ACTIVATE:
  452. minimized_ = HIWORD(wParam) != 0;
  453. if (LOWORD(wParam) == WA_INACTIVE)
  454. MakeInactive();
  455. else
  456. {
  457. if (!minimized_)
  458. activated_ = true;
  459. }
  460. eventData[P_HANDLED] = true;
  461. break;
  462. case WM_KEYDOWN:
  463. keyCode = ConvertKeyCode(wParam, lParam);
  464. if (keyCode)
  465. SetKey(keyCode, true);
  466. eventData[P_HANDLED] = true;
  467. break;
  468. case WM_SYSKEYDOWN:
  469. keyCode = ConvertKeyCode(wParam, lParam);
  470. if (keyCode)
  471. SetKey(keyCode, true);
  472. if (keyCode != KEY_F4)
  473. eventData[P_HANDLED] = true;
  474. break;
  475. case WM_KEYUP:
  476. case WM_SYSKEYUP:
  477. keyCode = ConvertKeyCode(wParam, lParam);
  478. if (keyCode)
  479. SetKey(keyCode, false);
  480. eventData[P_HANDLED] = true;
  481. break;
  482. case WM_CHAR:
  483. if (!suppressNextChar_)
  484. {
  485. using namespace Char;
  486. VariantMap keyEventData;
  487. keyEventData[P_CHAR] = wParam;
  488. keyEventData[P_BUTTONS] = mouseButtonDown_;
  489. keyEventData[P_QUALIFIERS] = GetQualifiers();
  490. SendEvent(E_CHAR, keyEventData);
  491. }
  492. suppressNextChar_ = false;
  493. eventData[P_HANDLED] = true;
  494. break;
  495. }
  496. }
  497. #else
  498. void KeyCallback(int key, int action)
  499. {
  500. inputInstance->SetKey(key, action & GLFW_PRESS);
  501. }
  502. void CharCallback(int key, int action)
  503. {
  504. if (key < 256 && action == GLFW_PRESS)
  505. {
  506. using namespace Char;
  507. VariantMap keyEventData;
  508. keyEventData[P_CHAR] = key;
  509. keyEventData[P_BUTTONS] = inputInstance->mouseButtonDown_;
  510. keyEventData[P_QUALIFIERS] = inputInstance->GetQualifiers();
  511. inputInstance->SendEvent(E_CHAR, keyEventData);
  512. }
  513. }
  514. void MouseButtonCallback(int button, int action)
  515. {
  516. inputInstance->SetMouseButton(1 << button, action == GLFW_PRESS);
  517. }
  518. void MouseWheelCallback(int wheel)
  519. {
  520. inputInstance->SetMouseWheel(wheel - inputInstance->lastWheelPosition_);
  521. inputInstance->lastWheelPosition_ = wheel;
  522. }
  523. #endif
  524. IntVector2 Input::GetCursorPosition() const
  525. {
  526. IntVector2 ret(0, 0);
  527. if (!graphics_)
  528. return ret;
  529. #ifndef USE_OPENGL
  530. POINT mouse;
  531. GetCursorPos(&mouse);
  532. ScreenToClient((HWND)graphics_->GetWindowHandle(), &mouse);
  533. ret.x_ = mouse.x;
  534. ret.y_ = mouse.y;
  535. #else
  536. glfwGetMousePos(&ret.x_, &ret.y_);
  537. #endif
  538. return ret;
  539. }
  540. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  541. {
  542. if (!initialized_)
  543. Initialize();
  544. // Screen mode change may affect the cursor clipping behaviour. Also re-center the cursor (if needed) to the new screen size,
  545. // so that there is no erroneous mouse move event
  546. #ifndef USE_OPENGL
  547. SetClipCursor(true);
  548. #else
  549. // Re-enable GLFW callbacks each time the window has been recreated
  550. glfwDisable(GLFW_MOUSE_CURSOR);
  551. glfwEnable(GLFW_KEY_REPEAT);
  552. glfwSetKeyCallback(&KeyCallback);
  553. glfwSetCharCallback(&CharCallback);
  554. glfwSetMouseButtonCallback(&MouseButtonCallback);
  555. glfwSetMouseWheelCallback(&MouseWheelCallback);
  556. lastCursorPosition_ = GetCursorPosition();
  557. lastWheelPosition_ = glfwGetMouseWheel();
  558. #endif
  559. }
  560. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  561. {
  562. // Update input right at the beginning of the frame
  563. if (initialized_)
  564. Update();
  565. }