Input.cpp 9.1 KB

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