Input.cpp 12 KB

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