Input.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "Mutex.h"
  30. #include "ProcessUtils.h"
  31. #include "Profiler.h"
  32. #include "StringUtils.h"
  33. #include <cstring>
  34. #ifdef USE_OPENGL
  35. #include <GraphicsImpl.h>
  36. #else
  37. #include <windows.h>
  38. #endif
  39. #include "DebugNew.h"
  40. #ifdef USE_OPENGL
  41. static HashMap<unsigned, Input*> inputInstances;
  42. /// Return the Input subsystem instance corresponding to an SDL window ID.
  43. Input* GetInputInstance(unsigned windowID)
  44. {
  45. #ifndef ANDROID
  46. return windowID ? inputInstances[windowID] : 0;
  47. #else
  48. // On Android we support only a single instance of Urho3D in the process, and the window ID can not be relied on.
  49. return inputInstances.Size() ? inputInstances.Begin()->second_ : 0;
  50. #endif
  51. }
  52. /// Convert SDL keycode if necessary
  53. int ConvertSDLKeyCode(int keySym, int scanCode)
  54. {
  55. if (scanCode == SDL_SCANCODE_AC_BACK)
  56. return KEY_ESC;
  57. else return SDL_toupper(keySym);
  58. }
  59. #else
  60. /// Convert the virtual key code & scan code if necessary. Return non-zero if key should be posted
  61. int ConvertKeyCode(unsigned wParam, unsigned lParam)
  62. {
  63. unsigned scanCode = (lParam >> 16) & 0x1ff;
  64. // Recognize left/right qualifier key from the scan code
  65. switch (wParam)
  66. {
  67. case VK_SHIFT:
  68. if (scanCode == 54)
  69. return KEY_RSHIFT;
  70. else
  71. return KEY_LSHIFT;
  72. break;
  73. case VK_CONTROL:
  74. // Control might not be a real key, as Windows posts it whenever Alt-Gr is pressed (inspired by GLFW)
  75. {
  76. MSG nextMsg;
  77. DWORD msgTime = GetMessageTime();
  78. if (PeekMessage(&nextMsg, NULL, 0, 0, PM_NOREMOVE))
  79. {
  80. if ((nextMsg.message == WM_KEYDOWN || nextMsg.message == WM_SYSKEYDOWN) && nextMsg.wParam == VK_MENU &&
  81. (nextMsg.lParam & 0x01000000) != 0 && nextMsg.time == msgTime)
  82. return 0;
  83. }
  84. if (scanCode & 0x100)
  85. return KEY_RCTRL;
  86. else
  87. return KEY_LCTRL;
  88. }
  89. break;
  90. case VK_MENU:
  91. if (scanCode & 0x100)
  92. return KEY_RALT;
  93. else
  94. return KEY_LALT;
  95. default:
  96. return wParam;
  97. }
  98. }
  99. #endif
  100. OBJECTTYPESTATIC(Input);
  101. Input::Input(Context* context) :
  102. Object(context),
  103. #ifdef USE_OPENGL
  104. windowID_(0),
  105. #else
  106. showCursor_(true),
  107. #endif
  108. toggleFullscreen_(true),
  109. active_(false),
  110. minimized_(false),
  111. activated_(false),
  112. suppressNextMouseMove_(false),
  113. initialized_(false)
  114. {
  115. // Zero the initial state
  116. mouseButtonDown_ = 0;
  117. ResetState();
  118. #ifndef USE_OPENGL
  119. SubscribeToEvent(E_WINDOWMESSAGE, HANDLER(Input, HandleWindowMessage));
  120. #endif
  121. SubscribeToEvent(E_SCREENMODE, HANDLER(Input, HandleScreenMode));
  122. SubscribeToEvent(E_BEGINFRAME, HANDLER(Input, HandleBeginFrame));
  123. // Try to initialize right now, but skip if screen mode is not yet set
  124. Initialize();
  125. }
  126. Input::~Input()
  127. {
  128. #ifdef USE_OPENGL
  129. // Remove input instance mapping
  130. if (initialized_)
  131. {
  132. MutexLock lock(GetStaticMutex());
  133. inputInstances.Erase(windowID_);
  134. }
  135. #endif
  136. }
  137. void Input::Update()
  138. {
  139. PROFILE(UpdateInput);
  140. if (!graphics_ || !graphics_->IsInitialized())
  141. return;
  142. // Reset input accumulation for this frame
  143. keyPress_.Clear();
  144. mouseButtonPress_ = 0;
  145. mouseMove_ = IntVector2::ZERO;
  146. mouseMoveWheel_ = 0;
  147. // Reset touch delta movement. Note: last coordinates are stored internally, but the frame delta is returned to user
  148. for (Map<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  149. {
  150. TouchState& state = i->second_;
  151. state.delta_ = state.position_;
  152. }
  153. #ifndef USE_OPENGL
  154. // Pump Win32 events
  155. MSG msg;
  156. while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
  157. {
  158. TranslateMessage(&msg);
  159. DispatchMessageW(&msg);
  160. }
  161. #else
  162. {
  163. MutexLock lock(GetStaticMutex());
  164. // Pump SDL events
  165. SDL_Event evt;
  166. SDL_PumpEvents();
  167. while (SDL_PollEvent(&evt))
  168. {
  169. // Dispatch event to the appropriate Input instance. However SDL_QUIT can not at the moment be handled for multiple
  170. // instances properly (OpenGL contexts running in other threads can not be closed from this thread), so we handle
  171. // it only for the own instance
  172. if (evt.type != SDL_QUIT)
  173. HandleSDLEvent(&evt);
  174. else
  175. graphics_->Close();
  176. }
  177. }
  178. // Poll SDL window activation state
  179. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  180. unsigned flags = SDL_GetWindowFlags(window);
  181. if ((flags & (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS)) == (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS))
  182. {
  183. if (!active_)
  184. activated_ = true;
  185. }
  186. else
  187. {
  188. if (active_)
  189. MakeInactive();
  190. }
  191. #endif
  192. // Activate input now if necessary
  193. if (activated_)
  194. MakeActive();
  195. // Finally send the mouse move event if motion has been accumulated
  196. if (active_)
  197. {
  198. IntVector2 mousePos = GetCursorPosition();
  199. mouseMove_ = mousePos - lastCursorPosition_;
  200. // Recenter the mouse cursor manually if it moved
  201. if (mouseMove_ != IntVector2::ZERO)
  202. {
  203. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  204. SetCursorPosition(center);
  205. lastCursorPosition_ = center;
  206. }
  207. else
  208. lastCursorPosition_ = mousePos;
  209. if (mouseMove_ != IntVector2::ZERO && suppressNextMouseMove_)
  210. {
  211. mouseMove_ = IntVector2::ZERO;
  212. suppressNextMouseMove_ = false;
  213. }
  214. if (mouseMove_ != IntVector2::ZERO)
  215. {
  216. using namespace MouseMove;
  217. VariantMap eventData;
  218. eventData[P_DX] = mouseMove_.x_;
  219. eventData[P_DY] = mouseMove_.y_;
  220. eventData[P_BUTTONS] = mouseButtonDown_;
  221. eventData[P_QUALIFIERS] = GetQualifiers();
  222. SendEvent(E_MOUSEMOVE, eventData);
  223. }
  224. }
  225. }
  226. void Input::SetToggleFullscreen(bool enable)
  227. {
  228. toggleFullscreen_ = enable;
  229. }
  230. bool Input::GetKeyDown(int key) const
  231. {
  232. return keyDown_.Contains(key);
  233. }
  234. bool Input::GetKeyPress(int key) const
  235. {
  236. return keyPress_.Contains(key);
  237. }
  238. bool Input::GetMouseButtonDown(int button) const
  239. {
  240. return (mouseButtonDown_ & button) != 0;
  241. }
  242. bool Input::GetMouseButtonPress(int button) const
  243. {
  244. return (mouseButtonPress_ & button) != 0;
  245. }
  246. bool Input::GetQualifierDown(int qualifier) const
  247. {
  248. if (qualifier == QUAL_SHIFT)
  249. return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
  250. if (qualifier == QUAL_CTRL)
  251. return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
  252. if (qualifier == QUAL_ALT)
  253. return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
  254. return false;
  255. }
  256. bool Input::GetQualifierPress(int qualifier) const
  257. {
  258. if (qualifier == QUAL_SHIFT)
  259. return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
  260. if (qualifier == QUAL_CTRL)
  261. return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
  262. if (qualifier == QUAL_ALT)
  263. return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
  264. return false;
  265. }
  266. int Input::GetQualifiers() const
  267. {
  268. int ret = 0;
  269. if (GetQualifierDown(QUAL_SHIFT))
  270. ret |= QUAL_SHIFT;
  271. if (GetQualifierDown(QUAL_CTRL))
  272. ret |= QUAL_CTRL;
  273. if (GetQualifierDown(QUAL_ALT))
  274. ret |= QUAL_ALT;
  275. return ret;
  276. }
  277. TouchState Input::GetTouch(unsigned index) const
  278. {
  279. unsigned cmpIndex = 0;
  280. for (Map<int, TouchState>::ConstIterator i = touches_.Begin(); i != touches_.End(); ++i)
  281. {
  282. if (cmpIndex == index)
  283. {
  284. TouchState ret = i->second_;
  285. // Convert last position to delta
  286. ret.delta_ = ret.position_ - ret.delta_;
  287. return ret;
  288. }
  289. else
  290. ++cmpIndex;
  291. }
  292. return TouchState();
  293. }
  294. void Input::Initialize()
  295. {
  296. Graphics* graphics = GetSubsystem<Graphics>();
  297. if (!graphics || !graphics->IsInitialized())
  298. return;
  299. graphics_ = graphics;
  300. // Set the initial activation
  301. activated_ = true;
  302. initialized_ = true;
  303. #ifdef USE_OPENGL
  304. {
  305. MutexLock lock(GetStaticMutex());
  306. // Store window ID to direct SDL events to the correct instance
  307. windowID_ = SDL_GetWindowID(graphics_->GetImpl()->GetWindow());
  308. inputInstances[windowID_] = this;
  309. }
  310. #endif
  311. LOGINFO("Initialized input");
  312. }
  313. void Input::MakeActive()
  314. {
  315. if (!graphics_ || !graphics_->IsInitialized())
  316. return;
  317. ResetState();
  318. active_ = true;
  319. activated_ = false;
  320. // Re-establish mouse cursor clipping as necessary
  321. #ifdef USE_OPENGL
  322. SDL_ShowCursor(SDL_FALSE);
  323. suppressNextMouseMove_ = true;
  324. #else
  325. SetClipCursor(true);
  326. SetCursorVisible(false);
  327. #endif
  328. using namespace Activation;
  329. VariantMap eventData;
  330. eventData[P_ACTIVE] = active_;
  331. eventData[P_MINIMIZED] = minimized_;
  332. SendEvent(E_ACTIVATION, eventData);
  333. }
  334. void Input::MakeInactive()
  335. {
  336. if (!graphics_ || !graphics_->IsInitialized())
  337. return;
  338. ResetState();
  339. active_ = false;
  340. activated_ = false;
  341. // Free and show the mouse cursor
  342. #ifndef USE_OPENGL
  343. ReleaseCapture();
  344. ClipCursor(0);
  345. SetCursorVisible(true);
  346. #else
  347. SDL_ShowCursor(SDL_TRUE);
  348. #endif
  349. using namespace Activation;
  350. VariantMap eventData;
  351. eventData[P_ACTIVE] = active_;
  352. eventData[P_MINIMIZED] = minimized_;
  353. SendEvent(E_ACTIVATION, eventData);
  354. }
  355. void Input::ResetState()
  356. {
  357. keyDown_.Clear();
  358. keyPress_.Clear();
  359. // When clearing touch states, send the corresponding touch end events
  360. for (Map<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  361. {
  362. TouchState& state = i->second_;
  363. using namespace TouchEnd;
  364. VariantMap eventData;
  365. eventData[P_TOUCHID] = state.touchID_;
  366. eventData[P_X] = state.position_.x_;
  367. eventData[P_Y] = state.position_.y_;
  368. SendEvent(E_TOUCHEND, eventData);
  369. }
  370. // Use SetMouseButton() to reset the state so that mouse events will be sent properly
  371. SetMouseButton(MOUSEB_LEFT, false);
  372. SetMouseButton(MOUSEB_RIGHT, false);
  373. SetMouseButton(MOUSEB_MIDDLE, false);
  374. mouseMove_ = IntVector2::ZERO;
  375. mouseMoveWheel_ = 0;
  376. mouseButtonPress_ = 0;
  377. }
  378. void Input::SetMouseButton(int button, bool newState)
  379. {
  380. // If we are not active yet, do not react to the mouse button down
  381. if (newState && !active_)
  382. return;
  383. if (newState)
  384. {
  385. if (!(mouseButtonDown_ & button))
  386. mouseButtonPress_ |= button;
  387. mouseButtonDown_ |= button;
  388. }
  389. else
  390. {
  391. if (!(mouseButtonDown_ & button))
  392. return;
  393. mouseButtonDown_ &= ~button;
  394. }
  395. using namespace MouseButtonDown;
  396. VariantMap eventData;
  397. eventData[P_BUTTON] = button;
  398. eventData[P_BUTTONS] = mouseButtonDown_;
  399. eventData[P_QUALIFIERS] = GetQualifiers();
  400. SendEvent(newState ? E_MOUSEBUTTONDOWN : E_MOUSEBUTTONUP, eventData);
  401. }
  402. void Input::SetKey(int key, bool newState)
  403. {
  404. // If we are not active yet, do not react to the key down
  405. if (newState && !active_)
  406. return;
  407. bool repeat = false;
  408. if (newState)
  409. {
  410. if (!keyDown_.Contains(key))
  411. {
  412. keyDown_.Insert(key);
  413. keyPress_.Insert(key);
  414. }
  415. else
  416. repeat = true;
  417. }
  418. else
  419. {
  420. if (!keyDown_.Erase(key))
  421. return;
  422. }
  423. using namespace KeyDown;
  424. VariantMap eventData;
  425. eventData[P_KEY] = key;
  426. eventData[P_BUTTONS] = mouseButtonDown_;
  427. eventData[P_QUALIFIERS] = GetQualifiers();
  428. if (newState)
  429. eventData[P_REPEAT] = repeat;
  430. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  431. if (key == KEY_RETURN && newState && !repeat && toggleFullscreen_ && (GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT)))
  432. graphics_->ToggleFullscreen();
  433. }
  434. void Input::SetMouseWheel(int delta)
  435. {
  436. // If we are not active yet, do not react to the wheel
  437. if (!active_)
  438. return;
  439. if (delta)
  440. {
  441. mouseMoveWheel_ += delta;
  442. using namespace MouseWheel;
  443. VariantMap eventData;
  444. eventData[P_WHEEL] = delta;
  445. eventData[P_BUTTONS] = mouseButtonDown_;
  446. eventData[P_QUALIFIERS] = GetQualifiers();
  447. SendEvent(E_MOUSEWHEEL, eventData);
  448. }
  449. }
  450. void Input::SetCursorPosition(const IntVector2& position)
  451. {
  452. if (!graphics_)
  453. return;
  454. #ifdef USE_OPENGL
  455. SDL_WarpMouseInWindow(graphics_->GetImpl()->GetWindow(), position.x_, position.y_);
  456. #else
  457. POINT point;
  458. point.x = position.x_;
  459. point.y = position.y_;
  460. ClientToScreen((HWND)graphics_->GetWindowHandle(), &point);
  461. SetCursorPos(point.x, point.y);
  462. #endif
  463. }
  464. IntVector2 Input::GetCursorPosition() const
  465. {
  466. IntVector2 ret = lastCursorPosition_;
  467. if (!graphics_ || !graphics_->IsInitialized())
  468. return ret;
  469. #ifdef USE_OPENGL
  470. SDL_GetMouseState(&ret.x_, &ret.y_);
  471. #else
  472. POINT mouse;
  473. GetCursorPos(&mouse);
  474. ScreenToClient((HWND)graphics_->GetWindowHandle(), &mouse);
  475. ret.x_ = mouse.x;
  476. ret.y_ = mouse.y;
  477. #endif
  478. return ret;
  479. }
  480. #ifndef USE_OPENGL
  481. void Input::SetClipCursor(bool enable)
  482. {
  483. if (!graphics_)
  484. return;
  485. if (!graphics_->GetFullscreen() && active_ && enable)
  486. {
  487. SetCursorPosition(IntVector2(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2));
  488. lastCursorPosition_ = GetCursorPosition();
  489. RECT clipRect;
  490. GetWindowRect((HWND)graphics_->GetWindowHandle(), &clipRect);
  491. ClipCursor(&clipRect);
  492. }
  493. else
  494. {
  495. if (graphics_->GetFullscreen() && active_ && enable)
  496. {
  497. SetCursorPosition(IntVector2(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2));
  498. lastCursorPosition_ = GetCursorPosition();
  499. }
  500. ClipCursor(0);
  501. }
  502. }
  503. void Input::SetCursorVisible(bool enable)
  504. {
  505. if (!graphics_)
  506. return;
  507. // When inactive, always show the cursor
  508. if (!active_)
  509. enable = true;
  510. if (showCursor_ == enable)
  511. return;
  512. ShowCursor(enable ? TRUE : FALSE);
  513. showCursor_ = enable;
  514. }
  515. void Input::HandleWindowMessage(StringHash eventType, VariantMap& eventData)
  516. {
  517. if (!initialized_)
  518. Initialize();
  519. using namespace WindowMessage;
  520. int msg = eventData[P_MSG].GetInt();
  521. int wParam = eventData[P_WPARAM].GetInt();
  522. int lParam = eventData[P_LPARAM].GetInt();
  523. int keyCode;
  524. switch (msg)
  525. {
  526. case WM_LBUTTONDOWN:
  527. SetMouseButton(MOUSEB_LEFT, true);
  528. eventData[P_HANDLED] = true;
  529. break;
  530. case WM_NCLBUTTONUP:
  531. case WM_LBUTTONUP:
  532. SetMouseButton(MOUSEB_LEFT, false);
  533. eventData[P_HANDLED] = true;
  534. break;
  535. case WM_RBUTTONDOWN:
  536. SetMouseButton(MOUSEB_RIGHT, true);
  537. eventData[P_HANDLED] = true;
  538. break;
  539. case WM_NCRBUTTONUP:
  540. case WM_RBUTTONUP:
  541. SetMouseButton(MOUSEB_RIGHT, false);
  542. eventData[P_HANDLED] = true;
  543. break;
  544. case WM_MBUTTONDOWN:
  545. SetMouseButton(MOUSEB_MIDDLE, true);
  546. eventData[P_HANDLED] = true;
  547. break;
  548. case WM_NCMBUTTONUP:
  549. case WM_MBUTTONUP:
  550. SetMouseButton(MOUSEB_MIDDLE, false);
  551. eventData[P_HANDLED] = true;
  552. break;
  553. case WM_MOUSEWHEEL:
  554. SetMouseWheel(wParam >> 16);
  555. eventData[P_HANDLED] = true;
  556. break;
  557. case WM_ACTIVATE:
  558. minimized_ = HIWORD(wParam) != 0;
  559. if (LOWORD(wParam) == WA_INACTIVE)
  560. MakeInactive();
  561. else
  562. {
  563. if (!minimized_)
  564. activated_ = true;
  565. }
  566. eventData[P_HANDLED] = true;
  567. break;
  568. case WM_KEYDOWN:
  569. keyCode = ConvertKeyCode(wParam, lParam);
  570. if (keyCode)
  571. SetKey(keyCode, true);
  572. eventData[P_HANDLED] = true;
  573. break;
  574. case WM_SYSKEYDOWN:
  575. keyCode = ConvertKeyCode(wParam, lParam);
  576. if (keyCode)
  577. SetKey(keyCode, true);
  578. if (keyCode != KEY_F4)
  579. eventData[P_HANDLED] = true;
  580. break;
  581. case WM_KEYUP:
  582. case WM_SYSKEYUP:
  583. keyCode = ConvertKeyCode(wParam, lParam);
  584. if (keyCode)
  585. SetKey(keyCode, false);
  586. eventData[P_HANDLED] = true;
  587. break;
  588. case WM_CHAR:
  589. {
  590. using namespace Char;
  591. VariantMap keyEventData;
  592. keyEventData[P_CHAR] = wParam;
  593. keyEventData[P_BUTTONS] = mouseButtonDown_;
  594. keyEventData[P_QUALIFIERS] = GetQualifiers();
  595. SendEvent(E_CHAR, keyEventData);
  596. }
  597. eventData[P_HANDLED] = true;
  598. break;
  599. }
  600. }
  601. #else
  602. void Input::HandleSDLEvent(void* sdlEvent)
  603. {
  604. SDL_Event& evt = *static_cast<SDL_Event*>(sdlEvent);
  605. Input* input = 0;
  606. switch (evt.type)
  607. {
  608. case SDL_KEYDOWN:
  609. // Convert to uppercase to match Win32 virtual key codes
  610. input = GetInputInstance(evt.key.windowID);
  611. if (input)
  612. input->SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), true);
  613. break;
  614. case SDL_KEYUP:
  615. input = GetInputInstance(evt.key.windowID);
  616. if (input)
  617. input->SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), false);
  618. break;
  619. case SDL_TEXTINPUT:
  620. input = GetInputInstance(evt.text.windowID);
  621. if (input && evt.text.text[0])
  622. {
  623. String text(&evt.text.text[0]);
  624. unsigned unicode = text.AtUTF8(0);
  625. if (unicode)
  626. {
  627. using namespace Char;
  628. VariantMap keyEventData;
  629. keyEventData[P_CHAR] = unicode;
  630. keyEventData[P_BUTTONS] = input->mouseButtonDown_;
  631. keyEventData[P_QUALIFIERS] = input->GetQualifiers();
  632. input->SendEvent(E_CHAR, keyEventData);
  633. }
  634. }
  635. break;
  636. case SDL_MOUSEBUTTONDOWN:
  637. input = GetInputInstance(evt.button.windowID);
  638. if (input)
  639. input->SetMouseButton(1 << (evt.button.button - 1), true);
  640. break;
  641. case SDL_MOUSEBUTTONUP:
  642. input = GetInputInstance(evt.button.windowID);
  643. if (input)
  644. input->SetMouseButton(1 << (evt.button.button - 1), false);
  645. break;
  646. case SDL_MOUSEWHEEL:
  647. input = GetInputInstance(evt.wheel.windowID);
  648. if (input)
  649. input->SetMouseWheel(evt.wheel.y);
  650. break;
  651. case SDL_FINGERDOWN:
  652. input = GetInputInstance(evt.tfinger.windowID);
  653. if (input)
  654. {
  655. int touchID = (int)evt.tfinger.fingerId;
  656. TouchState& state = input->touches_[touchID];
  657. state.touchID_ = touchID;
  658. state.delta_ = state.position_ = IntVector2(evt.tfinger.x * input->graphics_->GetWidth() / 32768,
  659. evt.tfinger.y * input->graphics_->GetHeight() / 32768);
  660. state.pressure_ = evt.tfinger.pressure;
  661. using namespace TouchBegin;
  662. VariantMap eventData;
  663. eventData[P_TOUCHID] = touchID;
  664. eventData[P_X] = state.position_.x_;
  665. eventData[P_Y] = state.position_.y_;
  666. eventData[P_PRESSURE] = state.pressure_;
  667. input->SendEvent(E_TOUCHBEGIN, eventData);
  668. }
  669. break;
  670. case SDL_FINGERUP:
  671. input = GetInputInstance(evt.tfinger.windowID);
  672. if (input)
  673. {
  674. int touchID = (int)evt.tfinger.fingerId;
  675. input->touches_.Erase(touchID);
  676. using namespace TouchEnd;
  677. VariantMap eventData;
  678. eventData[P_TOUCHID] = touchID;
  679. eventData[P_X] = evt.tfinger.x * input->graphics_->GetWidth() / 32768;
  680. eventData[P_Y] = evt.tfinger.y * input->graphics_->GetHeight() / 32768;
  681. input->SendEvent(E_TOUCHEND, eventData);
  682. }
  683. break;
  684. case SDL_FINGERMOTION:
  685. input = GetInputInstance(evt.tfinger.windowID);
  686. if (input)
  687. {
  688. int touchID = (int)evt.tfinger.fingerId;
  689. TouchState& state = input->touches_[touchID];
  690. state.touchID_ = touchID;
  691. state.position_ = IntVector2(evt.tfinger.x * input->graphics_->GetWidth() / 32768,
  692. evt.tfinger.y * input->graphics_->GetHeight() / 32768);
  693. state.pressure_ = evt.tfinger.pressure;
  694. using namespace TouchMove;
  695. VariantMap eventData;
  696. eventData[P_TOUCHID] = touchID;
  697. eventData[P_X] = state.position_.x_;
  698. eventData[P_Y] = state.position_.y_;
  699. eventData[P_DX] = evt.tfinger.dx * input->graphics_->GetWidth() / 32768;
  700. eventData[P_DY] = evt.tfinger.dy * input->graphics_->GetHeight() / 32768;
  701. eventData[P_PRESSURE] = state.pressure_;
  702. input->SendEvent(E_TOUCHMOVE, eventData);
  703. }
  704. break;
  705. case SDL_WINDOWEVENT:
  706. if (evt.window.event == SDL_WINDOWEVENT_CLOSE)
  707. {
  708. input = GetInputInstance(evt.window.windowID);
  709. if (input)
  710. input->GetSubsystem<Graphics>()->Close();
  711. }
  712. break;
  713. }
  714. }
  715. #endif
  716. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  717. {
  718. // Reset input state on subsequent initializations
  719. if (!initialized_)
  720. Initialize();
  721. else
  722. ResetState();
  723. // Re-enable cursor clipping, and re-center the cursor (if needed) to the new screen size, so that there is no erroneous
  724. // mouse move event. Also, in SDL mode reset the window ID
  725. #ifdef USE_OPENGL
  726. unsigned newWindowID = SDL_GetWindowID(graphics_->GetImpl()->GetWindow());
  727. if (newWindowID != windowID_)
  728. {
  729. MutexLock lock(GetStaticMutex());
  730. inputInstances.Erase(windowID_);
  731. inputInstances[newWindowID] = this;
  732. windowID_ = newWindowID;
  733. }
  734. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  735. SetCursorPosition(center);
  736. lastCursorPosition_ = center;
  737. activated_ = true;
  738. #else
  739. SetClipCursor(true);
  740. #endif
  741. }
  742. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  743. {
  744. // Update input right at the beginning of the frame
  745. if (initialized_)
  746. Update();
  747. }