SteamVR_Action_Boolean.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. /// Boolean actions are either true or false. There are a variety of helper events included that will fire for the given input source. They're prefixed with "on".
  13. /// </summary>
  14. public class SteamVR_Action_Boolean : SteamVR_Action_In<SteamVR_Action_Boolean_Source_Map, SteamVR_Action_Boolean_Source>, ISteamVR_Action_Boolean, ISerializationCallbackReceiver
  15. {
  16. public delegate void StateDownHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource);
  17. public delegate void StateUpHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource);
  18. public delegate void StateHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource);
  19. public delegate void ActiveChangeHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource, bool active);
  20. public delegate void ChangeHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource, bool newState);
  21. public delegate void UpdateHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource, bool newState);
  22. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> This event fires whenever a state changes from false to true or true to false</summary>
  23. public event ChangeHandler onChange
  24. { add { sourceMap[SteamVR_Input_Sources.Any].onChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onChange -= value; } }
  25. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> This event fires whenever the action is updated</summary>
  26. public event UpdateHandler onUpdate
  27. { add { sourceMap[SteamVR_Input_Sources.Any].onUpdate += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onUpdate -= value; } }
  28. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> This event fires whenever the boolean action is true and gets updated</summary>
  29. public event StateHandler onState
  30. { add { sourceMap[SteamVR_Input_Sources.Any].onState += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onState -= value; } }
  31. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> This event fires whenever the state of the boolean action has changed from false to true in the most recent update</summary>
  32. public event StateDownHandler onStateDown
  33. { add { sourceMap[SteamVR_Input_Sources.Any].onStateDown += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onStateDown -= value; } }
  34. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> This event fires whenever the state of the boolean action has changed from true to false in the most recent update</summary>
  35. public event StateUpHandler onStateUp
  36. { add { sourceMap[SteamVR_Input_Sources.Any].onStateUp += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onStateUp -= value; } }
  37. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Event fires when the active state (ActionSet active and binding active) changes</summary>
  38. public event ActiveChangeHandler onActiveChange
  39. { add { sourceMap[SteamVR_Input_Sources.Any].onActiveChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveChange -= value; } }
  40. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Event fires when the bound state of the binding changes</summary>
  41. public event ActiveChangeHandler onActiveBindingChange
  42. { add { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange -= value; } }
  43. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> True when the boolean action is true</summary>
  44. public bool state { get { return sourceMap[SteamVR_Input_Sources.Any].state; } }
  45. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> True when the boolean action is true and the last state was false</summary>
  46. public bool stateDown { get { return sourceMap[SteamVR_Input_Sources.Any].stateDown; } }
  47. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> True when the boolean action is false and the last state was true</summary>
  48. public bool stateUp { get { return sourceMap[SteamVR_Input_Sources.Any].stateUp; } }
  49. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> (previous update) True when the boolean action is true</summary>
  50. public bool lastState { get { return sourceMap[SteamVR_Input_Sources.Any].lastState; } }
  51. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> (previous update) True when the boolean action is true and the last state was false</summary>
  52. public bool lastStateDown { get { return sourceMap[SteamVR_Input_Sources.Any].lastStateDown; } }
  53. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> (previous update) True when the boolean action is false and the last state was true</summary>
  54. public bool lastStateUp { get { return sourceMap[SteamVR_Input_Sources.Any].lastStateUp; } }
  55. public SteamVR_Action_Boolean() { }
  56. /// <summary>Returns true if the value of the action has been changed to true (from false) in the most recent update.</summary>
  57. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  58. public bool GetStateDown(SteamVR_Input_Sources inputSource)
  59. {
  60. return sourceMap[inputSource].stateDown;
  61. }
  62. /// <summary>Returns true if the value of the action has been changed to false (from true) in the most recent update.</summary>
  63. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  64. public bool GetStateUp(SteamVR_Input_Sources inputSource)
  65. {
  66. return sourceMap[inputSource].stateUp;
  67. }
  68. /// <summary>Returns true if the value of the action (state) is currently true</summary>
  69. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  70. public bool GetState(SteamVR_Input_Sources inputSource)
  71. {
  72. return sourceMap[inputSource].state;
  73. }
  74. /// <summary>[For the previous update] Returns true if the value of the action has been set to true (from false).</summary>
  75. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  76. public bool GetLastStateDown(SteamVR_Input_Sources inputSource)
  77. {
  78. return sourceMap[inputSource].lastStateDown;
  79. }
  80. /// <summary>[For the previous update] Returns true if the value of the action has been set to false (from true).</summary>
  81. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  82. public bool GetLastStateUp(SteamVR_Input_Sources inputSource)
  83. {
  84. return sourceMap[inputSource].lastStateUp;
  85. }
  86. /// <summary>[For the previous update] Returns true if the value of the action was true.</summary>
  87. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  88. public bool GetLastState(SteamVR_Input_Sources inputSource)
  89. {
  90. return sourceMap[inputSource].lastState;
  91. }
  92. /// <summary>Executes a function when the *functional* active state of this action (with the specified inputSource) changes.
  93. /// This happens when the action is bound or unbound, or when the ActionSet changes state.</summary>
  94. /// <param name="functionToCall">A local function that receives the boolean action who's active state changes and the corresponding input source</param>
  95. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  96. public void AddOnActiveChangeListener(ActiveChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
  97. {
  98. sourceMap[inputSource].onActiveChange += functionToCall;
  99. }
  100. /// <summary>Stops executing a function when the *functional* active state of this action (with the specified inputSource) changes.
  101. /// This happens when the action is bound or unbound, or when the ActionSet changes state.</summary>
  102. /// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
  103. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  104. public void RemoveOnActiveChangeListener(ActiveChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  105. {
  106. sourceMap[inputSource].onActiveChange -= functionToStopCalling;
  107. }
  108. /// <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>
  109. /// <param name="functionToCall">A local function that receives the boolean action who's active state changes and the corresponding input source</param>
  110. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  111. public void AddOnActiveBindingChangeListener(ActiveChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
  112. {
  113. sourceMap[inputSource].onActiveBindingChange += functionToCall;
  114. }
  115. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  116. /// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
  117. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  118. public void RemoveOnActiveBindingChangeListener(ActiveChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  119. {
  120. sourceMap[inputSource].onActiveBindingChange -= functionToStopCalling;
  121. }
  122. /// <summary>Executes a function when the state of this action (with the specified inputSource) changes</summary>
  123. /// <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>
  124. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  125. public void AddOnChangeListener(ChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
  126. {
  127. sourceMap[inputSource].onChange += functionToCall;
  128. }
  129. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  130. /// <param name="functionToStopCalling">The local function that you've setup to receive on change events</param>
  131. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  132. public void RemoveOnChangeListener(ChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  133. {
  134. sourceMap[inputSource].onChange -= functionToStopCalling;
  135. }
  136. /// <summary>Executes a function when the state of this action (with the specified inputSource) is updated.</summary>
  137. /// <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>
  138. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  139. public void AddOnUpdateListener(UpdateHandler functionToCall, SteamVR_Input_Sources inputSource)
  140. {
  141. sourceMap[inputSource].onUpdate += functionToCall;
  142. }
  143. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  144. /// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
  145. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  146. public void RemoveOnUpdateListener(UpdateHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  147. {
  148. sourceMap[inputSource].onUpdate -= functionToStopCalling;
  149. }
  150. /// <summary>Executes a function when the state of this action (with the specified inputSource) changes to true (from false).</summary>
  151. /// <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>
  152. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  153. public void AddOnStateDownListener(StateDownHandler functionToCall, SteamVR_Input_Sources inputSource)
  154. {
  155. sourceMap[inputSource].onStateDown += functionToCall;
  156. }
  157. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  158. /// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
  159. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  160. public void RemoveOnStateDownListener(StateDownHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  161. {
  162. sourceMap[inputSource].onStateDown -= functionToStopCalling;
  163. }
  164. /// <summary>Executes a function when the state of this action (with the specified inputSource) changes to false (from true).</summary>
  165. /// <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>
  166. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  167. public void AddOnStateUpListener(StateUpHandler functionToCall, SteamVR_Input_Sources inputSource)
  168. {
  169. sourceMap[inputSource].onStateUp += functionToCall;
  170. }
  171. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  172. /// <param name="functionToStopCalling">The local function that you've setup to receive events</param>
  173. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  174. public void RemoveOnStateUpListener(StateUpHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
  175. {
  176. sourceMap[inputSource].onStateUp -= functionToStopCalling;
  177. }
  178. /// <summary>
  179. /// Remove all listeners registered in the source, useful for Dispose pattern
  180. /// </summary>
  181. public void RemoveAllListeners(SteamVR_Input_Sources input_Sources)
  182. {
  183. sourceMap[input_Sources].RemoveAllListeners();
  184. }
  185. void ISerializationCallbackReceiver.OnBeforeSerialize()
  186. {
  187. }
  188. void ISerializationCallbackReceiver.OnAfterDeserialize()
  189. {
  190. InitAfterDeserialize();
  191. }
  192. }
  193. public class SteamVR_Action_Boolean_Source_Map : SteamVR_Action_In_Source_Map<SteamVR_Action_Boolean_Source>
  194. {
  195. }
  196. public class SteamVR_Action_Boolean_Source : SteamVR_Action_In_Source, ISteamVR_Action_Boolean
  197. {
  198. protected static uint actionData_size = 0;
  199. /// <summary>Event fires when the state of the action changes from false to true</summary>
  200. public event SteamVR_Action_Boolean.StateDownHandler onStateDown;
  201. /// <summary>Event fires when the state of the action changes from true to false</summary>
  202. public event SteamVR_Action_Boolean.StateUpHandler onStateUp;
  203. /// <summary>Event fires when the state of the action is true and the action gets updated</summary>
  204. public event SteamVR_Action_Boolean.StateHandler onState;
  205. /// <summary>Event fires when the active state (ActionSet active and binding active) changes</summary>
  206. public event SteamVR_Action_Boolean.ActiveChangeHandler onActiveChange;
  207. /// <summary>Event fires when the active state of the binding changes</summary>
  208. public event SteamVR_Action_Boolean.ActiveChangeHandler onActiveBindingChange;
  209. /// <summary>Event fires when the state of the action changes from false to true or true to false</summary>
  210. public event SteamVR_Action_Boolean.ChangeHandler onChange;
  211. /// <summary>Event fires when the action is updated</summary>
  212. public event SteamVR_Action_Boolean.UpdateHandler onUpdate;
  213. /// <summary>The current value of the boolean action. Note: Will only return true if the action is also active.</summary>
  214. public bool state { get { return active && actionData.bState; } }
  215. /// <summary>True when the action's state changes from false to true. Note: Will only return true if the action is also active.</summary>
  216. /// <remarks>Will only return true if the action is also active.</remarks>
  217. public bool stateDown { get { return active && actionData.bState && actionData.bChanged; } }
  218. /// <summary>True when the action's state changes from true to false. Note: Will only return true if the action is also active.</summary>
  219. /// <remarks>Will only return true if the action is also active.</remarks>
  220. public bool stateUp { get { return active && actionData.bState == false && actionData.bChanged; } }
  221. /// <summary>True when the action's state changed during the most recent update. Note: Will only return true if the action is also active.</summary>
  222. /// <remarks>ActionSet is ignored since get is coming from the native struct.</remarks>
  223. public override bool changed { get { return active && actionData.bChanged; } protected set { } }
  224. /// <summary>The value of the action's 'state' during the previous update</summary>
  225. /// <remarks>Always returns the previous update state</remarks>
  226. public bool lastState { get { return lastActionData.bState; } }
  227. /// <summary>The value of the action's 'stateDown' during the previous update</summary>
  228. /// <remarks>Always returns the previous update state</remarks>
  229. public bool lastStateDown { get { return lastActionData.bState && lastActionData.bChanged; } }
  230. /// <summary>The value of the action's 'stateUp' during the previous update</summary>
  231. /// <remarks>Always returns the previous update state</remarks>
  232. public bool lastStateUp { get { return lastActionData.bState == false && lastActionData.bChanged; } }
  233. /// <summary>The value of the action's 'changed' during the previous update</summary>
  234. /// <remarks>Always returns the previous update state. Set is ignored since get is coming from the native struct.</remarks>
  235. public override bool lastChanged { get { return lastActionData.bChanged; } protected set { } }
  236. /// <summary>The handle to the origin of the component that was used to update the value for this action</summary>
  237. public override ulong activeOrigin
  238. {
  239. get
  240. {
  241. if (active)
  242. return actionData.activeOrigin;
  243. return 0;
  244. }
  245. }
  246. /// <summary>The handle to the origin of the component that was used to update the value for this action (for the previous update)</summary>
  247. public override ulong lastActiveOrigin { get { return lastActionData.activeOrigin; } }
  248. /// <summary>Returns true if this action is bound and the ActionSet is active</summary>
  249. public override bool active { get { return activeBinding && action.actionSet.IsActive(inputSource); } }
  250. /// <summary>Returns true if the action is bound</summary>
  251. public override bool activeBinding { get { return actionData.bActive; } }
  252. /// <summary>Returns true if the action was bound and the ActionSet was active during the previous update</summary>
  253. public override bool lastActive { get; protected set; }
  254. /// <summary>Returns true if the action was bound during the previous update</summary>
  255. public override bool lastActiveBinding { get { return lastActionData.bActive; } }
  256. protected InputDigitalActionData_t actionData = new InputDigitalActionData_t();
  257. protected InputDigitalActionData_t lastActionData = new InputDigitalActionData_t();
  258. protected SteamVR_Action_Boolean booleanAction;
  259. /// <summary>
  260. /// <strong>[Should not be called by user code]</strong> Sets up the internals of the action source before SteamVR has been initialized.
  261. /// </summary>
  262. public override void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource)
  263. {
  264. base.Preinitialize(wrappingAction, forInputSource);
  265. booleanAction = (SteamVR_Action_Boolean)wrappingAction;
  266. }
  267. /// <summary>
  268. /// <strong>[Should not be called by user code]</strong>
  269. /// Initializes the handle for the inputSource, the action data size, and any other related SteamVR data.
  270. /// </summary>
  271. public override void Initialize()
  272. {
  273. base.Initialize();
  274. if (actionData_size == 0)
  275. actionData_size = (uint)Marshal.SizeOf(typeof(InputDigitalActionData_t));
  276. }
  277. /// <summary>
  278. /// Remove all listeners, useful for Dispose pattern
  279. /// </summary>
  280. public void RemoveAllListeners()
  281. {
  282. Delegate[] delegates;
  283. if (onStateDown != null)
  284. {
  285. delegates = onStateDown.GetInvocationList();
  286. if (delegates != null)
  287. foreach (Delegate existingDelegate in delegates)
  288. onStateDown -= (SteamVR_Action_Boolean.StateDownHandler)existingDelegate;
  289. }
  290. if (onStateUp != null)
  291. {
  292. delegates = onStateUp.GetInvocationList();
  293. if (delegates != null)
  294. foreach (Delegate existingDelegate in delegates)
  295. onStateUp -= (SteamVR_Action_Boolean.StateUpHandler)existingDelegate;
  296. }
  297. if (onState != null)
  298. {
  299. delegates = onState.GetInvocationList();
  300. if (delegates != null)
  301. foreach (Delegate existingDelegate in delegates)
  302. onState -= (SteamVR_Action_Boolean.StateHandler)existingDelegate;
  303. }
  304. }
  305. /// <summary><strong>[Should not be called by user code]</strong>
  306. /// Updates the data for this action and this input source. Sends related events.
  307. /// </summary>
  308. public override void UpdateValue()
  309. {
  310. lastActionData = actionData;
  311. lastActive = active;
  312. EVRInputError err = OpenVR.Input.GetDigitalActionData(action.handle, ref actionData, actionData_size, inputSourceHandle);
  313. if (err != EVRInputError.None)
  314. Debug.LogError("<b>[SteamVR]</b> GetDigitalActionData error (" + action.fullPath + "): " + err.ToString() + " handle: " + action.handle.ToString());
  315. if (changed)
  316. changedTime = Time.realtimeSinceStartup + actionData.fUpdateTime;
  317. updateTime = Time.realtimeSinceStartup;
  318. if (active)
  319. {
  320. if (onStateDown != null && stateDown)
  321. onStateDown.Invoke(booleanAction, inputSource);
  322. if (onStateUp != null && stateUp)
  323. onStateUp.Invoke(booleanAction, inputSource);
  324. if (onState != null && state)
  325. onState.Invoke(booleanAction, inputSource);
  326. if (onChange != null && changed)
  327. onChange.Invoke(booleanAction, inputSource, state);
  328. if (onUpdate != null)
  329. onUpdate.Invoke(booleanAction, inputSource, state);
  330. }
  331. if (onActiveBindingChange != null && lastActiveBinding != activeBinding)
  332. onActiveBindingChange.Invoke(booleanAction, inputSource, activeBinding);
  333. if (onActiveChange != null && lastActive != active)
  334. onActiveChange.Invoke(booleanAction, inputSource, activeBinding);
  335. }
  336. }
  337. public interface ISteamVR_Action_Boolean : ISteamVR_Action_In_Source
  338. {
  339. /// <summary>The current value of the boolean action. Note: Will only return true if the action is also active.</summary>
  340. bool state { get; }
  341. /// <summary>True when the action's state changes from false to true. Note: Will only return true if the action is also active.</summary>
  342. bool stateDown { get; }
  343. /// <summary>True when the action's state changes from true to false. Note: Will only return true if the action is also active.</summary>
  344. bool stateUp { get; }
  345. /// <summary>The value of the action's 'state' during the previous update</summary>
  346. /// <remarks>Always returns the previous update state</remarks>
  347. bool lastState { get; }
  348. /// <summary>The value of the action's 'stateDown' during the previous update</summary>
  349. /// <remarks>Always returns the previous update state</remarks>
  350. bool lastStateDown { get; }
  351. /// <summary>The value of the action's 'stateUp' during the previous update</summary>
  352. /// <remarks>Always returns the previous update state</remarks>
  353. bool lastStateUp { get; }
  354. }
  355. }