Input.cpp 56 KB

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