Input.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 game controller.
  99. SDL_GameController* controller_;
  100. /// UI element containing the screen joystick.
  101. SharedPtr<UIElement> screenJoystick_;
  102. /// Joystick name.
  103. String name_;
  104. /// Button up/down state.
  105. PODVector<bool> buttons_;
  106. /// Button pressed on this frame.
  107. PODVector<bool> buttonPress_;
  108. /// Axis position from -1 to 1.
  109. PODVector<float> axes_;
  110. /// POV hat bits.
  111. PODVector<int> hats_;
  112. };
  113. /// %Input subsystem. Converts operating system window messages to input state and events.
  114. class URHO3D_API Input : public Object
  115. {
  116. OBJECT(Input);
  117. public:
  118. /// Construct.
  119. Input(Context* context);
  120. /// Destruct.
  121. virtual ~Input();
  122. /// Poll for window messages. Called by HandleBeginFrame().
  123. void Update();
  124. /// Set whether ALT-ENTER fullscreen toggle is enabled.
  125. void SetToggleFullscreen(bool enable);
  126. /// Set whether the operating system mouse cursor is visible. When not visible (default), is kept centered to prevent leaving the window.
  127. void SetMouseVisible(bool enable);
  128. /// Open a joystick. Return true if successful.
  129. bool OpenJoystick(unsigned index);
  130. /// Close a joystick.
  131. void CloseJoystick(unsigned index);
  132. /// Redetect joysticks. Return true if successful.
  133. bool DetectJoysticks();
  134. /// Add screen joystick. Return the joystick index number when successful or M_MAX_UNSIGNED when error. If layout file is not given, use the default screen joystick layout. If style file is not given, use the default style file from root UI element.
  135. unsigned AddScreenJoystick(XMLFile* layoutFile = 0, XMLFile* styleFile = 0);
  136. /// Remove screen joystick by index. Return true if successful.
  137. bool RemoveScreenJoystick(unsigned index);
  138. /// Show or hide on-screen keyboard on platforms that support it. When shown, keypresses from it are delivered as key events.
  139. void SetScreenKeyboardVisible(bool enable);
  140. /// 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.
  141. bool RecordGesture();
  142. /// Save all in-memory touch gestures. Return true if successful.
  143. bool SaveGestures(Serializer& dest);
  144. /// Save a specific in-memory touch gesture to a file. Return true if successful.
  145. bool SaveGesture(Serializer& dest, unsigned gestureID);
  146. /// Load touch gestures from a file. Return number of loaded gestures, or 0 on failure.
  147. unsigned LoadGestures(Deserializer& source);
  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 name by index.
  195. const String& GetJoystickName(unsigned index) const;
  196. /// Return joystick state by index. Automatically open if not opened yet.
  197. JoystickState* GetJoystick(unsigned index);
  198. /// Return whether fullscreen toggle is enabled.
  199. bool GetToggleFullscreen() const { return toggleFullscreen_; }
  200. /// Return whether on-screen keyboard is supported.
  201. bool GetScreenKeyboardSupport() const;
  202. /// Return whether on-screen keyboard is being shown.
  203. bool IsScreenKeyboardVisible() const;
  204. /// Return whether the operating system mouse cursor is visible.
  205. bool IsMouseVisible() const { return mouseVisible_; }
  206. /// Return whether application window has input focus.
  207. bool HasFocus() { return inputFocus_; }
  208. /// Return whether application window is minimized.
  209. bool IsMinimized() const;
  210. private:
  211. /// Initialize when screen mode initially set.
  212. void Initialize();
  213. /// Setup internal joystick structures.
  214. void ResetJoysticks();
  215. /// Prepare input state for application gaining input focus.
  216. void GainFocus();
  217. /// Prepare input state for application losing input focus.
  218. void LoseFocus();
  219. /// Clear input state.
  220. void ResetState();
  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. Vector<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. /// Input focus flag.
  272. bool inputFocus_;
  273. /// Minimized flag.
  274. bool minimized_;
  275. /// Gained focus on this frame flag.
  276. bool focusedThisFrame_;
  277. /// Next mouse move suppress flag.
  278. bool suppressNextMouseMove_;
  279. /// Initialized flag.
  280. bool initialized_;
  281. /// Map SDL joystick ID to internal index.
  282. HashMap<int, unsigned> joystickIDMap_;
  283. };
  284. }