BsInput.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. public:
  30. Input();
  31. ~Input();
  32. /**
  33. * @brief Triggered whenever a button is first pressed.
  34. */
  35. Event<void(const ButtonEvent&)> onButtonDown;
  36. /**
  37. * @brief Triggered whenever a button is first released.
  38. */
  39. Event<void(const ButtonEvent&)> onButtonUp;
  40. /**
  41. * @brief Triggered whenever user inputs a text character.
  42. */
  43. Event<void(const TextInputEvent&)> onCharInput;
  44. /**
  45. * @brief Triggers when some pointing device (mouse cursor, touch) moves.
  46. */
  47. Event<void(const PointerEvent&)> onPointerMoved;
  48. /**
  49. * @brief Triggers when some pointing device (mouse cursor, touch) button is pressed.
  50. */
  51. Event<void(const PointerEvent&)> onPointerPressed;
  52. /**
  53. * @brief Triggers when some pointing device (mouse cursor, touch) button is released.
  54. */
  55. Event<void(const PointerEvent&)> onPointerReleased;
  56. /**
  57. * @brief Triggers when some pointing device (mouse cursor, touch) button is double clicked.
  58. */
  59. Event<void(const PointerEvent&)> onPointerDoubleClick;
  60. // TODO Low priority: Remove this, I can emulate it using virtual input
  61. /**
  62. * @brief Triggers on special input commands.
  63. */
  64. Event<void(InputCommandType)> onInputCommand;
  65. /**
  66. * @brief Registers a new input handler. Replaces any previous input handler.
  67. *
  68. * @note Internal method.
  69. */
  70. void _registerRawInputHandler(std::shared_ptr<RawInputHandler> inputHandler);
  71. /**
  72. * @brief Called every frame. Dispatches any callbacks resulting from input by the user.
  73. *
  74. * @note Internal method.
  75. */
  76. void _update();
  77. /**
  78. * @brief Returns smoothed mouse/joystick input in the horizontal axis.
  79. *
  80. * @return The horizontal axis value ranging [-1.0f, 1.0f].
  81. */
  82. float getHorizontalAxis() const;
  83. /**
  84. * @brief Returns smoothed mouse/joystick input in the vertical axis.
  85. *
  86. * @return The vertical axis value ranging [-1.0f, 1.0f].
  87. */
  88. float getVerticalAxis() const;
  89. /**
  90. * @brief Query if the provided button is currently being held (this frame or previous frames).
  91. */
  92. bool isButtonHeld(ButtonCode keyCode) const;
  93. /**
  94. * @brief Query if the provided button is currently being released (one true for one frame).
  95. */
  96. bool isButtonUp(ButtonCode keyCode) const;
  97. /**
  98. * @brief Query if the provided button is currently being pressed (one true for one frame).
  99. */
  100. bool isButtonDown(ButtonCode keyCode) const;
  101. /**
  102. * @brief Returns mouse cursor position.
  103. */
  104. Vector2I getCursorPosition() const { return mMouseAbsPos; }
  105. private:
  106. std::shared_ptr<RawInputHandler> mRawInputHandler;
  107. std::shared_ptr<OSInputHandler> mOSInputHandler;
  108. float mSmoothHorizontalAxis;
  109. float mSmoothVerticalAxis;
  110. float* mHorizontalHistoryBuffer;
  111. float* mVerticalHistoryBuffer;
  112. float* mTimesHistoryBuffer;
  113. int mCurrentBufferIdx;
  114. Vector2I mMouseLastRel;
  115. Vector2I mMouseAbsPos;
  116. RawAxisState mAxes[RawInputAxis::Count];
  117. ButtonState mKeyState[BC_Count];
  118. void buttonDown(ButtonCode code, UINT64 timestamp);
  119. void buttonUp(ButtonCode code, UINT64 timestamp);
  120. void charInput(UINT32 chr);
  121. /**
  122. * @brief Raw mouse/joystick axis input.
  123. */
  124. void axisMoved(const RawAxisState& state, RawInputAxis axis);
  125. /**
  126. * @brief Cursor movement as OS reports it. Used for screen cursor position.
  127. */
  128. void cursorMoved(const PointerEvent& event);
  129. /**
  130. * @brief Cursor button presses as OS reports it.
  131. */
  132. void cursorPressed(const PointerEvent& event);
  133. /**
  134. * @brief Cursor button releases as OS reports it.
  135. */
  136. void cursorReleased(const PointerEvent& event);
  137. /**
  138. * @brief Cursor button releases as OS reports it.
  139. */
  140. void cursorDoubleClick(const PointerEvent& event);
  141. /**
  142. * @brief Input commands as OS reports them.
  143. */
  144. void inputCommandEntered(InputCommandType commandType);
  145. /**
  146. * @brief Updates the axis input values that need smoothing.
  147. */
  148. void updateSmoothInput();
  149. /**
  150. * @brief Called when window in focus changes, as reported by the OS.
  151. */
  152. void inputWindowChanged(RenderWindow& win);
  153. /************************************************************************/
  154. /* STATICS */
  155. /************************************************************************/
  156. static const int HISTORY_BUFFER_SIZE; // Size of buffer used for input smoothing
  157. static const float WEIGHT_MODIFIER;
  158. };
  159. /**
  160. * @copydoc Input
  161. */
  162. BS_CORE_EXPORT Input& gInput();
  163. }