Input.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 Graphics;
  30. /// %Input state for a finger touch.
  31. struct TouchState
  32. {
  33. /// Touch (finger) ID.
  34. int touchID_;
  35. /// Position in screen coordinates.
  36. IntVector2 position_;
  37. /// Last position in screen coordinates.
  38. IntVector2 lastPosition_;
  39. /// Movement since last frame.
  40. IntVector2 delta_;
  41. /// Finger pressure.
  42. float pressure_;
  43. };
  44. /// %Input state for a joystick.
  45. struct JoystickState
  46. {
  47. /// Construct with defaults.
  48. JoystickState() :
  49. joystick_(0), controller_(0)
  50. {
  51. }
  52. /// Return number of buttons.
  53. unsigned GetNumButtons() const { return buttons_.Size(); }
  54. /// Return number of axes.
  55. unsigned GetNumAxes() const { return axes_.Size(); }
  56. /// Return number of hats.
  57. unsigned GetNumHats() const { return hats_.Size(); }
  58. /// Check if a button is held down.
  59. bool GetButtonDown(unsigned index) const
  60. {
  61. if (index < buttons_.Size())
  62. return buttons_[index];
  63. else
  64. return false;
  65. }
  66. /// Check if a button has been pressed on this frame.
  67. bool GetButtonPress(unsigned index) const
  68. {
  69. if (index < buttons_.Size())
  70. return buttonPress_[index];
  71. else
  72. return false;
  73. }
  74. /// Return axis position.
  75. float GetAxisPosition(unsigned index) const
  76. {
  77. if (index < axes_.Size())
  78. return axes_[index];
  79. else
  80. return 0.0f;
  81. }
  82. /// Return hat position.
  83. int GetHatPosition(unsigned index) const
  84. {
  85. if (index < hats_.Size())
  86. return hats_[index];
  87. else
  88. return HAT_CENTER;
  89. }
  90. /// SDL joystick.
  91. SDL_Joystick* joystick_;
  92. /// SDL game controller
  93. SDL_GameController* controller_;
  94. /// Joystick name.
  95. String name_;
  96. /// Button up/down state.
  97. PODVector<bool> buttons_;
  98. /// Button pressed on this frame.
  99. PODVector<bool> buttonPress_;
  100. /// Axis position from -1 to 1.
  101. PODVector<float> axes_;
  102. /// POV hat bits.
  103. PODVector<int> hats_;
  104. };
  105. /// %Input subsystem. Converts operating system window messages to input state and events.
  106. class URHO3D_API Input : public Object
  107. {
  108. OBJECT(Input);
  109. public:
  110. /// Construct.
  111. Input(Context* context);
  112. /// Destruct.
  113. virtual ~Input();
  114. /// Poll for window messages. Called by HandleBeginFrame().
  115. void Update();
  116. /// Set whether ALT-ENTER fullscreen toggle is enabled.
  117. void SetToggleFullscreen(bool enable);
  118. /// Set whether the operating system mouse cursor is visible. When not visible (default), is kept centered to prevent leaving the window.
  119. void SetMouseVisible(bool enable);
  120. /// Open a joystick. Return true if successful.
  121. bool OpenJoystick(unsigned index);
  122. /// Close a joystick.
  123. void CloseJoystick(unsigned index);
  124. /// Redetect joysticks. Return true if successful.
  125. bool DetectJoysticks();
  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. /// Return keycode from key name.
  129. int GetKeyFromName(const String& name) const;
  130. /// Return keycode from scancode.
  131. int GetKeyFromScancode(int scancode) const;
  132. /// Return name of key from keycode.
  133. String GetKeyName(int key) const;
  134. /// Return scancode from keycode.
  135. int GetScancodeFromKey(int key) const;
  136. /// Return scancode from key name.
  137. int GetScancodeFromName(const String& name) const;
  138. /// Return name of key from scancode.
  139. String GetScancodeName(int scancode) const;
  140. /// Check if a key is held down.
  141. bool GetKeyDown(int key) const;
  142. /// Check if a key has been pressed on this frame.
  143. bool GetKeyPress(int key) const;
  144. /// Check if a key is held down by scancode.
  145. bool GetScancodeDown(int scancode) const;
  146. /// Check if a key has been pressed on this frame by scancode.
  147. bool GetScancodePress(int scanode) const;
  148. /// Check if a mouse button is held down.
  149. bool GetMouseButtonDown(int button) const;
  150. /// Check if a mouse button has been pressed on this frame.
  151. bool GetMouseButtonPress(int button) const;
  152. /// Check if a qualifier key is held down.
  153. bool GetQualifierDown(int qualifier) const;
  154. /// Check if a qualifier key has been pressed on this frame.
  155. bool GetQualifierPress(int qualifier) const;
  156. /// Return the currently held down qualifiers.
  157. int GetQualifiers() const;
  158. /// Return mouse position within window. Should only be used with a visible mouse cursor.
  159. IntVector2 GetMousePosition() const;
  160. /// Return mouse movement since last frame.
  161. const IntVector2& GetMouseMove() const { return mouseMove_; }
  162. /// Return horizontal mouse movement since last frame.
  163. int GetMouseMoveX() const { return mouseMove_.x_; }
  164. /// Return vertical mouse movement since last frame.
  165. int GetMouseMoveY() const { return mouseMove_.y_; }
  166. /// Return mouse wheel movement since last frame.
  167. int GetMouseMoveWheel() const { return mouseMoveWheel_; }
  168. /// Return number of active finger touches.
  169. unsigned GetNumTouches() const { return touches_.Size(); }
  170. /// Return active finger touch by index.
  171. TouchState* GetTouch(unsigned index) const;
  172. /// Return number of connected joysticks.
  173. unsigned GetNumJoysticks() const { return joysticks_.Size(); }
  174. /// Return joystick name by index.
  175. const String& GetJoystickName(unsigned index) const;
  176. /// Return joystick state by index. Automatically open if not opened yet.
  177. JoystickState* GetJoystick(unsigned index);
  178. /// Return whether fullscreen toggle is enabled.
  179. bool GetToggleFullscreen() const { return toggleFullscreen_; }
  180. /// Return whether on-screen keyboard is supported.
  181. bool GetScreenKeyboardSupport() const;
  182. /// Return whether on-screen keyboard is being shown.
  183. bool IsScreenKeyboardVisible() const;
  184. /// Return whether the operating system mouse cursor is visible.
  185. bool IsMouseVisible() const { return mouseVisible_; }
  186. /// Return whether application window has input focus.
  187. bool HasFocus() { return inputFocus_; }
  188. /// Return whether application window is minimized.
  189. bool IsMinimized() const;
  190. private:
  191. /// Initialize when screen mode initially set.
  192. void Initialize();
  193. /// Setup internal joystick structures.
  194. void ResetJoysticks();
  195. /// Prepare input state for application gaining input focus.
  196. void GainFocus();
  197. /// Prepare input state for application losing input focus.
  198. void LoseFocus();
  199. /// Clear input state.
  200. void ResetState();
  201. /// Send an input focus or window minimization change event.
  202. void SendInputFocusEvent();
  203. /// Handle a mouse button change.
  204. void SetMouseButton(int button, bool newState);
  205. /// Handle a key change.
  206. void SetKey(int key, int scancode, unsigned raw, bool newState);
  207. /// Handle mousewheel change.
  208. void SetMouseWheel(int delta);
  209. /// Internal function to set the mouse cursor position.
  210. void SetMousePosition(const IntVector2& position);
  211. /// Handle screen mode event.
  212. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  213. /// Handle frame start event.
  214. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  215. /// Handle SDL event.
  216. void HandleSDLEvent(void* sdlEvent);
  217. /// Graphics subsystem.
  218. WeakPtr<Graphics> graphics_;
  219. /// Key down state.
  220. HashSet<int> keyDown_;
  221. /// Key pressed state.
  222. HashSet<int> keyPress_;
  223. /// Key down state by scancode.
  224. HashSet<int> scancodeDown_;
  225. /// Key pressed state by scancode.
  226. HashSet<int> scancodePress_;
  227. /// Active finger touches.
  228. HashMap<int, TouchState> touches_;
  229. /// String for text input.
  230. String textInput_;
  231. /// Opened joysticks.
  232. Vector<JoystickState> joysticks_;
  233. /// Mouse buttons' down state.
  234. unsigned mouseButtonDown_;
  235. /// Mouse buttons' pressed state.
  236. unsigned mouseButtonPress_;
  237. /// Last mouse position for calculating movement.
  238. IntVector2 lastMousePosition_;
  239. /// Mouse movement since last frame.
  240. IntVector2 mouseMove_;
  241. /// Mouse wheel movement since last frame.
  242. int mouseMoveWheel_;
  243. /// SDL window ID.
  244. unsigned windowID_;
  245. /// Fullscreen toggle flag.
  246. bool toggleFullscreen_;
  247. /// Operating system mouse cursor visible flag.
  248. bool mouseVisible_;
  249. /// Input focus flag.
  250. bool inputFocus_;
  251. /// Minimized flag.
  252. bool minimized_;
  253. /// Gained focus on this frame flag.
  254. bool focusedThisFrame_;
  255. /// Next mouse move suppress flag.
  256. bool suppressNextMouseMove_;
  257. /// Initialized flag.
  258. bool initialized_;
  259. /// Map SDL joystick ID to internal index.
  260. HashMap<int, unsigned> joystickIDMap_;
  261. };
  262. }