Input.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /// 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.
  129. bool RecordGesture();
  130. /// Save all in-memory touch gestures. Return true if successful.
  131. bool SaveGestures(Serializer& dest);
  132. /// Save a specific in-memory touch gesture to a file. Return true if successful.
  133. bool SaveGesture(Serializer& dest, unsigned gestureID);
  134. /// Load touch gestures from a file. Return number of loaded gestures, or 0 on failure.
  135. unsigned LoadGestures(Deserializer& source);
  136. /// Return keycode from key name.
  137. int GetKeyFromName(const String& name) const;
  138. /// Return keycode from scancode.
  139. int GetKeyFromScancode(int scancode) const;
  140. /// Return name of key from keycode.
  141. String GetKeyName(int key) const;
  142. /// Return scancode from keycode.
  143. int GetScancodeFromKey(int key) const;
  144. /// Return scancode from key name.
  145. int GetScancodeFromName(const String& name) const;
  146. /// Return name of key from scancode.
  147. String GetScancodeName(int scancode) const;
  148. /// Check if a key is held down.
  149. bool GetKeyDown(int key) const;
  150. /// Check if a key has been pressed on this frame.
  151. bool GetKeyPress(int key) const;
  152. /// Check if a key is held down by scancode.
  153. bool GetScancodeDown(int scancode) const;
  154. /// Check if a key has been pressed on this frame by scancode.
  155. bool GetScancodePress(int scanode) const;
  156. /// Check if a mouse button is held down.
  157. bool GetMouseButtonDown(int button) const;
  158. /// Check if a mouse button has been pressed on this frame.
  159. bool GetMouseButtonPress(int button) const;
  160. /// Check if a qualifier key is held down.
  161. bool GetQualifierDown(int qualifier) const;
  162. /// Check if a qualifier key has been pressed on this frame.
  163. bool GetQualifierPress(int qualifier) const;
  164. /// Return the currently held down qualifiers.
  165. int GetQualifiers() const;
  166. /// Return mouse position within window. Should only be used with a visible mouse cursor.
  167. IntVector2 GetMousePosition() const;
  168. /// Return mouse movement since last frame.
  169. const IntVector2& GetMouseMove() const { return mouseMove_; }
  170. /// Return horizontal mouse movement since last frame.
  171. int GetMouseMoveX() const { return mouseMove_.x_; }
  172. /// Return vertical mouse movement since last frame.
  173. int GetMouseMoveY() const { return mouseMove_.y_; }
  174. /// Return mouse wheel movement since last frame.
  175. int GetMouseMoveWheel() const { return mouseMoveWheel_; }
  176. /// Return number of active finger touches.
  177. unsigned GetNumTouches() const { return touches_.Size(); }
  178. /// Return active finger touch by index.
  179. TouchState* GetTouch(unsigned index) const;
  180. /// Return number of connected joysticks.
  181. unsigned GetNumJoysticks() const { return joysticks_.Size(); }
  182. /// Return joystick state by ID, or null if does not exist.
  183. JoystickState* GetJoystick(SDL_JoystickID id);
  184. /// Return joystick state by index, or null if does not exist. 0 = first connected joystick.
  185. JoystickState* GetJoystickByIndex(unsigned index);
  186. /// Return whether fullscreen toggle is enabled.
  187. bool GetToggleFullscreen() const { return toggleFullscreen_; }
  188. /// Return whether a virtual joystick is visible.
  189. bool IsScreenJoystickVisible(SDL_JoystickID id) const;
  190. /// Return whether on-screen keyboard is supported.
  191. bool GetScreenKeyboardSupport() const;
  192. /// Return whether on-screen keyboard is being shown.
  193. bool IsScreenKeyboardVisible() const;
  194. /// Return whether the operating system mouse cursor is visible.
  195. bool IsMouseVisible() const { return mouseVisible_; }
  196. /// Return whether the mouse is currently being grabbed by an operation.
  197. bool IsMouseGrabbed() const { return mouseGrabbed_; }
  198. /// Return whether application window has input focus.
  199. bool HasFocus() { return inputFocus_; }
  200. /// Return whether application window is minimized.
  201. bool IsMinimized() const;
  202. private:
  203. /// Initialize when screen mode initially set.
  204. void Initialize();
  205. /// Open a joystick and return its ID. Return -1 if no joystick.
  206. SDL_JoystickID OpenJoystick(unsigned index);
  207. /// Setup internal joystick structures.
  208. void ResetJoysticks();
  209. /// Prepare input state for application gaining input focus.
  210. void GainFocus();
  211. /// Prepare input state for application losing input focus.
  212. void LoseFocus();
  213. /// Clear input state.
  214. void ResetState();
  215. /// Send an input focus or window minimization change event.
  216. void SendInputFocusEvent();
  217. /// Handle a mouse button change.
  218. void SetMouseButton(int button, bool newState);
  219. /// Handle a key change.
  220. void SetKey(int key, int scancode, unsigned raw, bool newState);
  221. /// Handle mousewheel change.
  222. void SetMouseWheel(int delta);
  223. /// Internal function to set the mouse cursor position.
  224. void SetMousePosition(const IntVector2& position);
  225. /// Handle screen mode event.
  226. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  227. /// Handle frame start event.
  228. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  229. /// Handle touch events from the controls of screen joystick(s).
  230. void HandleScreenJoystickTouch(StringHash eventType, VariantMap& eventData);
  231. /// Handle SDL event.
  232. void HandleSDLEvent(void* sdlEvent);
  233. /// Graphics subsystem.
  234. WeakPtr<Graphics> graphics_;
  235. /// Key down state.
  236. HashSet<int> keyDown_;
  237. /// Key pressed state.
  238. HashSet<int> keyPress_;
  239. /// Key down state by scancode.
  240. HashSet<int> scancodeDown_;
  241. /// Key pressed state by scancode.
  242. HashSet<int> scancodePress_;
  243. /// Active finger touches.
  244. HashMap<int, TouchState> touches_;
  245. /// String for text input.
  246. String textInput_;
  247. /// Opened joysticks.
  248. HashMap<SDL_JoystickID, JoystickState> joysticks_;
  249. /// Mouse buttons' down state.
  250. unsigned mouseButtonDown_;
  251. /// Mouse buttons' pressed state.
  252. unsigned mouseButtonPress_;
  253. /// Last mouse position for calculating movement.
  254. IntVector2 lastMousePosition_;
  255. /// Mouse movement since last frame.
  256. IntVector2 mouseMove_;
  257. /// Mouse wheel movement since last frame.
  258. int mouseMoveWheel_;
  259. /// SDL window ID.
  260. unsigned windowID_;
  261. /// Fullscreen toggle flag.
  262. bool toggleFullscreen_;
  263. /// Operating system mouse cursor visible flag.
  264. bool mouseVisible_;
  265. /// 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.
  266. bool mouseGrabbed_;
  267. /// Input focus flag.
  268. bool inputFocus_;
  269. /// Minimized flag.
  270. bool minimized_;
  271. /// Gained focus on this frame flag.
  272. bool focusedThisFrame_;
  273. /// Next mouse move suppress flag.
  274. bool suppressNextMouseMove_;
  275. /// Initialized flag.
  276. bool initialized_;
  277. };
  278. }