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