Input.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // Copyright (c) 2008-2013 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)
  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. /// Joystick name.
  93. String name_;
  94. /// Button up/down state.
  95. PODVector<bool> buttons_;
  96. /// Button pressed on this frame.
  97. PODVector<bool> buttonPress_;
  98. /// Axis position from -1 to 1.
  99. PODVector<float> axes_;
  100. /// POV hat bits.
  101. PODVector<int> hats_;
  102. };
  103. /// %Input subsystem. Converts operating system window messages to input state and events.
  104. class URHO3D_API Input : public Object
  105. {
  106. OBJECT(Input);
  107. public:
  108. /// Construct.
  109. Input(Context* context);
  110. /// Destruct.
  111. virtual ~Input();
  112. /// Poll for window messages. Called by HandleBeginFrame().
  113. void Update();
  114. /// Set whether ALT-ENTER fullscreen toggle is enabled.
  115. void SetToggleFullscreen(bool enable);
  116. /// Set whether the operating system mouse cursor is visible. When not visible (default), is kept centered to prevent leaving the window.
  117. void SetMouseVisible(bool enable);
  118. /// Open a joystick. Return true if successful.
  119. bool OpenJoystick(unsigned index);
  120. /// Close a joystick.
  121. void CloseJoystick(unsigned index);
  122. /// Redetect joysticks. Return true if successful.
  123. bool DetectJoysticks();
  124. /// Check if a key is held down.
  125. bool GetKeyDown(int key) const;
  126. /// Check if a key has been pressed on this frame.
  127. bool GetKeyPress(int key) const;
  128. /// Check if a mouse button is held down.
  129. bool GetMouseButtonDown(int button) const;
  130. /// Check if a mouse button has been pressed on this frame.
  131. bool GetMouseButtonPress(int button) const;
  132. /// Check if a qualifier key is held down.
  133. bool GetQualifierDown(int qualifier) const;
  134. /// Check if a qualifier key has been pressed on this frame.
  135. bool GetQualifierPress(int qualifier) const;
  136. /// Return the currently held down qualifiers.
  137. int GetQualifiers() const;
  138. /// Return mouse position within window. Should only be used with a visible mouse cursor.
  139. IntVector2 GetMousePosition() const;
  140. /// Return mouse movement since last frame.
  141. const IntVector2& GetMouseMove() const { return mouseMove_; }
  142. /// Return horizontal mouse movement since last frame.
  143. int GetMouseMoveX() const { return mouseMove_.x_; }
  144. /// Return vertical mouse movement since last frame.
  145. int GetMouseMoveY() const { return mouseMove_.y_; }
  146. /// Return mouse wheel movement since last frame.
  147. int GetMouseMoveWheel() const { return mouseMoveWheel_; }
  148. /// Return number of active finger touches.
  149. unsigned GetNumTouches() const { return touches_.Size(); }
  150. /// Return active finger touch by index.
  151. TouchState* GetTouch(unsigned index) const;
  152. /// Return number of connected joysticks.
  153. unsigned GetNumJoysticks() const { return joysticks_.Size(); }
  154. /// Return joystick name by index.
  155. const String& GetJoystickName(unsigned index) const;
  156. /// Return joystick state by index. Automatically open if not opened yet.
  157. JoystickState* GetJoystick(unsigned index);
  158. /// Return whether fullscreen toggle is enabled.
  159. bool GetToggleFullscreen() const { return toggleFullscreen_; }
  160. /// Return whether the operating system mouse cursor is visible.
  161. bool IsMouseVisible() const { return mouseVisible_; }
  162. /// Return whether application window has input focus.
  163. bool HasFocus() { return inputFocus_; }
  164. /// Return whether application window is minimized.
  165. bool IsMinimized() const;
  166. private:
  167. /// Initialize when screen mode initially set.
  168. void Initialize();
  169. /// Setup internal joystick structures.
  170. void ResetJoysticks();
  171. /// Prepare input state for application gaining input focus.
  172. void GainFocus();
  173. /// Prepare input state for application losing input focus.
  174. void LoseFocus();
  175. /// Clear input state.
  176. void ResetState();
  177. /// Send an input focus or window minimization change event.
  178. void SendInputFocusEvent();
  179. /// Handle a mouse button change.
  180. void SetMouseButton(int button, bool newState);
  181. /// Handle a key change.
  182. void SetKey(int key, bool newState);
  183. /// Handle mousewheel change.
  184. void SetMouseWheel(int delta);
  185. /// Internal function to set the mouse cursor position.
  186. void SetMousePosition(const IntVector2& position);
  187. /// Handle screen mode event.
  188. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  189. /// Handle frame start event.
  190. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  191. /// Handle SDL event.
  192. void HandleSDLEvent(void* sdlEvent);
  193. /// Graphics subsystem.
  194. WeakPtr<Graphics> graphics_;
  195. /// Key down state.
  196. HashSet<int> keyDown_;
  197. /// Key pressed state.
  198. HashSet<int> keyPress_;
  199. /// Active finger touches.
  200. HashMap<int, TouchState> touches_;
  201. /// Opened joysticks.
  202. Vector<JoystickState> joysticks_;
  203. /// Mouse buttons' down state.
  204. unsigned mouseButtonDown_;
  205. /// Mouse buttons' pressed state.
  206. unsigned mouseButtonPress_;
  207. /// Last mouse position for calculating movement.
  208. IntVector2 lastMousePosition_;
  209. /// Mouse movement since last frame.
  210. IntVector2 mouseMove_;
  211. /// Mouse wheel movement since last frame.
  212. int mouseMoveWheel_;
  213. /// SDL window ID.
  214. unsigned windowID_;
  215. /// Fullscreen toggle flag.
  216. bool toggleFullscreen_;
  217. /// Operating system mouse cursor visible flag.
  218. bool mouseVisible_;
  219. /// Input focus flag.
  220. bool inputFocus_;
  221. /// Minimized flag.
  222. bool minimized_;
  223. /// Gained focus on this frame flag.
  224. bool focusedThisFrame_;
  225. /// Next mouse move suppress flag.
  226. bool suppressNextMouseMove_;
  227. /// Initialized flag.
  228. bool initialized_;
  229. /// Map SDL joystick ID to internal index.
  230. HashMap<int, unsigned> joystickIDMap_;
  231. };
  232. }