SteamVR_Action_Single.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. namespace Valve.VR
  6. {
  7. [Serializable]
  8. /// <summary>
  9. /// An analog action with a value generally from 0 to 1. Also provides a delta since the last update.
  10. /// </summary>
  11. public class SteamVR_Action_Single : SteamVR_Action_In<SteamVR_Action_Single_Source_Map, SteamVR_Action_Single_Source>, ISteamVR_Action_Single, ISerializationCallbackReceiver
  12. {
  13. public delegate void AxisHandler(SteamVR_Action_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta);
  14. public delegate void ActiveChangeHandler(SteamVR_Action_Single fromAction, SteamVR_Input_Sources fromSource, bool active);
  15. public delegate void ChangeHandler(SteamVR_Action_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta);
  16. public delegate void UpdateHandler(SteamVR_Action_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta);
  17. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> This event fires whenever the axis changes by more than the specified changeTolerance</summary>
  18. public event ChangeHandler onChange
  19. { add { sourceMap[SteamVR_Input_Sources.Any].onChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onChange -= value; } }
  20. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> This event fires whenever the action is updated</summary>
  21. public event UpdateHandler onUpdate
  22. { add { sourceMap[SteamVR_Input_Sources.Any].onUpdate += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onUpdate -= value; } }
  23. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> This event will fire whenever the float value of the action is non-zero</summary>
  24. public event AxisHandler onAxis
  25. { add { sourceMap[SteamVR_Input_Sources.Any].onAxis += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onAxis -= value; } }
  26. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> This event fires when the active state (ActionSet active and binding active) changes</summary>
  27. public event ActiveChangeHandler onActiveChange
  28. { add { sourceMap[SteamVR_Input_Sources.Any].onActiveChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveChange -= value; } }
  29. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> This event fires when the active state of the binding changes</summary>
  30. public event ActiveChangeHandler onActiveBindingChange
  31. { add { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange -= value; } }
  32. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The current float value of the action.
  33. /// Note: Will only return non-zero if the action is also active.</summary>
  34. public float axis { get { return sourceMap[SteamVR_Input_Sources.Any].axis; } }
  35. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The float value of the action from the previous update.
  36. /// Note: Will only return non-zero if the action is also active.</summary>
  37. public float lastAxis { get { return sourceMap[SteamVR_Input_Sources.Any].lastAxis; } }
  38. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The float value difference between this update and the previous update.
  39. /// Note: Will only return non-zero if the action is also active.</summary>
  40. public float delta { get { return sourceMap[SteamVR_Input_Sources.Any].delta; } }
  41. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The float value difference between the previous update and update before that.
  42. /// Note: Will only return non-zero if the action is also active.</summary>
  43. public float lastDelta { get { return sourceMap[SteamVR_Input_Sources.Any].lastDelta; } }
  44. public SteamVR_Action_Single() { }
  45. /// <summary>The current float value of the action</summary>
  46. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  47. public float GetAxis(SteamVR_Input_Sources inputSource)
  48. {
  49. return sourceMap[inputSource].axis;
  50. }
  51. /// <summary>The float value difference between this update and the previous update.</summary>
  52. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  53. public float GetAxisDelta(SteamVR_Input_Sources inputSource)
  54. {
  55. return sourceMap[inputSource].delta;
  56. }
  57. /// <summary>The float value of the action from the previous update.</summary>
  58. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  59. public float GetLastAxis(SteamVR_Input_Sources inputSource)
  60. {
  61. return sourceMap[inputSource].lastAxis;
  62. }
  63. /// <summary>The float value difference between the previous update and update before that. </summary>
  64. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  65. public float GetLastAxisDelta(SteamVR_Input_Sources inputSource)
  66. {
  67. return sourceMap[inputSource].lastDelta;
  68. }
  69. /// <summary>Executes a function when the *functional* active state of this action (with the specified inputSource) changes.
  70. /// This happens when the action is bound or unbound, or when the ActionSet changes state.</summary>
  71. /// <param name="functionToCall">A local function that receives the boolean action who's active state changes and the corresponding input source</param>
  72. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  73. public void AddOnActiveChangeListener(ActiveChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
  74. {
  75. sourceMap[inputSource].onActiveChange += functionToCall;
  76. }
  77. /// <summary>Stops executing a function when the *functional* active state of this action (with the specified inputSource) changes.
  78. /// This happens when the action is bound or unbound, or when the ActionSet changes state.</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 RemoveOnActiveChangeListener(ActiveChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  82. {
  83. sourceMap[inputSource].onActiveChange -= functionToStopCalling;
  84. }
  85. /// <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>
  86. /// <param name="functionToCall">A local function that receives the boolean action who's active state changes and the corresponding input source</param>
  87. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  88. public void AddOnActiveBindingChangeListener(ActiveChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
  89. {
  90. sourceMap[inputSource].onActiveBindingChange += functionToCall;
  91. }
  92. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  93. /// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
  94. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  95. public void RemoveOnActiveBindingChangeListener(ActiveChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  96. {
  97. sourceMap[inputSource].onActiveBindingChange -= functionToStopCalling;
  98. }
  99. /// <summary>Executes a function when the axis changes by more than the specified changeTolerance</summary>
  100. /// <param name="functionToCall">A local function that receives the boolean action who's state has changed, the corresponding input source, and the new value</param>
  101. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  102. public void AddOnChangeListener(ChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
  103. {
  104. sourceMap[inputSource].onChange += functionToCall;
  105. }
  106. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  107. /// <param name="functionToStopCalling">The local function that you've setup to receive on change events</param>
  108. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  109. public void RemoveOnChangeListener(ChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  110. {
  111. sourceMap[inputSource].onChange -= functionToStopCalling;
  112. }
  113. /// <summary>Executes a function when the state of this action (with the specified inputSource) is updated.</summary>
  114. /// <param name="functionToCall">A local function that receives the boolean action who's state has changed, the corresponding input source, and the new value</param>
  115. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  116. public void AddOnUpdateListener(UpdateHandler functionToCall, SteamVR_Input_Sources inputSource)
  117. {
  118. sourceMap[inputSource].onUpdate += functionToCall;
  119. }
  120. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  121. /// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
  122. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  123. public void RemoveOnUpdateListener(UpdateHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  124. {
  125. sourceMap[inputSource].onUpdate -= functionToStopCalling;
  126. }
  127. /// <summary>Executes a function when the float value of the action is non-zero.</summary>
  128. /// <param name="functionToCall">A local function that receives the boolean action who's state has changed, the corresponding input source, and the new value</param>
  129. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  130. public void AddOnAxisListener(AxisHandler functionToCall, SteamVR_Input_Sources inputSource)
  131. {
  132. sourceMap[inputSource].onAxis += functionToCall;
  133. }
  134. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  135. /// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
  136. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  137. public void RemoveOnAxisListener(AxisHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  138. {
  139. sourceMap[inputSource].onAxis -= functionToStopCalling;
  140. }
  141. public void RemoveAllListeners(SteamVR_Input_Sources inputSource)
  142. {
  143. sourceMap[inputSource].RemoveAllListeners();
  144. }
  145. void ISerializationCallbackReceiver.OnBeforeSerialize()
  146. {
  147. }
  148. void ISerializationCallbackReceiver.OnAfterDeserialize()
  149. {
  150. InitAfterDeserialize();
  151. }
  152. }
  153. public class SteamVR_Action_Single_Source_Map : SteamVR_Action_In_Source_Map<SteamVR_Action_Single_Source>
  154. {
  155. }
  156. public class SteamVR_Action_Single_Source : SteamVR_Action_In_Source, ISteamVR_Action_Single
  157. {
  158. protected static uint actionData_size = 0;
  159. /// <summary>The amount the axis needs to change before a change is detected</summary>
  160. public float changeTolerance = Mathf.Epsilon;
  161. /// <summary>Event fires when the value of the action is non-zero</summary>
  162. public event SteamVR_Action_Single.AxisHandler onAxis;
  163. /// <summary>Event fires when the active state (ActionSet active and binding active) changes</summary>
  164. public event SteamVR_Action_Single.ActiveChangeHandler onActiveChange;
  165. /// <summary>Event fires when the active state of the binding changes</summary>
  166. public event SteamVR_Action_Single.ActiveChangeHandler onActiveBindingChange;
  167. /// <summary>This event fires whenever the axis changes by more than the specified changeTolerance</summary>
  168. public event SteamVR_Action_Single.ChangeHandler onChange;
  169. /// <summary>Event fires when the action is updated</summary>
  170. public event SteamVR_Action_Single.UpdateHandler onUpdate;
  171. /// <summary>The current float value of the action.
  172. /// Note: Will only return non-zero if the action is also active.</summary>
  173. public float axis { get { if (active) return actionData.x; else return 0; } }
  174. /// <summary>The float value of the action from the previous update.
  175. /// Note: Will only return non-zero if the action is also active.</summary>
  176. public float lastAxis { get { if (active) return lastActionData.x; else return 0; } }
  177. /// <summary>The float value difference between this update and the previous update.
  178. /// Note: Will only return non-zero if the action is also active.</summary>
  179. public float delta { get { if (active) return actionData.deltaX; else return 0; } }
  180. /// <summary>The float value difference between the previous update and update before that.
  181. /// Note: Will only return non-zero if the action is also active.</summary>
  182. public float lastDelta { get { if (active) return lastActionData.deltaX; else return 0; } }
  183. /// <summary>If the float value of this action has changed more than the changeTolerance since the last update</summary>
  184. public override bool changed { get; protected set; }
  185. /// <summary>If the float value of this action has changed more than the changeTolerance between the previous update and the update before that</summary>
  186. public override bool lastChanged { get; protected set; }
  187. /// <summary>The handle to the origin of the component that was used to update the value for this action</summary>
  188. public override ulong activeOrigin
  189. {
  190. get
  191. {
  192. if (active)
  193. return actionData.activeOrigin;
  194. return 0;
  195. }
  196. }
  197. /// <summary>The handle to the origin of the component that was used to update the value for this action (for the previous update)</summary>
  198. public override ulong lastActiveOrigin { get { return lastActionData.activeOrigin; } }
  199. /// <summary>Returns true if this action is bound and the ActionSet is active</summary>
  200. public override bool active { get { return activeBinding && action.actionSet.IsActive(inputSource); } }
  201. /// <summary>Returns true if the action is bound</summary>
  202. public override bool activeBinding { get { return actionData.bActive; } }
  203. /// <summary>Returns true if the action was bound and the ActionSet was active during the previous update</summary>
  204. public override bool lastActive { get; protected set; }
  205. /// <summary>Returns true if the action was bound during the previous update</summary>
  206. public override bool lastActiveBinding { get { return lastActionData.bActive; } }
  207. protected InputAnalogActionData_t actionData = new InputAnalogActionData_t();
  208. protected InputAnalogActionData_t lastActionData = new InputAnalogActionData_t();
  209. protected SteamVR_Action_Single singleAction;
  210. /// <summary>
  211. /// <strong>[Should not be called by user code]</strong> Sets up the internals of the action source before SteamVR has been initialized.
  212. /// </summary>
  213. public override void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource)
  214. {
  215. base.Preinitialize(wrappingAction, forInputSource);
  216. singleAction = (SteamVR_Action_Single)wrappingAction;
  217. }
  218. /// <summary>
  219. /// <strong>[Should not be called by user code]</strong>
  220. /// Initializes the handle for the inputSource, the action data size, and any other related SteamVR data.
  221. /// </summary>
  222. public override void Initialize()
  223. {
  224. base.Initialize();
  225. if (actionData_size == 0)
  226. actionData_size = (uint)Marshal.SizeOf(typeof(InputAnalogActionData_t));
  227. }
  228. /// <summary>
  229. /// Removes all listeners, useful for dispose pattern
  230. /// </summary>
  231. public void RemoveAllListeners()
  232. {
  233. Delegate[] delegates;
  234. if (onAxis != null)
  235. {
  236. delegates = onAxis.GetInvocationList();
  237. if (delegates != null)
  238. foreach (Delegate existingDelegate in delegates)
  239. onAxis -= (SteamVR_Action_Single.AxisHandler)existingDelegate;
  240. }
  241. if (onUpdate != null)
  242. {
  243. delegates = onUpdate.GetInvocationList();
  244. if (delegates != null)
  245. foreach (Delegate existingDelegate in delegates)
  246. onUpdate -= (SteamVR_Action_Single.UpdateHandler)existingDelegate;
  247. }
  248. if (onChange != null)
  249. {
  250. delegates = onChange.GetInvocationList();
  251. if (delegates != null)
  252. foreach (Delegate existingDelegate in delegates)
  253. onChange -= (SteamVR_Action_Single.ChangeHandler)existingDelegate;
  254. }
  255. }
  256. /// <summary><strong>[Should not be called by user code]</strong>
  257. /// Updates the data for this action and this input source. Sends related events.
  258. /// </summary>
  259. public override void UpdateValue()
  260. {
  261. lastActionData = actionData;
  262. lastActive = active;
  263. EVRInputError err = OpenVR.Input.GetAnalogActionData(handle, ref actionData, actionData_size, SteamVR_Input_Source.GetHandle(inputSource));
  264. if (err != EVRInputError.None)
  265. Debug.LogError("<b>[SteamVR]</b> GetAnalogActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString());
  266. updateTime = Time.realtimeSinceStartup;
  267. changed = false;
  268. if (active)
  269. {
  270. if (delta > changeTolerance || delta < -changeTolerance)
  271. {
  272. changed = true;
  273. changedTime = Time.realtimeSinceStartup + actionData.fUpdateTime; //fUpdateTime is the time from the time the action was called that the action changed
  274. if (onChange != null)
  275. onChange.Invoke(singleAction, inputSource, axis, delta);
  276. }
  277. if (axis != 0)
  278. {
  279. if (onAxis != null)
  280. onAxis.Invoke(singleAction, inputSource, axis, delta);
  281. }
  282. if (onUpdate != null)
  283. {
  284. onUpdate.Invoke(singleAction, inputSource, axis, delta);
  285. }
  286. }
  287. if (onActiveBindingChange != null && lastActiveBinding != activeBinding)
  288. onActiveBindingChange.Invoke(singleAction, inputSource, activeBinding);
  289. if (onActiveChange != null && lastActive != active)
  290. onActiveChange.Invoke(singleAction, inputSource, activeBinding);
  291. }
  292. }
  293. public interface ISteamVR_Action_Single : ISteamVR_Action_In_Source
  294. {
  295. /// <summary>The current float value of the action.
  296. /// Note: Will only return non-zero if the action is also active.</summary>
  297. float axis { get; }
  298. /// <summary>The float value of the action from the previous update.
  299. /// Note: Will only return non-zero if the action is also active.</summary>
  300. float lastAxis { get; }
  301. /// <summary>The float value difference between this update and the previous update.
  302. /// Note: Will only return non-zero if the action is also active.</summary>
  303. float delta { get; }
  304. /// <summary>The float value difference between the previous update and update before that.
  305. /// Note: Will only return non-zero if the action is also active.</summary>
  306. float lastDelta { get; }
  307. }
  308. }