InputConfiguration.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Input
  9. * @{
  10. */
  11. /// <summary>
  12. /// Contains virtual to physical key mappings.
  13. /// </summary>
  14. public sealed class InputConfiguration : ScriptObject
  15. {
  16. /// <summary>
  17. /// Creates a new empty key configuration.
  18. /// </summary>
  19. public InputConfiguration()
  20. {
  21. Internal_CreateInstance(this);
  22. }
  23. /// <summary>
  24. /// Repeat interval for held virtual buttons. Buttons will be continously triggered in interval increments as long
  25. /// as they button is being held.
  26. /// </summary>
  27. public UInt64 RepeatInternal
  28. {
  29. get
  30. {
  31. return Internal_GetRepeatInterval(mCachedPtr);
  32. }
  33. set
  34. {
  35. Internal_SetRepeatInterval(mCachedPtr, value);
  36. }
  37. }
  38. /// <summary>
  39. /// Registers a new virtual button.
  40. /// </summary>
  41. /// <param name="name">Unique name used to access the virtual button.</param>
  42. /// <param name="buttonCode">Physical button the virtual button is triggered by.</param>
  43. /// <param name="modifiers">Modifiers required to be pressed with the physical button to trigger the virtual button.
  44. /// </param>
  45. /// <param name="repeatable">If true, the virtual button events will be sent continually while the physical button
  46. /// is being held.</param>
  47. public void RegisterButton(String name, ButtonCode buttonCode,
  48. ButtonModifier modifiers = ButtonModifier.None, bool repeatable = false)
  49. {
  50. Internal_RegisterButton(mCachedPtr, name, buttonCode, modifiers, repeatable);
  51. }
  52. /// <summary>
  53. /// Unregisters a virtual button with the specified name. Events will no longer be generated for that button.
  54. /// </summary>
  55. /// <param name="name">Unique name used to access the virtual button.</param>
  56. public void UnregisterButton(String name)
  57. {
  58. Internal_UnregisterButton(mCachedPtr, name);
  59. }
  60. /// <summary>
  61. /// Registers a new virtual axis.
  62. /// </summary>
  63. /// <param name="name">Unique name used to access the virtual axis.</param>
  64. /// <param name="type">Type of physical axis to map to.</param>
  65. /// <param name="deadZone">Value below which to ignore axis value and consider it 0.</param>
  66. /// <param name="sensitivity">Higher sensitivity means the axis will more easily reach its maximum values.</param>
  67. /// <param name="invert">Should axis values be inverted.</param>
  68. public void RegisterAxis(String name, InputAxis type, float deadZone = 0.0001f,
  69. float sensitivity = 1.0f, bool invert = false)
  70. {
  71. Internal_RegisterAxis(mCachedPtr, name, type, deadZone, sensitivity, invert);
  72. }
  73. /// <summary>
  74. /// Unregisters a virtual axis with the specified name. You will no longer be able to retrieve valid values for
  75. /// that axis.
  76. /// </summary>
  77. /// <param name="name">Unique name used to access the virtual axis.</param>
  78. public void UnregisterAxis(String name)
  79. {
  80. Internal_UnregisterAxis(mCachedPtr, name);
  81. }
  82. [MethodImpl(MethodImplOptions.InternalCall)]
  83. private static extern void Internal_CreateInstance(InputConfiguration inputConfig);
  84. [MethodImpl(MethodImplOptions.InternalCall)]
  85. private static extern void Internal_RegisterButton(IntPtr thisPtr, String name, ButtonCode buttonCode,
  86. ButtonModifier modifiers, bool repeatable);
  87. [MethodImpl(MethodImplOptions.InternalCall)]
  88. private static extern void Internal_UnregisterButton(IntPtr thisPtr, String name);
  89. [MethodImpl(MethodImplOptions.InternalCall)]
  90. private static extern void Internal_RegisterAxis(IntPtr thisPtr, String name, InputAxis type, float deadZone,
  91. float sensitivity, bool invert);
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. private static extern void Internal_UnregisterAxis(IntPtr thisPtr, String name);
  94. [MethodImpl(MethodImplOptions.InternalCall)]
  95. private static extern void Internal_SetRepeatInterval(IntPtr thisPtr, UInt64 milliseconds);
  96. [MethodImpl(MethodImplOptions.InternalCall)]
  97. private static extern UInt64 Internal_GetRepeatInterval(IntPtr thisPtr);
  98. }
  99. /// <summary>
  100. /// Button modifiers used with along with keyboard buttons.
  101. /// </summary>
  102. public enum ButtonModifier // Note: Must match C++ enum ButtonModifier
  103. {
  104. None = 0x00,
  105. Shift = 0x01,
  106. Ctrl = 0x02,
  107. Alt = 0x04,
  108. ShiftCtrl = 0x03,
  109. CtrlAlt = 0x06,
  110. ShiftAlt = 0x05,
  111. ShiftCtrlAlt = 0x07
  112. };
  113. /// <summary>
  114. /// Handle for a virtual button. Virtual buttons allow you to map custom buttons to physical buttons and deal with them
  115. /// without knowing the underlying physical buttons, allowing easy input remapping.
  116. /// </summary>
  117. [StructLayout(LayoutKind.Sequential)]
  118. public struct VirtualButton // Note: Must match C++ class VirtualButton
  119. {
  120. private readonly int buttonId;
  121. /// <summary>
  122. /// Creates a new virtual button handle.
  123. /// </summary>
  124. /// <param name="name">Unique name of the virtual button.</param>
  125. public VirtualButton(string name)
  126. {
  127. buttonId = Internal_InitVirtualButton(name);
  128. }
  129. public static bool operator ==(VirtualButton lhs, VirtualButton rhs)
  130. {
  131. return lhs.buttonId == rhs.buttonId;
  132. }
  133. public static bool operator !=(VirtualButton lhs, VirtualButton rhs)
  134. {
  135. return !(lhs == rhs);
  136. }
  137. /// <inheritdoc/>
  138. public override int GetHashCode()
  139. {
  140. return buttonId.GetHashCode();
  141. }
  142. /// <inheritdoc/>
  143. public override bool Equals(object other)
  144. {
  145. if (!(other is VirtualButton))
  146. return false;
  147. VirtualButton otherBtn = (VirtualButton)other;
  148. if (buttonId.Equals(otherBtn.buttonId))
  149. return true;
  150. return false;
  151. }
  152. [MethodImpl(MethodImplOptions.InternalCall)]
  153. private static extern int Internal_InitVirtualButton(String name);
  154. }
  155. /// <summary>
  156. /// Handle for a virtual axis. Virtual axes allow you to map custom axes without needing to know the actual physical
  157. /// device handling those axes.
  158. /// </summary>
  159. [StructLayout(LayoutKind.Sequential)]
  160. public struct VirtualAxis // Note: Must match C++ class VirtualAxis
  161. {
  162. private readonly int axisId;
  163. /// <summary>
  164. /// Creates a new virual axis handle.
  165. /// </summary>
  166. /// <param name="name">Unique name of the virtual axis.</param>
  167. public VirtualAxis(string name)
  168. {
  169. axisId = Internal_InitVirtualAxis(name);
  170. }
  171. public static bool operator ==(VirtualAxis lhs, VirtualAxis rhs)
  172. {
  173. return lhs.axisId == rhs.axisId;
  174. }
  175. public static bool operator !=(VirtualAxis lhs, VirtualAxis rhs)
  176. {
  177. return !(lhs == rhs);
  178. }
  179. /// <inheritdoc/>
  180. public override int GetHashCode()
  181. {
  182. return axisId.GetHashCode();
  183. }
  184. /// <inheritdoc/>
  185. public override bool Equals(object other)
  186. {
  187. if (!(other is VirtualAxis))
  188. return false;
  189. VirtualAxis otherAxis = (VirtualAxis)other;
  190. if (axisId.Equals(otherAxis.axisId))
  191. return true;
  192. return false;
  193. }
  194. [MethodImpl(MethodImplOptions.InternalCall)]
  195. private static extern int Internal_InitVirtualAxis(String name);
  196. }
  197. /** @} */
  198. }