SteamVR_Action_Boolean.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. void ISerializationCallbackReceiver.OnBeforeSerialize()
  179. {
  180. }
  181. void ISerializationCallbackReceiver.OnAfterDeserialize()
  182. {
  183. InitAfterDeserialize();
  184. }
  185. }
  186. public class SteamVR_Action_Boolean_Source_Map : SteamVR_Action_In_Source_Map<SteamVR_Action_Boolean_Source>
  187. {
  188. }
  189. public class SteamVR_Action_Boolean_Source : SteamVR_Action_In_Source, ISteamVR_Action_Boolean
  190. {
  191. protected static uint actionData_size = 0;
  192. /// <summary>Event fires when the state of the action changes from false to true</summary>
  193. public event SteamVR_Action_Boolean.StateDownHandler onStateDown;
  194. /// <summary>Event fires when the state of the action changes from true to false</summary>
  195. public event SteamVR_Action_Boolean.StateUpHandler onStateUp;
  196. /// <summary>Event fires when the state of the action is true and the action gets updated</summary>
  197. public event SteamVR_Action_Boolean.StateHandler onState;
  198. /// <summary>Event fires when the active state (ActionSet active and binding active) changes</summary>
  199. public event SteamVR_Action_Boolean.ActiveChangeHandler onActiveChange;
  200. /// <summary>Event fires when the active state of the binding changes</summary>
  201. public event SteamVR_Action_Boolean.ActiveChangeHandler onActiveBindingChange;
  202. /// <summary>Event fires when the state of the action changes from false to true or true to false</summary>
  203. public event SteamVR_Action_Boolean.ChangeHandler onChange;
  204. /// <summary>Event fires when the action is updated</summary>
  205. public event SteamVR_Action_Boolean.UpdateHandler onUpdate;
  206. /// <summary>The current value of the boolean action. Note: Will only return true if the action is also active.</summary>
  207. public bool state { get { return active && actionData.bState; } }
  208. /// <summary>True when the action's state changes from false to true. Note: Will only return true if the action is also active.</summary>
  209. /// <remarks>Will only return true if the action is also active.</remarks>
  210. public bool stateDown { get { return active && actionData.bState && actionData.bChanged; } }
  211. /// <summary>True when the action's state changes from true to false. Note: Will only return true if the action is also active.</summary>
  212. /// <remarks>Will only return true if the action is also active.</remarks>
  213. public bool stateUp { get { return active && actionData.bState == false && actionData.bChanged; } }
  214. /// <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>
  215. /// <remarks>ActionSet is ignored since get is coming from the native struct.</remarks>
  216. public override bool changed { get { return active && actionData.bChanged; } protected set { } }
  217. /// <summary>The value of the action's 'state' during the previous update</summary>
  218. /// <remarks>Always returns the previous update state</remarks>
  219. public bool lastState { get { return lastActionData.bState; } }
  220. /// <summary>The value of the action's 'stateDown' during the previous update</summary>
  221. /// <remarks>Always returns the previous update state</remarks>
  222. public bool lastStateDown { get { return lastActionData.bState && lastActionData.bChanged; } }
  223. /// <summary>The value of the action's 'stateUp' during the previous update</summary>
  224. /// <remarks>Always returns the previous update state</remarks>
  225. public bool lastStateUp { get { return lastActionData.bState == false && lastActionData.bChanged; } }
  226. /// <summary>The value of the action's 'changed' during the previous update</summary>
  227. /// <remarks>Always returns the previous update state. Set is ignored since get is coming from the native struct.</remarks>
  228. public override bool lastChanged { get { return lastActionData.bChanged; } protected set { } }
  229. /// <summary>The handle to the origin of the component that was used to update the value for this action</summary>
  230. public override ulong activeOrigin
  231. {
  232. get
  233. {
  234. if (active)
  235. return actionData.activeOrigin;
  236. return 0;
  237. }
  238. }
  239. /// <summary>The handle to the origin of the component that was used to update the value for this action (for the previous update)</summary>
  240. public override ulong lastActiveOrigin { get { return lastActionData.activeOrigin; } }
  241. /// <summary>Returns true if this action is bound and the ActionSet is active</summary>
  242. public override bool active { get { return activeBinding && action.actionSet.IsActive(inputSource); } }
  243. /// <summary>Returns true if the action is bound</summary>
  244. public override bool activeBinding { get { return actionData.bActive; } }
  245. /// <summary>Returns true if the action was bound and the ActionSet was active during the previous update</summary>
  246. public override bool lastActive { get; protected set; }
  247. /// <summary>Returns true if the action was bound during the previous update</summary>
  248. public override bool lastActiveBinding { get { return lastActionData.bActive; } }
  249. protected InputDigitalActionData_t actionData = new InputDigitalActionData_t();
  250. protected InputDigitalActionData_t lastActionData = new InputDigitalActionData_t();
  251. protected SteamVR_Action_Boolean booleanAction;
  252. /// <summary>
  253. /// <strong>[Should not be called by user code]</strong> Sets up the internals of the action source before SteamVR has been initialized.
  254. /// </summary>
  255. public override void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource)
  256. {
  257. base.Preinitialize(wrappingAction, forInputSource);
  258. booleanAction = (SteamVR_Action_Boolean)wrappingAction;
  259. }
  260. /// <summary>
  261. /// <strong>[Should not be called by user code]</strong>
  262. /// Initializes the handle for the inputSource, the action data size, and any other related SteamVR data.
  263. /// </summary>
  264. public override void Initialize()
  265. {
  266. base.Initialize();
  267. if (actionData_size == 0)
  268. actionData_size = (uint)Marshal.SizeOf(typeof(InputDigitalActionData_t));
  269. }
  270. /// <summary><strong>[Should not be called by user code]</strong>
  271. /// Updates the data for this action and this input source. Sends related events.
  272. /// </summary>
  273. public override void UpdateValue()
  274. {
  275. lastActionData = actionData;
  276. lastActive = active;
  277. EVRInputError err = OpenVR.Input.GetDigitalActionData(action.handle, ref actionData, actionData_size, inputSourceHandle);
  278. if (err != EVRInputError.None)
  279. Debug.LogError("<b>[SteamVR]</b> GetDigitalActionData error (" + action.fullPath + "): " + err.ToString() + " handle: " + action.handle.ToString());
  280. if (changed)
  281. changedTime = Time.realtimeSinceStartup + actionData.fUpdateTime;
  282. updateTime = Time.realtimeSinceStartup;
  283. if (active)
  284. {
  285. if (onStateDown != null && stateDown)
  286. onStateDown.Invoke(booleanAction, inputSource);
  287. if (onStateUp != null && stateUp)
  288. onStateUp.Invoke(booleanAction, inputSource);
  289. if (onState != null && state)
  290. onState.Invoke(booleanAction, inputSource);
  291. if (onChange != null && changed)
  292. onChange.Invoke(booleanAction, inputSource, state);
  293. if (onUpdate != null)
  294. onUpdate.Invoke(booleanAction, inputSource, state);
  295. }
  296. if (onActiveBindingChange != null && lastActiveBinding != activeBinding)
  297. onActiveBindingChange.Invoke(booleanAction, inputSource, activeBinding);
  298. if (onActiveChange != null && lastActive != active)
  299. onActiveChange.Invoke(booleanAction, inputSource, activeBinding);
  300. }
  301. }
  302. public interface ISteamVR_Action_Boolean : ISteamVR_Action_In_Source
  303. {
  304. /// <summary>The current value of the boolean action. Note: Will only return true if the action is also active.</summary>
  305. bool state { get; }
  306. /// <summary>True when the action's state changes from false to true. Note: Will only return true if the action is also active.</summary>
  307. bool stateDown { get; }
  308. /// <summary>True when the action's state changes from true to false. Note: Will only return true if the action is also active.</summary>
  309. bool stateUp { get; }
  310. /// <summary>The value of the action's 'state' during the previous update</summary>
  311. /// <remarks>Always returns the previous update state</remarks>
  312. bool lastState { get; }
  313. /// <summary>The value of the action's 'stateDown' during the previous update</summary>
  314. /// <remarks>Always returns the previous update state</remarks>
  315. bool lastStateDown { get; }
  316. /// <summary>The value of the action's 'stateUp' during the previous update</summary>
  317. /// <remarks>Always returns the previous update state</remarks>
  318. bool lastStateUp { get; }
  319. }
  320. }