1
0

SteamVR_Action_Vector3.cs 20 KB

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