Input.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 "Context.h"
  24. #include "CoreEvents.h"
  25. #include "Graphics.h"
  26. #include "GraphicsEvents.h"
  27. #include "Input.h"
  28. #include "Log.h"
  29. #include "Profiler.h"
  30. #include <cstring>
  31. #include <Windows.h>
  32. #include "DebugNew.h"
  33. OBJECTTYPESTATIC(Input);
  34. Input::Input(Context* context) :
  35. Object(context),
  36. clipCursor_(true),
  37. showCursor_(true),
  38. toggleFullscreen_(true),
  39. active_(false),
  40. minimized_(false),
  41. activated_(false),
  42. suppressNextChar_(false),
  43. initialized_(false)
  44. {
  45. // Zero the initial state
  46. memset(&keyDown_, 0, sizeof(keyDown_));
  47. memset(&keyPress_, 0, sizeof(keyPress_));
  48. mouseButtonDown_ = 0;
  49. mouseButtonPress_ = 0;
  50. lastMousePosition_ = IntVector2::ZERO;
  51. SubscribeToEvent(E_WINDOWMESSAGE, HANDLER(Input, HandleWindowMessage));
  52. SubscribeToEvent(E_SCREENMODE, HANDLER(Input, HandleScreenMode));
  53. SubscribeToEvent(E_BEGINFRAME, HANDLER(Input, HandleBeginFrame));
  54. // Try to initialize right now, but skip if screen mode is not yet set
  55. Initialize();
  56. }
  57. Input::~Input()
  58. {
  59. }
  60. void Input::Update()
  61. {
  62. PROFILE(UpdateInput);
  63. if (!graphics_)
  64. return;
  65. memset(keyPress_, 0, sizeof(keyPress_));
  66. mouseButtonPress_ = 0;
  67. mouseMove_ = IntVector2::ZERO;
  68. mouseMoveWheel_ = 0;
  69. graphics_->MessagePump();
  70. if (activated_)
  71. MakeActive();
  72. CheckMouseMove();
  73. }
  74. void Input::SetClipCursor(bool enable)
  75. {
  76. clipCursor_ = enable;
  77. if (!graphics_)
  78. return;
  79. if ((!graphics_->GetFullscreen()) && (active_) && (clipCursor_))
  80. {
  81. RECT clipRect;
  82. SetMousePosition(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  83. lastMousePosition_ = GetMousePosition();
  84. GetWindowRect((HWND)graphics_->GetWindowHandle(), &clipRect);
  85. ClipCursor(&clipRect);
  86. }
  87. else
  88. {
  89. if ((graphics_->GetFullscreen()) && (active_) && (clipCursor_))
  90. {
  91. SetMousePosition(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  92. lastMousePosition_ = GetMousePosition();
  93. }
  94. ClipCursor(0);
  95. }
  96. }
  97. void Input::SetToggleFullscreen(bool enable)
  98. {
  99. toggleFullscreen_ = enable;
  100. }
  101. void Input::SetMousePosition(const IntVector2& position)
  102. {
  103. if (!graphics_)
  104. return;
  105. POINT point;
  106. point.x = position.x_;
  107. point.y = position.y_;
  108. ClientToScreen((HWND)graphics_->GetWindowHandle(), &point);
  109. SetCursorPos(point.x, point.y);
  110. }
  111. void Input::SetMousePosition(int x, int y)
  112. {
  113. SetMousePosition(IntVector2(x, y));
  114. }
  115. void Input::SuppressNextChar()
  116. {
  117. suppressNextChar_ = true;
  118. }
  119. bool Input::GetKeyDown(int key) const
  120. {
  121. if ((key < 0) || (key >= MAX_KEYS))
  122. return false;
  123. return keyDown_[key];
  124. }
  125. bool Input::GetKeyPress(int key) const
  126. {
  127. if ((key < 0) || (key >= MAX_KEYS))
  128. return false;
  129. return keyPress_[key];
  130. }
  131. IntVector2 Input::GetMousePosition() const
  132. {
  133. if (!graphics_)
  134. return IntVector2(0, 0);
  135. POINT mouse;
  136. GetCursorPos(&mouse);
  137. ScreenToClient((HWND)graphics_->GetWindowHandle(), &mouse);
  138. return IntVector2(mouse.x, mouse.y);
  139. }
  140. bool Input::GetMouseButtonDown(int button) const
  141. {
  142. return (mouseButtonDown_ & button) != 0;
  143. }
  144. bool Input::GetMouseButtonPress(int button) const
  145. {
  146. return (mouseButtonPress_ & button) != 0;
  147. }
  148. bool Input::GetQualifierDown(int qualifier) const
  149. {
  150. if (qualifier == QUAL_SHIFT)
  151. return keyDown_[KEY_SHIFT] != 0;
  152. if (qualifier == QUAL_CTRL)
  153. return keyDown_[KEY_CTRL] != 0;
  154. if (qualifier == QUAL_ALT)
  155. return keyDown_[KEY_ALT] != 0;
  156. return false;
  157. }
  158. bool Input::GetQualifierPress(int qualifier) const
  159. {
  160. if (qualifier == QUAL_SHIFT)
  161. return keyPress_[KEY_SHIFT] != 0;
  162. if (qualifier == QUAL_CTRL)
  163. return keyPress_[KEY_CTRL] != 0;
  164. if (qualifier == QUAL_ALT)
  165. return keyPress_[KEY_ALT] != 0;
  166. return false;
  167. }
  168. int Input::GetQualifiers() const
  169. {
  170. int ret = 0;
  171. if (keyDown_[KEY_SHIFT] != 0)
  172. ret |= QUAL_SHIFT;
  173. if (keyDown_[KEY_CTRL] != 0)
  174. ret |= QUAL_CTRL;
  175. if (keyDown_[KEY_ALT] != 0)
  176. ret |= QUAL_ALT;
  177. return ret;
  178. }
  179. void Input::Initialize()
  180. {
  181. Graphics* graphics = GetSubsystem<Graphics>();
  182. if ((!graphics) || (!graphics->IsInitialized()))
  183. return;
  184. graphics_ = graphics;
  185. // In fullscreen mode there is no initial window activation message. Therefore assume activation
  186. if (graphics_->GetFullscreen())
  187. activated_ = true;
  188. initialized_ = true;
  189. LOGINFO("Initialized input");
  190. }
  191. void Input::MakeActive()
  192. {
  193. if (!graphics_)
  194. return;
  195. clearState();
  196. active_ = true;
  197. activated_ = false;
  198. // Re-establish mouse cursor clipping if necessary
  199. SetClipCursor(clipCursor_);
  200. SetCursorVisible(false);
  201. SendEvent(E_ACTIVATED);
  202. }
  203. void Input::MakeInactive()
  204. {
  205. if (!graphics_)
  206. return;
  207. clearState();
  208. active_ = false;
  209. activated_ = false;
  210. // Free and show the mouse cursor
  211. ReleaseCapture();
  212. ClipCursor(0);
  213. SetCursorVisible(true);
  214. SendEvent(E_INACTIVATED);
  215. }
  216. void Input::clearState()
  217. {
  218. // Use SetKey() & SetMouseButton() to reset the state so that events will be sent properly
  219. for (unsigned i = 0; i < MAX_KEYS; ++i)
  220. SetKey(i, false);
  221. SetMouseButton(MOUSEB_LEFT, false);
  222. SetMouseButton(MOUSEB_RIGHT, false);
  223. SetMouseButton(MOUSEB_MIDDLE, false);
  224. mouseMove_ = IntVector2::ZERO;
  225. mouseMoveWheel_ = 0;
  226. mouseButtonPress_ = 0;
  227. memset(&keyPress_, 0, sizeof(keyPress_));
  228. }
  229. void Input::SetMouseButton(int button, bool newState)
  230. {
  231. // If we are not active yet, do not react to the mouse button down
  232. if ((newState) && (!active_))
  233. return;
  234. if (newState)
  235. {
  236. if (!(mouseButtonDown_ & button))
  237. mouseButtonPress_ |= button;
  238. mouseButtonDown_ |= button;
  239. }
  240. else
  241. {
  242. if (!(mouseButtonDown_ & button))
  243. return;
  244. mouseButtonDown_ &= ~button;
  245. }
  246. using namespace MouseButtonDown;
  247. VariantMap eventData;
  248. eventData[P_BUTTON] = button;
  249. eventData[P_BUTTONS] = mouseButtonDown_;
  250. eventData[P_QUALIFIERS] = GetQualifiers();
  251. SendEvent(newState ? E_MOUSEBUTTONDOWN : E_MOUSEBUTTONUP, eventData);
  252. // In non-confined mode, while any of the mouse buttons are down, capture the mouse so that we get the button release reliably
  253. if ((graphics_) && (!clipCursor_))
  254. {
  255. if (mouseButtonDown_)
  256. SetCapture((HWND)graphics_->GetWindowHandle());
  257. else
  258. ReleaseCapture();
  259. }
  260. }
  261. void Input::SetKey(int key, bool newState)
  262. {
  263. if ((key < 0) || (key >= MAX_KEYS))
  264. return;
  265. bool repeat = false;
  266. if (newState)
  267. {
  268. if (!keyDown_[key])
  269. keyPress_[key] = true;
  270. else
  271. repeat = true;
  272. }
  273. else
  274. {
  275. if (!keyDown_[key])
  276. return;
  277. }
  278. keyDown_[key] = newState;
  279. using namespace KeyDown;
  280. VariantMap eventData;
  281. eventData[P_KEY] = key;
  282. eventData[P_BUTTONS] = mouseButtonDown_;
  283. eventData[P_QUALIFIERS] = GetQualifiers();
  284. if (newState)
  285. eventData[P_REPEAT] = repeat;
  286. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  287. }
  288. void Input::SetMouseWheel(int delta)
  289. {
  290. if (!active_)
  291. return;
  292. if (delta)
  293. {
  294. mouseMoveWheel_ += delta;
  295. using namespace MouseWheel;
  296. VariantMap eventData;
  297. eventData[P_WHEEL] = delta;
  298. eventData[P_BUTTONS] = mouseButtonDown_;
  299. eventData[P_QUALIFIERS] = GetQualifiers();
  300. SendEvent(E_MOUSEWHEEL, eventData);
  301. }
  302. }
  303. void Input::CheckMouseMove()
  304. {
  305. if (!active_)
  306. return;
  307. IntVector2 mousePos = GetMousePosition();
  308. IntVector2 mouseMove = mousePos - lastMousePosition_;
  309. if ((clipCursor_) && (mouseMove != IntVector2::ZERO))
  310. {
  311. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  312. SetMousePosition(center);
  313. lastMousePosition_ = GetMousePosition();
  314. }
  315. else
  316. lastMousePosition_ = mousePos;
  317. if (mouseMove != IntVector2::ZERO)
  318. {
  319. mouseMove_ += mouseMove;
  320. using namespace MouseMove;
  321. VariantMap eventData;
  322. eventData[P_X] = lastMousePosition_.x_;
  323. eventData[P_Y] = lastMousePosition_.y_;
  324. eventData[P_DX] = mouseMove.x_;
  325. eventData[P_DY] = mouseMove.y_;
  326. eventData[P_BUTTONS] = mouseButtonDown_;
  327. eventData[P_QUALIFIERS] = GetQualifiers();
  328. eventData[P_CLIPCURSOR] = clipCursor_;
  329. SendEvent(E_MOUSEMOVE, eventData);
  330. }
  331. }
  332. void Input::SetCursorVisible(bool enable)
  333. {
  334. if (!graphics_)
  335. return;
  336. // When inactive, always show the cursor
  337. if (!active_)
  338. enable = true;
  339. if (showCursor_ == enable)
  340. return;
  341. ShowCursor(enable ? TRUE : FALSE);
  342. showCursor_ = enable;
  343. }
  344. void Input::HandleWindowMessage(StringHash eventType, VariantMap& eventData)
  345. {
  346. using namespace WindowMessage;
  347. int msg = eventData[P_MSG].GetInt();
  348. int wParam = eventData[P_WPARAM].GetInt();
  349. int lParam = eventData[P_LPARAM].GetInt();
  350. switch (msg)
  351. {
  352. case WM_LBUTTONDOWN:
  353. SetMouseButton(MOUSEB_LEFT, true);
  354. eventData[P_HANDLED] = true;
  355. break;
  356. case WM_NCLBUTTONUP:
  357. case WM_LBUTTONUP:
  358. SetMouseButton(MOUSEB_LEFT, false);
  359. eventData[P_HANDLED] = true;
  360. break;
  361. case WM_RBUTTONDOWN:
  362. SetMouseButton(MOUSEB_RIGHT, true);
  363. eventData[P_HANDLED] = true;
  364. break;
  365. case WM_NCRBUTTONUP:
  366. case WM_RBUTTONUP:
  367. SetMouseButton(MOUSEB_RIGHT, false);
  368. eventData[P_HANDLED] = true;
  369. break;
  370. case WM_MBUTTONDOWN:
  371. SetMouseButton(MOUSEB_MIDDLE, true);
  372. eventData[P_HANDLED] = true;
  373. break;
  374. case WM_NCMBUTTONUP:
  375. case WM_MBUTTONUP:
  376. SetMouseButton(MOUSEB_MIDDLE, false);
  377. eventData[P_HANDLED] = true;
  378. break;
  379. case WM_MOUSEWHEEL:
  380. SetMouseWheel(wParam >> 16);
  381. eventData[P_HANDLED] = true;
  382. break;
  383. case WM_ACTIVATE:
  384. if (LOWORD(wParam) == WA_INACTIVE)
  385. {
  386. MakeInactive();
  387. if (graphics_->GetFullscreen())
  388. minimized_ = true;
  389. }
  390. else
  391. {
  392. if (!minimized_)
  393. activated_ = true;
  394. }
  395. eventData[P_HANDLED] = true;
  396. break;
  397. case WM_SIZE:
  398. if (wParam == SIZE_MINIMIZED)
  399. {
  400. minimized_ = true;
  401. MakeInactive();
  402. }
  403. if ((wParam == SIZE_RESTORED) || (wParam == SIZE_MAXIMIZED))
  404. {
  405. minimized_ = false;
  406. activated_ = true;
  407. }
  408. eventData[P_HANDLED] = true;
  409. break;
  410. case WM_KEYDOWN:
  411. SetKey(wParam, true);
  412. eventData[P_HANDLED] = true;
  413. break;
  414. case WM_SYSKEYDOWN:
  415. SetKey(wParam, true);
  416. if ((wParam == KEY_RETURN) && (toggleFullscreen_))
  417. graphics_->ToggleFullscreen();
  418. if (wParam != KEY_F4)
  419. eventData[P_HANDLED] = true;
  420. break;
  421. case WM_KEYUP:
  422. SetKey(wParam, false);
  423. eventData[P_HANDLED] = true;
  424. break;
  425. case WM_SYSKEYUP:
  426. SetKey(wParam, false);
  427. eventData[P_HANDLED] = true;
  428. break;
  429. case WM_CHAR:
  430. if (!suppressNextChar_)
  431. {
  432. using namespace Char;
  433. VariantMap keyEventData;
  434. keyEventData[P_CHAR] = wParam;
  435. keyEventData[P_BUTTONS] = mouseButtonDown_;
  436. keyEventData[P_QUALIFIERS] = GetQualifiers();
  437. SendEvent(E_CHAR, keyEventData);
  438. }
  439. suppressNextChar_ = false;
  440. eventData[P_HANDLED] = true;
  441. break;
  442. case WM_SETCURSOR:
  443. if ((lParam & 0xffff) == HTCLIENT)
  444. {
  445. SetCursorVisible(false);
  446. eventData[P_HANDLED] = true;
  447. }
  448. else
  449. SetCursorVisible(true);
  450. break;
  451. }
  452. }
  453. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  454. {
  455. if (!initialized_)
  456. Initialize();
  457. // Screen mode change may affect the cursor clipping behaviour. Also re-center the cursor (if needed) to the new screen size,
  458. // so that there is no erroneous mouse move event
  459. else
  460. SetClipCursor(clipCursor_);
  461. }
  462. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  463. {
  464. // Update input right at the beginning of the frame
  465. if (initialized_)
  466. Update();
  467. }