Input.h 14 KB

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