2
0

VirtualInput.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Runtime.CompilerServices;
  2. namespace BansheeEngine
  3. {
  4. /// <summary>
  5. /// Handles virtual input that allows you to receive virtual input events that hide the actual physical input, allowing
  6. /// you to easily change the input keys while being transparent to the external code.
  7. /// </summary>
  8. public static class VirtualInput
  9. {
  10. public delegate void OnButtonEventDelegate(VirtualButton btn, int deviceIdx);
  11. /// <summary>
  12. /// Triggered when a physical button combination corresponding to a virtual button is pressed.
  13. /// </summary>
  14. public static event OnButtonEventDelegate OnButtonDown;
  15. /// <summary>
  16. /// Triggered when a physical button combination corresponding to a virtual button is released.
  17. /// </summary>
  18. public static event OnButtonEventDelegate OnButtonUp;
  19. /// <summary>
  20. /// Triggered every frame while a physical button combination corresponding to a virtual button is being held down.
  21. /// </summary>
  22. public static event OnButtonEventDelegate OnButtonHeld;
  23. /// <summary>
  24. /// Input configuration that describes how physical keys map to virtual keys.
  25. /// </summary>
  26. public static InputConfiguration KeyConfig
  27. {
  28. get
  29. {
  30. return Internal_GetKeyConfig();
  31. }
  32. set
  33. {
  34. Internal_SetKeyConfig(value);
  35. }
  36. }
  37. /// <summary>
  38. /// Checks if the physical button combination corresponding to the specified virtual button is being pressed this
  39. /// frame.
  40. /// </summary>
  41. /// <param name="button">Virtual button to check.</param>
  42. /// <param name="deviceIdx">Optional device index in case multiple input devices are available.</param>
  43. public static bool IsButtonDown(VirtualButton button, int deviceIdx = 0)
  44. {
  45. return Internal_IsButtonDown(ref button, deviceIdx);
  46. }
  47. /// <summary>
  48. /// Checks if the physical button combination corresponding to the specified virtual button is being released this
  49. /// frame.
  50. /// </summary>
  51. /// <param name="button">Virtual button to check.</param>
  52. /// <param name="deviceIdx">Optional device index in case multiple input devices are available.</param>
  53. public static bool IsButtonUp(VirtualButton button, int deviceIdx = 0)
  54. {
  55. return Internal_IsButtonUp(ref button, deviceIdx);
  56. }
  57. /// <summary>
  58. /// Checks if the physical button combination corresponding to the specified virtual button is being held down.
  59. /// </summary>
  60. /// <param name="button">Virtual button to check.</param>
  61. /// <param name="deviceIdx">Index of the device to check.</param>
  62. public static bool IsButtonHeld(VirtualButton button, int deviceIdx = 0)
  63. {
  64. return Internal_IsButtonHeld(ref button, deviceIdx);
  65. }
  66. /// <summary>
  67. /// Returns normalized value for the specified input axis.
  68. /// </summary>
  69. /// <param name="axis">Virtual axis identifier.</param>
  70. /// <param name="deviceIdx">Optional device index in case multiple input devices are available.</param>
  71. /// <returns>Axis value, normally in [-1.0, 1.0] range, but can be outside the range for devices with unbound axes
  72. /// (e.g. mouse).</returns>
  73. public static float GetAxisValue(VirtualAxis axis, int deviceIdx = 0)
  74. {
  75. return Internal_GetAxisValue(ref axis, deviceIdx);
  76. }
  77. /// <summary>
  78. /// Triggered by the runtime when the virtual button is pressed.
  79. /// </summary>
  80. /// <param name="button">Virtual button that was pressed.</param>
  81. /// <param name="deviceIdx">Index of the device the button was pressed on.</param>
  82. private static void Internal_TriggerButtonDown(VirtualButton button, int deviceIdx)
  83. {
  84. if (OnButtonDown != null)
  85. OnButtonDown(button, deviceIdx);
  86. }
  87. /// <summary>
  88. /// Triggered by the runtime when the virtual button is released.
  89. /// </summary>
  90. /// <param name="button">Virtual button that was released.</param>
  91. /// <param name="deviceIdx">Index of the device the button was released on.</param>
  92. private static void Internal_TriggerButtonUp(VirtualButton button, int deviceIdx)
  93. {
  94. if (OnButtonUp != null)
  95. OnButtonUp(button, deviceIdx);
  96. }
  97. /// <summary>
  98. /// Triggered by the runtime every frame while a virtual button is being held down.
  99. /// </summary>
  100. /// <param name="button">Virtual button that is being held down.</param>
  101. /// <param name="deviceIdx">Index of the device the button is being held down on.</param>
  102. private static void Internal_TriggerButtonHeld(VirtualButton button, int deviceIdx)
  103. {
  104. if (OnButtonHeld != null)
  105. OnButtonHeld(button, deviceIdx);
  106. }
  107. [MethodImpl(MethodImplOptions.InternalCall)]
  108. private static extern InputConfiguration Internal_GetKeyConfig();
  109. [MethodImpl(MethodImplOptions.InternalCall)]
  110. private static extern void Internal_SetKeyConfig(InputConfiguration inputConfig);
  111. [MethodImpl(MethodImplOptions.InternalCall)]
  112. private static extern bool Internal_IsButtonDown(ref VirtualButton button, int deviceIdx);
  113. [MethodImpl(MethodImplOptions.InternalCall)]
  114. private static extern bool Internal_IsButtonUp(ref VirtualButton button, int deviceIdx);
  115. [MethodImpl(MethodImplOptions.InternalCall)]
  116. private static extern bool Internal_IsButtonHeld(ref VirtualButton button, int deviceIdx);
  117. [MethodImpl(MethodImplOptions.InternalCall)]
  118. private static extern float Internal_GetAxisValue(ref VirtualAxis button, int deviceIdx);
  119. };
  120. }