Input.h 13 KB

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