Input.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // Copyright (c) 2008-2015 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 "../Input/InputEvents.h"
  25. #include "../Core/Mutex.h"
  26. #include "../Core/Object.h"
  27. #include "../Container/List.h"
  28. #include "../UI/Cursor.h"
  29. namespace Atomic
  30. {
  31. /// %Input Mouse Modes.
  32. enum MouseMode
  33. {
  34. MM_ABSOLUTE = 0,
  35. MM_RELATIVE,
  36. MM_WRAP
  37. };
  38. class Deserializer;
  39. class Graphics;
  40. class Serializer;
  41. class UIElement;
  42. class XMLFile;
  43. const IntVector2 MOUSE_POSITION_OFFSCREEN = IntVector2(M_MIN_INT, M_MIN_INT);
  44. /// %Input state for a finger touch.
  45. struct TouchState
  46. {
  47. /// Return last touched UI element, used by scripting integration.
  48. UIElement* GetTouchedElement();
  49. /// Touch (finger) ID.
  50. int touchID_;
  51. /// Position in screen coordinates.
  52. IntVector2 position_;
  53. /// Last position in screen coordinates.
  54. IntVector2 lastPosition_;
  55. /// Movement since last frame.
  56. IntVector2 delta_;
  57. /// Finger pressure.
  58. float pressure_;
  59. /// Last touched UI element from screen joystick.
  60. WeakPtr<UIElement> touchedElement_;
  61. };
  62. /// %Input state for a joystick.
  63. struct JoystickState
  64. {
  65. /// Construct with defaults.
  66. JoystickState() :
  67. joystick_(0), controller_(0), screenJoystick_(0)
  68. {
  69. }
  70. /// Initialize the number of buttons, axes and hats and set them to neutral state.
  71. void Initialize(unsigned numButtons, unsigned numAxes, unsigned numHats);
  72. /// Reset button, axis and hat states to neutral.
  73. void Reset();
  74. /// Return whether is a game controller. Game controllers will use standardized axis and button mappings.
  75. bool IsController() const { return controller_ != 0; }
  76. /// Return number of buttons.
  77. unsigned GetNumButtons() const { return buttons_.Size(); }
  78. /// Return number of axes.
  79. unsigned GetNumAxes() const { return axes_.Size(); }
  80. /// Return number of hats.
  81. unsigned GetNumHats() const { return hats_.Size(); }
  82. /// Check if a button is held down.
  83. bool GetButtonDown(unsigned index) const { return index < buttons_.Size() ? buttons_[index] : false; }
  84. /// Check if a button has been pressed on this frame.
  85. bool GetButtonPress(unsigned index) const { return index < buttonPress_.Size() ? buttonPress_[index] : false; }
  86. /// Return axis position.
  87. float GetAxisPosition(unsigned index) const { return index < axes_.Size() ? axes_[index] : 0.0f; }
  88. /// Return hat position.
  89. int GetHatPosition(unsigned index) const { return index < hats_.Size() ? hats_[index] : HAT_CENTER; }
  90. /// SDL joystick.
  91. SDL_Joystick* joystick_;
  92. /// SDL joystick instance ID.
  93. SDL_JoystickID joystickID_;
  94. /// SDL game controller.
  95. SDL_GameController* controller_;
  96. /// UI element containing the screen joystick.
  97. UIElement* screenJoystick_;
  98. /// Joystick name.
  99. String name_;
  100. /// Button up/down state.
  101. PODVector<bool> buttons_;
  102. /// Button pressed on this frame.
  103. PODVector<bool> buttonPress_;
  104. /// Axis position from -1 to 1.
  105. PODVector<float> axes_;
  106. /// POV hat bits.
  107. PODVector<int> hats_;
  108. };
  109. #ifdef EMSCRIPTEN
  110. class EmscriptenInput;
  111. #endif
  112. /// %Input subsystem. Converts operating system window messages to input state and events.
  113. class ATOMIC_API Input : public Object
  114. {
  115. #ifdef EMSCRIPTEN
  116. friend class EmscriptenInput;
  117. #endif
  118. OBJECT(Input);
  119. public:
  120. /// Construct.
  121. Input(Context* context);
  122. /// Destruct.
  123. virtual ~Input();
  124. /// Poll for window messages. Called by HandleBeginFrame().
  125. void Update();
  126. /// Set whether ALT-ENTER fullscreen toggle is enabled.
  127. void SetToggleFullscreen(bool enable);
  128. /// Set whether the operating system mouse cursor is visible. When not visible (default), is kept centered to prevent leaving the window. Mouse visiblility event can be suppressed-- this also recalls any unsuppressed SetMouseVisible which can be returned by ResetMouseVisible().
  129. void SetMouseVisible(bool enable, bool suppressEvent = false);
  130. /// Reset last mouse visibilty that was not suppressed in SetMouseVisible.
  131. void ResetMouseVisible();
  132. /// Set whether the mouse is currently being grabbed by an operation.
  133. void SetMouseGrabbed(bool grab);
  134. /// Set the mouse mode.
  135. /** Set the mouse mode behaviour.
  136. * MM_ABSOLUTE is the default behaviour, allowing the toggling of operating system cursor visibility and allowing the cursor to escape the window when visible.
  137. * When the operating system cursor is invisible in absolute mouse mode, the mouse is confined to the window.
  138. * 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).
  139. * SetMouseMode(MM_ABSOLUTE) will call SetMouseGrabbed(false).
  140. *
  141. * MM_RELATIVE sets the operating system cursor to invisible and confines the cursor to the window.
  142. * 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.
  143. * When the virtual cursor is also invisible, UI interaction will still function as normal (eg: drag events will trigger).
  144. * SetMouseMode(MM_RELATIVE) will call SetMouseGrabbed(true).
  145. *
  146. * 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.
  147. * SetMouseMode(MM_WRAP) will call SetMouseGrabbed(true).
  148. */
  149. void SetMouseMode(MouseMode mode);
  150. /// Add screen joystick.
  151. /** Return the joystick instance ID when successful or negative on error.
  152. * If layout file is not given, use the default screen joystick layout.
  153. * If style file is not given, use the default style file from root UI element.
  154. *
  155. * This method should only be called in main thread.
  156. */
  157. int AddScreenJoystick(XMLFile* layoutFile = 0, XMLFile* styleFile = 0);
  158. /// Remove screen joystick by instance ID.
  159. /** Return true if successful.
  160. *
  161. * This method should only be called in main thread.
  162. */
  163. bool RemoveScreenJoystick(SDL_JoystickID id);
  164. /// Set whether the virtual joystick is visible.
  165. void SetScreenJoystickVisible(SDL_JoystickID id, bool enable);
  166. /// Show or hide on-screen keyboard on platforms that support it. When shown, keypresses from it are delivered as key events.
  167. void SetScreenKeyboardVisible(bool enable);
  168. /// 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.
  169. void SetTouchEmulation(bool enable);
  170. /// 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.
  171. bool RecordGesture();
  172. /// Save all in-memory touch gestures. Return true if successful.
  173. bool SaveGestures(Serializer& dest);
  174. /// Save a specific in-memory touch gesture to a file. Return true if successful.
  175. bool SaveGesture(Serializer& dest, unsigned gestureID);
  176. /// Load touch gestures from a file. Return number of loaded gestures, or 0 on failure.
  177. unsigned LoadGestures(Deserializer& source);
  178. /// Remove an in-memory gesture by ID. Return true if was found.
  179. bool RemoveGesture(unsigned gestureID);
  180. /// Remove all in-memory gestures.
  181. void RemoveAllGestures();
  182. /// Return keycode from key name.
  183. int GetKeyFromName(const String& name) const;
  184. /// Return keycode from scancode.
  185. int GetKeyFromScancode(int scancode) const;
  186. /// Return name of key from keycode.
  187. String GetKeyName(int key) const;
  188. /// Return scancode from keycode.
  189. int GetScancodeFromKey(int key) const;
  190. /// Return scancode from key name.
  191. int GetScancodeFromName(const String& name) const;
  192. /// Return name of key from scancode.
  193. String GetScancodeName(int scancode) const;
  194. /// Check if a key is held down.
  195. bool GetKeyDown(int key) const;
  196. /// Check if a key has been pressed on this frame.
  197. bool GetKeyPress(int key) const;
  198. /// Check if a key is held down by scancode.
  199. bool GetScancodeDown(int scancode) const;
  200. /// Check if a key has been pressed on this frame by scancode.
  201. bool GetScancodePress(int scanode) const;
  202. /// Check if a mouse button is held down.
  203. bool GetMouseButtonDown(int button) const;
  204. /// Check if a mouse button has been pressed on this frame.
  205. bool GetMouseButtonPress(int button) const;
  206. /// Check if a qualifier key is held down.
  207. bool GetQualifierDown(int qualifier) const;
  208. /// Check if a qualifier key has been pressed on this frame.
  209. bool GetQualifierPress(int qualifier) const;
  210. /// Return the currently held down qualifiers.
  211. int GetQualifiers() const;
  212. /// Return mouse position within window. Should only be used with a visible mouse cursor.
  213. IntVector2 GetMousePosition() const;
  214. /// Return mouse movement since last frame.
  215. const IntVector2& GetMouseMove() const { return mouseMove_; }
  216. /// Return horizontal mouse movement since last frame.
  217. int GetMouseMoveX() const { return mouseMove_.x_; }
  218. /// Return vertical mouse movement since last frame.
  219. int GetMouseMoveY() const { return mouseMove_.y_; }
  220. /// Return mouse wheel movement since last frame.
  221. int GetMouseMoveWheel() const { return mouseMoveWheel_; }
  222. /// Return number of active finger touches.
  223. unsigned GetNumTouches() const { return touches_.Size(); }
  224. /// Return active finger touch by index.
  225. TouchState* GetTouch(unsigned index) const;
  226. /// Return number of connected joysticks.
  227. unsigned GetNumJoysticks() const { return joysticks_.Size(); }
  228. /// Return joystick state by ID, or null if does not exist.
  229. JoystickState* GetJoystick(SDL_JoystickID id);
  230. /// Return joystick state by index, or null if does not exist. 0 = first connected joystick.
  231. JoystickState* GetJoystickByIndex(unsigned index);
  232. /// Return whether fullscreen toggle is enabled.
  233. bool GetToggleFullscreen() const { return toggleFullscreen_; }
  234. /// Return whether a virtual joystick is visible.
  235. bool IsScreenJoystickVisible(SDL_JoystickID id) const;
  236. /// Return whether on-screen keyboard is supported.
  237. bool GetScreenKeyboardSupport() const;
  238. /// Return whether on-screen keyboard is being shown.
  239. bool IsScreenKeyboardVisible() const;
  240. /// Return whether touch emulation is enabled.
  241. bool GetTouchEmulation() const { return touchEmulation_; }
  242. /// Return whether the operating system mouse cursor is visible.
  243. bool IsMouseVisible() const { return mouseVisible_; }
  244. /// Return whether the mouse is currently being grabbed by an operation.
  245. bool IsMouseGrabbed() const { return mouseGrabbed_; }
  246. /// Return the mouse mode.
  247. MouseMode GetMouseMode() const { return mouseMode_; }
  248. /// Return whether application window has input focus.
  249. bool HasFocus() { return inputFocus_; }
  250. /// Return whether application window is minimized.
  251. bool IsMinimized() const;
  252. private:
  253. /// Initialize when screen mode initially set.
  254. void Initialize();
  255. /// Open a joystick and return its ID. Return -1 if no joystick.
  256. SDL_JoystickID OpenJoystick(unsigned index);
  257. /// Setup internal joystick structures.
  258. void ResetJoysticks();
  259. /// Prepare input state for application gaining input focus.
  260. void GainFocus();
  261. /// Prepare input state for application losing input focus.
  262. void LoseFocus();
  263. /// Clear input state.
  264. void ResetState();
  265. /// Clear touch states and send touch end events.
  266. void ResetTouches();
  267. /// Get the index of a touch based on the touch ID.
  268. unsigned GetTouchIndexFromID(int touchID);
  269. /// Used internally to return and remove the next available touch index.
  270. unsigned PopTouchIndex();
  271. /// Push a touch index back into the list of available when finished with it.
  272. void PushTouchIndex(int touchID);
  273. /// Send an input focus or window minimization change event.
  274. void SendInputFocusEvent();
  275. /// Handle a mouse button change.
  276. void SetMouseButton(int button, bool newState);
  277. /// Handle a key change.
  278. void SetKey(int key, int scancode, unsigned raw, bool newState);
  279. #ifdef EMSCRIPTEN
  280. void SetMouseVisibleEmscripten(bool enable);
  281. void SetMouseModeEmscripten(MouseMode mode);
  282. #endif
  283. /// Handle mousewheel change.
  284. void SetMouseWheel(int delta);
  285. /// Internal function to set the mouse cursor position.
  286. void SetMousePosition(const IntVector2& position);
  287. /// Handle screen mode event.
  288. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  289. /// Handle frame start event.
  290. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  291. /// Handle touch events from the controls of screen joystick(s).
  292. void HandleScreenJoystickTouch(StringHash eventType, VariantMap& eventData);
  293. /// Handle SDL event.
  294. void HandleSDLEvent(void* sdlEvent);
  295. /// Graphics subsystem.
  296. WeakPtr<Graphics> graphics_;
  297. /// Key down state.
  298. HashSet<int> keyDown_;
  299. /// Key pressed state.
  300. HashSet<int> keyPress_;
  301. /// Key down state by scancode.
  302. HashSet<int> scancodeDown_;
  303. /// Key pressed state by scancode.
  304. HashSet<int> scancodePress_;
  305. /// Active finger touches.
  306. HashMap<int, TouchState> touches_;
  307. /// List that maps between event touch IDs and normalised touch IDs
  308. List<int> availableTouchIDs_;
  309. /// Mapping of touch indicies
  310. HashMap<int, int> touchIDMap_;
  311. /// String for text input.
  312. String textInput_;
  313. /// Opened joysticks.
  314. HashMap<SDL_JoystickID, JoystickState> joysticks_;
  315. /// Mouse buttons' down state.
  316. unsigned mouseButtonDown_;
  317. /// Mouse buttons' pressed state.
  318. unsigned mouseButtonPress_;
  319. /// Last mouse position for calculating movement.
  320. IntVector2 lastMousePosition_;
  321. /// Last mouse position before being set to not visible.
  322. IntVector2 lastVisibleMousePosition_;
  323. /// Mouse movement since last frame.
  324. IntVector2 mouseMove_;
  325. /// Mouse wheel movement since last frame.
  326. int mouseMoveWheel_;
  327. /// SDL window ID.
  328. unsigned windowID_;
  329. /// Fullscreen toggle flag.
  330. bool toggleFullscreen_;
  331. /// Operating system mouse cursor visible flag.
  332. bool mouseVisible_;
  333. /// The last operating system mouse cursor visible flag set by end use call to SetMouseVisible.
  334. bool lastMouseVisible_;
  335. /// 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.
  336. bool mouseGrabbed_;
  337. /// Determines the mode of mouse behaviour.
  338. MouseMode mouseMode_;
  339. /// Touch emulation mode flag.
  340. bool touchEmulation_;
  341. /// Input focus flag.
  342. bool inputFocus_;
  343. /// Minimized flag.
  344. bool minimized_;
  345. /// Gained focus on this frame flag.
  346. bool focusedThisFrame_;
  347. /// Next mouse move suppress flag.
  348. bool suppressNextMouseMove_;
  349. /// Initialized flag.
  350. bool initialized_;
  351. #ifdef EMSCRIPTEN
  352. /// Emscripten Input glue instance.
  353. EmscriptenInput* emscriptenInput_;
  354. /// Flag used to detect mouse jump when exiting pointer lock.
  355. bool emscriptenExitingPointerLock_;
  356. /// Flag used to detect mouse jump on initial mouse click when entering pointer lock.
  357. bool emscriptenEnteredPointerLock_;
  358. #endif
  359. };
  360. }