Input.cpp 54 KB

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