SteamVR_Action_Vibration.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using System.Collections;
  4. using System;
  5. using Valve.VR;
  6. using System.Runtime.InteropServices;
  7. using System.Collections.Generic;
  8. #pragma warning disable 0067
  9. namespace Valve.VR
  10. {
  11. [Serializable]
  12. /// <summary>
  13. /// Vibration actions are used to trigger haptic feedback in vr controllers.
  14. /// </summary>
  15. public class SteamVR_Action_Vibration : SteamVR_Action_Out<SteamVR_Action_Vibration_Source_Map, SteamVR_Action_Vibration_Source>, ISerializationCallbackReceiver
  16. {
  17. public delegate void ActiveChangeHandler(SteamVR_Action_Vibration fromAction, SteamVR_Input_Sources fromSource, bool active);
  18. public delegate void ExecuteHandler(SteamVR_Action_Vibration fromAction, SteamVR_Input_Sources fromSource, float secondsFromNow, float durationSeconds, float frequency, float amplitude);
  19. /// <summary><strong>[SteamVR_Input_Sources.Any]</strong> This event fires whenever a change happens in the action</summary>
  20. public event ActiveChangeHandler onActiveChange
  21. { add { sourceMap[SteamVR_Input_Sources.Any].onActiveChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveChange -= value; } }
  22. /// <summary><strong>[SteamVR_Input_Sources.Any]</strong> This event fires whenever a change happens in the action</summary>
  23. public event ActiveChangeHandler onActiveBindingChange
  24. { add { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange -= value; } }
  25. /// <summary><strong>[SteamVR_Input_Sources.Any]</strong> This event fires whenever this action is executed</summary>
  26. public event ExecuteHandler onExecute
  27. { add { sourceMap[SteamVR_Input_Sources.Any].onExecute += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onExecute -= value; } }
  28. public SteamVR_Action_Vibration() { }
  29. /// <summary>
  30. /// Trigger the haptics at a certain time for a certain length
  31. /// </summary>
  32. /// <param name="secondsFromNow">How long from the current time to execute the action (in seconds - can be 0)</param>
  33. /// <param name="durationSeconds">How long the haptic action should last (in seconds)</param>
  34. /// <param name="frequency">How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful)</param>
  35. /// <param name="amplitude">How intense the haptic action should be (0 - 1)</param>
  36. /// <param name="inputSource">The device you would like to execute the haptic action. Any if the action is not device specific.</param>
  37. public void Execute(float secondsFromNow, float durationSeconds, float frequency, float amplitude, SteamVR_Input_Sources inputSource)
  38. {
  39. sourceMap[inputSource].Execute(secondsFromNow, durationSeconds, frequency, amplitude);
  40. }
  41. /// <summary>Executes a function when the *functional* active state of this action (with the specified inputSource) changes.
  42. /// This happens when the action is bound or unbound, or when the ActionSet changes state.</summary>
  43. /// <param name="functionToCall">A local function that receives the boolean action who's active state changes and the corresponding input source</param>
  44. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  45. public void AddOnActiveChangeListener(ActiveChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
  46. {
  47. sourceMap[inputSource].onActiveChange += functionToCall;
  48. }
  49. /// <summary>Stops executing a function when the *functional* active state of this action (with the specified inputSource) changes.
  50. /// This happens when the action is bound or unbound, or when the ActionSet changes state.</summary>
  51. /// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
  52. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  53. public void RemoveOnActiveChangeListener(ActiveChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  54. {
  55. sourceMap[inputSource].onActiveChange -= functionToStopCalling;
  56. }
  57. /// <summary>Executes a function when the active state of this action (with the specified inputSource) changes. This happens when the action is bound or unbound</summary>
  58. /// <param name="functionToCall">A local function that receives the boolean action who's active state changes and the corresponding input source</param>
  59. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  60. public void AddOnActiveBindingChangeListener(ActiveChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
  61. {
  62. sourceMap[inputSource].onActiveBindingChange += functionToCall;
  63. }
  64. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  65. /// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
  66. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  67. public void RemoveOnActiveBindingChangeListener(ActiveChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  68. {
  69. sourceMap[inputSource].onActiveBindingChange -= functionToStopCalling;
  70. }
  71. /// <summary>Executes a function when the execute method of this action (with the specified inputSource) is called. This happens when the action is bound or unbound</summary>
  72. /// <param name="functionToCall">A local function that receives the boolean action who's active state changes and the corresponding input source</param>
  73. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  74. public void AddOnExecuteListener(ExecuteHandler functionToCall, SteamVR_Input_Sources inputSource)
  75. {
  76. sourceMap[inputSource].onExecute += functionToCall;
  77. }
  78. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  79. /// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
  80. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  81. public void RemoveOnExecuteListener(ExecuteHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  82. {
  83. sourceMap[inputSource].onExecute -= functionToStopCalling;
  84. }
  85. /// <summary>
  86. /// Returns the last time this action was executed
  87. /// </summary>
  88. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  89. public override float GetTimeLastChanged(SteamVR_Input_Sources inputSource)
  90. {
  91. return sourceMap[inputSource].timeLastExecuted;
  92. }
  93. void ISerializationCallbackReceiver.OnBeforeSerialize()
  94. {
  95. }
  96. void ISerializationCallbackReceiver.OnAfterDeserialize()
  97. {
  98. InitAfterDeserialize();
  99. }
  100. public override bool IsUpdating(SteamVR_Input_Sources inputSource)
  101. {
  102. return sourceMap.IsUpdating(inputSource);
  103. }
  104. }
  105. public class SteamVR_Action_Vibration_Source_Map : SteamVR_Action_Source_Map<SteamVR_Action_Vibration_Source>
  106. {
  107. public bool IsUpdating(SteamVR_Input_Sources inputSource)
  108. {
  109. return sources[inputSource].timeLastExecuted != 0;
  110. }
  111. }
  112. public class SteamVR_Action_Vibration_Source : SteamVR_Action_Out_Source
  113. {
  114. /// <summary>Event fires when the active state (ActionSet active and binding active) changes</summary>
  115. public event SteamVR_Action_Vibration.ActiveChangeHandler onActiveChange;
  116. /// <summary>Event fires when the active state of the binding changes</summary>
  117. public event SteamVR_Action_Vibration.ActiveChangeHandler onActiveBindingChange;
  118. /// <summary>Event fires whenever this action is executed</summary>
  119. public event SteamVR_Action_Vibration.ExecuteHandler onExecute;
  120. //todo: fix the active state of out actions
  121. /// <summary>Returns true if this action is bound and the ActionSet is active</summary>
  122. public override bool active { get { return activeBinding && setActive; } }
  123. /// <summary>Returns true if the action is bound</summary>
  124. public override bool activeBinding { get { return true; } }
  125. /// <summary>Returns true if the action was bound and the ActionSet was active during the previous update</summary>
  126. public override bool lastActive { get; protected set; }
  127. /// <summary>Returns true if the action was bound during the previous update</summary>
  128. public override bool lastActiveBinding { get { return true; } }
  129. /// <summary>The last time the execute method was called on this action</summary>
  130. public float timeLastExecuted { get; protected set; }
  131. protected SteamVR_Action_Vibration vibrationAction;
  132. /// <summary>
  133. /// <strong>[Should not be called by user code]</strong>
  134. /// Initializes the handle for the inputSource, and any other related SteamVR data.
  135. /// </summary>
  136. public override void Initialize()
  137. {
  138. base.Initialize();
  139. lastActive = true;
  140. }
  141. public override void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource)
  142. {
  143. base.Preinitialize(wrappingAction, forInputSource);
  144. vibrationAction = (SteamVR_Action_Vibration)wrappingAction;
  145. }
  146. /// <summary>
  147. /// Trigger the haptics at a certain time for a certain length
  148. /// </summary>
  149. /// <param name="secondsFromNow">How long from the current time to execute the action (in seconds - can be 0)</param>
  150. /// <param name="durationSeconds">How long the haptic action should last (in seconds)</param>
  151. /// <param name="frequency">How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful)</param>
  152. /// <param name="amplitude">How intense the haptic action should be (0 - 1)</param>
  153. /// <param name="inputSource">The device you would like to execute the haptic action. Any if the action is not device specific.</param>
  154. public void Execute(float secondsFromNow, float durationSeconds, float frequency, float amplitude)
  155. {
  156. if (SteamVR_Input.isStartupFrame)
  157. return;
  158. timeLastExecuted = Time.realtimeSinceStartup;
  159. EVRInputError err = OpenVR.Input.TriggerHapticVibrationAction(handle, secondsFromNow, durationSeconds, frequency, amplitude, inputSourceHandle);
  160. //Debug.Log(string.Format("[{5}: haptic] secondsFromNow({0}), durationSeconds({1}), frequency({2}), amplitude({3}), inputSource({4})", secondsFromNow, durationSeconds, frequency, amplitude, inputSource, this.GetShortName()));
  161. if (err != EVRInputError.None)
  162. Debug.LogError("<b>[SteamVR]</b> TriggerHapticVibrationAction (" + fullPath + ") error: " + err.ToString() + " handle: " + handle.ToString());
  163. if (onExecute != null)
  164. onExecute.Invoke(vibrationAction, inputSource, secondsFromNow, durationSeconds, frequency, amplitude);
  165. }
  166. }
  167. /// <summary>
  168. /// Vibration actions are used to trigger haptic feedback in vr controllers.
  169. /// </summary>
  170. public interface ISteamVR_Action_Vibration : ISteamVR_Action_Out
  171. {
  172. /// <summary>
  173. /// Trigger the haptics at a certain time for a certain length
  174. /// </summary>
  175. /// <param name="secondsFromNow">How long from the current time to execute the action (in seconds - can be 0)</param>
  176. /// <param name="durationSeconds">How long the haptic action should last (in seconds)</param>
  177. /// <param name="frequency">How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful)</param>
  178. /// <param name="amplitude">How intense the haptic action should be (0 - 1)</param>
  179. /// <param name="inputSource">The device you would like to execute the haptic action. Any if the action is not device specific.</param>
  180. void Execute(float secondsFromNow, float durationSeconds, float frequency, float amplitude, SteamVR_Input_Sources inputSource);
  181. }
  182. }
  183. #pragma warning restore 0067