SteamVR_Action_Vector2.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. void ISerializationCallbackReceiver.OnBeforeSerialize()
  145. {
  146. }
  147. void ISerializationCallbackReceiver.OnAfterDeserialize()
  148. {
  149. InitAfterDeserialize();
  150. }
  151. }
  152. /// <summary>
  153. /// Boolean actions are either true or false. There is an onStateUp and onStateDown event for the rising and falling edge.
  154. /// </summary>
  155. public class SteamVR_Action_Vector2_Source_Map : SteamVR_Action_In_Source_Map<SteamVR_Action_Vector2_Source>
  156. {
  157. }
  158. public class SteamVR_Action_Vector2_Source : SteamVR_Action_In_Source, ISteamVR_Action_Vector2
  159. {
  160. protected static uint actionData_size = 0;
  161. /// <summary>The amount the axis needs to change before a change is detected</summary>
  162. public float changeTolerance = Mathf.Epsilon;
  163. /// <summary>Event fires when the value of the action is non-zero</summary>
  164. public event SteamVR_Action_Vector2.AxisHandler onAxis;
  165. /// <summary>Event fires when the active state (ActionSet active and binding active) changes</summary>
  166. public event SteamVR_Action_Vector2.ActiveChangeHandler onActiveChange;
  167. /// <summary>Event fires when the active state of the binding changes</summary>
  168. public event SteamVR_Action_Vector2.ActiveChangeHandler onActiveBindingChange;
  169. /// <summary>This event fires whenever the axis changes by more than the specified changeTolerance</summary>
  170. public event SteamVR_Action_Vector2.ChangeHandler onChange;
  171. /// <summary>Event fires when the action is updated</summary>
  172. public event SteamVR_Action_Vector2.UpdateHandler onUpdate;
  173. /// <summary>The current Vector2 value of the action.
  174. /// Note: Will only return non-zero if the action is also active.</summary>
  175. public Vector2 axis { get; protected set; }
  176. /// <summary>The Vector2 value of the action from the previous update.
  177. /// Note: Will only return non-zero if the action is also active.</summary>
  178. public Vector2 lastAxis { get; protected set; }
  179. /// <summary>The Vector2 value difference between this update and the previous update.
  180. /// Note: Will only return non-zero if the action is also active.</summary>
  181. public Vector2 delta { get; protected set; }
  182. /// <summary>The Vector2 value difference between the previous update and update before that.
  183. /// Note: Will only return non-zero if the action is also active.</summary>
  184. public Vector2 lastDelta { get; protected set; }
  185. /// <summary>If the Vector2 value of this action has changed more than the changeTolerance since the last update</summary>
  186. public override bool changed { get; protected set; }
  187. /// <summary>If the Vector2 value of this action has changed more than the changeTolerance between the previous update and the update before that</summary>
  188. public override bool lastChanged { get; protected set; }
  189. /// <summary>The handle to the origin of the component that was used to update the value for this action</summary>
  190. public override ulong activeOrigin
  191. {
  192. get
  193. {
  194. if (active)
  195. return actionData.activeOrigin;
  196. return 0;
  197. }
  198. }
  199. /// <summary>The handle to the origin of the component that was used to update the value for this action (for the previous update)</summary>
  200. public override ulong lastActiveOrigin { get { return lastActionData.activeOrigin; } }
  201. /// <summary>Returns true if this action is bound and the ActionSet is active</summary>
  202. public override bool active { get { return activeBinding && action.actionSet.IsActive(inputSource); } }
  203. /// <summary>Returns true if the action is bound</summary>
  204. public override bool activeBinding { get { return actionData.bActive; } }
  205. /// <summary>Returns true if the action was bound and the ActionSet was active during the previous update</summary>
  206. public override bool lastActive { get; protected set; }
  207. /// <summary>Returns true if the action was bound during the previous update</summary>
  208. public override bool lastActiveBinding { get { return lastActionData.bActive; } }
  209. protected InputAnalogActionData_t actionData = new InputAnalogActionData_t();
  210. protected InputAnalogActionData_t lastActionData = new InputAnalogActionData_t();
  211. protected SteamVR_Action_Vector2 vector2Action;
  212. /// <summary>
  213. /// <strong>[Should not be called by user code]</strong> Sets up the internals of the action source before SteamVR has been initialized.
  214. /// </summary>
  215. public override void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource)
  216. {
  217. base.Preinitialize(wrappingAction, forInputSource);
  218. vector2Action = (SteamVR_Action_Vector2)wrappingAction;
  219. }
  220. /// <summary>
  221. /// <strong>[Should not be called by user code]</strong>
  222. /// Initializes the handle for the inputSource, the action data size, and any other related SteamVR data.
  223. /// </summary>
  224. public override void Initialize()
  225. {
  226. base.Initialize();
  227. if (actionData_size == 0)
  228. actionData_size = (uint)Marshal.SizeOf(typeof(InputAnalogActionData_t));
  229. }
  230. /// <summary><strong>[Should not be called by user code]</strong>
  231. /// Updates the data for this action and this input source. Sends related events.
  232. /// </summary>
  233. public override void UpdateValue()
  234. {
  235. lastActionData = actionData;
  236. lastActive = active;
  237. lastAxis = axis;
  238. lastDelta = delta;
  239. EVRInputError err = OpenVR.Input.GetAnalogActionData(handle, ref actionData, actionData_size, SteamVR_Input_Source.GetHandle(inputSource));
  240. if (err != EVRInputError.None)
  241. Debug.LogError("<b>[SteamVR]</b> GetAnalogActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString());
  242. updateTime = Time.realtimeSinceStartup;
  243. axis = new Vector2(actionData.x, actionData.y);
  244. delta = new Vector2(actionData.deltaX, actionData.deltaY);
  245. changed = false;
  246. if (active)
  247. {
  248. if (delta.magnitude > changeTolerance)
  249. {
  250. changed = true;
  251. changedTime = Time.realtimeSinceStartup + actionData.fUpdateTime; //fUpdateTime is the time from the time the action was called that the action changed
  252. if (onChange != null)
  253. onChange.Invoke(vector2Action, inputSource, axis, delta);
  254. }
  255. if (axis != Vector2.zero)
  256. {
  257. if (onAxis != null)
  258. onAxis.Invoke(vector2Action, inputSource, axis, delta);
  259. }
  260. if (onUpdate != null)
  261. {
  262. onUpdate.Invoke(vector2Action, inputSource, axis, delta);
  263. }
  264. }
  265. if (onActiveBindingChange != null && lastActiveBinding != activeBinding)
  266. onActiveBindingChange.Invoke(vector2Action, inputSource, activeBinding);
  267. if (onActiveChange != null && lastActive != active)
  268. onActiveChange.Invoke(vector2Action, inputSource, activeBinding);
  269. }
  270. }
  271. public interface ISteamVR_Action_Vector2 : ISteamVR_Action_In_Source
  272. {
  273. /// <summary>The current float value of the action.
  274. /// Note: Will only return non-zero if the action is also active.</summary>
  275. Vector2 axis { get; }
  276. /// <summary>The float value of the action from the previous update.
  277. /// Note: Will only return non-zero if the action is also active.</summary>
  278. Vector2 lastAxis { get; }
  279. /// <summary>The float value difference between this update and the previous update.
  280. /// Note: Will only return non-zero if the action is also active.</summary>
  281. Vector2 delta { get; }
  282. /// <summary>The float value difference between the previous update and update before that.
  283. /// Note: Will only return non-zero if the action is also active.</summary>
  284. Vector2 lastDelta { get; }
  285. }
  286. }