Input.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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)
  56. {
  57. }
  58. /// Return number of buttons.
  59. unsigned GetNumButtons() const { return buttons_.Size(); }
  60. /// Return number of axes.
  61. unsigned GetNumAxes() const { return axes_.Size(); }
  62. /// Return number of hats.
  63. unsigned GetNumHats() const { return hats_.Size(); }
  64. /// Check if a button is held down.
  65. bool GetButtonDown(unsigned index) const
  66. {
  67. if (index < buttons_.Size())
  68. return buttons_[index];
  69. else
  70. return false;
  71. }
  72. /// Check if a button has been pressed on this frame.
  73. bool GetButtonPress(unsigned index) const
  74. {
  75. if (index < buttons_.Size())
  76. return buttonPress_[index];
  77. else
  78. return false;
  79. }
  80. /// Return axis position.
  81. float GetAxisPosition(unsigned index) const
  82. {
  83. if (index < axes_.Size())
  84. return axes_[index];
  85. else
  86. return 0.0f;
  87. }
  88. /// Return hat position.
  89. int GetHatPosition(unsigned index) const
  90. {
  91. if (index < hats_.Size())
  92. return hats_[index];
  93. else
  94. return HAT_CENTER;
  95. }
  96. /// SDL joystick.
  97. SDL_Joystick* joystick_;
  98. /// SDL joystick instance ID
  99. SDL_JoystickID instanceID_;
  100. /// SDL game controller.
  101. SDL_GameController* controller_;
  102. /// UI element containing the screen joystick.
  103. SharedPtr<UIElement> screenJoystick_;
  104. /// Joystick name.
  105. String name_;
  106. /// Button up/down state.
  107. PODVector<bool> buttons_;
  108. /// Button pressed on this frame.
  109. PODVector<bool> buttonPress_;
  110. /// Axis position from -1 to 1.
  111. PODVector<float> axes_;
  112. /// POV hat bits.
  113. PODVector<int> hats_;
  114. };
  115. /// %Input subsystem. Converts operating system window messages to input state and events.
  116. class URHO3D_API Input : public Object
  117. {
  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.
  129. void SetMouseVisible(bool enable);
  130. /// Set whether the virtual joystick is visible.
  131. void SetScreenJoystickVisible(SDL_JoystickID index, bool enable);
  132. /// Add screen joystick.
  133. /** Return the joystick index number when successful or M_MAX_UNSIGNED when error.
  134. * If layout file is not given, use the default screen joystick layout.
  135. * If style file is not given, use the default style file from root UI element.
  136. *
  137. * This method should only be called in main thread.
  138. */
  139. SDL_JoystickID AddScreenJoystick(XMLFile* layoutFile = 0, XMLFile* styleFile = 0);
  140. /// Remove screen joystick by index.
  141. /** Return true if successful.
  142. *
  143. * This method should only be called in main thread.
  144. */
  145. bool RemoveScreenJoystick(unsigned index);
  146. /// Show or hide on-screen keyboard on platforms that support it. When shown, keypresses from it are delivered as key events.
  147. void SetScreenKeyboardVisible(bool enable);
  148. /// 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.
  149. bool RecordGesture();
  150. /// Save all in-memory touch gestures. Return true if successful.
  151. bool SaveGestures(Serializer& dest);
  152. /// Save a specific in-memory touch gesture to a file. Return true if successful.
  153. bool SaveGesture(Serializer& dest, unsigned gestureID);
  154. /// Load touch gestures from a file. Return number of loaded gestures, or 0 on failure.
  155. unsigned LoadGestures(Deserializer& source);
  156. /// Return keycode from key name.
  157. int GetKeyFromName(const String& name) const;
  158. /// Return keycode from scancode.
  159. int GetKeyFromScancode(int scancode) const;
  160. /// Return name of key from keycode.
  161. String GetKeyName(int key) const;
  162. /// Return scancode from keycode.
  163. int GetScancodeFromKey(int key) const;
  164. /// Return scancode from key name.
  165. int GetScancodeFromName(const String& name) const;
  166. /// Return name of key from scancode.
  167. String GetScancodeName(int scancode) const;
  168. /// Check if a key is held down.
  169. bool GetKeyDown(int key) const;
  170. /// Check if a key has been pressed on this frame.
  171. bool GetKeyPress(int key) const;
  172. /// Check if a key is held down by scancode.
  173. bool GetScancodeDown(int scancode) const;
  174. /// Check if a key has been pressed on this frame by scancode.
  175. bool GetScancodePress(int scanode) const;
  176. /// Check if a mouse button is held down.
  177. bool GetMouseButtonDown(int button) const;
  178. /// Check if a mouse button has been pressed on this frame.
  179. bool GetMouseButtonPress(int button) const;
  180. /// Check if a qualifier key is held down.
  181. bool GetQualifierDown(int qualifier) const;
  182. /// Check if a qualifier key has been pressed on this frame.
  183. bool GetQualifierPress(int qualifier) const;
  184. /// Return the currently held down qualifiers.
  185. int GetQualifiers() const;
  186. /// Return mouse position within window. Should only be used with a visible mouse cursor.
  187. IntVector2 GetMousePosition() const;
  188. /// Return mouse movement since last frame.
  189. const IntVector2& GetMouseMove() const { return mouseMove_; }
  190. /// Return horizontal mouse movement since last frame.
  191. int GetMouseMoveX() const { return mouseMove_.x_; }
  192. /// Return vertical mouse movement since last frame.
  193. int GetMouseMoveY() const { return mouseMove_.y_; }
  194. /// Return mouse wheel movement since last frame.
  195. int GetMouseMoveWheel() const { return mouseMoveWheel_; }
  196. /// Return number of active finger touches.
  197. unsigned GetNumTouches() const { return touches_.Size(); }
  198. /// Return active finger touch by index.
  199. TouchState* GetTouch(unsigned index) const;
  200. /// Return number of connected joysticks.
  201. unsigned GetNumJoysticks() const { return joysticks_.Size(); }
  202. /// Return joystick name by index.
  203. const String& GetJoystickName(SDL_JoystickID index) const;
  204. /// Return joystick state by index. Automatically open if not opened yet.
  205. JoystickState* GetJoystick(SDL_JoystickID index);
  206. /// Return whether fullscreen toggle is enabled.
  207. bool GetToggleFullscreen() const { return toggleFullscreen_; }
  208. /// Return whether on-screen keyboard is supported.
  209. bool GetScreenKeyboardSupport() const;
  210. /// Return whether on-screen keyboard is being shown.
  211. bool IsScreenKeyboardVisible() const;
  212. /// Return whether the operating system mouse cursor is visible.
  213. bool IsMouseVisible() const { return mouseVisible_; }
  214. /// Return whether application window has input focus.
  215. bool HasFocus() { return inputFocus_; }
  216. /// Return whether application window is minimized.
  217. bool IsMinimized() const;
  218. private:
  219. /// Initialize when screen mode initially set.
  220. void Initialize();
  221. /// Open a joystick. Return -1 if no joystick.
  222. SDL_JoystickID OpenJoystick(unsigned index);
  223. /// Setup internal joystick structures.
  224. void ResetJoysticks();
  225. /// Prepare input state for application gaining input focus.
  226. void GainFocus();
  227. /// Prepare input state for application losing input focus.
  228. void LoseFocus();
  229. /// Clear input state.
  230. void ResetState();
  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. /// Input focus flag.
  282. bool inputFocus_;
  283. /// Minimized flag.
  284. bool minimized_;
  285. /// Gained focus on this frame flag.
  286. bool focusedThisFrame_;
  287. /// Next mouse move suppress flag.
  288. bool suppressNextMouseMove_;
  289. /// Initialized flag.
  290. bool initialized_;
  291. };
  292. }