Input.cpp 10 KB

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