2
0

InputConfiguration.cs 7.9 KB

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