Input.h 8.6 KB

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