Input.cpp 51 KB

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