Input.h 21 KB

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