Input.h 13 KB

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