Input.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. return false;
  155. }
  156. bool Input::GetQualifierPress(int qualifier) const
  157. {
  158. if (qualifier == QUAL_SHIFT)
  159. return keyPress_[KEY_SHIFT] != 0;
  160. if (qualifier == QUAL_CTRL)
  161. return keyPress_[KEY_CTRL] != 0;
  162. return false;
  163. }
  164. int Input::GetQualifiers() const
  165. {
  166. int ret = 0;
  167. if (keyDown_[KEY_SHIFT] != 0)
  168. ret |= QUAL_SHIFT;
  169. if (keyDown_[KEY_CTRL] != 0)
  170. ret |= QUAL_CTRL;
  171. return ret;
  172. }
  173. void Input::Initialize()
  174. {
  175. Graphics* graphics = GetSubsystem<Graphics>();
  176. if ((!graphics) || (!graphics->IsInitialized()))
  177. return;
  178. graphics_ = graphics;
  179. // In fullscreen mode there is no initial window activation message. Therefore assume activation
  180. if (graphics_->GetFullscreen())
  181. activated_ = true;
  182. initialized_ = true;
  183. LOGINFO("Initialized input");
  184. }
  185. void Input::MakeActive()
  186. {
  187. if (!graphics_)
  188. return;
  189. clearState();
  190. active_ = true;
  191. activated_ = false;
  192. // Re-establish mouse cursor clipping if necessary
  193. SetClipCursor(clipCursor_);
  194. SetCursorVisible(false);
  195. SendEvent(E_ACTIVATED);
  196. }
  197. void Input::MakeInactive()
  198. {
  199. if (!graphics_)
  200. return;
  201. clearState();
  202. active_ = false;
  203. activated_ = false;
  204. // Free and show the mouse cursor
  205. ReleaseCapture();
  206. ClipCursor(0);
  207. SetCursorVisible(true);
  208. SendEvent(E_INACTIVATED);
  209. }
  210. void Input::clearState()
  211. {
  212. // Use SetKey() & SetMouseButton() to reset the state so that events will be sent properly
  213. for (unsigned i = 0; i < MAX_KEYS; ++i)
  214. SetKey(i, false);
  215. SetMouseButton(MOUSEB_LEFT, false);
  216. SetMouseButton(MOUSEB_RIGHT, false);
  217. SetMouseButton(MOUSEB_MIDDLE, false);
  218. mouseMove_ = IntVector2::ZERO;
  219. mouseMoveWheel_ = 0;
  220. mouseButtonPress_ = 0;
  221. memset(&keyPress_, 0, sizeof(keyPress_));
  222. }
  223. void Input::SetMouseButton(int button, bool newState)
  224. {
  225. // If we are not active yet, do not react to the mouse button down
  226. if ((newState) && (!active_))
  227. return;
  228. if (newState)
  229. {
  230. if (!(mouseButtonDown_ & button))
  231. mouseButtonPress_ |= button;
  232. mouseButtonDown_ |= button;
  233. }
  234. else
  235. {
  236. if (!(mouseButtonDown_ & button))
  237. return;
  238. mouseButtonDown_ &= ~button;
  239. }
  240. using namespace MouseButtonDown;
  241. VariantMap eventData;
  242. eventData[P_BUTTON] = button;
  243. eventData[P_BUTTONS] = mouseButtonDown_;
  244. eventData[P_QUALIFIERS] = GetQualifiers();
  245. SendEvent(newState ? E_MOUSEBUTTONDOWN : E_MOUSEBUTTONUP, eventData);
  246. // In non-confined mode, while any of the mouse buttons are down, capture the mouse so that we get the button release reliably
  247. if ((graphics_) && (!clipCursor_))
  248. {
  249. if (mouseButtonDown_)
  250. SetCapture((HWND)graphics_->GetWindowHandle());
  251. else
  252. ReleaseCapture();
  253. }
  254. }
  255. void Input::SetKey(int key, bool newState)
  256. {
  257. if ((key < 0) || (key >= MAX_KEYS))
  258. return;
  259. bool repeat = false;
  260. if (newState)
  261. {
  262. if (!keyDown_[key])
  263. keyPress_[key] = true;
  264. else
  265. repeat = true;
  266. }
  267. else
  268. {
  269. if (!keyDown_[key])
  270. return;
  271. }
  272. keyDown_[key] = newState;
  273. using namespace KeyDown;
  274. VariantMap eventData;
  275. eventData[P_KEY] = key;
  276. eventData[P_BUTTONS] = mouseButtonDown_;
  277. eventData[P_QUALIFIERS] = GetQualifiers();
  278. if (newState)
  279. eventData[P_REPEAT] = repeat;
  280. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  281. }
  282. void Input::SetMouseWheel(int delta)
  283. {
  284. if (!active_)
  285. return;
  286. if (delta)
  287. {
  288. mouseMoveWheel_ += delta;
  289. using namespace MouseWheel;
  290. VariantMap eventData;
  291. eventData[P_WHEEL] = delta;
  292. eventData[P_BUTTONS] = mouseButtonDown_;
  293. eventData[P_QUALIFIERS] = GetQualifiers();
  294. SendEvent(E_MOUSEWHEEL, eventData);
  295. }
  296. }
  297. void Input::CheckMouseMove()
  298. {
  299. if (!active_)
  300. return;
  301. IntVector2 mousePos = GetMousePosition();
  302. IntVector2 mouseMove = mousePos - lastMousePosition_;
  303. if ((clipCursor_) && (mouseMove != IntVector2::ZERO))
  304. {
  305. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  306. SetMousePosition(center);
  307. lastMousePosition_ = GetMousePosition();
  308. }
  309. else
  310. lastMousePosition_ = mousePos;
  311. if (mouseMove != IntVector2::ZERO)
  312. {
  313. mouseMove_ += mouseMove;
  314. using namespace MouseMove;
  315. VariantMap eventData;
  316. eventData[P_X] = lastMousePosition_.x_;
  317. eventData[P_Y] = lastMousePosition_.y_;
  318. eventData[P_DX] = mouseMove.x_;
  319. eventData[P_DY] = mouseMove.y_;
  320. eventData[P_BUTTONS] = mouseButtonDown_;
  321. eventData[P_QUALIFIERS] = GetQualifiers();
  322. eventData[P_CLIPCURSOR] = clipCursor_;
  323. SendEvent(E_MOUSEMOVE, eventData);
  324. }
  325. }
  326. void Input::SetCursorVisible(bool enable)
  327. {
  328. if (!graphics_)
  329. return;
  330. // When inactive, always show the cursor
  331. if (!active_)
  332. enable = true;
  333. if (showCursor_ == enable)
  334. return;
  335. ShowCursor(enable ? TRUE : FALSE);
  336. showCursor_ = enable;
  337. }
  338. void Input::HandleWindowMessage(StringHash eventType, VariantMap& eventData)
  339. {
  340. using namespace WindowMessage;
  341. int msg = eventData[P_MSG].GetInt();
  342. int wParam = eventData[P_WPARAM].GetInt();
  343. int lParam = eventData[P_LPARAM].GetInt();
  344. switch (msg)
  345. {
  346. case WM_LBUTTONDOWN:
  347. SetMouseButton(MOUSEB_LEFT, true);
  348. eventData[P_HANDLED] = true;
  349. break;
  350. case WM_NCLBUTTONUP:
  351. case WM_LBUTTONUP:
  352. SetMouseButton(MOUSEB_LEFT, false);
  353. eventData[P_HANDLED] = true;
  354. break;
  355. case WM_RBUTTONDOWN:
  356. SetMouseButton(MOUSEB_RIGHT, true);
  357. eventData[P_HANDLED] = true;
  358. break;
  359. case WM_NCRBUTTONUP:
  360. case WM_RBUTTONUP:
  361. SetMouseButton(MOUSEB_RIGHT, false);
  362. eventData[P_HANDLED] = true;
  363. break;
  364. case WM_MBUTTONDOWN:
  365. SetMouseButton(MOUSEB_MIDDLE, true);
  366. eventData[P_HANDLED] = true;
  367. break;
  368. case WM_NCMBUTTONUP:
  369. case WM_MBUTTONUP:
  370. SetMouseButton(MOUSEB_MIDDLE, false);
  371. eventData[P_HANDLED] = true;
  372. break;
  373. case WM_MOUSEWHEEL:
  374. SetMouseWheel(wParam >> 16);
  375. eventData[P_HANDLED] = true;
  376. break;
  377. case WM_ACTIVATE:
  378. if (LOWORD(wParam) == WA_INACTIVE)
  379. {
  380. MakeInactive();
  381. if (graphics_->GetFullscreen())
  382. minimized_ = true;
  383. }
  384. else
  385. {
  386. if (!minimized_)
  387. activated_ = true;
  388. }
  389. eventData[P_HANDLED] = true;
  390. break;
  391. case WM_SIZE:
  392. if (wParam == SIZE_MINIMIZED)
  393. {
  394. minimized_ = true;
  395. MakeInactive();
  396. }
  397. if ((wParam == SIZE_RESTORED) || (wParam == SIZE_MAXIMIZED))
  398. {
  399. minimized_ = false;
  400. activated_ = true;
  401. }
  402. eventData[P_HANDLED] = true;
  403. break;
  404. case WM_KEYDOWN:
  405. SetKey(wParam, true);
  406. eventData[P_HANDLED] = true;
  407. break;
  408. case WM_SYSKEYDOWN:
  409. SetKey(wParam, true);
  410. if ((wParam == KEY_RETURN) && (toggleFullscreen_))
  411. graphics_->ToggleFullscreen();
  412. if (wParam != KEY_F4)
  413. eventData[P_HANDLED] = true;
  414. break;
  415. case WM_KEYUP:
  416. SetKey(wParam, false);
  417. eventData[P_HANDLED] = true;
  418. break;
  419. case WM_SYSKEYUP:
  420. SetKey(wParam, false);
  421. eventData[P_HANDLED] = true;
  422. break;
  423. case WM_CHAR:
  424. if (!suppressNextChar_)
  425. {
  426. using namespace Char;
  427. VariantMap keyEventData;
  428. keyEventData[P_CHAR] = wParam;
  429. keyEventData[P_BUTTONS] = mouseButtonDown_;
  430. keyEventData[P_QUALIFIERS] = GetQualifiers();
  431. SendEvent(E_CHAR, keyEventData);
  432. }
  433. suppressNextChar_ = false;
  434. eventData[P_HANDLED] = true;
  435. break;
  436. case WM_SETCURSOR:
  437. if ((lParam & 0xffff) == HTCLIENT)
  438. {
  439. SetCursorVisible(false);
  440. eventData[P_HANDLED] = true;
  441. }
  442. else
  443. SetCursorVisible(true);
  444. break;
  445. }
  446. }
  447. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  448. {
  449. if (!initialized_)
  450. Initialize();
  451. // Screen mode change may affect the cursor clipping behaviour. Also re-center the cursor (if needed) to the new screen size,
  452. // so that there is no erroneous mouse move event
  453. else
  454. SetClipCursor(clipCursor_);
  455. }
  456. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  457. {
  458. // Update input right at the beginning of the frame
  459. if (initialized_)
  460. Update();
  461. }