Input.cpp 10 KB

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