Input.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Container/FlagSet.h"
  6. #include "../Container/HashSet.h"
  7. #include "../Core/Mutex.h"
  8. #include "../Core/Object.h"
  9. #include "../Container/List.h"
  10. #include "../Input/InputEvents.h"
  11. #include "../UI/Cursor.h"
  12. namespace Urho3D
  13. {
  14. /// %Input Mouse Modes.
  15. enum MouseMode
  16. {
  17. MM_ABSOLUTE = 0,
  18. MM_RELATIVE,
  19. MM_WRAP,
  20. MM_FREE,
  21. MM_INVALID
  22. };
  23. class Deserializer;
  24. class Graphics;
  25. class Serializer;
  26. class UIElement;
  27. class XMLFile;
  28. inline const IntVector2 MOUSE_POSITION_OFFSCREEN = IntVector2(M_MIN_INT, M_MIN_INT);
  29. /// %Input state for a finger touch.
  30. /// @nocount
  31. struct TouchState
  32. {
  33. /// Return last touched UI element, used by scripting integration.
  34. /// @property
  35. UIElement* GetTouchedElement();
  36. /// Touch (finger) ID.
  37. int touchID_;
  38. /// Position in screen coordinates.
  39. IntVector2 position_;
  40. /// Last position in screen coordinates.
  41. IntVector2 lastPosition_;
  42. /// Movement since last frame.
  43. IntVector2 delta_;
  44. /// Finger pressure.
  45. float pressure_;
  46. /// Last touched UI element from screen joystick.
  47. WeakPtr<UIElement> touchedElement_;
  48. };
  49. /// %Input state for a joystick.
  50. /// @nocount
  51. struct JoystickState
  52. {
  53. /// Initialize the number of buttons, axes and hats and set them to neutral state.
  54. void Initialize(i32 numButtons, i32 numAxes, i32 numHats);
  55. /// Reset button, axis and hat states to neutral.
  56. void Reset();
  57. /// Return whether is a game controller. Game controllers will use standardized axis and button mappings.
  58. /// @property
  59. bool IsController() const { return controller_ != nullptr; }
  60. /// Return number of buttons.
  61. /// @property
  62. i32 GetNumButtons() const { return buttons_.Size(); }
  63. /// Return number of axes.
  64. /// @property
  65. i32 GetNumAxes() const { return axes_.Size(); }
  66. /// Return number of hats.
  67. /// @property
  68. i32 GetNumHats() const { return hats_.Size(); }
  69. /// Check if a button is held down.
  70. /// @property
  71. bool GetButtonDown(i32 index) const
  72. {
  73. assert(index >= 0);
  74. return index < buttons_.Size() ? buttons_[index] : false;
  75. }
  76. /// Check if a button has been pressed on this frame.
  77. /// @property
  78. bool GetButtonPress(i32 index) const
  79. {
  80. assert(index >= 0);
  81. return index < buttonPress_.Size() ? buttonPress_[index] : false;
  82. }
  83. /// Return axis position.
  84. /// @property
  85. float GetAxisPosition(i32 index) const
  86. {
  87. assert(index >= 0);
  88. return index < axes_.Size() ? axes_[index] : 0.0f;
  89. }
  90. /// Return hat position.
  91. /// @property
  92. int GetHatPosition(i32 index) const
  93. {
  94. assert(index >= 0);
  95. return index < hats_.Size() ? hats_[index] : int(HAT_CENTER);
  96. }
  97. /// SDL joystick.
  98. SDL_Joystick* joystick_{};
  99. /// SDL joystick instance ID.
  100. SDL_JoystickID joystickID_{};
  101. /// SDL game controller.
  102. SDL_GameController* controller_{};
  103. /// UI element containing the screen joystick.
  104. UIElement* screenJoystick_{};
  105. /// Joystick name.
  106. String name_;
  107. /// Button up/down state.
  108. Vector<bool> buttons_;
  109. /// Button pressed on this frame.
  110. Vector<bool> buttonPress_;
  111. /// Axis position from -1 to 1.
  112. Vector<float> axes_;
  113. /// POV hat bits.
  114. Vector<int> hats_;
  115. };
  116. #ifdef __EMSCRIPTEN__
  117. class EmscriptenInput;
  118. #endif
  119. /// %Input subsystem. Converts operating system window messages to input state and events.
  120. class URHO3D_API Input : public Object
  121. {
  122. URHO3D_OBJECT(Input, Object);
  123. #ifdef __EMSCRIPTEN__
  124. friend class EmscriptenInput;
  125. #endif
  126. public:
  127. /// Construct.
  128. explicit Input(Context* context);
  129. /// Destruct.
  130. ~Input() override;
  131. /// Poll for window messages. Called by HandleBeginFrame().
  132. void Update();
  133. /// Set whether ALT-ENTER fullscreen toggle is enabled.
  134. /// @property
  135. void SetToggleFullscreen(bool enable);
  136. /// Set whether the operating system mouse cursor is visible. When not visible (default), is kept centered to prevent leaving the window. Mouse visibility event can be suppressed-- this also recalls any unsuppressed SetMouseVisible which can be returned by ResetMouseVisible().
  137. void SetMouseVisible(bool enable, bool suppressEvent = false);
  138. /// Reset last mouse visibility that was not suppressed in SetMouseVisible.
  139. void ResetMouseVisible();
  140. /// Set whether the mouse is currently being grabbed by an operation.
  141. void SetMouseGrabbed(bool grab, bool suppressEvent = false);
  142. /// Reset the mouse grabbed to the last unsuppressed SetMouseGrabbed call.
  143. void ResetMouseGrabbed();
  144. /// Set the mouse mode.
  145. /** Set the mouse mode behaviour.
  146. * MM_ABSOLUTE is the default behaviour, allowing the toggling of operating system cursor visibility and allowing the cursor to escape the window when visible.
  147. * When the operating system cursor is invisible in absolute mouse mode, the mouse is confined to the window.
  148. * If the operating system and UI cursors are both invisible, interaction with the Urho UI will be limited (eg: drag move / drag end events will not trigger).
  149. * SetMouseMode(MM_ABSOLUTE) will call SetMouseGrabbed(false).
  150. *
  151. * MM_RELATIVE sets the operating system cursor to invisible and confines the cursor to the window.
  152. * The operating system cursor cannot be set to be visible in this mode via SetMouseVisible(), however changes are tracked and will be restored when another mouse mode is set.
  153. * When the virtual cursor is also invisible, UI interaction will still function as normal (eg: drag events will trigger).
  154. * SetMouseMode(MM_RELATIVE) will call SetMouseGrabbed(true).
  155. *
  156. * MM_WRAP grabs the mouse from the operating system and confines the operating system cursor to the window, wrapping the cursor when it is near the edges.
  157. * SetMouseMode(MM_WRAP) will call SetMouseGrabbed(true).
  158. *
  159. * MM_FREE does not grab/confine the mouse cursor even when it is hidden. This can be used for cases where the cursor should render using the operating system
  160. * outside the window, and perform custom rendering (with SetMouseVisible(false)) inside.
  161. */
  162. void SetMouseMode(MouseMode mode, bool suppressEvent = false);
  163. /// Reset the last mouse mode that wasn't suppressed in SetMouseMode.
  164. void ResetMouseMode();
  165. /// Add screen joystick.
  166. /** Return the joystick instance ID when successful or negative on error.
  167. * If layout file is not given, use the default screen joystick layout.
  168. * If style file is not given, use the default style file from root UI element.
  169. *
  170. * This method should only be called in main thread.
  171. */
  172. SDL_JoystickID AddScreenJoystick(XMLFile* layoutFile = nullptr, XMLFile* styleFile = nullptr);
  173. /// Remove screen joystick by instance ID.
  174. /** Return true if successful.
  175. *
  176. * This method should only be called in main thread.
  177. */
  178. bool RemoveScreenJoystick(SDL_JoystickID id);
  179. /// Set whether the virtual joystick is visible.
  180. /// @property
  181. void SetScreenJoystickVisible(SDL_JoystickID id, bool enable);
  182. /// Show or hide on-screen keyboard on platforms that support it. When shown, keypresses from it are delivered as key events.
  183. /// @property
  184. void SetScreenKeyboardVisible(bool enable);
  185. /// Set touch emulation by mouse. Only available on desktop platforms. When enabled, actual mouse events are no longer sent and the mouse cursor is forced visible.
  186. /// @property
  187. void SetTouchEmulation(bool enable);
  188. /// Begin recording a touch gesture. Return true if successful. The E_GESTURERECORDED event (which contains the ID for the new gesture) will be sent when recording finishes.
  189. bool RecordGesture();
  190. /// Save all in-memory touch gestures. Return true if successful.
  191. bool SaveGestures(Serializer& dest);
  192. /// Save a specific in-memory touch gesture to a file. Return true if successful.
  193. bool SaveGesture(Serializer& dest, unsigned gestureID);
  194. /// Load touch gestures from a file. Return number of loaded gestures, or 0 on failure.
  195. i32 LoadGestures(Deserializer& source);
  196. /// Remove an in-memory gesture by ID. Return true if was found.
  197. bool RemoveGesture(unsigned gestureID);
  198. /// Remove all in-memory gestures.
  199. void RemoveAllGestures();
  200. /// Set the mouse cursor position. Uses the backbuffer (Graphics width/height) coordinates.
  201. /// @property
  202. void SetMousePosition(const IntVector2& position);
  203. /// Center the mouse position.
  204. void CenterMousePosition();
  205. /// Return keycode from key name.
  206. Key GetKeyFromName(const String& name) const;
  207. /// Return keycode from scancode.
  208. Key GetKeyFromScancode(Scancode scancode) const;
  209. /// Return name of key from keycode.
  210. String GetKeyName(Key key) const;
  211. /// Return scancode from keycode.
  212. Scancode GetScancodeFromKey(Key key) const;
  213. /// Return scancode from key name.
  214. Scancode GetScancodeFromName(const String& name) const;
  215. /// Return name of key from scancode.
  216. String GetScancodeName(Scancode scancode) const;
  217. /// Check if a key is held down.
  218. /// @property
  219. bool GetKeyDown(Key key) const;
  220. /// Check if a key has been pressed on this frame.
  221. /// @property
  222. bool GetKeyPress(Key key) const;
  223. /// Check if a key is held down by scancode.
  224. /// @property
  225. bool GetScancodeDown(Scancode scancode) const;
  226. /// Check if a key has been pressed on this frame by scancode.
  227. /// @property
  228. bool GetScancodePress(Scancode scancode) const;
  229. /// Check if a mouse button is held down.
  230. /// @property
  231. bool GetMouseButtonDown(MouseButtonFlags button) const;
  232. /// Check if a mouse button has been pressed on this frame.
  233. /// @property
  234. bool GetMouseButtonPress(MouseButtonFlags button) const;
  235. /// Check if a qualifier key is held down.
  236. /// @property
  237. bool GetQualifierDown(Qualifier qualifier) const;
  238. /// Check if a qualifier key has been pressed on this frame.
  239. /// @property
  240. bool GetQualifierPress(Qualifier qualifier) const;
  241. /// Return the currently held down qualifiers.
  242. /// @property
  243. QualifierFlags GetQualifiers() const;
  244. /// Return mouse position within window. Should only be used with a visible mouse cursor. Uses the backbuffer (Graphics width/height) coordinates.
  245. /// @property
  246. IntVector2 GetMousePosition() const;
  247. /// Return mouse movement since last frame.
  248. /// @property
  249. IntVector2 GetMouseMove() const;
  250. /// Return horizontal mouse movement since last frame.
  251. /// @property
  252. int GetMouseMoveX() const;
  253. /// Return vertical mouse movement since last frame.
  254. /// @property
  255. int GetMouseMoveY() const;
  256. /// Return mouse wheel movement since last frame.
  257. /// @property
  258. int GetMouseMoveWheel() const { return mouseMoveWheel_; }
  259. /// Return input coordinate scaling. Should return non-unity on High DPI display.
  260. /// @property
  261. Vector2 GetInputScale() const { return inputScale_; }
  262. /// Return number of active finger touches.
  263. /// @property
  264. i32 GetNumTouches() const { return touches_.Size(); }
  265. /// Return active finger touch by index.
  266. /// @property{get_touches}
  267. TouchState* GetTouch(i32 index) const;
  268. /// Return number of connected joysticks.
  269. /// @property
  270. i32 GetNumJoysticks() const { return joysticks_.Size(); }
  271. /// Return joystick state by ID, or null if does not exist.
  272. /// @property{get_joysticks}
  273. JoystickState* GetJoystick(SDL_JoystickID id);
  274. /// Return joystick state by index, or null if does not exist. 0 = first connected joystick.
  275. /// @property{get_joysticksByIndex}
  276. JoystickState* GetJoystickByIndex(i32 index);
  277. /// Return joystick state by name, or null if does not exist.
  278. /// @property{get_joysticksByName}
  279. JoystickState* GetJoystickByName(const String& name);
  280. /// Return whether fullscreen toggle is enabled.
  281. /// @property
  282. bool GetToggleFullscreen() const { return toggleFullscreen_; }
  283. /// Return whether a virtual joystick is visible.
  284. /// @property
  285. bool IsScreenJoystickVisible(SDL_JoystickID id) const;
  286. /// Return whether on-screen keyboard is supported.
  287. /// @property
  288. bool GetScreenKeyboardSupport() const;
  289. /// Return whether on-screen keyboard is being shown.
  290. /// @property
  291. bool IsScreenKeyboardVisible() const;
  292. /// Return whether touch emulation is enabled.
  293. /// @property
  294. bool GetTouchEmulation() const { return touchEmulation_; }
  295. /// Return whether the operating system mouse cursor is visible.
  296. /// @property
  297. bool IsMouseVisible() const { return mouseVisible_; }
  298. /// Return whether the mouse is currently being grabbed by an operation.
  299. /// @property
  300. bool IsMouseGrabbed() const { return mouseGrabbed_; }
  301. /// Return whether the mouse is locked to the window.
  302. /// @property
  303. bool IsMouseLocked() const;
  304. /// Return the mouse mode.
  305. /// @property
  306. MouseMode GetMouseMode() const { return mouseMode_; }
  307. /// Return whether application window has input focus.
  308. /// @property{get_focus}
  309. bool HasFocus() { return inputFocus_; }
  310. /// Return whether application window is minimized.
  311. /// @property
  312. bool IsMinimized() const;
  313. private:
  314. /// Initialize when screen mode initially set.
  315. void Initialize();
  316. /// Open a joystick and return its ID. Return -1 if no joystick.
  317. SDL_JoystickID OpenJoystick(i32 index);
  318. /// Setup internal joystick structures.
  319. void ResetJoysticks();
  320. /// Prepare input state for application gaining input focus.
  321. void GainFocus();
  322. /// Prepare input state for application losing input focus.
  323. void LoseFocus();
  324. /// Clear input state.
  325. void ResetState();
  326. /// Clear touch states and send touch end events.
  327. void ResetTouches();
  328. /// Reset input accumulation.
  329. void ResetInputAccumulation();
  330. /// Get the index of a touch based on the touch ID.
  331. i32 GetTouchIndexFromID(int touchID);
  332. /// Used internally to return and remove the next available touch index.
  333. i32 PopTouchIndex();
  334. /// Push a touch index back into the list of available when finished with it.
  335. void PushTouchIndex(int touchID);
  336. /// Send an input focus or window minimization change event.
  337. void SendInputFocusEvent();
  338. /// Handle a mouse button change.
  339. void SetMouseButton(MouseButton button, bool newState, int clicks);
  340. /// Handle a key change.
  341. void SetKey(Key key, Scancode scancode, bool newState);
  342. /// Handle mouse wheel change.
  343. void SetMouseWheel(int delta);
  344. /// Suppress next mouse movement.
  345. void SuppressNextMouseMove();
  346. /// Unsuppress mouse movement.
  347. void UnsuppressMouseMove();
  348. /// Handle screen mode event.
  349. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  350. /// Handle frame start event.
  351. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  352. /// Handle touch events from the controls of screen joystick(s).
  353. void HandleScreenJoystickTouch(StringHash eventType, VariantMap& eventData);
  354. /// Handle SDL event.
  355. void HandleSDLEvent(void* sdlEvent);
  356. #ifndef __EMSCRIPTEN__
  357. /// Set SDL mouse mode relative.
  358. void SetMouseModeRelative(SDL_bool enable);
  359. /// Set SDL mouse mode absolute.
  360. void SetMouseModeAbsolute(SDL_bool enable);
  361. #else
  362. /// Set whether the operating system mouse cursor is visible (Emscripten platform only).
  363. void SetMouseVisibleEmscripten(bool enable, bool suppressEvent = false);
  364. /// Set mouse mode final resolution (Emscripten platform only).
  365. void SetMouseModeEmscriptenFinal(MouseMode mode, bool suppressEvent = false);
  366. /// SetMouseMode (Emscripten platform only).
  367. void SetMouseModeEmscripten(MouseMode mode, bool suppressEvent);
  368. /// Handle frame end event.
  369. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  370. #endif
  371. /// Graphics subsystem.
  372. WeakPtr<Graphics> graphics_;
  373. /// Key down state.
  374. HashSet<int> keyDown_;
  375. /// Key pressed state.
  376. HashSet<int> keyPress_;
  377. /// Key down state by scancode.
  378. HashSet<int> scancodeDown_;
  379. /// Key pressed state by scancode.
  380. HashSet<int> scancodePress_;
  381. /// Active finger touches.
  382. HashMap<int, TouchState> touches_;
  383. /// List that maps between event touch IDs and normalised touch IDs.
  384. List<int> availableTouchIDs_;
  385. /// Mapping of touch indices.
  386. HashMap<int, int> touchIDMap_;
  387. /// String for text input.
  388. String textInput_;
  389. /// Opened joysticks.
  390. HashMap<SDL_JoystickID, JoystickState> joysticks_;
  391. /// Mouse buttons' down state.
  392. MouseButtonFlags mouseButtonDown_;
  393. /// Mouse buttons' pressed state.
  394. MouseButtonFlags mouseButtonPress_;
  395. /// Last mouse position for calculating movement.
  396. IntVector2 lastMousePosition_;
  397. /// Last mouse position before being set to not visible.
  398. IntVector2 lastVisibleMousePosition_;
  399. /// Mouse movement since last frame.
  400. IntVector2 mouseMove_;
  401. /// Mouse wheel movement since last frame.
  402. int mouseMoveWheel_;
  403. /// Input coordinate scaling. Non-unity when window and backbuffer have different sizes (e.g. Retina display).
  404. Vector2 inputScale_;
  405. /// SDL window ID.
  406. unsigned windowID_;
  407. /// Fullscreen toggle flag.
  408. bool toggleFullscreen_;
  409. /// Operating system mouse cursor visible flag.
  410. bool mouseVisible_;
  411. /// The last operating system mouse cursor visible flag set by end use call to SetMouseVisible.
  412. bool lastMouseVisible_;
  413. /// Flag to indicate the mouse is being grabbed by an operation. Subsystems like UI that uses mouse should temporarily ignore the mouse hover or click events.
  414. bool mouseGrabbed_;
  415. /// The last mouse grabbed set by SetMouseGrabbed.
  416. bool lastMouseGrabbed_;
  417. /// Determines the mode of mouse behaviour.
  418. MouseMode mouseMode_;
  419. /// The last mouse mode set by SetMouseMode.
  420. MouseMode lastMouseMode_;
  421. #ifndef __EMSCRIPTEN__
  422. /// Flag to determine whether SDL mouse relative was used.
  423. bool sdlMouseRelative_;
  424. #endif
  425. /// Touch emulation mode flag.
  426. bool touchEmulation_;
  427. /// Input focus flag.
  428. bool inputFocus_;
  429. /// Minimized flag.
  430. bool minimized_;
  431. /// Gained focus on this frame flag.
  432. bool focusedThisFrame_;
  433. /// Next mouse move suppress flag.
  434. bool suppressNextMouseMove_;
  435. /// Whether mouse move is accumulated in backbuffer scale or not (when using events directly).
  436. bool mouseMoveScaled_;
  437. /// Initialized flag.
  438. bool initialized_;
  439. #ifdef __EMSCRIPTEN__
  440. /// Emscripten Input glue instance.
  441. std::unique_ptr<EmscriptenInput> emscriptenInput_;
  442. /// Flag used to detect mouse jump when exiting pointer-lock.
  443. bool emscriptenExitingPointerLock_;
  444. /// Flag used to detect mouse jump on initial mouse click when entering pointer-lock.
  445. bool emscriptenEnteredPointerLock_;
  446. /// Flag indicating current pointer-lock status.
  447. bool emscriptenPointerLock_;
  448. #endif
  449. };
  450. }