Input.cpp 14 KB

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