Input.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 "Input.h"
  24. #include "Log.h"
  25. #include "Profiler.h"
  26. #include "Renderer.h"
  27. #include "RendererEvents.h"
  28. #include <cstring>
  29. #include <windows.h>
  30. #include "DebugNew.h"
  31. Input::Input(Renderer* renderer) :
  32. mRenderer(renderer),
  33. mClipCursor(true),
  34. mShowCursor(true),
  35. mToggleFullscreen(true),
  36. mActive(false),
  37. mMinimized(false),
  38. mActivated(true),
  39. mSuppressNextChar(false)
  40. {
  41. LOGINFO("Input created");
  42. // Zero the initial state so that clearState() does not send any extra events at the start
  43. memset(&mKeyDown, 0, sizeof(mKeyDown));
  44. memset(&mKeyPress, 0, sizeof(mKeyPress));
  45. mMouseButtonDown = 0;
  46. mMouseButtonPress = 0;
  47. mLastMousePosition = IntVector2::sZero;
  48. if (mRenderer)
  49. {
  50. subscribeToEvent(mRenderer, EVENT_WINDOWMESSAGE, EVENT_HANDLER(Input, handleWindowMessage));
  51. subscribeToEvent(mRenderer, EVENT_SCREENMODE, EVENT_HANDLER(Input, handleScreenMode));
  52. }
  53. // Perform the initial update immediately so that the mouse cursor gets hidden
  54. update();
  55. }
  56. Input::~Input()
  57. {
  58. LOGINFO("Input shut down");
  59. }
  60. void Input::update()
  61. {
  62. PROFILE(Input_Update);
  63. if (!mRenderer)
  64. return;
  65. memset(mKeyPress, 0, sizeof(mKeyPress));
  66. mMouseButtonPress = 0;
  67. mMouseMove = IntVector2::sZero;
  68. mMouseMoveWheel = 0;
  69. mRenderer->messagePump();
  70. if (mActivated)
  71. makeActive();
  72. checkMouseMove();
  73. }
  74. void Input::setClipCursor(bool enable)
  75. {
  76. mClipCursor = enable;
  77. if (!mRenderer)
  78. return;
  79. if ((!mRenderer->getFullscreen()) && (mActive) && (mClipCursor))
  80. {
  81. HWND window = (HWND)mRenderer->getWindowHandle();
  82. RECT clipRect;
  83. setMousePosition(mRenderer->getWidth() / 2, mRenderer->getHeight() / 2);
  84. mLastMousePosition = getMousePosition();
  85. GetWindowRect(window, &clipRect);
  86. ClipCursor(&clipRect);
  87. }
  88. else
  89. {
  90. if ((mRenderer->getFullscreen()) && (mActive) && (mClipCursor))
  91. {
  92. setMousePosition(mRenderer->getWidth() / 2, mRenderer->getHeight() / 2);
  93. mLastMousePosition = getMousePosition();
  94. }
  95. ClipCursor(0);
  96. }
  97. }
  98. void Input::setToggleFullscreen(bool enable)
  99. {
  100. mToggleFullscreen = enable;
  101. }
  102. void Input::setMousePosition(const IntVector2& position)
  103. {
  104. POINT point;
  105. point.x = position.mX;
  106. point.y = position.mY;
  107. ClientToScreen((HWND)mRenderer->getWindowHandle(), &point);
  108. SetCursorPos(point.x, point.y);
  109. }
  110. void Input::setMousePosition(int x, int y)
  111. {
  112. setMousePosition(IntVector2(x, y));
  113. }
  114. void Input::suppressNextChar()
  115. {
  116. mSuppressNextChar = true;
  117. }
  118. bool Input::getKeyDown(int key) const
  119. {
  120. if ((key < 0) || (key >= MAX_KEYS))
  121. return false;
  122. return mKeyDown[key];
  123. }
  124. bool Input::getKeyPress(int key) const
  125. {
  126. if ((key < 0) || (key >= MAX_KEYS))
  127. return false;
  128. return mKeyPress[key];
  129. }
  130. IntVector2 Input::getMousePosition() const
  131. {
  132. POINT mouse;
  133. GetCursorPos(&mouse);
  134. ScreenToClient((HWND)mRenderer->getWindowHandle(), &mouse);
  135. return IntVector2(mouse.x, mouse.y);
  136. }
  137. bool Input::getMouseButtonDown(int button) const
  138. {
  139. return (mMouseButtonDown & button) != 0;
  140. }
  141. bool Input::getMouseButtonPress(int button) const
  142. {
  143. return (mMouseButtonPress & button) != 0;
  144. }
  145. bool Input::getQualifierDown(int qualifier) const
  146. {
  147. if (qualifier == QUAL_SHIFT)
  148. return mKeyDown[KEY_SHIFT] != 0;
  149. if (qualifier == QUAL_CTRL)
  150. return mKeyDown[KEY_CTRL] != 0;
  151. return false;
  152. }
  153. bool Input::getQualifierPress(int qualifier) const
  154. {
  155. if (qualifier == QUAL_SHIFT)
  156. return mKeyPress[KEY_SHIFT] != 0;
  157. if (qualifier == QUAL_CTRL)
  158. return mKeyPress[KEY_CTRL] != 0;
  159. return false;
  160. }
  161. int Input::getQualifiers() const
  162. {
  163. int ret = 0;
  164. if (mKeyDown[KEY_SHIFT] != 0)
  165. ret |= QUAL_SHIFT;
  166. if (mKeyDown[KEY_CTRL] != 0)
  167. ret |= QUAL_CTRL;
  168. return ret;
  169. }
  170. void Input::handleWindowMessage(StringHash eventType, VariantMap& eventData)
  171. {
  172. using namespace WindowMessage;
  173. if (!mRenderer)
  174. return;
  175. int msg = eventData[P_MSG].getInt();
  176. int wParam = eventData[P_WPARAM].getInt();
  177. int lParam = eventData[P_LPARAM].getInt();
  178. switch (msg)
  179. {
  180. case WM_LBUTTONDOWN:
  181. mouseButtonChange(MOUSEB_LEFT, true);
  182. eventData[P_HANDLED] = true;
  183. break;
  184. case WM_NCLBUTTONUP:
  185. case WM_LBUTTONUP:
  186. mouseButtonChange(MOUSEB_LEFT, false);
  187. eventData[P_HANDLED] = true;
  188. break;
  189. case WM_RBUTTONDOWN:
  190. mouseButtonChange(MOUSEB_RIGHT, true);
  191. eventData[P_HANDLED] = true;
  192. break;
  193. case WM_NCRBUTTONUP:
  194. case WM_RBUTTONUP:
  195. mouseButtonChange(MOUSEB_RIGHT, false);
  196. eventData[P_HANDLED] = true;
  197. break;
  198. case WM_MBUTTONDOWN:
  199. mouseButtonChange(MOUSEB_MIDDLE, true);
  200. eventData[P_HANDLED] = true;
  201. break;
  202. case WM_NCMBUTTONUP:
  203. case WM_MBUTTONUP:
  204. mouseButtonChange(MOUSEB_MIDDLE, false);
  205. eventData[P_HANDLED] = true;
  206. break;
  207. case WM_MOUSEWHEEL:
  208. mouseWheelChange(wParam >> 16);
  209. eventData[P_HANDLED] = true;
  210. break;
  211. case WM_ACTIVATE:
  212. if (LOWORD(wParam) == WA_INACTIVE)
  213. {
  214. makeInactive();
  215. if (mRenderer->getFullscreen())
  216. mMinimized = true;
  217. }
  218. else
  219. {
  220. if (!mMinimized)
  221. mActivated = true;
  222. }
  223. eventData[P_HANDLED] = true;
  224. break;
  225. case WM_SIZE:
  226. if (wParam == SIZE_MINIMIZED)
  227. {
  228. mMinimized = true;
  229. makeInactive();
  230. }
  231. if ((wParam == SIZE_RESTORED) || (wParam == SIZE_MAXIMIZED))
  232. {
  233. mMinimized = false;
  234. mActivated = true;
  235. }
  236. eventData[P_HANDLED] = true;
  237. break;
  238. case WM_KEYDOWN:
  239. keyChange(wParam, true);
  240. eventData[P_HANDLED] = true;
  241. break;
  242. case WM_SYSKEYDOWN:
  243. keyChange(wParam, true);
  244. if ((wParam == KEY_RETURN) && (mToggleFullscreen))
  245. mRenderer->toggleFullscreen();
  246. if (wParam != KEY_F4)
  247. eventData[P_HANDLED] = true;
  248. break;
  249. case WM_KEYUP:
  250. keyChange(wParam, false);
  251. eventData[P_HANDLED] = true;
  252. break;
  253. case WM_SYSKEYUP:
  254. keyChange(wParam, false);
  255. eventData[P_HANDLED] = true;
  256. break;
  257. case WM_CHAR:
  258. if (!mSuppressNextChar)
  259. {
  260. using namespace Char;
  261. VariantMap keyEventData;
  262. keyEventData[P_CHAR] = wParam;
  263. eventData[P_BUTTONS] = mMouseButtonDown;
  264. eventData[P_QUALIFIERS] = getQualifiers();
  265. sendEvent(EVENT_CHAR, keyEventData);
  266. }
  267. mSuppressNextChar = false;
  268. eventData[P_HANDLED] = true;
  269. break;
  270. case WM_SETCURSOR:
  271. if ((lParam & 0xffff) == HTCLIENT)
  272. {
  273. setCursorVisible(false);
  274. eventData[P_HANDLED] = true;
  275. }
  276. else
  277. setCursorVisible(true);
  278. break;
  279. }
  280. }
  281. void Input::handleScreenMode(StringHash eventType, VariantMap& eventData)
  282. {
  283. // Screen mode change may affect the cursor clipping behaviour. Also re-center the cursor (if needed) to the
  284. // new screen size, so that there is no erroneous mouse move event
  285. setClipCursor(mClipCursor);
  286. }
  287. void Input::makeActive()
  288. {
  289. clearState();
  290. if (!mRenderer)
  291. {
  292. mActive = true;
  293. return;
  294. }
  295. mActive = true;
  296. mActivated = false;
  297. // Re-establish mouse cursor clipping if necessary
  298. setClipCursor(mClipCursor);
  299. setCursorVisible(false);
  300. sendEvent(EVENT_ACTIVATED);
  301. }
  302. void Input::makeInactive()
  303. {
  304. clearState();
  305. if (!mRenderer)
  306. {
  307. mActive = false;
  308. return;
  309. }
  310. mActive = false;
  311. mActivated = false;
  312. // Free and show the mouse cursor
  313. ReleaseCapture();
  314. ClipCursor(0);
  315. setCursorVisible(true);
  316. sendEvent(EVENT_INACTIVATED);
  317. }
  318. void Input::clearState()
  319. {
  320. // Use keyChange() & mouseButtonChange() to reset the state so that events will be sent properly
  321. for (unsigned i = 0; i < MAX_KEYS; ++i)
  322. keyChange(i, false);
  323. mouseButtonChange(MOUSEB_LEFT, false);
  324. mouseButtonChange(MOUSEB_RIGHT, false);
  325. mouseButtonChange(MOUSEB_MIDDLE, false);
  326. mMouseMove = IntVector2::sZero;
  327. mMouseMoveWheel = 0;
  328. mMouseButtonPress = 0;
  329. memset(&mKeyPress, 0, sizeof(mKeyPress));
  330. }
  331. void Input::mouseButtonChange(int button, bool newState)
  332. {
  333. // If we are not active yet, do not react to the mouse button down
  334. if ((newState) && (!mActive))
  335. return;
  336. // Check mouse move before sending the button up/down so that the click will be interpreted correctly
  337. //checkMouseMove();
  338. if (newState)
  339. {
  340. if (!(mMouseButtonDown & button))
  341. mMouseButtonPress |= button;
  342. mMouseButtonDown |= button;
  343. }
  344. else
  345. {
  346. if (!(mMouseButtonDown & button))
  347. return;
  348. mMouseButtonDown &= ~button;
  349. }
  350. using namespace MouseButtonDown;
  351. VariantMap eventData;
  352. eventData[P_BUTTON] = button;
  353. eventData[P_BUTTONS] = mMouseButtonDown;
  354. eventData[P_QUALIFIERS] = getQualifiers();
  355. sendEvent(newState ? EVENT_MOUSEBUTTONDOWN : EVENT_MOUSEBUTTONUP, eventData);
  356. // In non-confined mode, while any of the mouse buttons are down, capture the mouse so that we get the button release reliably
  357. if ((mRenderer) && (!mClipCursor))
  358. {
  359. if (mMouseButtonDown)
  360. SetCapture((HWND)mRenderer->getWindowHandle());
  361. else
  362. ReleaseCapture();
  363. }
  364. }
  365. void Input::keyChange(int key, bool newState)
  366. {
  367. if ((key < 0) || (key >= MAX_KEYS))
  368. return;
  369. bool repeat = false;
  370. if (newState)
  371. {
  372. if (!mKeyDown[key])
  373. mKeyPress[key] = true;
  374. else
  375. repeat = true;
  376. }
  377. else
  378. {
  379. if (!mKeyDown[key])
  380. return;
  381. }
  382. mKeyDown[key] = newState;
  383. using namespace KeyDown;
  384. VariantMap eventData;
  385. eventData[P_KEY] = key;
  386. eventData[P_BUTTONS] = mMouseButtonDown;
  387. eventData[P_QUALIFIERS] = getQualifiers();
  388. if (newState)
  389. eventData[P_REPEAT] = repeat;
  390. sendEvent(newState ? EVENT_KEYDOWN : EVENT_KEYUP, eventData);
  391. }
  392. void Input::mouseWheelChange(int delta)
  393. {
  394. if (!mActive)
  395. return;
  396. if (delta)
  397. {
  398. mMouseMoveWheel += delta;
  399. using namespace MouseWheel;
  400. VariantMap eventData;
  401. eventData[P_WHEEL] = delta;
  402. eventData[P_BUTTONS] = mMouseButtonDown;
  403. eventData[P_QUALIFIERS] = getQualifiers();
  404. sendEvent(EVENT_MOUSEWHEEL, eventData);
  405. }
  406. }
  407. void Input::checkMouseMove()
  408. {
  409. if (!mActive)
  410. return;
  411. IntVector2 mousePos = getMousePosition();
  412. IntVector2 mouseMove = mousePos - mLastMousePosition;
  413. if ((mClipCursor) && (mouseMove != IntVector2::sZero))
  414. {
  415. IntVector2 center(mRenderer->getWidth() / 2, mRenderer->getHeight() / 2);
  416. setMousePosition(center);
  417. mLastMousePosition = getMousePosition();
  418. }
  419. else
  420. mLastMousePosition = mousePos;
  421. if (mouseMove != IntVector2::sZero)
  422. {
  423. mMouseMove += mouseMove;
  424. using namespace MouseMove;
  425. VariantMap eventData;
  426. eventData[P_X] = mLastMousePosition.mX;
  427. eventData[P_Y] = mLastMousePosition.mY;
  428. eventData[P_DX] = mouseMove.mX;
  429. eventData[P_DY] = mouseMove.mY;
  430. eventData[P_BUTTONS] = mMouseButtonDown;
  431. eventData[P_QUALIFIERS] = getQualifiers();
  432. eventData[P_CLIPCURSOR] = mClipCursor;
  433. sendEvent(EVENT_MOUSEMOVE, eventData);
  434. }
  435. }
  436. void Input::setCursorVisible(bool enable)
  437. {
  438. // When inactive, always show the cursor
  439. if (!mActive)
  440. enable = true;
  441. if (mShowCursor == enable)
  442. return;
  443. ShowCursor(enable ? TRUE : FALSE);
  444. mShowCursor = enable;
  445. }