BsInput.h 8.3 KB

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