Input.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "HashSet.h"
  25. #include "InputEvents.h"
  26. #include "Object.h"
  27. class Graphics;
  28. /// %Input state for a finger touch.
  29. struct TouchState
  30. {
  31. /// Touch (finger) ID.
  32. int touchID_;
  33. /// Position in screen coordinates.
  34. IntVector2 position_;
  35. /// Last position in screen coordinates.
  36. IntVector2 lastPosition_;
  37. /// Movement since last frame.
  38. IntVector2 delta_;
  39. /// Finger pressure.
  40. float pressure_;
  41. };
  42. /// %Input state for a joystick.
  43. struct JoystickState
  44. {
  45. /// Construct with defaults.
  46. JoystickState() :
  47. joystick_(0)
  48. {
  49. }
  50. /// Return number of buttons.
  51. unsigned GetNumButtons() const { return buttons_.Size(); }
  52. /// Return number of axes.
  53. unsigned GetNumAxes() const { return axes_.Size(); }
  54. /// Return number of hats.
  55. unsigned GetNumHats() const { return hats_.Size(); }
  56. /// Check if a button is held down.
  57. bool GetButtonDown(unsigned index) const
  58. {
  59. if (index < buttons_.Size())
  60. return buttons_[index];
  61. else
  62. return false;
  63. }
  64. /// Check if a button has been pressed on this frame.
  65. bool GetButtonPress(unsigned index) const
  66. {
  67. if (index < buttons_.Size())
  68. return buttonPress_[index];
  69. else
  70. return false;
  71. }
  72. /// Return axis position.
  73. float GetAxisPosition(unsigned index) const
  74. {
  75. if (index < axes_.Size())
  76. return axes_[index];
  77. else
  78. return 0.0f;
  79. }
  80. /// Return hat position.
  81. int GetHatPosition(unsigned index) const
  82. {
  83. if (index < hats_.Size())
  84. return hats_[index];
  85. else
  86. return HAT_CENTER;
  87. }
  88. /// SDL joystick.
  89. SDL_Joystick* joystick_;
  90. /// Joystick name.
  91. String name_;
  92. /// Button up/down state.
  93. PODVector<bool> buttons_;
  94. /// Button pressed on this frame.
  95. PODVector<bool> buttonPress_;
  96. /// Axis position from -1 to 1.
  97. PODVector<float> axes_;
  98. /// POV hat bits.
  99. PODVector<int> hats_;
  100. };
  101. /// %Input subsystem. Converts operating system window messages to input state and events.
  102. class Input : public Object
  103. {
  104. OBJECT(Input);
  105. public:
  106. /// Construct.
  107. Input(Context* context);
  108. /// Destruct.
  109. virtual ~Input();
  110. /// Poll for window messages. Called by HandleBeginFrame().
  111. void Update();
  112. /// Set whether ALT-ENTER fullscreen toggle is enabled.
  113. void SetToggleFullscreen(bool enable);
  114. /// Set whether the operating system mouse cursor is visible. When not visible (default), is kept centered to prevent leaving the window.
  115. void SetMouseVisible(bool enable);
  116. /// Open a joystick. Return true if successful.
  117. bool OpenJoystick(unsigned index);
  118. /// Close a joystick.
  119. void CloseJoystick(unsigned index);
  120. /// Redetect joysticks. Return true if successful.
  121. bool DetectJoysticks();
  122. /// Check if a key is held down.
  123. bool GetKeyDown(int key) const;
  124. /// Check if a key has been pressed on this frame.
  125. bool GetKeyPress(int key) const;
  126. /// Check if a mouse button is held down.
  127. bool GetMouseButtonDown(int button) const;
  128. /// Check if a mouse button has been pressed on this frame.
  129. bool GetMouseButtonPress(int button) const;
  130. /// Check if a qualifier key is held down.
  131. bool GetQualifierDown(int qualifier) const;
  132. /// Check if a qualifier key has been pressed on this frame.
  133. bool GetQualifierPress(int qualifier) const;
  134. /// Return the currently held down qualifiers.
  135. int GetQualifiers() const;
  136. /// Return mouse position within window. Should only be used with a visible mouse cursor.
  137. IntVector2 GetMousePosition() const;
  138. /// Return mouse movement since last frame.
  139. const IntVector2& GetMouseMove() const { return mouseMove_; }
  140. /// Return horizontal mouse movement since last frame.
  141. int GetMouseMoveX() const { return mouseMove_.x_; }
  142. /// Return vertical mouse movement since last frame.
  143. int GetMouseMoveY() const { return mouseMove_.y_; }
  144. /// Return mouse wheel movement since last frame.
  145. int GetMouseMoveWheel() const { return mouseMoveWheel_; }
  146. /// Return number of active finger touches.
  147. unsigned GetNumTouches() const { return touches_.Size(); }
  148. /// Return active finger touch by index.
  149. TouchState* GetTouch(unsigned index) const;
  150. /// Return number of connected joysticks.
  151. unsigned GetNumJoysticks() const { return joysticks_.Size(); }
  152. /// Return joystick name by index.
  153. const String& GetJoystickName(unsigned index) const;
  154. /// Return joystick state by index. Automatically open if not opened yet.
  155. JoystickState* GetJoystick(unsigned index);
  156. /// Return whether fullscreen toggle is enabled.
  157. bool GetToggleFullscreen() const { return toggleFullscreen_; }
  158. /// Return whether the operating system mouse cursor is visible.
  159. bool IsMouseVisible() const { return mouseVisible_; }
  160. /// Return whether application window is active.
  161. bool IsActive() { return active_; }
  162. /// Return whether application window is minimized.
  163. bool IsMinimized() const;
  164. private:
  165. /// Initialize when screen mode initially set.
  166. void Initialize();
  167. /// Setup internal joystick structures.
  168. void ResetJoysticks();
  169. /// Activate the application.
  170. void MakeActive();
  171. /// Deactivate the application.
  172. void MakeInactive();
  173. /// Clear input state.
  174. void ResetState();
  175. /// Send an activation event. Called when minimized or active status changes.
  176. void SendActivationEvent();
  177. /// Handle a mouse button change.
  178. void SetMouseButton(int button, bool newState);
  179. /// Handle a key change.
  180. void SetKey(int key, bool newState);
  181. /// Handle mousewheel change.
  182. void SetMouseWheel(int delta);
  183. /// Internal function to set the mouse cursor position.
  184. void SetMousePosition(const IntVector2& position);
  185. /// Handle screen mode event.
  186. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  187. /// Handle frame start event.
  188. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  189. /// Handle SDL event.
  190. static void HandleSDLEvent(void* sdlEvent);
  191. /// Graphics subsystem.
  192. WeakPtr<Graphics> graphics_;
  193. /// Key down state.
  194. HashSet<int> keyDown_;
  195. /// Key pressed state.
  196. HashSet<int> keyPress_;
  197. /// Active finger touches.
  198. HashMap<int, TouchState> touches_;
  199. /// Opened joysticks.
  200. Vector<JoystickState> joysticks_;
  201. /// Mouse buttons' down state.
  202. unsigned mouseButtonDown_;
  203. /// Mouse buttons' pressed state.
  204. unsigned mouseButtonPress_;
  205. /// Last mouse position for calculating movement.
  206. IntVector2 lastMousePosition_;
  207. /// Mouse movement since last frame.
  208. IntVector2 mouseMove_;
  209. /// Mouse wheel movement since last frame.
  210. int mouseMoveWheel_;
  211. /// SDL window ID.
  212. unsigned windowID_;
  213. /// Fullscreen toggle flag.
  214. bool toggleFullscreen_;
  215. /// Operating system mouse cursor visible flag.
  216. bool mouseVisible_;
  217. /// Active flag.
  218. bool active_;
  219. /// Minimized flag.
  220. bool minimized_;
  221. /// Activated on this frame flag.
  222. bool activated_;
  223. /// Next mouse move suppress flag.
  224. bool suppressNextMouseMove_;
  225. /// Initialized flag.
  226. bool initialized_;
  227. };