2
0

SteamVR_Action_Vector3.cs 21 KB

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