BsInputConfiguration.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "BsInputFwd.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Describes a virtual button. Virtual buttons allow you to
  10. * map custom actions without needing to know about what
  11. * physical buttons trigger those actions.
  12. */
  13. struct BS_EXPORT VIRTUAL_BUTTON_DESC
  14. {
  15. VIRTUAL_BUTTON_DESC();
  16. /**
  17. * @brief Constructs a virtual button descriptor.
  18. *
  19. * @param buttonCode Physical button the virtual button is triggered by.
  20. * @param modifiers Modifiers required to be pressed with the physical button to trigger the virtual button.
  21. * @param repeatable If true, the virtual button events will be sent continually while the physical button is being held.
  22. */
  23. VIRTUAL_BUTTON_DESC(ButtonCode buttonCode, ButtonModifier modifiers = ButtonModifier::None, bool repeatable = false);
  24. ButtonCode buttonCode;
  25. ButtonModifier modifiers;
  26. bool repeatable;
  27. };
  28. /**
  29. * @brief Describes a virtual axis. Virtual axes allow you to map
  30. * custom axes without needing to know the actual physical device
  31. * handling those axes.
  32. */
  33. struct BS_EXPORT VIRTUAL_AXIS_DESC
  34. {
  35. VIRTUAL_AXIS_DESC();
  36. /**
  37. * @brief Constructs a new virtual axis descriptor.
  38. *
  39. * @param type Type of physical axis to map to. See InputAxis type for common types, but you are not limited to those values.
  40. * @param deadZone Value below which to ignore axis value and consider it 0.
  41. * @param sensitivity Higher sensitivity means the axis will more easily reach its maximum values.
  42. * @param invert Should axis values be inverted.
  43. */
  44. VIRTUAL_AXIS_DESC(UINT32 type, float deadZone = 0.0001f, float sensitivity = 1.0f, bool invert = false);
  45. float deadZone;
  46. float sensitivity;
  47. bool invert;
  48. UINT32 type;
  49. };
  50. /**
  51. * @brief Identifier for a virtual button.
  52. *
  53. * @note Primary purpose of this class is to avoid expensive string compare (i.e. button names),
  54. * and instead use a unique button identifier for compare. Generally you want to create
  55. * one of these using the button name, and then store it for later use.
  56. *
  57. * This class is not thread safe and should only be used on the sim thread.
  58. *
  59. * @see VIRTUAL_BUTTON_DESC
  60. */
  61. class BS_EXPORT VirtualButton
  62. {
  63. public:
  64. VirtualButton();
  65. VirtualButton(const String& name);
  66. bool operator== (const VirtualButton& rhs) const
  67. {
  68. return (buttonIdentifier == rhs.buttonIdentifier);
  69. }
  70. UINT32 buttonIdentifier;
  71. private:
  72. static Map<String, UINT32> UniqueButtonIds;
  73. static UINT32 NextButtonId;
  74. };
  75. /**
  76. * @brief Identifier for a virtual axis.
  77. *
  78. * @note Primary purpose of this class is to avoid expensive string compare (i.e. axis names),
  79. * and instead use a unique axis identifier for compare. Generally you want to create
  80. * one of these using the axis name, and then store it for later use.
  81. *
  82. * This class is not thread safe and should only be used on the sim thread.
  83. *
  84. * @see VIRTUAL_AXIS_DESC
  85. */
  86. class BS_EXPORT VirtualAxis
  87. {
  88. public:
  89. VirtualAxis();
  90. VirtualAxis(const String& name);
  91. UINT32 axisIdentifier;
  92. bool operator== (const VirtualAxis& rhs) const
  93. {
  94. return (axisIdentifier == rhs.axisIdentifier);
  95. }
  96. private:
  97. static Map<String, UINT32> UniqueAxisIds;
  98. static UINT32 NextAxisId;
  99. };
  100. /**
  101. * @brief Contains virtual <-> physical key mappings.
  102. */
  103. class BS_EXPORT InputConfiguration
  104. {
  105. static const int MAX_NUM_DEVICES_PER_TYPE = 8;
  106. static const int MAX_NUM_DEVICES = (UINT32)InputDevice::Count * MAX_NUM_DEVICES_PER_TYPE;
  107. /**
  108. * @brief Internal virtual button data container.
  109. */
  110. struct VirtualButtonData
  111. {
  112. String name;
  113. VirtualButton button;
  114. VIRTUAL_BUTTON_DESC desc;
  115. };
  116. /**
  117. * @brief Internal virtual axis data container.
  118. */
  119. struct VirtualAxisData
  120. {
  121. String name;
  122. VirtualAxis axis;
  123. VIRTUAL_AXIS_DESC desc;
  124. };
  125. /**
  126. * @brief Internal container for holding axis data for all devices.
  127. */
  128. struct DeviceAxisData
  129. {
  130. VirtualAxisData axes[(UINT32)InputAxis::Count];
  131. };
  132. public:
  133. InputConfiguration();
  134. /**
  135. * @brief Registers a new virtual button.
  136. *
  137. * @param name Unique name used to access the virtual button.
  138. * @param buttonCode Physical button the virtual button is triggered by.
  139. * @param modifiers Modifiers required to be pressed with the physical button to trigger the virtual button.
  140. * @param repeatable If true, the virtual button events will be sent continually while the physical button is being held.
  141. */
  142. void registerButton(const String& name, ButtonCode buttonCode, ButtonModifier modifiers = ButtonModifier::None, bool repeatable = false);
  143. /**
  144. * @brief Unregisters a virtual button with the specified name. Events will no longer be generated for that button.
  145. */
  146. void unregisterButton(const String& name);
  147. /**
  148. * @brief Registers a new virtual axis.
  149. *
  150. * @param name Unique name used to access the axis.
  151. * @param desc Descriptor structure containing virtual axis creation parameters.
  152. */
  153. void registerAxis(const String& name, const VIRTUAL_AXIS_DESC& desc);
  154. /**
  155. * @brief Unregisters a virtual axis with the specified name. You will no longer
  156. * be able to retrieve valid values for that axis.
  157. */
  158. void unregisterAxis(const String& name);
  159. /**
  160. * @brief Sets repeat interval for held virtual buttons. Buttons will be continously triggered in
  161. * interval increments as long as they button is being held.
  162. */
  163. void setRepeatInterval(UINT64 milliseconds) { mRepeatInterval = milliseconds; }
  164. /**
  165. * @brief Gets the currently set repeat interval for held virtual buttons.
  166. */
  167. UINT64 getRepeatInterval() const { return mRepeatInterval; }
  168. /**
  169. * @brief Returns data about virtual buttons that are triggered
  170. * by the specified physical button code and modifier flags.
  171. */
  172. bool _getButtons(ButtonCode code, UINT32 modifiers, Vector<VirtualButton>& btns, Vector<VIRTUAL_BUTTON_DESC>& btnDescs) const;
  173. /**
  174. * @brief Retrieves virtual axis descriptor for the provided axis.
  175. */
  176. bool _getAxis(const VirtualAxis& axis, VIRTUAL_AXIS_DESC& axisDesc) const;
  177. private:
  178. Vector<VirtualButtonData> mButtons[BC_Count];
  179. Vector<VirtualAxisData> mAxes;
  180. UINT64 mRepeatInterval;
  181. };
  182. }