SteamVR_Action_Single.cs 20 KB

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