Input.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  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 ShortStringHash VAR_BUTTON_KEY_BINDING("VAR_BUTTON_KEY_BINDING");
  52. const ShortStringHash VAR_BUTTON_MOUSE_BUTTON_BINDING("VAR_BUTTON_MOUSE_BUTTON_BINDING");
  53. const ShortStringHash VAR_LAST_KEYSYM("VAR_LAST_KEYSYM");
  54. const ShortStringHash 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);
  508. }
  509. bool Input::SaveGestures(Serializer& dest)
  510. {
  511. RWOpsWrapper<Serializer> wrapper(dest);
  512. return SDL_SaveAllDollarTemplates(wrapper.GetRWOps());
  513. }
  514. bool Input::SaveGesture(Serializer& dest, unsigned gestureID)
  515. {
  516. RWOpsWrapper<Serializer> wrapper(dest);
  517. return SDL_SaveDollarTemplate(gestureID, wrapper.GetRWOps());
  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. SDL_JoystickID Input::OpenJoystick(unsigned index)
  531. {
  532. SDL_Joystick* joystick = SDL_JoystickOpen(index);
  533. if (!joystick)
  534. {
  535. LOGERRORF("Cannot open joystick #%d", index);
  536. return -1;
  537. }
  538. // Create joystick state for the new joystick
  539. int joystickID = SDL_JoystickInstanceID(joystick);
  540. JoystickState& state = joysticks_[joystickID];
  541. state.joystick_ = joystick;
  542. state.joystickID_ = joystickID;
  543. state.name_ = SDL_JoystickName(joystick);
  544. if (SDL_IsGameController(index))
  545. state.controller_ = SDL_GameControllerOpen(index);
  546. unsigned numButtons = SDL_JoystickNumButtons(joystick);
  547. unsigned numAxes = SDL_JoystickNumAxes(joystick);
  548. unsigned numHats = SDL_JoystickNumHats(joystick);
  549. // When the joystick is a controller, make sure there's enough axes & buttons for the standard controller mappings
  550. if (state.controller_)
  551. {
  552. if (numButtons < SDL_CONTROLLER_BUTTON_MAX)
  553. numButtons = SDL_CONTROLLER_BUTTON_MAX;
  554. if (numAxes < SDL_CONTROLLER_AXIS_MAX)
  555. numAxes = SDL_CONTROLLER_AXIS_MAX;
  556. }
  557. state.Initialize(numButtons, numAxes, numHats);
  558. return joystickID;
  559. }
  560. int Input::GetKeyFromName(const String& name) const
  561. {
  562. return SDL_GetKeyFromName(name.CString());
  563. }
  564. int Input::GetKeyFromScancode(int scancode) const
  565. {
  566. return SDL_GetKeyFromScancode((SDL_Scancode)scancode);
  567. }
  568. String Input::GetKeyName(int key) const
  569. {
  570. return String(SDL_GetKeyName(key));
  571. }
  572. int Input::GetScancodeFromKey(int key) const
  573. {
  574. return SDL_GetScancodeFromKey(key);
  575. }
  576. int Input::GetScancodeFromName(const String& name) const
  577. {
  578. return SDL_GetScancodeFromName(name.CString());
  579. }
  580. String Input::GetScancodeName(int scancode) const
  581. {
  582. return SDL_GetScancodeName((SDL_Scancode)scancode);
  583. }
  584. bool Input::GetKeyDown(int key) const
  585. {
  586. return keyDown_.Contains(SDL_toupper(key));
  587. }
  588. bool Input::GetKeyPress(int key) const
  589. {
  590. return keyPress_.Contains(SDL_toupper(key));
  591. }
  592. bool Input::GetScancodeDown(int scancode) const
  593. {
  594. return scancodeDown_.Contains(scancode);
  595. }
  596. bool Input::GetScancodePress(int scancode) const
  597. {
  598. return scancodePress_.Contains(scancode);
  599. }
  600. bool Input::GetMouseButtonDown(int button) const
  601. {
  602. return (mouseButtonDown_ & button) != 0;
  603. }
  604. bool Input::GetMouseButtonPress(int button) const
  605. {
  606. return (mouseButtonPress_ & button) != 0;
  607. }
  608. bool Input::GetQualifierDown(int qualifier) const
  609. {
  610. if (qualifier == QUAL_SHIFT)
  611. return GetKeyDown(KEY_LSHIFT) || GetKeyDown(KEY_RSHIFT);
  612. if (qualifier == QUAL_CTRL)
  613. return GetKeyDown(KEY_LCTRL) || GetKeyDown(KEY_RCTRL);
  614. if (qualifier == QUAL_ALT)
  615. return GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT);
  616. return false;
  617. }
  618. bool Input::GetQualifierPress(int qualifier) const
  619. {
  620. if (qualifier == QUAL_SHIFT)
  621. return GetKeyPress(KEY_LSHIFT) || GetKeyPress(KEY_RSHIFT);
  622. if (qualifier == QUAL_CTRL)
  623. return GetKeyPress(KEY_LCTRL) || GetKeyPress(KEY_RCTRL);
  624. if (qualifier == QUAL_ALT)
  625. return GetKeyPress(KEY_LALT) || GetKeyPress(KEY_RALT);
  626. return false;
  627. }
  628. int Input::GetQualifiers() const
  629. {
  630. int ret = 0;
  631. if (GetQualifierDown(QUAL_SHIFT))
  632. ret |= QUAL_SHIFT;
  633. if (GetQualifierDown(QUAL_CTRL))
  634. ret |= QUAL_CTRL;
  635. if (GetQualifierDown(QUAL_ALT))
  636. ret |= QUAL_ALT;
  637. return ret;
  638. }
  639. IntVector2 Input::GetMousePosition() const
  640. {
  641. IntVector2 ret = IntVector2::ZERO;
  642. if (!initialized_)
  643. return ret;
  644. SDL_GetMouseState(&ret.x_, &ret.y_);
  645. return ret;
  646. }
  647. TouchState* Input::GetTouch(unsigned index) const
  648. {
  649. if (index >= touches_.Size())
  650. return 0;
  651. HashMap<int, TouchState>::ConstIterator i = touches_.Begin();
  652. while (index--)
  653. ++i;
  654. return const_cast<TouchState*>(&i->second_);
  655. }
  656. JoystickState* Input::GetJoystickByIndex(unsigned index)
  657. {
  658. unsigned compare = 0;
  659. for (HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  660. {
  661. if (compare++ == index)
  662. return &(i->second_);
  663. }
  664. return 0;
  665. }
  666. JoystickState* Input::GetJoystick(SDL_JoystickID id)
  667. {
  668. HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Find(id);
  669. return i != joysticks_.End() ? &(i->second_) : 0;
  670. }
  671. bool Input::IsScreenJoystickVisible(SDL_JoystickID id) const
  672. {
  673. HashMap<SDL_JoystickID, JoystickState>::ConstIterator i = joysticks_.Find(id);
  674. return i != joysticks_.End() && i->second_.screenJoystick_ && i->second_.screenJoystick_->IsVisible();
  675. }
  676. bool Input::GetScreenKeyboardSupport() const
  677. {
  678. return graphics_ ? SDL_HasScreenKeyboardSupport() : false;
  679. }
  680. bool Input::IsScreenKeyboardVisible() const
  681. {
  682. if (graphics_)
  683. {
  684. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  685. return SDL_IsScreenKeyboardShown(window);
  686. }
  687. else
  688. return false;
  689. }
  690. bool Input::IsMinimized() const
  691. {
  692. // Return minimized state also when unfocused in fullscreen
  693. if (!inputFocus_ && graphics_ && graphics_->GetFullscreen())
  694. return true;
  695. else
  696. return minimized_;
  697. }
  698. void Input::Initialize()
  699. {
  700. Graphics* graphics = GetSubsystem<Graphics>();
  701. if (!graphics || !graphics->IsInitialized())
  702. return;
  703. graphics_ = graphics;
  704. // In external window mode only visible mouse is supported
  705. if (graphics_->GetExternalWindow())
  706. mouseVisible_ = true;
  707. // Set the initial activation
  708. focusedThisFrame_ = true;
  709. initialized_ = true;
  710. ResetJoysticks();
  711. ResetState();
  712. SubscribeToEvent(E_BEGINFRAME, HANDLER(Input, HandleBeginFrame));
  713. LOGINFO("Initialized input");
  714. }
  715. void Input::ResetJoysticks()
  716. {
  717. joysticks_.Clear();
  718. // Open each detected joystick automatically on startup
  719. int size = SDL_NumJoysticks();
  720. for (int i = 0; i < size; ++i)
  721. OpenJoystick(i);
  722. }
  723. void Input::GainFocus()
  724. {
  725. ResetState();
  726. inputFocus_ = true;
  727. focusedThisFrame_ = false;
  728. // Re-establish mouse cursor hiding as necessary
  729. if (!mouseVisible_)
  730. {
  731. SDL_ShowCursor(SDL_FALSE);
  732. suppressNextMouseMove_ = true;
  733. }
  734. else
  735. lastMousePosition_ = GetMousePosition();
  736. SendInputFocusEvent();
  737. }
  738. void Input::LoseFocus()
  739. {
  740. ResetState();
  741. inputFocus_ = false;
  742. focusedThisFrame_ = false;
  743. // Show the mouse cursor when inactive
  744. SDL_ShowCursor(SDL_TRUE);
  745. SendInputFocusEvent();
  746. }
  747. void Input::ResetState()
  748. {
  749. keyDown_.Clear();
  750. keyPress_.Clear();
  751. scancodeDown_.Clear();
  752. scancodePress_.Clear();
  753. /// \todo Check if resetting joystick state on input focus loss is even necessary
  754. for (HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
  755. i->second_.Reset();
  756. ResetTouches();
  757. // Use SetMouseButton() to reset the state so that mouse events will be sent properly
  758. SetMouseButton(MOUSEB_LEFT, false);
  759. SetMouseButton(MOUSEB_RIGHT, false);
  760. SetMouseButton(MOUSEB_MIDDLE, false);
  761. mouseMove_ = IntVector2::ZERO;
  762. mouseMoveWheel_ = 0;
  763. mouseButtonPress_ = 0;
  764. }
  765. void Input::ResetTouches()
  766. {
  767. for (HashMap<int, TouchState>::Iterator i = touches_.Begin(); i != touches_.End(); ++i)
  768. {
  769. TouchState& state = i->second_;
  770. using namespace TouchEnd;
  771. VariantMap& eventData = GetEventDataMap();
  772. eventData[P_TOUCHID] = state.touchID_;
  773. eventData[P_X] = state.position_.x_;
  774. eventData[P_Y] = state.position_.y_;
  775. SendEvent(E_TOUCHEND, eventData);
  776. }
  777. touches_.Clear();
  778. }
  779. void Input::SendInputFocusEvent()
  780. {
  781. using namespace InputFocus;
  782. VariantMap& eventData = GetEventDataMap();
  783. eventData[P_FOCUS] = HasFocus();
  784. eventData[P_MINIMIZED] = IsMinimized();
  785. SendEvent(E_INPUTFOCUS, eventData);
  786. }
  787. void Input::SetMouseButton(int button, bool newState)
  788. {
  789. #ifdef REQUIRE_CLICK_TO_FOCUS
  790. if (!mouseVisible_ && !graphics_->GetFullscreen())
  791. {
  792. if (!inputFocus_ && newState && button == MOUSEB_LEFT)
  793. focusedThisFrame_ = true;
  794. }
  795. #endif
  796. // If we do not have focus yet, do not react to the mouse button down
  797. if (!graphics_->GetExternalWindow() && newState && !inputFocus_)
  798. return;
  799. if (newState)
  800. {
  801. if (!(mouseButtonDown_ & button))
  802. mouseButtonPress_ |= button;
  803. mouseButtonDown_ |= button;
  804. }
  805. else
  806. {
  807. if (!(mouseButtonDown_ & button))
  808. return;
  809. mouseButtonDown_ &= ~button;
  810. }
  811. using namespace MouseButtonDown;
  812. VariantMap& eventData = GetEventDataMap();
  813. eventData[P_BUTTON] = button;
  814. eventData[P_BUTTONS] = mouseButtonDown_;
  815. eventData[P_QUALIFIERS] = GetQualifiers();
  816. SendEvent(newState ? E_MOUSEBUTTONDOWN : E_MOUSEBUTTONUP, eventData);
  817. }
  818. void Input::SetKey(int key, int scancode, unsigned raw, bool newState)
  819. {
  820. // If we do not have focus yet, do not react to the key down
  821. if (!graphics_->GetExternalWindow() && newState && !inputFocus_)
  822. return;
  823. bool repeat = false;
  824. if (newState)
  825. {
  826. scancodeDown_.Insert(scancode);
  827. scancodePress_.Insert(scancode);
  828. if (!keyDown_.Contains(key))
  829. {
  830. keyDown_.Insert(key);
  831. keyPress_.Insert(key);
  832. }
  833. else
  834. repeat = true;
  835. }
  836. else
  837. {
  838. scancodeDown_.Erase(scancode);
  839. if (!keyDown_.Erase(key))
  840. return;
  841. }
  842. using namespace KeyDown;
  843. VariantMap& eventData = GetEventDataMap();
  844. eventData[P_KEY] = key;
  845. eventData[P_SCANCODE] = scancode;
  846. eventData[P_RAW] = raw;
  847. eventData[P_BUTTONS] = mouseButtonDown_;
  848. eventData[P_QUALIFIERS] = GetQualifiers();
  849. if (newState)
  850. eventData[P_REPEAT] = repeat;
  851. SendEvent(newState ? E_KEYDOWN : E_KEYUP, eventData);
  852. if ((key == KEY_RETURN || key == KEY_RETURN2 || key == KEY_KP_ENTER) && newState && !repeat && toggleFullscreen_ &&
  853. (GetKeyDown(KEY_LALT) || GetKeyDown(KEY_RALT)))
  854. graphics_->ToggleFullscreen();
  855. }
  856. void Input::SetMouseWheel(int delta)
  857. {
  858. // If we do not have focus yet, do not react to the wheel
  859. if (!graphics_->GetExternalWindow() && !inputFocus_)
  860. return;
  861. if (delta)
  862. {
  863. mouseMoveWheel_ += delta;
  864. using namespace MouseWheel;
  865. VariantMap& eventData = GetEventDataMap();
  866. eventData[P_WHEEL] = delta;
  867. eventData[P_BUTTONS] = mouseButtonDown_;
  868. eventData[P_QUALIFIERS] = GetQualifiers();
  869. SendEvent(E_MOUSEWHEEL, eventData);
  870. }
  871. }
  872. void Input::SetMousePosition(const IntVector2& position)
  873. {
  874. if (!graphics_)
  875. return;
  876. SDL_WarpMouseInWindow(graphics_->GetImpl()->GetWindow(), position.x_, position.y_);
  877. }
  878. void Input::HandleSDLEvent(void* sdlEvent)
  879. {
  880. SDL_Event& evt = *static_cast<SDL_Event*>(sdlEvent);
  881. switch (evt.type)
  882. {
  883. case SDL_KEYDOWN:
  884. // Convert to uppercase to match Win32 virtual key codes
  885. SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), evt.key.keysym.scancode, evt.key.keysym.raw, true);
  886. break;
  887. case SDL_KEYUP:
  888. SetKey(ConvertSDLKeyCode(evt.key.keysym.sym, evt.key.keysym.scancode), evt.key.keysym.scancode, evt.key.keysym.raw, false);
  889. break;
  890. case SDL_TEXTINPUT:
  891. {
  892. textInput_ = &evt.text.text[0];
  893. unsigned unicode = textInput_.AtUTF8(0);
  894. if (unicode)
  895. {
  896. using namespace TextInput;
  897. VariantMap textInputEventData;
  898. textInputEventData[P_TEXT] = textInput_;
  899. textInputEventData[P_BUTTONS] = mouseButtonDown_;
  900. textInputEventData[P_QUALIFIERS] = GetQualifiers();
  901. SendEvent(E_TEXTINPUT, textInputEventData);
  902. }
  903. }
  904. break;
  905. case SDL_MOUSEBUTTONDOWN:
  906. if (!touchEmulation_)
  907. SetMouseButton(1 << (evt.button.button - 1), true);
  908. else
  909. {
  910. int x, y;
  911. SDL_GetMouseState(&x, &y);
  912. SDL_Event event;
  913. event.type = SDL_FINGERDOWN;
  914. event.tfinger.touchId = 0;
  915. event.tfinger.fingerId = evt.button.button - 1;
  916. event.tfinger.pressure = 1.0f;
  917. event.tfinger.x = (float)x / (float)graphics_->GetWidth();
  918. event.tfinger.y = (float)y / (float)graphics_->GetHeight();
  919. event.tfinger.dx = 0;
  920. event.tfinger.dy = 0;
  921. SDL_PushEvent(&event);
  922. }
  923. break;
  924. case SDL_MOUSEBUTTONUP:
  925. if (!touchEmulation_)
  926. SetMouseButton(1 << (evt.button.button - 1), false);
  927. else
  928. {
  929. int x, y;
  930. SDL_GetMouseState(&x, &y);
  931. SDL_Event event;
  932. event.type = SDL_FINGERUP;
  933. event.tfinger.touchId = 0;
  934. event.tfinger.fingerId = evt.button.button - 1;
  935. event.tfinger.pressure = 0.0f;
  936. event.tfinger.x = (float)x / (float)graphics_->GetWidth();
  937. event.tfinger.y = (float)y / (float)graphics_->GetHeight();
  938. event.tfinger.dx = 0;
  939. event.tfinger.dy = 0;
  940. SDL_PushEvent(&event);
  941. }
  942. break;
  943. case SDL_MOUSEMOTION:
  944. if (mouseVisible_ && !touchEmulation_)
  945. {
  946. mouseMove_.x_ += evt.motion.xrel;
  947. mouseMove_.y_ += evt.motion.yrel;
  948. using namespace MouseMove;
  949. VariantMap& eventData = GetEventDataMap();
  950. if (mouseVisible_)
  951. {
  952. eventData[P_X] = evt.motion.x;
  953. eventData[P_Y] = evt.motion.y;
  954. }
  955. eventData[P_DX] = evt.motion.xrel;
  956. eventData[P_DY] = evt.motion.yrel;
  957. eventData[P_BUTTONS] = mouseButtonDown_;
  958. eventData[P_QUALIFIERS] = GetQualifiers();
  959. SendEvent(E_MOUSEMOVE, eventData);
  960. }
  961. // Only the left mouse button "finger" moves along with the mouse movement
  962. else if (touchEmulation_ && touches_.Contains(0))
  963. {
  964. int x, y;
  965. SDL_GetMouseState(&x, &y);
  966. SDL_Event event;
  967. event.type = SDL_FINGERMOTION;
  968. event.tfinger.touchId = 0;
  969. event.tfinger.fingerId = 0;
  970. event.tfinger.pressure = 1.0f;
  971. event.tfinger.x = (float)x / (float)graphics_->GetWidth();
  972. event.tfinger.y = (float)y / (float)graphics_->GetHeight();
  973. event.tfinger.dx = (float)evt.motion.xrel / (float)graphics_->GetWidth();
  974. event.tfinger.dy = (float)evt.motion.yrel / (float)graphics_->GetHeight();
  975. SDL_PushEvent(&event);
  976. }
  977. break;
  978. case SDL_MOUSEWHEEL:
  979. if (!touchEmulation_)
  980. SetMouseWheel(evt.wheel.y);
  981. break;
  982. case SDL_FINGERDOWN:
  983. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  984. {
  985. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  986. TouchState& state = touches_[touchID];
  987. state.touchID_ = touchID;
  988. state.lastPosition_ = state.position_ = IntVector2((int)(evt.tfinger.x * graphics_->GetWidth()),
  989. (int)(evt.tfinger.y * graphics_->GetHeight()));
  990. state.delta_ = IntVector2::ZERO;
  991. state.pressure_ = evt.tfinger.pressure;
  992. using namespace TouchBegin;
  993. VariantMap& eventData = GetEventDataMap();
  994. eventData[P_TOUCHID] = touchID;
  995. eventData[P_X] = state.position_.x_;
  996. eventData[P_Y] = state.position_.y_;
  997. eventData[P_PRESSURE] = state.pressure_;
  998. SendEvent(E_TOUCHBEGIN, eventData);
  999. }
  1000. break;
  1001. case SDL_FINGERUP:
  1002. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  1003. {
  1004. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  1005. TouchState& state = touches_[touchID];
  1006. using namespace TouchEnd;
  1007. VariantMap& eventData = GetEventDataMap();
  1008. // Do not trust the position in the finger up event. Instead use the last position stored in the
  1009. // touch structure
  1010. eventData[P_TOUCHID] = touchID;
  1011. eventData[P_X] = state.position_.x_;
  1012. eventData[P_Y] = state.position_.y_;
  1013. SendEvent(E_TOUCHEND, eventData);
  1014. touches_.Erase(touchID);
  1015. }
  1016. break;
  1017. case SDL_FINGERMOTION:
  1018. if (evt.tfinger.touchId != SDL_TOUCH_MOUSEID)
  1019. {
  1020. int touchID = evt.tfinger.fingerId & 0x7ffffff;
  1021. TouchState& state = touches_[touchID];
  1022. state.touchID_ = touchID;
  1023. state.position_ = IntVector2((int)(evt.tfinger.x * graphics_->GetWidth()),
  1024. (int)(evt.tfinger.y * graphics_->GetHeight()));
  1025. state.delta_ = state.position_ - state.lastPosition_;
  1026. state.pressure_ = evt.tfinger.pressure;
  1027. using namespace TouchMove;
  1028. VariantMap& eventData = GetEventDataMap();
  1029. eventData[P_TOUCHID] = touchID;
  1030. eventData[P_X] = state.position_.x_;
  1031. eventData[P_Y] = state.position_.y_;
  1032. eventData[P_DX] = (int)(evt.tfinger.dx * graphics_->GetWidth());
  1033. eventData[P_DY] = (int)(evt.tfinger.dy * graphics_->GetHeight());
  1034. eventData[P_PRESSURE] = state.pressure_;
  1035. SendEvent(E_TOUCHMOVE, eventData);
  1036. }
  1037. break;
  1038. case SDL_DOLLARRECORD:
  1039. {
  1040. using namespace GestureRecorded;
  1041. VariantMap& eventData = GetEventDataMap();
  1042. eventData[P_GESTUREID] = (int)evt.dgesture.gestureId;
  1043. SendEvent(E_GESTURERECORDED, eventData);
  1044. }
  1045. break;
  1046. case SDL_DOLLARGESTURE:
  1047. {
  1048. using namespace GestureInput;
  1049. VariantMap& eventData = GetEventDataMap();
  1050. eventData[P_GESTUREID] = (int)evt.dgesture.gestureId;
  1051. eventData[P_CENTERX] = (int)(evt.dgesture.x * graphics_->GetWidth());
  1052. eventData[P_CENTERY] = (int)(evt.dgesture.y * graphics_->GetHeight());
  1053. eventData[P_NUMFINGERS] = (int)evt.dgesture.numFingers;
  1054. eventData[P_ERROR] = evt.dgesture.error;
  1055. SendEvent(E_GESTUREINPUT, eventData);
  1056. }
  1057. break;
  1058. case SDL_MULTIGESTURE:
  1059. {
  1060. using namespace MultiGesture;
  1061. VariantMap& eventData = GetEventDataMap();
  1062. eventData[P_CENTERX] = (int)(evt.mgesture.x * graphics_->GetWidth());
  1063. eventData[P_CENTERY] = (int)(evt.mgesture.y * graphics_->GetHeight());
  1064. eventData[P_NUMFINGERS] = (int)evt.mgesture.numFingers;
  1065. eventData[P_DTHETA] = M_RADTODEG * evt.mgesture.dTheta;
  1066. eventData[P_DDIST] = evt.mgesture.dDist;
  1067. SendEvent(E_MULTIGESTURE, eventData);
  1068. }
  1069. break;
  1070. case SDL_JOYDEVICEADDED:
  1071. {
  1072. using namespace JoystickConnected;
  1073. SDL_JoystickID joystickID = OpenJoystick(evt.jdevice.which);
  1074. VariantMap& eventData = GetEventDataMap();
  1075. eventData[P_JOYSTICKID] = joystickID;
  1076. SendEvent(E_JOYSTICKCONNECTED, eventData);
  1077. }
  1078. break;
  1079. case SDL_JOYDEVICEREMOVED:
  1080. {
  1081. using namespace JoystickDisconnected;
  1082. joysticks_.Erase(evt.jdevice.which);
  1083. VariantMap& eventData = GetEventDataMap();
  1084. eventData[P_JOYSTICKID] = evt.jdevice.which;
  1085. SendEvent(E_JOYSTICKDISCONNECTED, eventData);
  1086. }
  1087. break;
  1088. case SDL_JOYBUTTONDOWN:
  1089. {
  1090. using namespace JoystickButtonDown;
  1091. unsigned button = evt.jbutton.button;
  1092. SDL_JoystickID joystickID = evt.jbutton.which;
  1093. JoystickState& state = joysticks_[joystickID];
  1094. // Skip ordinary joystick event for a controller
  1095. if (!state.controller_)
  1096. {
  1097. VariantMap& eventData = GetEventDataMap();
  1098. eventData[P_JOYSTICKID] = joystickID;
  1099. eventData[P_BUTTON] = button;
  1100. if (button < state.buttons_.Size())
  1101. {
  1102. state.buttons_[button] = true;
  1103. state.buttonPress_[button] = true;
  1104. SendEvent(E_JOYSTICKBUTTONDOWN, eventData);
  1105. }
  1106. }
  1107. }
  1108. break;
  1109. case SDL_JOYBUTTONUP:
  1110. {
  1111. using namespace JoystickButtonUp;
  1112. unsigned button = evt.jbutton.button;
  1113. SDL_JoystickID joystickID = evt.jbutton.which;
  1114. JoystickState& state = joysticks_[joystickID];
  1115. if (!state.controller_)
  1116. {
  1117. VariantMap& eventData = GetEventDataMap();
  1118. eventData[P_JOYSTICKID] = joystickID;
  1119. eventData[P_BUTTON] = button;
  1120. if (button < state.buttons_.Size())
  1121. {
  1122. if (!state.controller_)
  1123. state.buttons_[button] = false;
  1124. SendEvent(E_JOYSTICKBUTTONUP, eventData);
  1125. }
  1126. }
  1127. }
  1128. break;
  1129. case SDL_JOYAXISMOTION:
  1130. {
  1131. using namespace JoystickAxisMove;
  1132. SDL_JoystickID joystickID = evt.jaxis.which;
  1133. JoystickState& state = joysticks_[joystickID];
  1134. if (!state.controller_)
  1135. {
  1136. VariantMap& eventData = GetEventDataMap();
  1137. eventData[P_JOYSTICKID] = joystickID;
  1138. eventData[P_AXIS] = evt.jaxis.axis;
  1139. eventData[P_POSITION] = Clamp((float)evt.jaxis.value / 32767.0f, -1.0f, 1.0f);
  1140. if (evt.jaxis.axis < state.axes_.Size())
  1141. {
  1142. // If the joystick is a controller, only use the controller axis mappings
  1143. // (we'll also get the controller event)
  1144. if (!state.controller_)
  1145. state.axes_[evt.jaxis.axis] = eventData[P_POSITION].GetFloat();
  1146. SendEvent(E_JOYSTICKAXISMOVE, eventData);
  1147. }
  1148. }
  1149. }
  1150. break;
  1151. case SDL_JOYHATMOTION:
  1152. {
  1153. using namespace JoystickHatMove;
  1154. SDL_JoystickID joystickID = evt.jaxis.which;
  1155. JoystickState& state = joysticks_[joystickID];
  1156. VariantMap& eventData = GetEventDataMap();
  1157. eventData[P_JOYSTICKID] = joystickID;
  1158. eventData[P_HAT] = evt.jhat.hat;
  1159. eventData[P_POSITION] = evt.jhat.value;
  1160. if (evt.jhat.hat < state.hats_.Size())
  1161. {
  1162. state.hats_[evt.jhat.hat] = evt.jhat.value;
  1163. SendEvent(E_JOYSTICKHATMOVE, eventData);
  1164. }
  1165. }
  1166. break;
  1167. case SDL_CONTROLLERBUTTONDOWN:
  1168. {
  1169. using namespace JoystickButtonDown;
  1170. unsigned button = evt.cbutton.button;
  1171. SDL_JoystickID joystickID = evt.cbutton.which;
  1172. JoystickState& state = joysticks_[joystickID];
  1173. VariantMap& eventData = GetEventDataMap();
  1174. eventData[P_JOYSTICKID] = joystickID;
  1175. eventData[P_BUTTON] = button;
  1176. if (button < state.buttons_.Size())
  1177. {
  1178. state.buttons_[button] = true;
  1179. state.buttonPress_[button] = true;
  1180. SendEvent(E_JOYSTICKBUTTONDOWN, eventData);
  1181. }
  1182. }
  1183. break;
  1184. case SDL_CONTROLLERBUTTONUP:
  1185. {
  1186. using namespace JoystickButtonUp;
  1187. unsigned button = evt.cbutton.button;
  1188. SDL_JoystickID joystickID = evt.cbutton.which;
  1189. JoystickState& state = joysticks_[joystickID];
  1190. VariantMap& eventData = GetEventDataMap();
  1191. eventData[P_JOYSTICKID] = joystickID;
  1192. eventData[P_BUTTON] = button;
  1193. if (button < state.buttons_.Size())
  1194. {
  1195. state.buttons_[button] = false;
  1196. SendEvent(E_JOYSTICKBUTTONUP, eventData);
  1197. }
  1198. }
  1199. break;
  1200. case SDL_CONTROLLERAXISMOTION:
  1201. {
  1202. using namespace JoystickAxisMove;
  1203. SDL_JoystickID joystickID = evt.caxis.which;
  1204. JoystickState& state = joysticks_[joystickID];
  1205. VariantMap& eventData = GetEventDataMap();
  1206. eventData[P_JOYSTICKID] = joystickID;
  1207. eventData[P_AXIS] = evt.caxis.axis;
  1208. eventData[P_POSITION] = Clamp((float)evt.caxis.value / 32767.0f, -1.0f, 1.0f);
  1209. if (evt.caxis.axis < state.axes_.Size())
  1210. {
  1211. state.axes_[evt.caxis.axis] = eventData[P_POSITION].GetFloat();
  1212. SendEvent(E_JOYSTICKAXISMOVE, eventData);
  1213. }
  1214. }
  1215. break;
  1216. case SDL_WINDOWEVENT:
  1217. {
  1218. switch (evt.window.event)
  1219. {
  1220. case SDL_WINDOWEVENT_MINIMIZED:
  1221. minimized_ = true;
  1222. SendInputFocusEvent();
  1223. break;
  1224. case SDL_WINDOWEVENT_MAXIMIZED:
  1225. case SDL_WINDOWEVENT_RESTORED:
  1226. minimized_ = false;
  1227. SendInputFocusEvent();
  1228. #ifdef IOS
  1229. // On iOS we never lose the GL context, but may have done GPU object changes that could not be applied yet.
  1230. // Apply them now
  1231. graphics_->Restore();
  1232. #endif
  1233. break;
  1234. #ifdef ANDROID
  1235. case SDL_WINDOWEVENT_FOCUS_GAINED:
  1236. // Restore GPU objects to the new GL context
  1237. graphics_->Restore();
  1238. break;
  1239. #endif
  1240. case SDL_WINDOWEVENT_RESIZED:
  1241. graphics_->WindowResized();
  1242. break;
  1243. }
  1244. }
  1245. break;
  1246. case SDL_DROPFILE:
  1247. {
  1248. using namespace DropFile;
  1249. VariantMap& eventData = GetEventDataMap();
  1250. eventData[P_FILENAME] = GetInternalPath(String(evt.drop.file));
  1251. SDL_free(evt.drop.file);
  1252. SendEvent(E_DROPFILE, eventData);
  1253. }
  1254. break;
  1255. case SDL_QUIT:
  1256. SendEvent(E_EXITREQUESTED);
  1257. break;
  1258. }
  1259. }
  1260. void Input::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  1261. {
  1262. // Reset input state on subsequent initializations
  1263. if (!initialized_)
  1264. Initialize();
  1265. else
  1266. ResetState();
  1267. // Re-enable cursor clipping, and re-center the cursor (if needed) to the new screen size, so that there is no erroneous
  1268. // mouse move event. Also get new window ID if it changed
  1269. SDL_Window* window = graphics_->GetImpl()->GetWindow();
  1270. windowID_ = SDL_GetWindowID(window);
  1271. if (!mouseVisible_)
  1272. {
  1273. IntVector2 center(graphics_->GetWidth() / 2, graphics_->GetHeight() / 2);
  1274. SetMousePosition(center);
  1275. lastMousePosition_ = center;
  1276. }
  1277. focusedThisFrame_ = true;
  1278. // After setting a new screen mode we should not be minimized
  1279. minimized_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0;
  1280. }
  1281. void Input::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  1282. {
  1283. // Update input right at the beginning of the frame
  1284. Update();
  1285. }
  1286. void Input::HandleScreenJoystickTouch(StringHash eventType, VariantMap& eventData)
  1287. {
  1288. using namespace TouchBegin;
  1289. // Only interested in events from screen joystick(s)
  1290. TouchState& state = touches_[eventData[P_TOUCHID].GetInt()];
  1291. IntVector2 position(state.position_.x_, state.position_.y_);
  1292. UIElement* element = eventType == E_TOUCHBEGIN ? GetSubsystem<UI>()->GetElementAt(position) : state.touchedElement_;
  1293. if (!element)
  1294. return;
  1295. Variant variant = element->GetVar(VAR_SCREEN_JOYSTICK_ID);
  1296. if (variant.IsEmpty())
  1297. return;
  1298. SDL_JoystickID joystickID = variant.GetInt();
  1299. if (eventType == E_TOUCHEND)
  1300. state.touchedElement_.Reset();
  1301. else
  1302. state.touchedElement_ = element;
  1303. // Prepare a fake SDL event
  1304. SDL_Event evt;
  1305. const String& name = element->GetName();
  1306. if (name.StartsWith("Button"))
  1307. {
  1308. if (eventType == E_TOUCHMOVE)
  1309. return;
  1310. // Determine whether to inject a joystick event or keyboard/mouse event
  1311. Variant keyBindingVar = element->GetVar(VAR_BUTTON_KEY_BINDING);
  1312. Variant mouseButtonBindingVar = element->GetVar(VAR_BUTTON_MOUSE_BUTTON_BINDING);
  1313. if (keyBindingVar.IsEmpty() && mouseButtonBindingVar.IsEmpty())
  1314. {
  1315. evt.type = eventType == E_TOUCHBEGIN ? SDL_JOYBUTTONDOWN : SDL_JOYBUTTONUP;
  1316. evt.jbutton.which = joystickID;
  1317. evt.jbutton.button = ToUInt(name.Substring(6));
  1318. }
  1319. else
  1320. {
  1321. if (!keyBindingVar.IsEmpty())
  1322. {
  1323. evt.type = eventType == E_TOUCHBEGIN ? SDL_KEYDOWN : SDL_KEYUP;
  1324. evt.key.keysym.sym = keyBindingVar.GetInt();
  1325. evt.key.keysym.scancode = SDL_SCANCODE_UNKNOWN;
  1326. }
  1327. if (!mouseButtonBindingVar.IsEmpty() && !touchEmulation_)
  1328. {
  1329. // Mouse button are sent as extra events besides key events
  1330. // Do not send mouse events in touch emulation mode, because those would in turn be re-interpreted as touch
  1331. SDL_Event evt;
  1332. evt.type = eventType == E_TOUCHBEGIN ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP;
  1333. evt.button.button = mouseButtonBindingVar.GetInt();
  1334. HandleSDLEvent(&evt);
  1335. }
  1336. }
  1337. }
  1338. else if (name.StartsWith("Hat"))
  1339. {
  1340. Variant keyBindingVar = element->GetVar(VAR_BUTTON_KEY_BINDING);
  1341. if (keyBindingVar.IsEmpty())
  1342. {
  1343. evt.type = SDL_JOYHATMOTION;
  1344. evt.jaxis.which = joystickID;
  1345. evt.jhat.hat = ToUInt(name.Substring(3));
  1346. evt.jhat.value = HAT_CENTER;
  1347. if (eventType != E_TOUCHEND)
  1348. {
  1349. IntVector2 relPosition = position - element->GetScreenPosition() - element->GetSize() / 2;
  1350. if (relPosition.y_ < 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1351. evt.jhat.value |= HAT_UP;
  1352. if (relPosition.y_ > 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1353. evt.jhat.value |= HAT_DOWN;
  1354. if (relPosition.x_ < 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1355. evt.jhat.value |= HAT_LEFT;
  1356. if (relPosition.x_ > 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1357. evt.jhat.value |= HAT_RIGHT;
  1358. }
  1359. }
  1360. else
  1361. {
  1362. // Hat is binded by 4 keys, like 'WASD'
  1363. String keyBinding = keyBindingVar.GetString();
  1364. if (eventType == E_TOUCHEND)
  1365. {
  1366. evt.type = SDL_KEYUP;
  1367. evt.key.keysym.sym = element->GetVar(VAR_LAST_KEYSYM).GetInt();
  1368. if (!evt.key.keysym.sym)
  1369. return;
  1370. element->SetVar(VAR_LAST_KEYSYM, 0);
  1371. }
  1372. else
  1373. {
  1374. evt.type = SDL_KEYDOWN;
  1375. IntVector2 relPosition = position - element->GetScreenPosition() - element->GetSize() / 2;
  1376. if (relPosition.y_ < 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1377. evt.key.keysym.sym = keyBinding[0];
  1378. else if (relPosition.y_ > 0 && Abs(relPosition.x_ * 3 / 2) < Abs(relPosition.y_))
  1379. evt.key.keysym.sym = keyBinding[1];
  1380. else if (relPosition.x_ < 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1381. evt.key.keysym.sym = keyBinding[2];
  1382. else if (relPosition.x_ > 0 && Abs(relPosition.y_ * 3 / 2) < Abs(relPosition.x_))
  1383. evt.key.keysym.sym = keyBinding[3];
  1384. else
  1385. return;
  1386. if (eventType == E_TOUCHMOVE && evt.key.keysym.sym != element->GetVar(VAR_LAST_KEYSYM).GetInt())
  1387. {
  1388. // Dragging past the directional boundary will cause an additional key up event for previous key symbol
  1389. SDL_Event evt;
  1390. evt.type = SDL_KEYUP;
  1391. evt.key.keysym.sym = element->GetVar(VAR_LAST_KEYSYM).GetInt();
  1392. if (evt.key.keysym.sym)
  1393. {
  1394. evt.key.keysym.scancode = SDL_SCANCODE_UNKNOWN;
  1395. HandleSDLEvent(&evt);
  1396. }
  1397. element->SetVar(VAR_LAST_KEYSYM, 0);
  1398. }
  1399. evt.key.keysym.scancode = SDL_SCANCODE_UNKNOWN;
  1400. element->SetVar(VAR_LAST_KEYSYM, evt.key.keysym.sym);
  1401. }
  1402. }
  1403. }
  1404. else
  1405. return;
  1406. // Handle the fake SDL event to turn it into Urho3D genuine event
  1407. HandleSDLEvent(&evt);
  1408. }
  1409. }