2
0

SteamVR_Action_Vibration.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. int sourceIndex = (int)inputSource;
  110. return sources[sourceIndex].timeLastExecuted != 0;
  111. }
  112. }
  113. public class SteamVR_Action_Vibration_Source : SteamVR_Action_Out_Source
  114. {
  115. /// <summary>Event fires when the active state (ActionSet active and binding active) changes</summary>
  116. public event SteamVR_Action_Vibration.ActiveChangeHandler onActiveChange;
  117. /// <summary>Event fires when the active state of the binding changes</summary>
  118. public event SteamVR_Action_Vibration.ActiveChangeHandler onActiveBindingChange;
  119. /// <summary>Event fires whenever this action is executed</summary>
  120. public event SteamVR_Action_Vibration.ExecuteHandler onExecute;
  121. //todo: fix the active state of out actions
  122. /// <summary>Returns true if this action is bound and the ActionSet is active</summary>
  123. public override bool active { get { return activeBinding && setActive; } }
  124. /// <summary>Returns true if the action is bound</summary>
  125. public override bool activeBinding { get { return true; } }
  126. /// <summary>Returns true if the action was bound and the ActionSet was active during the previous update</summary>
  127. public override bool lastActive { get; protected set; }
  128. /// <summary>Returns true if the action was bound during the previous update</summary>
  129. public override bool lastActiveBinding { get { return true; } }
  130. /// <summary>The last time the execute method was called on this action</summary>
  131. public float timeLastExecuted { get; protected set; }
  132. protected SteamVR_Action_Vibration vibrationAction;
  133. /// <summary>
  134. /// <strong>[Should not be called by user code]</strong>
  135. /// Initializes the handle for the inputSource, and any other related SteamVR data.
  136. /// </summary>
  137. public override void Initialize()
  138. {
  139. base.Initialize();
  140. lastActive = true;
  141. }
  142. public override void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource)
  143. {
  144. base.Preinitialize(wrappingAction, forInputSource);
  145. vibrationAction = (SteamVR_Action_Vibration)wrappingAction;
  146. }
  147. /// <summary>
  148. /// Trigger the haptics at a certain time for a certain length
  149. /// </summary>
  150. /// <param name="secondsFromNow">How long from the current time to execute the action (in seconds - can be 0)</param>
  151. /// <param name="durationSeconds">How long the haptic action should last (in seconds)</param>
  152. /// <param name="frequency">How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful)</param>
  153. /// <param name="amplitude">How intense the haptic action should be (0 - 1)</param>
  154. /// <param name="inputSource">The device you would like to execute the haptic action. Any if the action is not device specific.</param>
  155. public void Execute(float secondsFromNow, float durationSeconds, float frequency, float amplitude)
  156. {
  157. if (SteamVR_Input.isStartupFrame)
  158. return;
  159. timeLastExecuted = Time.realtimeSinceStartup;
  160. EVRInputError err = OpenVR.Input.TriggerHapticVibrationAction(handle, secondsFromNow, durationSeconds, frequency, amplitude, inputSourceHandle);
  161. //Debug.Log(string.Format("[{5}: haptic] secondsFromNow({0}), durationSeconds({1}), frequency({2}), amplitude({3}), inputSource({4})", secondsFromNow, durationSeconds, frequency, amplitude, inputSource, this.GetShortName()));
  162. if (err != EVRInputError.None)
  163. Debug.LogError("<b>[SteamVR]</b> TriggerHapticVibrationAction (" + fullPath + ") error: " + err.ToString() + " handle: " + handle.ToString());
  164. if (onExecute != null)
  165. onExecute.Invoke(vibrationAction, inputSource, secondsFromNow, durationSeconds, frequency, amplitude);
  166. }
  167. }
  168. /// <summary>
  169. /// Vibration actions are used to trigger haptic feedback in vr controllers.
  170. /// </summary>
  171. public interface ISteamVR_Action_Vibration : ISteamVR_Action_Out
  172. {
  173. /// <summary>
  174. /// Trigger the haptics at a certain time for a certain length
  175. /// </summary>
  176. /// <param name="secondsFromNow">How long from the current time to execute the action (in seconds - can be 0)</param>
  177. /// <param name="durationSeconds">How long the haptic action should last (in seconds)</param>
  178. /// <param name="frequency">How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful)</param>
  179. /// <param name="amplitude">How intense the haptic action should be (0 - 1)</param>
  180. /// <param name="inputSource">The device you would like to execute the haptic action. Any if the action is not device specific.</param>
  181. void Execute(float secondsFromNow, float durationSeconds, float frequency, float amplitude, SteamVR_Input_Sources inputSource);
  182. }
  183. }
  184. #pragma warning restore 0067