2
0

VirtualInput.cs 6.2 KB

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