Input.cpp 9.2 KB

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