BsInput.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. public:
  40. Input();
  41. ~Input();
  42. /**
  43. * @brief Triggered whenever a button is first pressed.
  44. */
  45. Event<void(const ButtonEvent&)> onButtonDown;
  46. /**
  47. * @brief Triggered whenever a button is first released.
  48. */
  49. Event<void(const ButtonEvent&)> onButtonUp;
  50. /**
  51. * @brief Triggered whenever user inputs a text character.
  52. */
  53. Event<void(const TextInputEvent&)> onCharInput;
  54. /**
  55. * @brief Triggers when some pointing device (mouse cursor, touch) moves.
  56. */
  57. Event<void(const PointerEvent&)> onPointerMoved;
  58. /**
  59. * @brief Triggers when some pointing device (mouse cursor, touch) button is pressed.
  60. */
  61. Event<void(const PointerEvent&)> onPointerPressed;
  62. /**
  63. * @brief Triggers when some pointing device (mouse cursor, touch) button is released.
  64. */
  65. Event<void(const PointerEvent&)> onPointerReleased;
  66. /**
  67. * @brief Triggers when some pointing device (mouse cursor, touch) button is double clicked.
  68. */
  69. Event<void(const PointerEvent&)> onPointerDoubleClick;
  70. // TODO Low priority: Remove this, I can emulate it using virtual input
  71. /**
  72. * @brief Triggers on special input commands.
  73. */
  74. Event<void(InputCommandType)> onInputCommand;
  75. /**
  76. * @brief Registers a new input handler. Replaces any previous input handler.
  77. *
  78. * @note Internal method.
  79. */
  80. void _registerRawInputHandler(std::shared_ptr<RawInputHandler> inputHandler);
  81. /**
  82. * @brief Called every frame. Dispatches any callbacks resulting from input by the user.
  83. *
  84. * @note Internal method.
  85. */
  86. void _update();
  87. /**
  88. * @brief Returns value of the specified input axis. Normally in range [-1.0, 1.0] but can be outside
  89. * the range for devices with unbound axes (e.g. mouse).
  90. *
  91. * @param type Type of axis to query. Usually a type from InputAxis but can be a custom value.
  92. * @param deviceIdx Index of the device in case more than one is hooked up (0 - primary).
  93. */
  94. float getAxisValue(UINT32 type, UINT32 deviceIdx = 0) const;
  95. /**
  96. * @brief Query if the provided button is currently being held (this frame or previous frames).
  97. *
  98. * @param keyCode Code of the button to query.
  99. * @param deviceIdx Device to query the button on (0 - primary).
  100. */
  101. bool isButtonHeld(ButtonCode keyCode, UINT32 deviceIdx = 0) const;
  102. /**
  103. * @brief Query if the provided button is currently being released (one true for one frame).
  104. *
  105. * @param keyCode Code of the button to query.
  106. * @param deviceIdx Device to query the button on (0 - primary).
  107. */
  108. bool isButtonUp(ButtonCode keyCode, UINT32 deviceIdx = 0) const;
  109. /**
  110. * @brief Query if the provided button is currently being pressed (one true for one frame).
  111. *
  112. * @param keyCode Code of the button to query.
  113. * @param deviceIdx Device to query the button on (0 - primary).
  114. */
  115. bool isButtonDown(ButtonCode keyCode, UINT32 deviceIdx = 0) const;
  116. /**
  117. * @brief Returns positions of the pointer (e.g. mouse cursor) relative to the screen.
  118. */
  119. Vector2I getPointerPosition() const;
  120. /**
  121. * @brief Returns difference between last and current pointer position.
  122. */
  123. Vector2I getPointerDelta() const { return mPointerDelta; }
  124. /**
  125. * @brief Query if the provided pointer button is currently
  126. * being held (this frame or previous frames).
  127. *
  128. * @param pointerButton Code of the button to query.
  129. */
  130. bool isPointerButtonHeld(PointerEventButton pointerButton) const;
  131. /**
  132. * @brief Query if the provided pointer button is currently
  133. * being released (one true for one frame).
  134. *
  135. * @param pointerButton Code of the button to query.
  136. */
  137. bool isPointerButtonUp(PointerEventButton pointerButton) const;
  138. /**
  139. * @brief Query if the provided pointer button is currently
  140. * being pressed (one true for one frame).
  141. *
  142. * @param pointerButton Code of the button to query.
  143. */
  144. bool isPointerButtonDown(PointerEventButton pointerButton) const;
  145. /**
  146. * @brief Query if the provided the left pointer button has been
  147. * double-clicked this frame.
  148. */
  149. bool isPointerDoubleClicked() const;
  150. /**
  151. * @brief Enables or disables mouse smoothing. Smoothing makes the changes to
  152. * mouse axes more gradual.
  153. */
  154. void setMouseSmoothing(bool enabled);
  155. private:
  156. /**
  157. * @brief Triggered by input handler when a button is pressed.
  158. */
  159. void buttonDown(UINT32 deviceIdx, ButtonCode code, UINT64 timestamp);
  160. /**
  161. * @brief Triggered by input handler when a button is released.
  162. */
  163. void buttonUp(UINT32 deviceIdx, ButtonCode code, UINT64 timestamp);
  164. /**
  165. * @brief Triggered by input handler when a single character is input.
  166. */
  167. void charInput(UINT32 chr);
  168. /**
  169. * @brief Triggered by input handler when a mouse/joystick axis is moved.
  170. */
  171. void axisMoved(UINT32 deviceIdx, const RawAxisState& state, UINT32 axis);
  172. /**
  173. * @brief Cursor movement as OS reports it. Used for screen cursor position.
  174. */
  175. void cursorMoved(const PointerEvent& event);
  176. /**
  177. * @brief Cursor button presses as OS reports it.
  178. */
  179. void cursorPressed(const PointerEvent& event);
  180. /**
  181. * @brief Cursor button releases as OS reports it.
  182. */
  183. void cursorReleased(const PointerEvent& event);
  184. /**
  185. * @brief Cursor button releases as OS reports it.
  186. */
  187. void cursorDoubleClick(const PointerEvent& event);
  188. /**
  189. * @brief Input commands as OS reports them.
  190. */
  191. void inputCommandEntered(InputCommandType commandType);
  192. /**
  193. * @brief Called when window in focus changes, as reported by the OS.
  194. */
  195. void inputWindowChanged(RenderWindow& win);
  196. private:
  197. std::shared_ptr<RawInputHandler> mRawInputHandler;
  198. std::shared_ptr<OSInputHandler> mOSInputHandler;
  199. Vector<DeviceData> mDevices;
  200. Vector2I mPointerPosition;
  201. Vector2I mPointerDelta;
  202. ButtonState mPointerButtonStates[3];
  203. bool mPointerDoubleClicked;
  204. bool mLastPositionSet;
  205. /************************************************************************/
  206. /* STATICS */
  207. /************************************************************************/
  208. static const int HISTORY_BUFFER_SIZE; // Size of buffer used for input smoothing
  209. static const float WEIGHT_MODIFIER;
  210. };
  211. /**
  212. * @copydoc Input
  213. */
  214. BS_CORE_EXPORT Input& gInput();
  215. }