Input.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "CoreEvents.h"
  25. #include "FileSystem.h"
  26. #include "Graphics.h"
  27. #include "GraphicsEvents.h"
  28. #include "GraphicsImpl.h"
  29. #include "Input.h"
  30. #include "Log.h"
  31. #include "Mutex.h"
  32. #include "ProcessUtils.h"
  33. #include "Profiler.h"
  34. #include "ResourceCache.h"
  35. #include "RWOpsWrapper.h"
  36. #include "StringUtils.h"
  37. #include "Text.h"
  38. #include "UI.h"
  39. #include <cstring>
  40. #include <SDL.h>
  41. #include "DebugNew.h"
  42. extern "C" int SDL_AddTouch(SDL_TouchID touchID, const char *name);
  43. // Require a click inside window before re-hiding mouse cursor on OSX, otherwise dragging the window
  44. // can be incorrectly interpreted as mouse movement inside the window
  45. #if defined(__APPLE__) && !defined(IOS)
  46. #define REQUIRE_CLICK_TO_FOCUS
  47. #endif
  48. namespace Urho3D
  49. {
  50. const int SCREEN_JOYSTICK_START_ID = 0x40000000;
  51. const StringHash VAR_BUTTON_KEY_BINDING("VAR_BUTTON_KEY_BINDING");
  52. const StringHash VAR_BUTTON_MOUSE_BUTTON_BINDING("VAR_BUTTON_MOUSE_BUTTON_BINDING");
  53. const StringHash VAR_LAST_KEYSYM("VAR_LAST_KEYSYM");
  54. const StringHash VAR_SCREEN_JOYSTICK_ID("VAR_SCREEN_JOYSTICK_ID");
  55. /// Convert SDL keycode if necessary.
  56. int ConvertSDLKeyCode(int keySym, int scanCode)
  57. {
  58. if (scanCode == SCANCODE_AC_BACK)
  59. return KEY_ESC;
  60. else
  61. return SDL_toupper(keySym);
  62. }
  63. UIElement* TouchState::GetTouchedElement()
  64. {
  65. return touchedElement_.Get();
  66. }
  67. void JoystickState::Initialize(unsigned numButtons, unsigned numAxes, unsigned numHats)
  68. {
  69. buttons_.Resize(numButtons);
  70. buttonPress_.Resize(numButtons);
  71. axes_.Resize(numAxes);
  72. hats_.Resize(numHats);
  73. Reset();
  74. }
  75. void JoystickState::Reset()
  76. {
  77. for (unsigned i = 0; i < buttons_.Size(); ++i)
  78. {
  79. buttons_[i] = false;
  80. buttonPress_[i] = false;
  81. }
  82. for (unsigned i = 0; i < axes_.Size(); ++i)
  83. axes_[i] = 0.0f;
  84. for (unsigned i = 0; i < hats_.Size(); ++i)
  85. hats_[i] = HAT_CENTER;
  86. }
  87. Input::Input(Context* context) :
  88. Object(context),
  89. mouseButtonDown_(0),
  90. mouseButtonPress_(0),
  91. mouseMoveWheel_(0),
  92. windowID_(0),
  93. toggleFullscreen_(true),
  94. mouseVisible_(false),
  95. mouseGrabbed_(false),
  96. touchEmulation_(false),
  97. inputFocus_(false),
  98. minimized_(false),
  99. focusedThisFrame_(false),
  100. suppressNextMouseMove_(false),
  101. initialized_(false)
  102. {
  103. SubscribeToEvent(E_SCREENMODE, HANDLER(Input, HandleScreenMode));
  104. // Try to initialize right now, but skip if screen mode is not yet set
  105. Initialize();
  106. }
  107. Input::~Input()
  108. {
  109. }
  110. void Input::Update()
  111. {
  112. assert(initialized_);
  113. PROFILE(UpdateInput);
  114. // Reset input accumulation for this frame
  115. keyPress_.Clear();
  116. scancodePress_.Clear();
  117. mouseButtonPress_ = 0;
  118. mouseMove_ = IntVector2::ZERO;
  119. mouseMoveWheel_ = 0;
  120. for (HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  121. {
  122. for (unsigned j = 0; j < i->second_.buttonPress_.Size(); ++j)
  123. i->second_.buttonPress_[j] = false;
  124. }
  125. // Reset touch delta movement
  126. for (HashMap<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  127. {
  128. TouchState& state = i->second_;
  129. state.lastPosition_ = state.position_;
  130. state.delta_ = IntVector2::ZERO;
  131. }
  132. // Check and handle SDL events
  133. SDL_PumpEvents();
  134. SDL_Event evt;
  135. while (SDL_PeepEvents(&evt, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0)
  136. HandleSDLEvent(&evt);
  137. // Check for activation and inactivation from SDL window flags. Must nullcheck the window pointer because it may have
  138. // been closed due to input events
  139. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  140. unsigned flags = window ? SDL_GetWindowFlags(window) & (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS) : 0;
  141. if (window)
  142. {
  143. #ifdef REQUIRE_CLICK_TO_FOCUS
  144. if (!inputFocus_ && (graphics_->GetFullscreen() || mouseVisible_) && flags == (SDL_WINDOW_INPUT_FOCUS |
  145. SDL_WINDOW_MOUSE_FOCUS))
  146. #else
  147. if (!inputFocus_ && (flags & SDL_WINDOW_INPUT_FOCUS))
  148. #endif
  149. focusedThisFrame_ = true;
  150. if (focusedThisFrame_)
  151. GainFocus();
  152. if (inputFocus_ && (flags & SDL_WINDOW_INPUT_FOCUS) == 0)
  153. LoseFocus();
  154. }
  155. else
  156. return;
  157. // Check for relative mode mouse move
  158. if (!touchEmulation_ && (graphics_->GetExternalWindow() || (!mouseVisible_ && inputFocus_ && (flags & SDL_WINDOW_MOUSE_FOCUS))))
  159. {
  160. IntVector2 mousePosition = GetMousePosition();
  161. mouseMove_ = mousePosition - lastMousePosition_;
  162. if (graphics_->GetExternalWindow())
  163. lastMousePosition_ = mousePosition;
  164. else
  165. {
  166. // Recenter the mouse cursor manually after move
  167. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  168. if (mousePosition != center)
  169. {
  170. SetMousePosition(center);
  171. lastMousePosition_ = center;
  172. }
  173. }
  174. // Send mouse move event if necessary
  175. if (mouseMove_ != IntVector2::ZERO)
  176. {
  177. if (suppressNextMouseMove_)
  178. {
  179. mouseMove_ = IntVector2::ZERO;
  180. suppressNextMouseMove_ = false;
  181. }
  182. else
  183. {
  184. using namespace MouseMove;
  185. VariantMap& eventData = GetEventDataMap();
  186. if (mouseVisible_)
  187. {
  188. eventData[P_X] = mousePosition.x_;
  189. eventData[P_Y] = mousePosition.y_;
  190. }
  191. eventData[P_DX] = mouseMove_.x_;
  192. eventData[P_DY] = mouseMove_.y_;
  193. eventData[P_BUTTONS] = mouseButtonDown_;
  194. eventData[P_QUALIFIERS] = GetQualifiers();
  195. SendEvent(E_MOUSEMOVE, eventData);
  196. }
  197. }
  198. }
  199. }
  200. void Input::SetMouseVisible(bool enable)
  201. {
  202. // In touch emulation mode only enabled mouse is allowed
  203. if (touchEmulation_)
  204. enable = true;
  205. // SDL Raspberry Pi "video driver" does not have proper OS mouse support yet, so no-op for now
  206. #ifndef RASPI
  207. if (enable != mouseVisible_)
  208. {
  209. mouseVisible_ = enable;
  210. if (initialized_)
  211. {
  212. // External windows can only support visible mouse cursor
  213. if (graphics_->GetExternalWindow())
  214. {
  215. mouseVisible_ = true;
  216. return;
  217. }
  218. if (!mouseVisible_ && inputFocus_)
  219. {
  220. SDL_ShowCursor(SDL_FALSE);
  221. // Recenter the mouse cursor manually when hiding it to avoid erratic mouse move for one frame
  222. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  223. SetMousePosition(center);
  224. lastMousePosition_ = center;
  225. }
  226. else
  227. SDL_ShowCursor(SDL_TRUE);
  228. }
  229. using namespace MouseVisibleChanged;
  230. VariantMap& eventData = GetEventDataMap();
  231. eventData[P_VISIBLE] = mouseVisible_;
  232. SendEvent(E_MOUSEVISIBLECHANGED, eventData);
  233. }
  234. #endif
  235. }
  236. void Input::SetMouseGrabbed(bool grab)
  237. {
  238. mouseGrabbed_ = grab;
  239. }
  240. void Input::SetToggleFullscreen(bool enable)
  241. {
  242. toggleFullscreen_ = enable;
  243. }
  244. static void PopulateKeyBindingMap(HashMap<String, int>& keyBindingMap)
  245. {
  246. if (keyBindingMap.Empty())
  247. {
  248. keyBindingMap.Insert(MakePair<String, int>("SPACE", KEY_SPACE));
  249. keyBindingMap.Insert(MakePair<String, int>("LCTRL", KEY_LCTRL));
  250. keyBindingMap.Insert(MakePair<String, int>("RCTRL", KEY_RCTRL));
  251. keyBindingMap.Insert(MakePair<String, int>("LSHIFT", KEY_LSHIFT));
  252. keyBindingMap.Insert(MakePair<String, int>("RSHIFT", KEY_RSHIFT));
  253. keyBindingMap.Insert(MakePair<String, int>("LALT", KEY_LALT));
  254. keyBindingMap.Insert(MakePair<String, int>("RALT", KEY_RALT));
  255. keyBindingMap.Insert(MakePair<String, int>("LGUI", KEY_LGUI));
  256. keyBindingMap.Insert(MakePair<String, int>("RGUI", KEY_RGUI));
  257. keyBindingMap.Insert(MakePair<String, int>("TAB", KEY_TAB));
  258. keyBindingMap.Insert(MakePair<String, int>("RETURN", KEY_RETURN));
  259. keyBindingMap.Insert(MakePair<String, int>("RETURN2", KEY_RETURN2));
  260. keyBindingMap.Insert(MakePair<String, int>("ENTER", KEY_KP_ENTER));
  261. keyBindingMap.Insert(MakePair<String, int>("SELECT", KEY_SELECT));
  262. keyBindingMap.Insert(MakePair<String, int>("LEFT", KEY_LEFT));
  263. keyBindingMap.Insert(MakePair<String, int>("RIGHT", KEY_RIGHT));
  264. keyBindingMap.Insert(MakePair<String, int>("UP", KEY_UP));
  265. keyBindingMap.Insert(MakePair<String, int>("DOWN", KEY_DOWN));
  266. keyBindingMap.Insert(MakePair<String, int>("PAGEUP", KEY_PAGEUP));
  267. keyBindingMap.Insert(MakePair<String, int>("PAGEDOWN", KEY_PAGEDOWN));
  268. keyBindingMap.Insert(MakePair<String, int>("F1", KEY_F1));
  269. keyBindingMap.Insert(MakePair<String, int>("F2", KEY_F2));
  270. keyBindingMap.Insert(MakePair<String, int>("F3", KEY_F3));
  271. keyBindingMap.Insert(MakePair<String, int>("F4", KEY_F4));
  272. keyBindingMap.Insert(MakePair<String, int>("F5", KEY_F5));
  273. keyBindingMap.Insert(MakePair<String, int>("F6", KEY_F6));
  274. keyBindingMap.Insert(MakePair<String, int>("F7", KEY_F7));
  275. keyBindingMap.Insert(MakePair<String, int>("F8", KEY_F8));
  276. keyBindingMap.Insert(MakePair<String, int>("F9", KEY_F9));
  277. keyBindingMap.Insert(MakePair<String, int>("F10", KEY_F10));
  278. keyBindingMap.Insert(MakePair<String, int>("F11", KEY_F11));
  279. keyBindingMap.Insert(MakePair<String, int>("F12", KEY_F12));
  280. }
  281. }
  282. static void PopulateMouseButtonBindingMap(HashMap<String, int>& mouseButtonBindingMap)
  283. {
  284. if (mouseButtonBindingMap.Empty())
  285. {
  286. mouseButtonBindingMap.Insert(MakePair<String, int>("LEFT", SDL_BUTTON_LEFT));
  287. mouseButtonBindingMap.Insert(MakePair<String, int>("MIDDLE", SDL_BUTTON_MIDDLE));
  288. mouseButtonBindingMap.Insert(MakePair<String, int>("RIGHT", SDL_BUTTON_RIGHT));
  289. mouseButtonBindingMap.Insert(MakePair<String, int>("X1", SDL_BUTTON_X1));
  290. mouseButtonBindingMap.Insert(MakePair<String, int>("X2", SDL_BUTTON_X2));
  291. }
  292. }
  293. SDL_JoystickID Input::AddScreenJoystick(XMLFile* layoutFile, XMLFile* styleFile)
  294. {
  295. static HashMap<String, int> keyBindingMap;
  296. static HashMap<String, int> mouseButtonBindingMap;
  297. if (!graphics_)
  298. {
  299. LOGWARNING("Cannot add screen joystick in headless mode");
  300. return -1;
  301. }
  302. // If layout file is not given, use the default screen joystick layout
  303. if (!layoutFile)
  304. {
  305. ResourceCache* cache = GetSubsystem<ResourceCache>();
  306. layoutFile = cache->GetResource<XMLFile>("UI/ScreenJoystick.xml");
  307. if (!layoutFile) // Error is already logged
  308. return -1;
  309. }
  310. UI* ui = GetSubsystem<UI>();
  311. SharedPtr<UIElement> screenJoystick = ui->LoadLayout(layoutFile, styleFile);
  312. if (!screenJoystick) // Error is already logged
  313. return -1;
  314. screenJoystick->SetSize(ui->GetRoot()->GetSize());
  315. ui->GetRoot()->AddChild(screenJoystick);
  316. // Get an unused ID for the screen joystick
  317. /// \todo After a real joystick has been plugged in 1073741824 times, the ranges will overlap
  318. SDL_JoystickID joystickID = SCREEN_JOYSTICK_START_ID;
  319. while (joysticks_.Contains(joystickID))
  320. ++joystickID;
  321. JoystickState& state = joysticks_[joystickID];
  322. state.joystickID_ = joystickID;
  323. state.name_ = screenJoystick->GetName();
  324. state.screenJoystick_ = screenJoystick;
  325. unsigned numButtons = 0;
  326. unsigned numAxes = 0;
  327. unsigned numHats = 0;
  328. const Vector<SharedPtr<UIElement> >& children = state.screenJoystick_->GetChildren();
  329. for (Vector<SharedPtr<UIElement> >::ConstIterator iter = children.Begin(); iter != children.End(); ++iter)
  330. {
  331. UIElement* element = iter->Get();
  332. String name = element->GetName();
  333. if (name.StartsWith("Button"))
  334. {
  335. ++numButtons;
  336. // Check whether the button has key binding
  337. Text* text = dynamic_cast<Text*>(element->GetChild("KeyBinding", false));
  338. if (text)
  339. {
  340. text->SetVisible(false);
  341. const String& key = text->GetText();
  342. int keyBinding;
  343. if (key.Length() == 1)
  344. keyBinding = key[0];
  345. else
  346. {
  347. PopulateKeyBindingMap(keyBindingMap);
  348. HashMap<String, int>::Iterator i = keyBindingMap.Find(key);
  349. if (i != keyBindingMap.End())
  350. keyBinding = i->second_;
  351. else
  352. {
  353. LOGERRORF("Unsupported key binding: %s", key.CString());
  354. keyBinding = M_MAX_INT;
  355. }
  356. }
  357. if (keyBinding != M_MAX_INT)
  358. element->SetVar(VAR_BUTTON_KEY_BINDING, keyBinding);
  359. }
  360. // Check whether the button has mouse button binding
  361. text = dynamic_cast<Text*>(element->GetChild("MouseButtonBinding", false));
  362. if (text)
  363. {
  364. text->SetVisible(false);
  365. const String& mouseButton = text->GetText();
  366. PopulateMouseButtonBindingMap(mouseButtonBindingMap);
  367. HashMap<String, int>::Iterator i = mouseButtonBindingMap.Find(mouseButton);
  368. if (i != mouseButtonBindingMap.End())
  369. element->SetVar(VAR_BUTTON_MOUSE_BUTTON_BINDING, i->second_);
  370. else
  371. LOGERRORF("Unsupported mouse button binding: %s", mouseButton.CString());
  372. }
  373. }
  374. else if (name.StartsWith("Axis"))
  375. {
  376. ++numAxes;
  377. ///\todo Axis emulation for screen joystick is not fully supported yet.
  378. LOGWARNING("Axis emulation for screen joystick is not fully supported yet");
  379. }
  380. else if (name.StartsWith("Hat"))
  381. {
  382. ++numHats;
  383. Text* text = dynamic_cast<Text*>(element->GetChild("KeyBinding", false));
  384. if (text)
  385. {
  386. text->SetVisible(false);
  387. String keyBinding = text->GetText();
  388. if (keyBinding.Contains(' ')) // e.g.: "UP DOWN LEFT RIGHT"
  389. {
  390. // Attempt to split the text using ' ' as separator
  391. Vector<String>keyBindings(keyBinding.Split(' '));
  392. String mappedKeyBinding;
  393. if (keyBindings.Size() == 4)
  394. {
  395. PopulateKeyBindingMap(keyBindingMap);
  396. for (unsigned j = 0; j < 4; ++j)
  397. {
  398. if (keyBindings[j].Length() == 1)
  399. mappedKeyBinding.Append(keyBindings[j][0]);
  400. else
  401. {
  402. HashMap<String, int>::Iterator i = keyBindingMap.Find(keyBindings[j]);
  403. if (i != keyBindingMap.End())
  404. mappedKeyBinding.Append(i->second_);
  405. else
  406. break;
  407. }
  408. }
  409. }
  410. if (mappedKeyBinding.Length() != 4)
  411. {
  412. LOGERRORF("%s has invalid key binding %s, fallback to WSAD", name.CString(), keyBinding.CString());
  413. keyBinding = "WSAD";
  414. }
  415. else
  416. keyBinding = mappedKeyBinding;
  417. }
  418. else if (keyBinding.Length() != 4)
  419. {
  420. LOGERRORF("%s has invalid key binding %s, fallback to WSAD", name.CString(), keyBinding.CString());
  421. keyBinding = "WSAD";
  422. }
  423. element->SetVar(VAR_BUTTON_KEY_BINDING, keyBinding);
  424. }
  425. }
  426. element->SetVar(VAR_SCREEN_JOYSTICK_ID, joystickID);
  427. }
  428. // Make sure all the children are non-focusable so they do not mistakenly to be considered as active UI input controls by application
  429. PODVector<UIElement*> allChildren;
  430. state.screenJoystick_->GetChildren(allChildren, true);
  431. for (PODVector<UIElement*>::Iterator iter = allChildren.Begin(); iter != allChildren.End(); ++iter)
  432. (*iter)->SetFocusMode(FM_NOTFOCUSABLE);
  433. state.Initialize(numButtons, numAxes, numHats);
  434. // There could be potentially more than one screen joystick, however they all will be handled by a same handler method
  435. // So there is no harm to replace the old handler with the new handler in each call to SubscribeToEvent()
  436. SubscribeToEvent(E_TOUCHBEGIN, HANDLER(Input, HandleScreenJoystickTouch));
  437. SubscribeToEvent(E_TOUCHMOVE, HANDLER(Input, HandleScreenJoystickTouch));
  438. SubscribeToEvent(E_TOUCHEND, HANDLER(Input, HandleScreenJoystickTouch));
  439. return joystickID;
  440. }
  441. bool Input::RemoveScreenJoystick(SDL_JoystickID id)
  442. {
  443. if (!joysticks_.Contains(id))
  444. {
  445. LOGERRORF("Failed to remove non-existing screen joystick ID #%d", id);
  446. return false;
  447. }
  448. JoystickState& state = joysticks_[id];
  449. if (!state.screenJoystick_)
  450. {
  451. LOGERRORF("Failed to remove joystick with ID #%d which is not a screen joystick", id);
  452. return false;
  453. }
  454. state.screenJoystick_->Remove();
  455. joysticks_.Erase(id);
  456. return true;
  457. }
  458. void Input::SetScreenJoystickVisible(SDL_JoystickID id, bool enable)
  459. {
  460. if (joysticks_.Contains(id))
  461. {
  462. JoystickState& state = joysticks_[id];
  463. if (state.screenJoystick_)
  464. state.screenJoystick_->SetVisible(enable);
  465. }
  466. }
  467. void Input::SetScreenKeyboardVisible(bool enable)
  468. {
  469. if (!graphics_)
  470. return;
  471. if (enable != IsScreenKeyboardVisible())
  472. {
  473. if (enable)
  474. SDL_StartTextInput();
  475. else
  476. SDL_StopTextInput();
  477. }
  478. }
  479. void Input::SetTouchEmulation(bool enable)
  480. {
  481. #if !defined(ANDROID) && !defined(IOS)
  482. if (enable != touchEmulation_)
  483. {
  484. if (enable)
  485. {
  486. // Touch emulation needs the mouse visible
  487. if (!mouseVisible_)
  488. SetMouseVisible(true);
  489. // Add a virtual touch device the first time we are enabling emulated touch
  490. if (!SDL_GetNumTouchDevices())
  491. SDL_AddTouch(0, "Emulated Touch");
  492. }
  493. else
  494. ResetTouches();
  495. touchEmulation_ = enable;
  496. }
  497. #endif
  498. }
  499. bool Input::RecordGesture()
  500. {
  501. // If have no touch devices, fail
  502. if (!SDL_GetNumTouchDevices())
  503. {
  504. LOGERROR("Can not record gesture: no touch devices");
  505. return false;
  506. }
  507. return SDL_RecordGesture(-1) != 0;
  508. }
  509. bool Input::SaveGestures(Serializer& dest)
  510. {
  511. RWOpsWrapper<Serializer> wrapper(dest);
  512. return SDL_SaveAllDollarTemplates(wrapper.GetRWOps()) != 0;
  513. }
  514. bool Input::SaveGesture(Serializer& dest, unsigned gestureID)
  515. {
  516. RWOpsWrapper<Serializer> wrapper(dest);
  517. return SDL_SaveDollarTemplate(gestureID, wrapper.GetRWOps()) != 0;
  518. }
  519. unsigned Input::LoadGestures(Deserializer& source)
  520. {
  521. // If have no touch devices, fail
  522. if (!SDL_GetNumTouchDevices())
  523. {
  524. LOGERROR("Can not load gestures: no touch devices");
  525. return 0;
  526. }
  527. RWOpsWrapper<Deserializer> wrapper(source);
  528. return SDL_LoadDollarTemplates(-1, wrapper.GetRWOps());
  529. }
  530. bool Input::RemoveGesture(unsigned gestureID)
  531. {
  532. return SDL_RemoveDollarTemplate(gestureID) != 0;
  533. }
  534. void Input::RemoveAllGestures()
  535. {
  536. SDL_RemoveAllDollarTemplates();
  537. }
  538. SDL_JoystickID Input::OpenJoystick(unsigned index)
  539. {
  540. SDL_Joystick* joystick = SDL_JoystickOpen(index);
  541. if (!joystick)
  542. {
  543. LOGERRORF("Cannot open joystick #%d", index);
  544. return -1;
  545. }
  546. // Create joystick state for the new joystick
  547. int joystickID = SDL_JoystickInstanceID(joystick);
  548. JoystickState& state = joysticks_[joystickID];
  549. state.joystick_ = joystick;
  550. state.joystickID_ = joystickID;
  551. state.name_ = SDL_JoystickName(joystick);
  552. if (SDL_IsGameController(index))
  553. state.controller_ = SDL_GameControllerOpen(index);
  554. unsigned numButtons = SDL_JoystickNumButtons(joystick);
  555. unsigned numAxes = SDL_JoystickNumAxes(joystick);
  556. unsigned numHats = SDL_JoystickNumHats(joystick);
  557. // When the joystick is a controller, make sure there's enough axes & buttons for the standard controller mappings
  558. if (state.controller_)
  559. {
  560. if (numButtons < SDL_CONTROLLER_BUTTON_MAX)
  561. numButtons = SDL_CONTROLLER_BUTTON_MAX;
  562. if (numAxes < SDL_CONTROLLER_AXIS_MAX)
  563. numAxes = SDL_CONTROLLER_AXIS_MAX;
  564. }
  565. state.Initialize(numButtons, numAxes, numHats);
  566. return joystickID;
  567. }
  568. int Input::GetKeyFromName(const String& name) const
  569. {
  570. return SDL_GetKeyFromName(name.CString());
  571. }
  572. int Input::GetKeyFromScancode(int scancode) const
  573. {
  574. return SDL_GetKeyFromScancode((SDL_Scancode)scancode);
  575. }
  576. String Input::GetKeyName(int key) const
  577. {
  578. return String(SDL_GetKeyName(key));
  579. }
  580. int Input::GetScancodeFromKey(int key) const
  581. {
  582. return SDL_GetScancodeFromKey(key);
  583. }
  584. int Input::GetScancodeFromName(const String& name) const
  585. {
  586. return SDL_GetScancodeFromName(name.CString());
  587. }
  588. String Input::GetScancodeName(int scancode) const
  589. {
  590. return SDL_GetScancodeName((SDL_Scancode)scancode);
  591. }
  592. bool Input::GetKeyDown(int key) const
  593. {
  594. return keyDown_.Contains(SDL_toupper(key));
  595. }
  596. bool Input::GetKeyPress(int key) const
  597. {
  598. return keyPress_.Contains(SDL_toupper(key));
  599. }
  600. bool Input::GetScancodeDown(int scancode) const
  601. {
  602. return scancodeDown_.Contains(scancode);
  603. }
  604. bool Input::GetScancodePress(int scancode) const
  605. {
  606. return scancodePress_.Contains(scancode);
  607. }
  608. bool Input::GetMouseButtonDown(int button) const
  609. {
  610. return (mouseButtonDown_ & button) != 0;
  611. }
  612. bool Input::GetMouseButtonPress(int button) const
  613. {
  614. return (mouseButtonPress_ & button) != 0;
  615. }
  616. bool Input::GetQualifierDown(int qualifier) const
  617. {
  618. if (qualifier == QUAL_SHIFT)
  619. return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
  620. if (qualifier == QUAL_CTRL)
  621. return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
  622. if (qualifier == QUAL_ALT)
  623. return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
  624. return false;
  625. }
  626. bool Input::GetQualifierPress(int qualifier) const
  627. {
  628. if (qualifier == QUAL_SHIFT)
  629. return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
  630. if (qualifier == QUAL_CTRL)
  631. return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
  632. if (qualifier == QUAL_ALT)
  633. return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
  634. return false;
  635. }
  636. int Input::GetQualifiers() const
  637. {
  638. int ret = 0;
  639. if (GetQualifierDown(QUAL_SHIFT))
  640. ret |= QUAL_SHIFT;
  641. if (GetQualifierDown(QUAL_CTRL))
  642. ret |= QUAL_CTRL;
  643. if (GetQualifierDown(QUAL_ALT))
  644. ret |= QUAL_ALT;
  645. return ret;
  646. }
  647. IntVector2 Input::GetMousePosition() const
  648. {
  649. IntVector2 ret = IntVector2::ZERO;
  650. if (!initialized_)
  651. return ret;
  652. SDL_GetMouseState(&ret.x_, &ret.y_);
  653. return ret;
  654. }
  655. TouchState* Input::GetTouch(unsigned index) const
  656. {
  657. if (index >= touches_.Size())
  658. return 0;
  659. HashMap<int, TouchState>::ConstIterator i = touches_.Begin();
  660. while (index--)
  661. ++i;
  662. return const_cast<TouchState*>(&i->second_);
  663. }
  664. JoystickState* Input::GetJoystickByIndex(unsigned index)
  665. {
  666. unsigned compare = 0;
  667. for (HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  668. {
  669. if (compare++ == index)
  670. return &(i->second_);
  671. }
  672. return 0;
  673. }
  674. JoystickState* Input::GetJoystick(SDL_JoystickID id)
  675. {
  676. HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Find(id);
  677. return i != joysticks_.End() ? &(i->second_) : 0;
  678. }
  679. bool Input::IsScreenJoystickVisible(SDL_JoystickID id) const
  680. {
  681. HashMap<SDL_JoystickID, JoystickState>::ConstIterator i = joysticks_.Find(id);
  682. return i != joysticks_.End() && i->second_.screenJoystick_ && i->second_.screenJoystick_->IsVisible();
  683. }
  684. bool Input::GetScreenKeyboardSupport() const
  685. {
  686. return graphics_ ? SDL_HasScreenKeyboardSupport() != 0 : false;
  687. }
  688. bool Input::IsScreenKeyboardVisible() const
  689. {
  690. if (graphics_)
  691. {
  692. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  693. return SDL_IsScreenKeyboardShown(window) != SDL_FALSE;
  694. }
  695. else
  696. return false;
  697. }
  698. bool Input::IsMinimized() const
  699. {
  700. // Return minimized state also when unfocused in fullscreen
  701. if (!inputFocus_ && graphics_ && graphics_->GetFullscreen())
  702. return true;
  703. else
  704. return minimized_;
  705. }
  706. void Input::Initialize()
  707. {
  708. Graphics* graphics = GetSubsystem<Graphics>();
  709. if (!graphics || !graphics->IsInitialized())
  710. return;
  711. graphics_ = graphics;
  712. // In external window mode only visible mouse is supported
  713. if (graphics_->GetExternalWindow())
  714. mouseVisible_ = true;
  715. // Set the initial activation
  716. focusedThisFrame_ = true;
  717. initialized_ = true;
  718. ResetJoysticks();
  719. ResetState();
  720. SubscribeToEvent(E_BEGINFRAME, HANDLER(Input, HandleBeginFrame));
  721. LOGINFO("Initialized input");
  722. }
  723. void Input::ResetJoysticks()
  724. {
  725. joysticks_.Clear();
  726. // Open each detected joystick automatically on startup
  727. int size = SDL_NumJoysticks();
  728. for (int i = 0; i < size; ++i)
  729. OpenJoystick(i);
  730. }
  731. void Input::GainFocus()
  732. {
  733. ResetState();
  734. inputFocus_ = true;
  735. focusedThisFrame_ = false;
  736. // Re-establish mouse cursor hiding as necessary
  737. if (!mouseVisible_)
  738. {
  739. SDL_ShowCursor(SDL_FALSE);
  740. suppressNextMouseMove_ = true;
  741. }
  742. else
  743. lastMousePosition_ = GetMousePosition();
  744. SendInputFocusEvent();
  745. }
  746. void Input::LoseFocus()
  747. {
  748. ResetState();
  749. inputFocus_ = false;
  750. focusedThisFrame_ = false;
  751. // Show the mouse cursor when inactive
  752. SDL_ShowCursor(SDL_TRUE);
  753. SendInputFocusEvent();
  754. }
  755. void Input::ResetState()
  756. {
  757. keyDown_.Clear();
  758. keyPress_.Clear();
  759. scancodeDown_.Clear();
  760. scancodePress_.Clear();
  761. /// \todo Check if resetting joystick state on input focus loss is even necessary
  762. for (HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  763. i->second_.Reset();
  764. ResetTouches();
  765. // Use SetMouseButton() to reset the state so that mouse events will be sent properly
  766. SetMouseButton(MOUSEB_LEFT, false);
  767. SetMouseButton(MOUSEB_RIGHT, false);
  768. SetMouseButton(MOUSEB_MIDDLE, false);
  769. mouseMove_ = IntVector2::ZERO;
  770. mouseMoveWheel_ = 0;
  771. mouseButtonPress_ = 0;
  772. }
  773. void Input::ResetTouches()
  774. {
  775. for (HashMap<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  776. {
  777. TouchState& state = i->second_;
  778. using namespace TouchEnd;
  779. VariantMap& eventData = GetEventDataMap();
  780. eventData[P_TOUCHID] = state.touchID_;
  781. eventData[P_X] = state.position_.x_;
  782. eventData[P_Y] = state.position_.y_;
  783. SendEvent(E_TOUCHEND, eventData);
  784. }
  785. touches_.Clear();
  786. }
  787. void Input::SendInputFocusEvent()
  788. {
  789. using namespace InputFocus;
  790. VariantMap& eventData = GetEventDataMap();
  791. eventData[P_FOCUS] = HasFocus();
  792. eventData[P_MINIMIZED] = IsMinimized();
  793. SendEvent(E_INPUTFOCUS, eventData);
  794. }
  795. void Input::SetMouseButton(int button, bool newState)
  796. {
  797. #ifdef REQUIRE_CLICK_TO_FOCUS
  798. if (!mouseVisible_ && !graphics_->GetFullscreen())
  799. {
  800. if (!inputFocus_ && newState && button == MOUSEB_LEFT)
  801. focusedThisFrame_ = true;
  802. }
  803. #endif
  804. // If we do not have focus yet, do not react to the mouse button down
  805. if (!graphics_->GetExternalWindow() && newState && !inputFocus_)
  806. return;
  807. if (newState)
  808. {
  809. if (!(mouseButtonDown_ & button))
  810. mouseButtonPress_ |= button;
  811. mouseButtonDown_ |= button;
  812. }
  813. else
  814. {
  815. if (!(mouseButtonDown_ & button))
  816. return;
  817. mouseButtonDown_ &= ~button;
  818. }
  819. using namespace MouseButtonDown;
  820. VariantMap& eventData = GetEventDataMap();
  821. eventData[P_BUTTON] = button;
  822. eventData[P_BUTTONS] = mouseButtonDown_;
  823. eventData[P_QUALIFIERS] = GetQualifiers();
  824. SendEvent(newState ? E_MOUSEBUTTONDOWN : E_MOUSEBUTTONUP, eventData);
  825. }
  826. void Input::SetKey(int key, int scancode, unsigned raw, bool newState)
  827. {
  828. // If we do not have focus yet, do not react to the key down
  829. if (!graphics_->GetExternalWindow() && newState && !inputFocus_)
  830. return;
  831. bool repeat = false;
  832. if (newState)
  833. {
  834. scancodeDown_.Insert(scancode);
  835. scancodePress_.Insert(scancode);
  836. if (!keyDown_.Contains(key))
  837. {
  838. keyDown_.Insert(key);
  839. keyPress_.Insert(key);
  840. }
  841. else
  842. repeat = true;
  843. }
  844. else
  845. {
  846. scancodeDown_.Erase(scancode);
  847. if (!keyDown_.Erase(key))
  848. return;
  849. }
  850. using namespace KeyDown;
  851. VariantMap& eventData = GetEventDataMap();
  852. eventData[P_KEY] = key;
  853. eventData[P_SCANCODE] = scancode;
  854. eventData[P_RAW] = raw;
  855. eventData[P_BUTTONS] = mouseButtonDown_;
  856. eventData[P_QUALIFIERS] = GetQualifiers();
  857. if (newState)
  858. eventData[P_REPEAT] = repeat;
  859. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  860. if ((key == KEY_RETURN || key == KEY_RETURN2 || key == KEY_KP_ENTER) && newState && !repeat && toggleFullscreen_ &&
  861. (GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT)))
  862. graphics_->ToggleFullscreen();
  863. }
  864. void Input::SetMouseWheel(int delta)
  865. {
  866. // If we do not have focus yet, do not react to the wheel
  867. if (!graphics_->GetExternalWindow() && !inputFocus_)
  868. return;
  869. if (delta)
  870. {
  871. mouseMoveWheel_ += delta;
  872. using namespace MouseWheel;
  873. VariantMap& eventData = GetEventDataMap();
  874. eventData[P_WHEEL] = delta;
  875. eventData[P_BUTTONS] = mouseButtonDown_;
  876. eventData[P_QUALIFIERS] = GetQualifiers();
  877. SendEvent(E_MOUSEWHEEL, eventData);
  878. }
  879. }
  880. void Input::SetMousePosition(const IntVector2& position)
  881. {
  882. if (!graphics_)
  883. return;
  884. SDL_WarpMouseInWindow(graphics_->GetImpl()->GetWindow(), position.x_, position.y_);
  885. }
  886. void Input::HandleSDLEvent(void* sdlEvent)
  887. {
  888. SDL_Event& evt = *static_cast<SDL_Event*>(sdlEvent);
  889. switch (evt.type)
  890. {
  891. case SDL_KEYDOWN:
  892. // Convert to uppercase to match Win32 virtual key codes
  893. SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), evt.key.keysym.scancode, evt.key.keysym.raw, true);
  894. break;
  895. case SDL_KEYUP:
  896. SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), evt.key.keysym.scancode, evt.key.keysym.raw, false);
  897. break;
  898. case SDL_TEXTINPUT:
  899. {
  900. textInput_ = &evt.text.text[0];
  901. unsigned unicode = textInput_.AtUTF8(0);
  902. if (unicode)
  903. {
  904. using namespace TextInput;
  905. VariantMap textInputEventData;
  906. textInputEventData[P_TEXT] = textInput_;
  907. textInputEventData[P_BUTTONS] = mouseButtonDown_;
  908. textInputEventData[P_QUALIFIERS] = GetQualifiers();
  909. SendEvent(E_TEXTINPUT, textInputEventData);
  910. }
  911. }
  912. break;
  913. case SDL_MOUSEBUTTONDOWN:
  914. if (!touchEmulation_)
  915. SetMouseButton(1 << (evt.button.button - 1), true);
  916. else
  917. {
  918. int x, y;
  919. SDL_GetMouseState(&x, &y);
  920. SDL_Event event;
  921. event.type = SDL_FINGERDOWN;
  922. event.tfinger.touchId = 0;
  923. event.tfinger.fingerId = evt.button.button - 1;
  924. event.tfinger.pressure = 1.0f;
  925. event.tfinger.x = (float)x / (float)graphics_->GetWidth();
  926. event.tfinger.y = (float)y / (float)graphics_->GetHeight();
  927. event.tfinger.dx = 0;
  928. event.tfinger.dy = 0;
  929. SDL_PushEvent(&event);
  930. }
  931. break;
  932. case SDL_MOUSEBUTTONUP:
  933. if (!touchEmulation_)
  934. SetMouseButton(1 << (evt.button.button - 1), false);
  935. else
  936. {
  937. int x, y;
  938. SDL_GetMouseState(&x, &y);
  939. SDL_Event event;
  940. event.type = SDL_FINGERUP;
  941. event.tfinger.touchId = 0;
  942. event.tfinger.fingerId = evt.button.button - 1;
  943. event.tfinger.pressure = 0.0f;
  944. event.tfinger.x = (float)x / (float)graphics_->GetWidth();
  945. event.tfinger.y = (float)y / (float)graphics_->GetHeight();
  946. event.tfinger.dx = 0;
  947. event.tfinger.dy = 0;
  948. SDL_PushEvent(&event);
  949. }
  950. break;
  951. case SDL_MOUSEMOTION:
  952. if (mouseVisible_ && !touchEmulation_)
  953. {
  954. mouseMove_.x_ += evt.motion.xrel;
  955. mouseMove_.y_ += evt.motion.yrel;
  956. using namespace MouseMove;
  957. VariantMap& eventData = GetEventDataMap();
  958. if (mouseVisible_)
  959. {
  960. eventData[P_X] = evt.motion.x;
  961. eventData[P_Y] = evt.motion.y;
  962. }
  963. eventData[P_DX] = evt.motion.xrel;
  964. eventData[P_DY] = evt.motion.yrel;
  965. eventData[P_BUTTONS] = mouseButtonDown_;
  966. eventData[P_QUALIFIERS] = GetQualifiers();
  967. SendEvent(E_MOUSEMOVE, eventData);
  968. }
  969. // Only the left mouse button "finger" moves along with the mouse movement
  970. else if (touchEmulation_ && touches_.Contains(0))
  971. {
  972. int x, y;
  973. SDL_GetMouseState(&x, &y);
  974. SDL_Event event;
  975. event.type = SDL_FINGERMOTION;
  976. event.tfinger.touchId = 0;
  977. event.tfinger.fingerId = 0;
  978. event.tfinger.pressure = 1.0f;
  979. event.tfinger.x = (float)x / (float)graphics_->GetWidth();
  980. event.tfinger.y = (float)y / (float)graphics_->GetHeight();
  981. event.tfinger.dx = (float)evt.motion.xrel / (float)graphics_->GetWidth();
  982. event.tfinger.dy = (float)evt.motion.yrel / (float)graphics_->GetHeight();
  983. SDL_PushEvent(&event);
  984. }
  985. break;
  986. case SDL_MOUSEWHEEL:
  987. if (!touchEmulation_)
  988. SetMouseWheel(evt.wheel.y);
  989. break;
  990. case SDL_FINGERDOWN:
  991. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  992. {
  993. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  994. TouchState& state = touches_[touchID];
  995. state.touchID_ = touchID;
  996. state.lastPosition_ = state.position_ = IntVector2((int)(evt.tfinger.x * graphics_->GetWidth()),
  997. (int)(evt.tfinger.y * graphics_->GetHeight()));
  998. state.delta_ = IntVector2::ZERO;
  999. state.pressure_ = evt.tfinger.pressure;
  1000. using namespace TouchBegin;
  1001. VariantMap& eventData = GetEventDataMap();
  1002. eventData[P_TOUCHID] = touchID;
  1003. eventData[P_X] = state.position_.x_;
  1004. eventData[P_Y] = state.position_.y_;
  1005. eventData[P_PRESSURE] = state.pressure_;
  1006. SendEvent(E_TOUCHBEGIN, eventData);
  1007. }
  1008. break;
  1009. case SDL_FINGERUP:
  1010. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  1011. {
  1012. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  1013. TouchState& state = touches_[touchID];
  1014. using namespace TouchEnd;
  1015. VariantMap& eventData = GetEventDataMap();
  1016. // Do not trust the position in the finger up event. Instead use the last position stored in the
  1017. // touch structure
  1018. eventData[P_TOUCHID] = touchID;
  1019. eventData[P_X] = state.position_.x_;
  1020. eventData[P_Y] = state.position_.y_;
  1021. SendEvent(E_TOUCHEND, eventData);
  1022. touches_.Erase(touchID);
  1023. }
  1024. break;
  1025. case SDL_FINGERMOTION:
  1026. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  1027. {
  1028. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  1029. TouchState& state = touches_[touchID];
  1030. state.touchID_ = touchID;
  1031. state.position_ = IntVector2((int)(evt.tfinger.x * graphics_->GetWidth()),
  1032. (int)(evt.tfinger.y * graphics_->GetHeight()));
  1033. state.delta_ = state.position_ - state.lastPosition_;
  1034. state.pressure_ = evt.tfinger.pressure;
  1035. using namespace TouchMove;
  1036. VariantMap& eventData = GetEventDataMap();
  1037. eventData[P_TOUCHID] = touchID;
  1038. eventData[P_X] = state.position_.x_;
  1039. eventData[P_Y] = state.position_.y_;
  1040. eventData[P_DX] = (int)(evt.tfinger.dx * graphics_->GetWidth());
  1041. eventData[P_DY] = (int)(evt.tfinger.dy * graphics_->GetHeight());
  1042. eventData[P_PRESSURE] = state.pressure_;
  1043. SendEvent(E_TOUCHMOVE, eventData);
  1044. }
  1045. break;
  1046. case SDL_DOLLARRECORD:
  1047. {
  1048. using namespace GestureRecorded;
  1049. VariantMap& eventData = GetEventDataMap();
  1050. eventData[P_GESTUREID] = (int)evt.dgesture.gestureId;
  1051. SendEvent(E_GESTURERECORDED, eventData);
  1052. }
  1053. break;
  1054. case SDL_DOLLARGESTURE:
  1055. {
  1056. using namespace GestureInput;
  1057. VariantMap& eventData = GetEventDataMap();
  1058. eventData[P_GESTUREID] = (int)evt.dgesture.gestureId;
  1059. eventData[P_CENTERX] = (int)(evt.dgesture.x * graphics_->GetWidth());
  1060. eventData[P_CENTERY] = (int)(evt.dgesture.y * graphics_->GetHeight());
  1061. eventData[P_NUMFINGERS] = (int)evt.dgesture.numFingers;
  1062. eventData[P_ERROR] = evt.dgesture.error;
  1063. SendEvent(E_GESTUREINPUT, eventData);
  1064. }
  1065. break;
  1066. case SDL_MULTIGESTURE:
  1067. {
  1068. using namespace MultiGesture;
  1069. VariantMap& eventData = GetEventDataMap();
  1070. eventData[P_CENTERX] = (int)(evt.mgesture.x * graphics_->GetWidth());
  1071. eventData[P_CENTERY] = (int)(evt.mgesture.y * graphics_->GetHeight());
  1072. eventData[P_NUMFINGERS] = (int)evt.mgesture.numFingers;
  1073. eventData[P_DTHETA] = M_RADTODEG * evt.mgesture.dTheta;
  1074. eventData[P_DDIST] = evt.mgesture.dDist;
  1075. SendEvent(E_MULTIGESTURE, eventData);
  1076. }
  1077. break;
  1078. case SDL_JOYDEVICEADDED:
  1079. {
  1080. using namespace JoystickConnected;
  1081. SDL_JoystickID joystickID = OpenJoystick(evt.jdevice.which);
  1082. VariantMap& eventData = GetEventDataMap();
  1083. eventData[P_JOYSTICKID] = joystickID;
  1084. SendEvent(E_JOYSTICKCONNECTED, eventData);
  1085. }
  1086. break;
  1087. case SDL_JOYDEVICEREMOVED:
  1088. {
  1089. using namespace JoystickDisconnected;
  1090. joysticks_.Erase(evt.jdevice.which);
  1091. VariantMap& eventData = GetEventDataMap();
  1092. eventData[P_JOYSTICKID] = evt.jdevice.which;
  1093. SendEvent(E_JOYSTICKDISCONNECTED, eventData);
  1094. }
  1095. break;
  1096. case SDL_JOYBUTTONDOWN:
  1097. {
  1098. using namespace JoystickButtonDown;
  1099. unsigned button = evt.jbutton.button;
  1100. SDL_JoystickID joystickID = evt.jbutton.which;
  1101. JoystickState& state = joysticks_[joystickID];
  1102. // Skip ordinary joystick event for a controller
  1103. if (!state.controller_)
  1104. {
  1105. VariantMap& eventData = GetEventDataMap();
  1106. eventData[P_JOYSTICKID] = joystickID;
  1107. eventData[P_BUTTON] = button;
  1108. if (button < state.buttons_.Size())
  1109. {
  1110. state.buttons_[button] = true;
  1111. state.buttonPress_[button] = true;
  1112. SendEvent(E_JOYSTICKBUTTONDOWN, eventData);
  1113. }
  1114. }
  1115. }
  1116. break;
  1117. case SDL_JOYBUTTONUP:
  1118. {
  1119. using namespace JoystickButtonUp;
  1120. unsigned button = evt.jbutton.button;
  1121. SDL_JoystickID joystickID = evt.jbutton.which;
  1122. JoystickState& state = joysticks_[joystickID];
  1123. if (!state.controller_)
  1124. {
  1125. VariantMap& eventData = GetEventDataMap();
  1126. eventData[P_JOYSTICKID] = joystickID;
  1127. eventData[P_BUTTON] = button;
  1128. if (button < state.buttons_.Size())
  1129. {
  1130. if (!state.controller_)
  1131. state.buttons_[button] = false;
  1132. SendEvent(E_JOYSTICKBUTTONUP, eventData);
  1133. }
  1134. }
  1135. }
  1136. break;
  1137. case SDL_JOYAXISMOTION:
  1138. {
  1139. using namespace JoystickAxisMove;
  1140. SDL_JoystickID joystickID = evt.jaxis.which;
  1141. JoystickState& state = joysticks_[joystickID];
  1142. if (!state.controller_)
  1143. {
  1144. VariantMap& eventData = GetEventDataMap();
  1145. eventData[P_JOYSTICKID] = joystickID;
  1146. eventData[P_AXIS] = evt.jaxis.axis;
  1147. eventData[P_POSITION] = Clamp((float)evt.jaxis.value / 32767.0f, -1.0f, 1.0f);
  1148. if (evt.jaxis.axis < state.axes_.Size())
  1149. {
  1150. // If the joystick is a controller, only use the controller axis mappings
  1151. // (we'll also get the controller event)
  1152. if (!state.controller_)
  1153. state.axes_[evt.jaxis.axis] = eventData[P_POSITION].GetFloat();
  1154. SendEvent(E_JOYSTICKAXISMOVE, eventData);
  1155. }
  1156. }
  1157. }
  1158. break;
  1159. case SDL_JOYHATMOTION:
  1160. {
  1161. using namespace JoystickHatMove;
  1162. SDL_JoystickID joystickID = evt.jaxis.which;
  1163. JoystickState& state = joysticks_[joystickID];
  1164. VariantMap& eventData = GetEventDataMap();
  1165. eventData[P_JOYSTICKID] = joystickID;
  1166. eventData[P_HAT] = evt.jhat.hat;
  1167. eventData[P_POSITION] = evt.jhat.value;
  1168. if (evt.jhat.hat < state.hats_.Size())
  1169. {
  1170. state.hats_[evt.jhat.hat] = evt.jhat.value;
  1171. SendEvent(E_JOYSTICKHATMOVE, eventData);
  1172. }
  1173. }
  1174. break;
  1175. case SDL_CONTROLLERBUTTONDOWN:
  1176. {
  1177. using namespace JoystickButtonDown;
  1178. unsigned button = evt.cbutton.button;
  1179. SDL_JoystickID joystickID = evt.cbutton.which;
  1180. JoystickState& state = joysticks_[joystickID];
  1181. VariantMap& eventData = GetEventDataMap();
  1182. eventData[P_JOYSTICKID] = joystickID;
  1183. eventData[P_BUTTON] = button;
  1184. if (button < state.buttons_.Size())
  1185. {
  1186. state.buttons_[button] = true;
  1187. state.buttonPress_[button] = true;
  1188. SendEvent(E_JOYSTICKBUTTONDOWN, eventData);
  1189. }
  1190. }
  1191. break;
  1192. case SDL_CONTROLLERBUTTONUP:
  1193. {
  1194. using namespace JoystickButtonUp;
  1195. unsigned button = evt.cbutton.button;
  1196. SDL_JoystickID joystickID = evt.cbutton.which;
  1197. JoystickState& state = joysticks_[joystickID];
  1198. VariantMap& eventData = GetEventDataMap();
  1199. eventData[P_JOYSTICKID] = joystickID;
  1200. eventData[P_BUTTON] = button;
  1201. if (button < state.buttons_.Size())
  1202. {
  1203. state.buttons_[button] = false;
  1204. SendEvent(E_JOYSTICKBUTTONUP, eventData);
  1205. }
  1206. }
  1207. break;
  1208. case SDL_CONTROLLERAXISMOTION:
  1209. {
  1210. using namespace JoystickAxisMove;
  1211. SDL_JoystickID joystickID = evt.caxis.which;
  1212. JoystickState& state = joysticks_[joystickID];
  1213. VariantMap& eventData = GetEventDataMap();
  1214. eventData[P_JOYSTICKID] = joystickID;
  1215. eventData[P_AXIS] = evt.caxis.axis;
  1216. eventData[P_POSITION] = Clamp((float)evt.caxis.value / 32767.0f, -1.0f, 1.0f);
  1217. if (evt.caxis.axis < state.axes_.Size())
  1218. {
  1219. state.axes_[evt.caxis.axis] = eventData[P_POSITION].GetFloat();
  1220. SendEvent(E_JOYSTICKAXISMOVE, eventData);
  1221. }
  1222. }
  1223. break;
  1224. case SDL_WINDOWEVENT:
  1225. {
  1226. switch (evt.window.event)
  1227. {
  1228. case SDL_WINDOWEVENT_MINIMIZED:
  1229. minimized_ = true;
  1230. SendInputFocusEvent();
  1231. break;
  1232. case SDL_WINDOWEVENT_MAXIMIZED:
  1233. case SDL_WINDOWEVENT_RESTORED:
  1234. minimized_ = false;
  1235. SendInputFocusEvent();
  1236. #ifdef IOS
  1237. // On iOS we never lose the GL context, but may have done GPU object changes that could not be applied yet.
  1238. // Apply them now
  1239. graphics_->Restore();
  1240. #endif
  1241. break;
  1242. #ifdef ANDROID
  1243. case SDL_WINDOWEVENT_FOCUS_GAINED:
  1244. // Restore GPU objects to the new GL context
  1245. graphics_->Restore();
  1246. break;
  1247. #endif
  1248. case SDL_WINDOWEVENT_RESIZED:
  1249. graphics_->WindowResized();
  1250. break;
  1251. }
  1252. }
  1253. break;
  1254. case SDL_DROPFILE:
  1255. {
  1256. using namespace DropFile;
  1257. VariantMap& eventData = GetEventDataMap();
  1258. eventData[P_FILENAME] = GetInternalPath(String(evt.drop.file));
  1259. SDL_free(evt.drop.file);
  1260. SendEvent(E_DROPFILE, eventData);
  1261. }
  1262. break;
  1263. case SDL_QUIT:
  1264. SendEvent(E_EXITREQUESTED);
  1265. break;
  1266. }
  1267. }
  1268. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  1269. {
  1270. // Reset input state on subsequent initializations
  1271. if (!initialized_)
  1272. Initialize();
  1273. else
  1274. ResetState();
  1275. // Re-enable cursor clipping, and re-center the cursor (if needed) to the new screen size, so that there is no erroneous
  1276. // mouse move event. Also get new window ID if it changed
  1277. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  1278. windowID_ = SDL_GetWindowID(window);
  1279. if (!mouseVisible_)
  1280. {
  1281. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  1282. SetMousePosition(center);
  1283. lastMousePosition_ = center;
  1284. }
  1285. focusedThisFrame_ = true;
  1286. // After setting a new screen mode we should not be minimized
  1287. minimized_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0;
  1288. }
  1289. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  1290. {
  1291. // Update input right at the beginning of the frame
  1292. Update();
  1293. }
  1294. void Input::HandleScreenJoystickTouch(StringHash eventType, VariantMap& eventData)
  1295. {
  1296. using namespace TouchBegin;
  1297. // Only interested in events from screen joystick(s)
  1298. TouchState& state = touches_[eventData[P_TOUCHID].GetInt()];
  1299. IntVector2 position(state.position_.x_, state.position_.y_);
  1300. UIElement* element = eventType == E_TOUCHBEGIN ? GetSubsystem<UI>()->GetElementAt(position) : state.touchedElement_;
  1301. if (!element)
  1302. return;
  1303. Variant variant = element->GetVar(VAR_SCREEN_JOYSTICK_ID);
  1304. if (variant.IsEmpty())
  1305. return;
  1306. SDL_JoystickID joystickID = variant.GetInt();
  1307. if (eventType == E_TOUCHEND)
  1308. state.touchedElement_.Reset();
  1309. else
  1310. state.touchedElement_ = element;
  1311. // Prepare a fake SDL event
  1312. SDL_Event evt;
  1313. const String& name = element->GetName();
  1314. if (name.StartsWith("Button"))
  1315. {
  1316. if (eventType == E_TOUCHMOVE)
  1317. return;
  1318. // Determine whether to inject a joystick event or keyboard/mouse event
  1319. Variant keyBindingVar = element->GetVar(VAR_BUTTON_KEY_BINDING);
  1320. Variant mouseButtonBindingVar = element->GetVar(VAR_BUTTON_MOUSE_BUTTON_BINDING);
  1321. if (keyBindingVar.IsEmpty() && mouseButtonBindingVar.IsEmpty())
  1322. {
  1323. evt.type = eventType == E_TOUCHBEGIN ? SDL_JOYBUTTONDOWN : SDL_JOYBUTTONUP;
  1324. evt.jbutton.which = joystickID;
  1325. evt.jbutton.button = ToUInt(name.Substring(6));
  1326. }
  1327. else
  1328. {
  1329. if (!keyBindingVar.IsEmpty())
  1330. {
  1331. evt.type = eventType == E_TOUCHBEGIN ? SDL_KEYDOWN : SDL_KEYUP;
  1332. evt.key.keysym.sym = keyBindingVar.GetInt();
  1333. evt.key.keysym.scancode = SDL_SCANCODE_UNKNOWN;
  1334. }
  1335. if (!mouseButtonBindingVar.IsEmpty())
  1336. {
  1337. // Mouse button are sent as extra events besides key events
  1338. // Disable touch emulation handling during this to prevent endless loop
  1339. bool oldTouchEmulation = touchEmulation_;
  1340. touchEmulation_ = false;
  1341. SDL_Event evt;
  1342. evt.type = eventType == E_TOUCHBEGIN ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP;
  1343. evt.button.button = mouseButtonBindingVar.GetInt();
  1344. HandleSDLEvent(&evt);
  1345. touchEmulation_ = oldTouchEmulation;
  1346. }
  1347. }
  1348. }
  1349. else if (name.StartsWith("Hat"))
  1350. {
  1351. Variant keyBindingVar = element->GetVar(VAR_BUTTON_KEY_BINDING);
  1352. if (keyBindingVar.IsEmpty())
  1353. {
  1354. evt.type = SDL_JOYHATMOTION;
  1355. evt.jaxis.which = joystickID;
  1356. evt.jhat.hat = ToUInt(name.Substring(3));
  1357. evt.jhat.value = HAT_CENTER;
  1358. if (eventType != E_TOUCHEND)
  1359. {
  1360. IntVector2 relPosition = position - element->GetScreenPosition() - element->GetSize() / 2;
  1361. if (relPosition.y_ < 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1362. evt.jhat.value |= HAT_UP;
  1363. if (relPosition.y_ > 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1364. evt.jhat.value |= HAT_DOWN;
  1365. if (relPosition.x_ < 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1366. evt.jhat.value |= HAT_LEFT;
  1367. if (relPosition.x_ > 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1368. evt.jhat.value |= HAT_RIGHT;
  1369. }
  1370. }
  1371. else
  1372. {
  1373. // Hat is binded by 4 keys, like 'WASD'
  1374. String keyBinding = keyBindingVar.GetString();
  1375. if (eventType == E_TOUCHEND)
  1376. {
  1377. evt.type = SDL_KEYUP;
  1378. evt.key.keysym.sym = element->GetVar(VAR_LAST_KEYSYM).GetInt();
  1379. if (!evt.key.keysym.sym)
  1380. return;
  1381. element->SetVar(VAR_LAST_KEYSYM, 0);
  1382. }
  1383. else
  1384. {
  1385. evt.type = SDL_KEYDOWN;
  1386. IntVector2 relPosition = position - element->GetScreenPosition() - element->GetSize() / 2;
  1387. if (relPosition.y_ < 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1388. evt.key.keysym.sym = keyBinding[0];
  1389. else if (relPosition.y_ > 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1390. evt.key.keysym.sym = keyBinding[1];
  1391. else if (relPosition.x_ < 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1392. evt.key.keysym.sym = keyBinding[2];
  1393. else if (relPosition.x_ > 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1394. evt.key.keysym.sym = keyBinding[3];
  1395. else
  1396. return;
  1397. if (eventType == E_TOUCHMOVE && evt.key.keysym.sym != element->GetVar(VAR_LAST_KEYSYM).GetInt())
  1398. {
  1399. // Dragging past the directional boundary will cause an additional key up event for previous key symbol
  1400. SDL_Event evt;
  1401. evt.type = SDL_KEYUP;
  1402. evt.key.keysym.sym = element->GetVar(VAR_LAST_KEYSYM).GetInt();
  1403. if (evt.key.keysym.sym)
  1404. {
  1405. evt.key.keysym.scancode = SDL_SCANCODE_UNKNOWN;
  1406. HandleSDLEvent(&evt);
  1407. }
  1408. element->SetVar(VAR_LAST_KEYSYM, 0);
  1409. }
  1410. evt.key.keysym.scancode = SDL_SCANCODE_UNKNOWN;
  1411. element->SetVar(VAR_LAST_KEYSYM, evt.key.keysym.sym);
  1412. }
  1413. }
  1414. }
  1415. else
  1416. return;
  1417. // Handle the fake SDL event to turn it into Urho3D genuine event
  1418. HandleSDLEvent(&evt);
  1419. }
  1420. }