SteamVR_Action_Vector2.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 two values generally from -1 to 1. Also provides a delta since the last update.
  13. /// </summary>
  14. public class SteamVR_Action_Vector2 : SteamVR_Action_In<SteamVR_Action_Vector2_Source_Map, SteamVR_Action_Vector2_Source>, ISteamVR_Action_Vector2, ISerializationCallbackReceiver
  15. {
  16. public delegate void AxisHandler(SteamVR_Action_Vector2 fromAction, SteamVR_Input_Sources fromSource, Vector2 axis, Vector2 delta);
  17. public delegate void ActiveChangeHandler(SteamVR_Action_Vector2 fromAction, SteamVR_Input_Sources fromSource, bool active);
  18. public delegate void ChangeHandler(SteamVR_Action_Vector2 fromAction, SteamVR_Input_Sources fromSource, Vector2 axis, Vector2 delta);
  19. public delegate void UpdateHandler(SteamVR_Action_Vector2 fromAction, SteamVR_Input_Sources fromSource, Vector2 axis, Vector2 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 Vector2 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 Vector2 value of the action.
  36. /// Note: Will only return non-zero if the action is also active.</summary>
  37. public Vector2 axis { get { return sourceMap[SteamVR_Input_Sources.Any].axis; } }
  38. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The Vector2 value of the action from the previous update.
  39. /// Note: Will only return non-zero if the action is also active.</summary>
  40. public Vector2 lastAxis { get { return sourceMap[SteamVR_Input_Sources.Any].lastAxis; } }
  41. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The Vector2 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 Vector2 delta { get { return sourceMap[SteamVR_Input_Sources.Any].delta; } }
  44. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The Vector2 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 Vector2 lastDelta { get { return sourceMap[SteamVR_Input_Sources.Any].lastDelta; } }
  47. public SteamVR_Action_Vector2() { }
  48. /// <summary>The current Vector2 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 Vector2 GetAxis(SteamVR_Input_Sources inputSource)
  51. {
  52. return sourceMap[inputSource].axis;
  53. }
  54. /// <summary>The Vector2 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 Vector2 GetAxisDelta(SteamVR_Input_Sources inputSource)
  57. {
  58. return sourceMap[inputSource].delta;
  59. }
  60. /// <summary>The Vector2 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 Vector2 GetLastAxis(SteamVR_Input_Sources inputSource)
  63. {
  64. return sourceMap[inputSource].lastAxis;
  65. }
  66. /// <summary>The Vector2 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 Vector2 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 Vector2 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. /// <summary>
  160. /// Boolean actions are either true or false. There is an onStateUp and onStateDown event for the rising and falling edge.
  161. /// </summary>
  162. public class SteamVR_Action_Vector2_Source_Map : SteamVR_Action_In_Source_Map<SteamVR_Action_Vector2_Source>
  163. {
  164. }
  165. public class SteamVR_Action_Vector2_Source : SteamVR_Action_In_Source, ISteamVR_Action_Vector2
  166. {
  167. protected static uint actionData_size = 0;
  168. /// <summary>The amount the axis needs to change before a change is detected</summary>
  169. public float changeTolerance = Mathf.Epsilon;
  170. /// <summary>Event fires when the value of the action is non-zero</summary>
  171. public event SteamVR_Action_Vector2.AxisHandler onAxis;
  172. /// <summary>Event fires when the active state (ActionSet active and binding active) changes</summary>
  173. public event SteamVR_Action_Vector2.ActiveChangeHandler onActiveChange;
  174. /// <summary>Event fires when the active state of the binding changes</summary>
  175. public event SteamVR_Action_Vector2.ActiveChangeHandler onActiveBindingChange;
  176. /// <summary>This event fires whenever the axis changes by more than the specified changeTolerance</summary>
  177. public event SteamVR_Action_Vector2.ChangeHandler onChange;
  178. /// <summary>Event fires when the action is updated</summary>
  179. public event SteamVR_Action_Vector2.UpdateHandler onUpdate;
  180. /// <summary>The current Vector2 value of the action.
  181. /// Note: Will only return non-zero if the action is also active.</summary>
  182. public Vector2 axis { get; protected set; }
  183. /// <summary>The Vector2 value of the action from the previous update.
  184. /// Note: Will only return non-zero if the action is also active.</summary>
  185. public Vector2 lastAxis { get; protected set; }
  186. /// <summary>The Vector2 value difference between this update and the previous update.
  187. /// Note: Will only return non-zero if the action is also active.</summary>
  188. public Vector2 delta { get; protected set; }
  189. /// <summary>The Vector2 value difference between the previous update and update before that.
  190. /// Note: Will only return non-zero if the action is also active.</summary>
  191. public Vector2 lastDelta { get; protected set; }
  192. /// <summary>If the Vector2 value of this action has changed more than the changeTolerance since the last update</summary>
  193. public override bool changed { get; protected set; }
  194. /// <summary>If the Vector2 value of this action has changed more than the changeTolerance between the previous update and the update before that</summary>
  195. public override bool lastChanged { get; protected set; }
  196. /// <summary>The handle to the origin of the component that was used to update the value for this action</summary>
  197. public override ulong activeOrigin
  198. {
  199. get
  200. {
  201. if (active)
  202. return actionData.activeOrigin;
  203. return 0;
  204. }
  205. }
  206. /// <summary>The handle to the origin of the component that was used to update the value for this action (for the previous update)</summary>
  207. public override ulong lastActiveOrigin { get { return lastActionData.activeOrigin; } }
  208. /// <summary>Returns true if this action is bound and the ActionSet is active</summary>
  209. public override bool active { get { return activeBinding && action.actionSet.IsActive(inputSource); } }
  210. /// <summary>Returns true if the action is bound</summary>
  211. public override bool activeBinding { get { return actionData.bActive; } }
  212. /// <summary>Returns true if the action was bound and the ActionSet was active during the previous update</summary>
  213. public override bool lastActive { get; protected set; }
  214. /// <summary>Returns true if the action was bound during the previous update</summary>
  215. public override bool lastActiveBinding { get { return lastActionData.bActive; } }
  216. protected InputAnalogActionData_t actionData = new InputAnalogActionData_t();
  217. protected InputAnalogActionData_t lastActionData = new InputAnalogActionData_t();
  218. protected SteamVR_Action_Vector2 vector2Action;
  219. /// <summary>
  220. /// <strong>[Should not be called by user code]</strong> Sets up the internals of the action source before SteamVR has been initialized.
  221. /// </summary>
  222. public override void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource)
  223. {
  224. base.Preinitialize(wrappingAction, forInputSource);
  225. vector2Action = (SteamVR_Action_Vector2)wrappingAction;
  226. }
  227. /// <summary>
  228. /// <strong>[Should not be called by user code]</strong>
  229. /// Initializes the handle for the inputSource, the action data size, and any other related SteamVR data.
  230. /// </summary>
  231. public override void Initialize()
  232. {
  233. base.Initialize();
  234. if (actionData_size == 0)
  235. actionData_size = (uint)Marshal.SizeOf(typeof(InputAnalogActionData_t));
  236. }
  237. /// <summary>
  238. /// Removes all listeners, useful for dispose pattern
  239. /// </summary>
  240. public void RemoveAllListeners()
  241. {
  242. Delegate[] delegates;
  243. if (onAxis != null)
  244. {
  245. delegates = onAxis.GetInvocationList();
  246. if (delegates != null)
  247. foreach (Delegate existingDelegate in delegates)
  248. onAxis -= (SteamVR_Action_Vector2.AxisHandler)existingDelegate;
  249. }
  250. if (onUpdate != null)
  251. {
  252. delegates = onUpdate.GetInvocationList();
  253. if (delegates != null)
  254. foreach (Delegate existingDelegate in delegates)
  255. onUpdate -= (SteamVR_Action_Vector2.UpdateHandler)existingDelegate;
  256. }
  257. if (onChange != null)
  258. {
  259. delegates = onChange.GetInvocationList();
  260. if (delegates != null)
  261. foreach (Delegate existingDelegate in delegates)
  262. onChange -= (SteamVR_Action_Vector2.ChangeHandler)existingDelegate;
  263. }
  264. }
  265. /// <summary><strong>[Should not be called by user code]</strong>
  266. /// Updates the data for this action and this input source. Sends related events.
  267. /// </summary>
  268. public override void UpdateValue()
  269. {
  270. lastActionData = actionData;
  271. lastActive = active;
  272. lastAxis = axis;
  273. lastDelta = delta;
  274. EVRInputError err = OpenVR.Input.GetAnalogActionData(handle, ref actionData, actionData_size, SteamVR_Input_Source.GetHandle(inputSource));
  275. if (err != EVRInputError.None)
  276. Debug.LogError("<b>[SteamVR]</b> GetAnalogActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString());
  277. updateTime = Time.realtimeSinceStartup;
  278. axis = new Vector2(actionData.x, actionData.y);
  279. delta = new Vector2(actionData.deltaX, actionData.deltaY);
  280. changed = false;
  281. if (active)
  282. {
  283. if (delta.magnitude > changeTolerance)
  284. {
  285. changed = true;
  286. changedTime = Time.realtimeSinceStartup + actionData.fUpdateTime; //fUpdateTime is the time from the time the action was called that the action changed
  287. if (onChange != null)
  288. onChange.Invoke(vector2Action, inputSource, axis, delta);
  289. }
  290. if (axis != Vector2.zero)
  291. {
  292. if (onAxis != null)
  293. onAxis.Invoke(vector2Action, inputSource, axis, delta);
  294. }
  295. if (onUpdate != null)
  296. {
  297. onUpdate.Invoke(vector2Action, inputSource, axis, delta);
  298. }
  299. }
  300. if (onActiveBindingChange != null && lastActiveBinding != activeBinding)
  301. onActiveBindingChange.Invoke(vector2Action, inputSource, activeBinding);
  302. if (onActiveChange != null && lastActive != active)
  303. onActiveChange.Invoke(vector2Action, inputSource, activeBinding);
  304. }
  305. }
  306. public interface ISteamVR_Action_Vector2 : 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. Vector2 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. Vector2 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. Vector2 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. Vector2 lastDelta { get; }
  320. }
  321. }