BsInput.h 6.0 KB

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