Input.cpp 11 KB

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