Input.cpp 8.7 KB

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