BsVirtualInput.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #pragma once
  2. #include "BsPrerequisites.h"
  3. #include "BsModule.h"
  4. #include "BsInputConfiguration.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Handles virtual input that allows you to receive virtual input events that
  9. * hide the actual physical input, allowing you to easily change the input keys
  10. * while being transparent to the external code.
  11. */
  12. class BS_EXPORT VirtualInput : public Module<VirtualInput>
  13. {
  14. /**
  15. * @brief Possible states of virtual buttons.
  16. */
  17. enum class ButtonState
  18. {
  19. Off,
  20. On,
  21. ToggledOn,
  22. ToggledOff
  23. };
  24. /**
  25. * @brief Data container for a single virtual button.
  26. */
  27. struct ButtonData
  28. {
  29. VirtualButton button;
  30. ButtonState state;
  31. UINT64 timestamp;
  32. bool allowRepeat;
  33. };
  34. /**
  35. * @brief Contains button data for a specific input device.
  36. */
  37. struct DeviceData
  38. {
  39. Map<UINT32, ButtonData> cachedStates;
  40. };
  41. /**
  42. * @brief Data container for a virtual button event.
  43. */
  44. struct VirtualButtonEvent
  45. {
  46. VirtualButton button;
  47. ButtonState state;
  48. UINT32 deviceIdx;
  49. };
  50. public:
  51. VirtualInput();
  52. /**
  53. * @brief Creates a new empty input configuration.
  54. */
  55. static std::shared_ptr<InputConfiguration> createConfiguration();
  56. /**
  57. * @brief Sets an input configuration that determines how physical keys map to virtual buttons.
  58. */
  59. void setConfiguration(const std::shared_ptr<InputConfiguration>& input);
  60. /**
  61. * @brief Retrieves the active input configuration that determines how physical keys map to virtual buttons.
  62. */
  63. std::shared_ptr<InputConfiguration> getConfiguration() const { return mInputConfiguration; }
  64. /**
  65. * @brief Check is the virtual button just getting pressed. This state is only
  66. * active for one frame.
  67. *
  68. * @param button Virtual button identifier.
  69. * @param deviceIdx Optional device index in case multiple input devices are available.
  70. */
  71. bool isButtonDown(const VirtualButton& button, UINT32 deviceIdx = 0) const;
  72. /**
  73. * @brief Check is the virtual button just getting released. This state is only
  74. * active for one frame.
  75. *
  76. * @param button Virtual button identifier.
  77. * @param deviceIdx Optional device index in case multiple input devices are available.
  78. */
  79. bool isButtonUp(const VirtualButton& button, UINT32 deviceIdx = 0) const;
  80. /**
  81. * @brief Check is the virtual button is being held. This state is active as long
  82. * as the button is being held down, i.e. possibly multiple frames.
  83. *
  84. * @param button Virtual button identifier.
  85. * @param deviceIdx Optional device index in case multiple input devices are available.
  86. */
  87. bool isButtonHeld(const VirtualButton& button, UINT32 deviceIdx = 0) const;
  88. /**
  89. * @brief Returns normalized value for the specified input axis.
  90. * Returned value will usually be in [-1.0, 1.0] range, but can be outside
  91. * the range for devices with unbound axes (e.g. mouse).
  92. *
  93. * @param axis Virtual axis identifier.
  94. * @param deviceIdx Optional device index in case multiple input devices are available.
  95. */
  96. float getAxisValue(const VirtualAxis& axis, UINT32 deviceIdx = 0) const;
  97. /**
  98. * @brief Called once every frame.
  99. *
  100. * @note Internal method.
  101. */
  102. void _update();
  103. /**
  104. * @brief Triggered when a virtual button is pressed.
  105. */
  106. Event<void(const VirtualButton&, UINT32 deviceIdx)> onButtonDown;
  107. /**
  108. * @brief Triggered when a virtual button is released.
  109. */
  110. Event<void(const VirtualButton&, UINT32 deviceIdx)> onButtonUp;
  111. /**
  112. * @brief Triggered every frame when a virtual button is being held down.
  113. */
  114. Event<void(const VirtualButton&, UINT32 deviceIdx)> onButtonHeld;
  115. private:
  116. friend class VirtualButton;
  117. /**
  118. * @brief Performs all logic related to a button press.
  119. */
  120. void buttonDown(const ButtonEvent& event);
  121. /**
  122. * @brief Performs all logic related to a button release.
  123. */
  124. void buttonUp(const ButtonEvent& event);
  125. std::shared_ptr<InputConfiguration> mInputConfiguration;
  126. Vector<DeviceData> mDevices;
  127. Queue<VirtualButtonEvent> mEvents;
  128. UINT32 mActiveModifiers;
  129. };
  130. /**
  131. * @copydoc VirtualInput
  132. */
  133. BS_EXPORT VirtualInput& gVirtualInput();
  134. }