2
0

VirtualInput.cs 5.9 KB

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