VirtualInput.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 { return Internal_GetKeyConfig(); }
  34. set { Internal_SetKeyConfig(value); }
  35. }
  36. /// <summary>
  37. /// Checks if the physical button combination corresponding to the specified virtual button is being pressed this
  38. /// frame.
  39. /// </summary>
  40. /// <param name="button">Virtual button to check.</param>
  41. /// <param name="deviceIdx">Optional device index in case multiple input devices are available.</param>
  42. public static bool IsButtonDown(VirtualButton button, int deviceIdx = 0)
  43. {
  44. return Internal_IsButtonDown(ref button, deviceIdx);
  45. }
  46. /// <summary>
  47. /// Checks if the physical button combination corresponding to the specified virtual button is being released this
  48. /// frame.
  49. /// </summary>
  50. /// <param name="button">Virtual button to check.</param>
  51. /// <param name="deviceIdx">Optional device index in case multiple input devices are available.</param>
  52. public static bool IsButtonUp(VirtualButton button, int deviceIdx = 0)
  53. {
  54. return Internal_IsButtonUp(ref button, deviceIdx);
  55. }
  56. /// <summary>
  57. /// Checks if the physical button combination corresponding to the specified virtual button is being held down.
  58. /// </summary>
  59. /// <param name="button">Virtual button to check.</param>
  60. /// <param name="deviceIdx">Index of the device to check.</param>
  61. public static bool IsButtonHeld(VirtualButton button, int deviceIdx = 0)
  62. {
  63. return Internal_IsButtonHeld(ref button, deviceIdx);
  64. }
  65. /// <summary>
  66. /// Returns normalized value for the specified input axis.
  67. /// </summary>
  68. /// <param name="axis">Virtual axis identifier.</param>
  69. /// <param name="deviceIdx">Optional device index in case multiple input devices are available.</param>
  70. /// <returns>Axis value, normally in [-1.0, 1.0] range, but can be outside the range for devices with unbound axes
  71. /// (for example mouse).</returns>
  72. public static float GetAxisValue(VirtualAxis axis, int deviceIdx = 0)
  73. {
  74. return Internal_GetAxisValue(ref axis, deviceIdx);
  75. }
  76. /// <summary>
  77. /// Triggered by the runtime when the virtual button is pressed.
  78. /// </summary>
  79. /// <param name="button">Virtual button that was pressed.</param>
  80. /// <param name="deviceIdx">Index of the device the button was pressed on.</param>
  81. private static void Internal_TriggerButtonDown(VirtualButton button, int deviceIdx)
  82. {
  83. if (OnButtonDown != null)
  84. OnButtonDown(button, deviceIdx);
  85. }
  86. /// <summary>
  87. /// Triggered by the runtime when the virtual button is released.
  88. /// </summary>
  89. /// <param name="button">Virtual button that was released.</param>
  90. /// <param name="deviceIdx">Index of the device the button was released on.</param>
  91. private static void Internal_TriggerButtonUp(VirtualButton button, int deviceIdx)
  92. {
  93. if (OnButtonUp != null)
  94. OnButtonUp(button, deviceIdx);
  95. }
  96. /// <summary>
  97. /// Triggered by the runtime every frame while a virtual button is being held down.
  98. /// </summary>
  99. /// <param name="button">Virtual button that is being held down.</param>
  100. /// <param name="deviceIdx">Index of the device the button is being held down on.</param>
  101. private static void Internal_TriggerButtonHeld(VirtualButton button, int deviceIdx)
  102. {
  103. if (OnButtonHeld != null)
  104. OnButtonHeld(button, deviceIdx);
  105. }
  106. [MethodImpl(MethodImplOptions.InternalCall)]
  107. private static extern InputConfiguration Internal_GetKeyConfig();
  108. [MethodImpl(MethodImplOptions.InternalCall)]
  109. private static extern void Internal_SetKeyConfig(InputConfiguration inputConfig);
  110. [MethodImpl(MethodImplOptions.InternalCall)]
  111. private static extern bool Internal_IsButtonDown(ref VirtualButton button, int deviceIdx);
  112. [MethodImpl(MethodImplOptions.InternalCall)]
  113. private static extern bool Internal_IsButtonUp(ref VirtualButton button, int deviceIdx);
  114. [MethodImpl(MethodImplOptions.InternalCall)]
  115. private static extern bool Internal_IsButtonHeld(ref VirtualButton button, int deviceIdx);
  116. [MethodImpl(MethodImplOptions.InternalCall)]
  117. private static extern float Internal_GetAxisValue(ref VirtualAxis button, int deviceIdx);
  118. };
  119. /** @} */
  120. }