BsInput.h 8.0 KB

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