BsInput.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsModule.h"
  4. #include "BsRectI.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. };
  29. /**
  30. * @brief Contains axis and device data per device
  31. */
  32. struct DeviceData
  33. {
  34. DeviceData();
  35. Vector<RawAxisState> axes;
  36. ButtonState keyStates[BC_Count];
  37. };
  38. public:
  39. Input();
  40. ~Input();
  41. /**
  42. * @brief Triggered whenever a button is first pressed.
  43. */
  44. Event<void(const ButtonEvent&)> onButtonDown;
  45. /**
  46. * @brief Triggered whenever a button is first released.
  47. */
  48. Event<void(const ButtonEvent&)> onButtonUp;
  49. /**
  50. * @brief Triggered whenever user inputs a text character.
  51. */
  52. Event<void(const TextInputEvent&)> onCharInput;
  53. /**
  54. * @brief Triggers when some pointing device (mouse cursor, touch) moves.
  55. */
  56. Event<void(const PointerEvent&)> onPointerMoved;
  57. /**
  58. * @brief Triggers when some pointing device (mouse cursor, touch) button is pressed.
  59. */
  60. Event<void(const PointerEvent&)> onPointerPressed;
  61. /**
  62. * @brief Triggers when some pointing device (mouse cursor, touch) button is released.
  63. */
  64. Event<void(const PointerEvent&)> onPointerReleased;
  65. /**
  66. * @brief Triggers when some pointing device (mouse cursor, touch) button is double clicked.
  67. */
  68. Event<void(const PointerEvent&)> onPointerDoubleClick;
  69. // TODO Low priority: Remove this, I can emulate it using virtual input
  70. /**
  71. * @brief Triggers on special input commands.
  72. */
  73. Event<void(InputCommandType)> onInputCommand;
  74. /**
  75. * @brief Registers a new input handler. Replaces any previous input handler.
  76. *
  77. * @note Internal method.
  78. */
  79. void _registerRawInputHandler(std::shared_ptr<RawInputHandler> inputHandler);
  80. /**
  81. * @brief Called every frame. Dispatches any callbacks resulting from input by the user.
  82. *
  83. * @note Internal method.
  84. */
  85. void _update();
  86. /**
  87. * @brief Returns value of the specified input axis in range [-1.0, 1.0].
  88. *
  89. * @param type Type of axis to query. Usually a type from InputAxis but can be a custom value.
  90. * @param deviceIdx Index of the device in case more than one is hooked up (0 - primary).
  91. */
  92. float getAxisValue(UINT32 type, UINT32 deviceIdx = 0) const;
  93. /**
  94. * @brief Query if the provided button is currently being held (this frame or previous frames).
  95. *
  96. * @param keyCode Code of the button to query.
  97. * @param deviceIdx Device to query the button on (0 - primary).
  98. */
  99. bool isButtonHeld(ButtonCode keyCode, UINT32 deviceIdx = 0) const;
  100. /**
  101. * @brief Query if the provided button is currently being released (one true for one frame).
  102. *
  103. * @param keyCode Code of the button to query.
  104. * @param deviceIdx Device to query the button on (0 - primary).
  105. */
  106. bool isButtonUp(ButtonCode keyCode, UINT32 deviceIdx = 0) const;
  107. /**
  108. * @brief Query if the provided button is currently being pressed (one true for one frame).
  109. *
  110. * @param keyCode Code of the button to query.
  111. * @param deviceIdx Device to query the button on (0 - primary).
  112. */
  113. bool isButtonDown(ButtonCode keyCode, UINT32 deviceIdx = 0) const;
  114. /**
  115. * @brief Enables or disables mouse smoothing. Smoothing makes the changes to
  116. * mouse axes more gradual.
  117. */
  118. void setMouseSmoothing(bool enabled);
  119. private:
  120. /**
  121. * @brief Triggered by input handler when a button is pressed.
  122. */
  123. void buttonDown(UINT32 deviceIdx, ButtonCode code, UINT64 timestamp);
  124. /**
  125. * @brief Triggered by input handler when a button is released.
  126. */
  127. void buttonUp(UINT32 deviceIdx, ButtonCode code, UINT64 timestamp);
  128. /**
  129. * @brief Triggered by input handler when a single character is input.
  130. */
  131. void charInput(UINT32 chr);
  132. /**
  133. * @brief Triggered by input handler when a mouse/joystick axis is moved.
  134. */
  135. void axisMoved(UINT32 deviceIdx, const RawAxisState& state, UINT32 axis);
  136. /**
  137. * @brief Cursor movement as OS reports it. Used for screen cursor position.
  138. */
  139. void cursorMoved(const PointerEvent& event);
  140. /**
  141. * @brief Cursor button presses as OS reports it.
  142. */
  143. void cursorPressed(const PointerEvent& event);
  144. /**
  145. * @brief Cursor button releases as OS reports it.
  146. */
  147. void cursorReleased(const PointerEvent& event);
  148. /**
  149. * @brief Cursor button releases as OS reports it.
  150. */
  151. void cursorDoubleClick(const PointerEvent& event);
  152. /**
  153. * @brief Input commands as OS reports them.
  154. */
  155. void inputCommandEntered(InputCommandType commandType);
  156. /**
  157. * @brief Called when window in focus changes, as reported by the OS.
  158. */
  159. void inputWindowChanged(RenderWindow& win);
  160. private:
  161. std::shared_ptr<RawInputHandler> mRawInputHandler;
  162. std::shared_ptr<OSInputHandler> mOSInputHandler;
  163. Vector<DeviceData> mDevices;
  164. /************************************************************************/
  165. /* STATICS */
  166. /************************************************************************/
  167. static const int HISTORY_BUFFER_SIZE; // Size of buffer used for input smoothing
  168. static const float WEIGHT_MODIFIER;
  169. };
  170. /**
  171. * @copydoc Input
  172. */
  173. BS_CORE_EXPORT Input& gInput();
  174. }