Input.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "Map.h"
  25. #include "HashSet.h"
  26. #include "InputEvents.h"
  27. #include "Object.h"
  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. /// Open a joystick. Return true if successful.
  116. bool OpenJoystick(unsigned index);
  117. /// Close a joystick.
  118. void CloseJoystick(unsigned index);
  119. /// Redetect joysticks. Return true if successful.
  120. bool DetectJoysticks();
  121. /// Check if a key is held down.
  122. bool GetKeyDown(int key) const;
  123. /// Check if a key has been pressed on this frame.
  124. bool GetKeyPress(int key) const;
  125. /// Check if a mouse button is held down.
  126. bool GetMouseButtonDown(int button) const;
  127. /// Check if a mouse button has been pressed on this frame.
  128. bool GetMouseButtonPress(int button) const;
  129. /// Check if a qualifier key is held down.
  130. bool GetQualifierDown(int qualifier) const;
  131. /// Check if a qualifier key has been pressed on this frame.
  132. bool GetQualifierPress(int qualifier) const;
  133. /// Return the currently held down qualifiers.
  134. int GetQualifiers() const;
  135. /// Return mouse movement since last frame.
  136. const IntVector2& GetMouseMove() const { return mouseMove_; }
  137. /// Return horizontal mouse movement since last frame.
  138. int GetMouseMoveX() const { return mouseMove_.x_; }
  139. /// Return vertical mouse movement since last frame.
  140. int GetMouseMoveY() const { return mouseMove_.y_; }
  141. /// Return mouse wheel movement since last frame.
  142. int GetMouseMoveWheel() const { return mouseMoveWheel_; }
  143. /// Return number of active finger touches.
  144. unsigned GetNumTouches() const { return touches_.Size(); }
  145. /// Return active finger touch by index.
  146. TouchState* GetTouch(unsigned index) const;
  147. /// Return number of connected joysticks.
  148. unsigned GetNumJoysticks() const { return joysticks_.Size(); }
  149. /// Return joystick name by index.
  150. const String& GetJoystickName(unsigned index) const;
  151. /// Return joystick state by index. Automatically open if not opened yet.
  152. JoystickState* GetJoystick(unsigned index);
  153. /// Return whether fullscreen toggle is enabled.
  154. bool GetToggleFullscreen() const { return toggleFullscreen_; }
  155. /// Return whether application window is active.
  156. bool IsActive() { return active_; }
  157. /// Return whether application window is minimized.
  158. bool IsMinimized() const;
  159. private:
  160. /// Initialize when screen mode initially set.
  161. void Initialize();
  162. /// Setup internal joystick structures.
  163. void ResetJoysticks();
  164. /// Activate the application.
  165. void MakeActive();
  166. /// Deactivate the application.
  167. void MakeInactive();
  168. /// Clear input state.
  169. void ResetState();
  170. /// Send an activation event. Called when minimized or active status changes.
  171. void SendActivationEvent();
  172. /// Handle a mouse button change.
  173. void SetMouseButton(int button, bool newState);
  174. /// Handle a key change.
  175. void SetKey(int key, bool newState);
  176. /// Handle mousewheel change.
  177. void SetMouseWheel(int delta);
  178. /// Internal function to set the mouse cursor position.
  179. void SetCursorPosition(const IntVector2& position);
  180. /// Internal function to get the mouse cursor position.
  181. IntVector2 GetCursorPosition() const;
  182. /// Handle screen mode event.
  183. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  184. /// Handle frame start event.
  185. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  186. /// Handle SDL event.
  187. static void HandleSDLEvent(void* sdlEvent);
  188. /// Graphics subsystem.
  189. WeakPtr<Graphics> graphics_;
  190. /// Key down state.
  191. HashSet<int> keyDown_;
  192. /// Key pressed state.
  193. HashSet<int> keyPress_;
  194. /// Active finger touches.
  195. Map<int, TouchState> touches_;
  196. /// Opened joysticks.
  197. Vector<JoystickState> joysticks_;
  198. /// Mouse buttons' down state.
  199. unsigned mouseButtonDown_;
  200. /// Mouse buttons' pressed state.
  201. unsigned mouseButtonPress_;
  202. /// Last mouse position for calculating movement.
  203. IntVector2 lastCursorPosition_;
  204. /// Mouse movement since last frame.
  205. IntVector2 mouseMove_;
  206. /// Mouse wheel movement since last frame.
  207. int mouseMoveWheel_;
  208. /// SDL window ID.
  209. unsigned windowID_;
  210. /// Fullscreen toggle flag.
  211. bool toggleFullscreen_;
  212. /// Active flag.
  213. bool active_;
  214. /// Minimized flag.
  215. bool minimized_;
  216. /// Activated on this frame flag.
  217. bool activated_;
  218. /// Next mouse move suppress flag.
  219. bool suppressNextMouseMove_;
  220. /// Initialized flag.
  221. bool initialized_;
  222. };