VirtualInput.cs 6.2 KB

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