BsInput.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsModule.h"
  4. #include "BsRect2I.h"
  5. #include "BsOSInputHandler.h"
  6. #include "BsRawInputHandler.h"
  7. #include "BsInputFwd.h"
  8. namespace BansheeEngine
  9. {
  10. /**
  11. * @brief Primary module used for dealing with input. Allows you to receieve
  12. * and query raw or OS input for mouse/keyboard/gamepad.
  13. *
  14. * All inputs are received through an input handler, which can be overriden to
  15. * provide custom input functionality.
  16. */
  17. class BS_CORE_EXPORT Input : public Module<Input>
  18. {
  19. /**
  20. * @brief Possible button states
  21. */
  22. enum class ButtonState
  23. {
  24. Off, /**< Button is not being pressed. */
  25. On, /**< Button is being pressed. */
  26. ToggledOn, /**< Button has been pressed this frame. */
  27. ToggledOff, /**< Button has been released this frame. */
  28. ToggledOnOff, /**< Button has been pressed and released this frame. */
  29. };
  30. /**
  31. * @brief Contains axis and device data per device
  32. */
  33. struct DeviceData
  34. {
  35. DeviceData();
  36. Vector<RawAxisState> axes;
  37. ButtonState keyStates[BC_Count];
  38. };
  39. /**
  40. * @brief Different types of possible input event callbacks.
  41. */
  42. enum class EventType
  43. {
  44. ButtonUp, ButtonDown, PointerMoved, PointerUp, PointerDown, PointerDoubleClick, TextInput, Command
  45. };
  46. /**
  47. * @brief Stores information about a queued input event that is to be triggered later.
  48. */
  49. struct QueuedEvent
  50. {
  51. QueuedEvent(EventType type, UINT32 idx)
  52. :type(type), idx(idx)
  53. { }
  54. EventType type;
  55. UINT32 idx;
  56. };
  57. public:
  58. Input();
  59. ~Input();
  60. /**
  61. * @brief Triggered whenever a button is first pressed.
  62. */
  63. Event<void(const ButtonEvent&)> onButtonDown;
  64. /**
  65. * @brief Triggered whenever a button is first released.
  66. */
  67. Event<void(const ButtonEvent&)> onButtonUp;
  68. /**
  69. * @brief Triggered whenever user inputs a text character.
  70. */
  71. Event<void(const TextInputEvent&)> onCharInput;
  72. /**
  73. * @brief Triggers when some pointing device (mouse cursor, touch) moves.
  74. */
  75. Event<void(const PointerEvent&)> onPointerMoved;
  76. /**
  77. * @brief Triggers when some pointing device (mouse cursor, touch) button is pressed.
  78. */
  79. Event<void(const PointerEvent&)> onPointerPressed;
  80. /**
  81. * @brief Triggers when some pointing device (mouse cursor, touch) button is released.
  82. */
  83. Event<void(const PointerEvent&)> onPointerReleased;
  84. /**
  85. * @brief Triggers when some pointing device (mouse cursor, touch) button is double clicked.
  86. */
  87. Event<void(const PointerEvent&)> onPointerDoubleClick;
  88. // TODO Low priority: Remove this, I can emulate it using virtual input
  89. /**
  90. * @brief Triggers on special input commands.
  91. */
  92. Event<void(InputCommandType)> onInputCommand;
  93. /**
  94. * @brief Registers a new input handler. Replaces any previous input handler.
  95. *
  96. * @note Internal method.
  97. */
  98. void _registerRawInputHandler(std::shared_ptr<RawInputHandler> inputHandler);
  99. /**
  100. * @brief Called every frame. Detects button state changes and prepares callback events to trigger
  101. * via a call to "_triggerCallbacks".
  102. *
  103. * @note Internal method.
  104. */
  105. void _update();
  106. /**
  107. * @brief Triggers any queued input event callbacks.
  108. */
  109. void _triggerCallbacks();
  110. /**
  111. * @brief Returns value of the specified input axis. Normally in range [-1.0, 1.0] but can be outside
  112. * the range for devices with unbound axes (e.g. mouse).
  113. *
  114. * @param type Type of axis to query. Usually a type from InputAxis but can be a custom value.
  115. * @param deviceIdx Index of the device in case more than one is hooked up (0 - primary).
  116. */
  117. float getAxisValue(UINT32 type, UINT32 deviceIdx = 0) const;
  118. /**
  119. * @brief Query if the provided button is currently being held (this frame or previous frames).
  120. *
  121. * @param keyCode Code of the button to query.
  122. * @param deviceIdx Device to query the button on (0 - primary).
  123. */
  124. bool isButtonHeld(ButtonCode keyCode, UINT32 deviceIdx = 0) const;
  125. /**
  126. * @brief Query if the provided button is currently being released (one true for one frame).
  127. *
  128. * @param keyCode Code of the button to query.
  129. * @param deviceIdx Device to query the button on (0 - primary).
  130. */
  131. bool isButtonUp(ButtonCode keyCode, UINT32 deviceIdx = 0) const;
  132. /**
  133. * @brief Query if the provided button is currently being pressed (one true for one frame).
  134. *
  135. * @param keyCode Code of the button to query.
  136. * @param deviceIdx Device to query the button on (0 - primary).
  137. */
  138. bool isButtonDown(ButtonCode keyCode, UINT32 deviceIdx = 0) const;
  139. /**
  140. * @brief Returns positions of the pointer (e.g. mouse cursor) relative to the screen.
  141. */
  142. Vector2I getPointerPosition() const;
  143. /**
  144. * @brief Returns difference between last and current pointer position.
  145. */
  146. Vector2I getPointerDelta() const { return mPointerDelta; }
  147. /**
  148. * @brief Query if the provided pointer button is currently
  149. * being held (this frame or previous frames).
  150. *
  151. * @param pointerButton Code of the button to query.
  152. */
  153. bool isPointerButtonHeld(PointerEventButton pointerButton) const;
  154. /**
  155. * @brief Query if the provided pointer button is currently
  156. * being released (one true for one frame).
  157. *
  158. * @param pointerButton Code of the button to query.
  159. */
  160. bool isPointerButtonUp(PointerEventButton pointerButton) const;
  161. /**
  162. * @brief Query if the provided pointer button is currently
  163. * being pressed (one true for one frame).
  164. *
  165. * @param pointerButton Code of the button to query.
  166. */
  167. bool isPointerButtonDown(PointerEventButton pointerButton) const;
  168. /**
  169. * @brief Query if the provided the left pointer button has been
  170. * double-clicked this frame.
  171. */
  172. bool isPointerDoubleClicked() const;
  173. /**
  174. * @brief Enables or disables mouse smoothing. Smoothing makes the changes to
  175. * mouse axes more gradual.
  176. */
  177. void setMouseSmoothing(bool enabled);
  178. private:
  179. /**
  180. * @brief Triggered by input handler when a button is pressed.
  181. */
  182. void buttonDown(UINT32 deviceIdx, ButtonCode code, UINT64 timestamp);
  183. /**
  184. * @brief Triggered by input handler when a button is released.
  185. */
  186. void buttonUp(UINT32 deviceIdx, ButtonCode code, UINT64 timestamp);
  187. /**
  188. * @brief Triggered by input handler when a single character is input.
  189. */
  190. void charInput(UINT32 chr);
  191. /**
  192. * @brief Triggered by input handler when a mouse/joystick axis is moved.
  193. */
  194. void axisMoved(UINT32 deviceIdx, const RawAxisState& state, UINT32 axis);
  195. /**
  196. * @brief Cursor movement as OS reports it. Used for screen cursor position.
  197. */
  198. void cursorMoved(const PointerEvent& event);
  199. /**
  200. * @brief Cursor button presses as OS reports it.
  201. */
  202. void cursorPressed(const PointerEvent& event);
  203. /**
  204. * @brief Cursor button releases as OS reports it.
  205. */
  206. void cursorReleased(const PointerEvent& event);
  207. /**
  208. * @brief Cursor button releases as OS reports it.
  209. */
  210. void cursorDoubleClick(const PointerEvent& event);
  211. /**
  212. * @brief Input commands as OS reports them.
  213. */
  214. void inputCommandEntered(InputCommandType commandType);
  215. /**
  216. * @brief Called when window in focus changes, as reported by the OS.
  217. */
  218. void inputWindowChanged(RenderWindow& win);
  219. private:
  220. std::shared_ptr<RawInputHandler> mRawInputHandler;
  221. std::shared_ptr<OSInputHandler> mOSInputHandler;
  222. Vector<DeviceData> mDevices;
  223. Vector2I mPointerPosition;
  224. Vector2I mPointerDelta;
  225. ButtonState mPointerButtonStates[3];
  226. bool mPointerDoubleClicked;
  227. bool mLastPositionSet;
  228. Vector<QueuedEvent> mQueuedEvents;
  229. Vector<TextInputEvent> mTextInputEvents;
  230. Vector<InputCommandType> mCommandEvents;
  231. Vector<PointerEvent> mPointerDoubleClickEvents;
  232. Vector<PointerEvent> mPointerReleasedEvents;
  233. Vector<PointerEvent> mPointerPressedEvents;
  234. Vector<PointerEvent> mPointerMovedEvents;
  235. Vector<ButtonEvent> mButtonDownEvents;
  236. Vector<ButtonEvent> mButtonUpEvents;
  237. /************************************************************************/
  238. /* STATICS */
  239. /************************************************************************/
  240. static const int HISTORY_BUFFER_SIZE; // Size of buffer used for input smoothing
  241. static const float WEIGHT_MODIFIER;
  242. };
  243. /**
  244. * @copydoc Input
  245. */
  246. BS_CORE_EXPORT Input& gInput();
  247. }