2
0

Input.cpp 18 KB

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