Input.h 8.0 KB

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