SteamVR_ActionSet.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. using System.Text;
  9. namespace Valve.VR
  10. {
  11. /// <summary>
  12. /// Action sets are logical groupings of actions. Multiple sets can be active at one time.
  13. /// </summary>
  14. [Serializable]
  15. public class SteamVR_ActionSet : IEquatable<SteamVR_ActionSet>, ISteamVR_ActionSet, ISerializationCallbackReceiver
  16. {
  17. public SteamVR_ActionSet() { }
  18. [SerializeField]
  19. private string actionSetPath;
  20. [NonSerialized]
  21. protected SteamVR_ActionSet_Data setData;
  22. /// <summary>All actions within this set (including out actions)</summary>
  23. public SteamVR_Action[] allActions
  24. {
  25. get
  26. {
  27. if (initialized == false)
  28. Initialize();
  29. return setData.allActions;
  30. }
  31. }
  32. /// <summary>All IN actions within this set that are NOT pose or skeleton actions</summary>
  33. public ISteamVR_Action_In[] nonVisualInActions
  34. {
  35. get
  36. {
  37. if (initialized == false)
  38. Initialize();
  39. return setData.nonVisualInActions;
  40. }
  41. }
  42. /// <summary>All pose and skeleton actions within this set</summary>
  43. public ISteamVR_Action_In[] visualActions
  44. {
  45. get
  46. {
  47. if (initialized == false)
  48. Initialize();
  49. return setData.visualActions;
  50. }
  51. }
  52. /// <summary>All pose actions within this set</summary>
  53. public SteamVR_Action_Pose[] poseActions
  54. {
  55. get
  56. {
  57. if (initialized == false)
  58. Initialize();
  59. return setData.poseActions;
  60. }
  61. }
  62. /// <summary>All skeleton actions within this set</summary>
  63. public SteamVR_Action_Skeleton[] skeletonActions
  64. {
  65. get
  66. {
  67. if (initialized == false)
  68. Initialize();
  69. return setData.skeletonActions;
  70. }
  71. }
  72. /// <summary>All out actions within this set</summary>
  73. public ISteamVR_Action_Out[] outActionArray
  74. {
  75. get
  76. {
  77. if (initialized == false)
  78. Initialize();
  79. return setData.outActionArray;
  80. }
  81. }
  82. /// <summary>The full path to this action set (ex: /actions/in/default)</summary>
  83. public string fullPath
  84. {
  85. get
  86. {
  87. if (initialized == false)
  88. Initialize();
  89. return setData.fullPath;
  90. }
  91. }
  92. public string usage
  93. {
  94. get
  95. {
  96. if (initialized == false)
  97. Initialize();
  98. return setData.usage;
  99. }
  100. }
  101. public ulong handle
  102. {
  103. get
  104. {
  105. if (initialized == false)
  106. Initialize();
  107. return setData.handle;
  108. }
  109. }
  110. [NonSerialized]
  111. protected bool initialized = false;
  112. public static CreateType Create<CreateType>(string newSetPath) where CreateType : SteamVR_ActionSet, new()
  113. {
  114. CreateType actionSet = new CreateType();
  115. actionSet.PreInitialize(newSetPath);
  116. return actionSet;
  117. }
  118. public static CreateType CreateFromName<CreateType>(string newSetName) where CreateType : SteamVR_ActionSet, new()
  119. {
  120. CreateType actionSet = new CreateType();
  121. actionSet.PreInitialize(SteamVR_Input_ActionFile_ActionSet.GetPathFromName(newSetName));
  122. return actionSet;
  123. }
  124. public void PreInitialize(string newActionPath)
  125. {
  126. actionSetPath = newActionPath;
  127. setData = new SteamVR_ActionSet_Data();
  128. setData.fullPath = actionSetPath;
  129. setData.PreInitialize();
  130. initialized = true;
  131. }
  132. public virtual void FinishPreInitialize()
  133. {
  134. setData.FinishPreInitialize();
  135. }
  136. /// <summary>
  137. /// Initializes the handle for the action
  138. /// </summary>
  139. public virtual void Initialize(bool createNew = false, bool throwErrors = true)
  140. {
  141. if (createNew)
  142. {
  143. setData.Initialize();
  144. }
  145. else
  146. {
  147. setData = SteamVR_Input.GetActionSetDataFromPath(actionSetPath);
  148. if (setData == null)
  149. {
  150. #if UNITY_EDITOR
  151. if (throwErrors)
  152. {
  153. if (string.IsNullOrEmpty(actionSetPath))
  154. {
  155. Debug.LogError("<b>[SteamVR]</b> Action has not been assigned.");
  156. }
  157. else
  158. {
  159. Debug.LogError("<b>[SteamVR]</b> Could not find action with path: " + actionSetPath);
  160. }
  161. }
  162. #endif
  163. }
  164. }
  165. initialized = true;
  166. }
  167. public string GetPath()
  168. {
  169. return actionSetPath;
  170. }
  171. /// <summary>
  172. /// Returns whether the set is currently active or not.
  173. /// </summary>
  174. /// <param name="source">The device to check. Any means all devices here (not left or right, but all)</param>
  175. public bool IsActive(SteamVR_Input_Sources source = SteamVR_Input_Sources.Any)
  176. {
  177. return setData.IsActive(source);
  178. }
  179. /// <summary>
  180. /// Returns the last time this action set was changed (set to active or inactive)
  181. /// </summary>
  182. /// <param name="source">The device to check. Any means all devices here (not left or right, but all)</param>
  183. public float GetTimeLastChanged(SteamVR_Input_Sources source = SteamVR_Input_Sources.Any)
  184. {
  185. return setData.GetTimeLastChanged(source);
  186. }
  187. /// <summary>
  188. /// Activate this set so its actions can be called
  189. /// </summary>
  190. /// <param name="disableAllOtherActionSets">Disable all other action sets at the same time</param>
  191. /// <param name="priority">The priority of this action set. If you have two actions bound to the same input (button) the higher priority set will override the lower priority. If they are the same priority both will execute.</param>
  192. /// <param name="activateForSource">Will activate this action set only for the specified source. Any if you want to activate for everything</param>
  193. public void Activate(SteamVR_Input_Sources activateForSource = SteamVR_Input_Sources.Any, int priority = 0, bool disableAllOtherActionSets = false)
  194. {
  195. setData.Activate(activateForSource, priority, disableAllOtherActionSets);
  196. }
  197. /// <summary>
  198. /// Deactivate the action set so its actions can no longer be called
  199. /// </summary>
  200. public void Deactivate(SteamVR_Input_Sources forSource = SteamVR_Input_Sources.Any)
  201. {
  202. setData.Deactivate(forSource);
  203. }
  204. /// <summary>Gets the last part of the path for this action. Removes "actions" and direction.</summary>
  205. public string GetShortName()
  206. {
  207. return setData.GetShortName();
  208. }
  209. /// <summary>
  210. /// Shows all the bindings for the actions in this set.
  211. /// </summary>
  212. /// <param name="originToHighlight">Highlights the binding of the passed in action (must be in an active set)</param>
  213. /// <returns></returns>
  214. public bool ShowBindingHints(ISteamVR_Action_In originToHighlight = null)
  215. {
  216. if (originToHighlight == null)
  217. return SteamVR_Input.ShowBindingHints(this);
  218. else
  219. return SteamVR_Input.ShowBindingHints(originToHighlight);
  220. }
  221. public bool ReadRawSetActive(SteamVR_Input_Sources inputSource)
  222. {
  223. return setData.ReadRawSetActive(inputSource);
  224. }
  225. public float ReadRawSetLastChanged(SteamVR_Input_Sources inputSource)
  226. {
  227. return setData.ReadRawSetLastChanged(inputSource);
  228. }
  229. public int ReadRawSetPriority(SteamVR_Input_Sources inputSource)
  230. {
  231. return setData.ReadRawSetPriority(inputSource);
  232. }
  233. public SteamVR_ActionSet_Data GetActionSetData()
  234. {
  235. return setData;
  236. }
  237. public CreateType GetCopy<CreateType>() where CreateType : SteamVR_ActionSet, new()
  238. {
  239. if (SteamVR_Input.ShouldMakeCopy()) //no need to make copies at runtime
  240. {
  241. CreateType actionSet = new CreateType();
  242. actionSet.actionSetPath = this.actionSetPath;
  243. actionSet.setData = this.setData;
  244. actionSet.initialized = true;
  245. return actionSet;
  246. }
  247. else
  248. {
  249. return (CreateType)this;
  250. }
  251. }
  252. public bool Equals(SteamVR_ActionSet other)
  253. {
  254. if (ReferenceEquals(null, other))
  255. return false;
  256. return this.actionSetPath == other.actionSetPath;
  257. }
  258. public override bool Equals(object other)
  259. {
  260. if (ReferenceEquals(null, other))
  261. {
  262. if (string.IsNullOrEmpty(this.actionSetPath)) //if we haven't set a path, say this action set is equal to null
  263. return true;
  264. return false;
  265. }
  266. if (ReferenceEquals(this, other))
  267. return true;
  268. if (other is SteamVR_ActionSet)
  269. return this.Equals((SteamVR_ActionSet)other);
  270. return false;
  271. }
  272. public override int GetHashCode()
  273. {
  274. if (actionSetPath == null)
  275. return 0;
  276. else
  277. return actionSetPath.GetHashCode();
  278. }
  279. public static bool operator !=(SteamVR_ActionSet set1, SteamVR_ActionSet set2)
  280. {
  281. return !(set1 == set2);
  282. }
  283. public static bool operator ==(SteamVR_ActionSet set1, SteamVR_ActionSet set2)
  284. {
  285. bool set1null = (ReferenceEquals(null, set1) || string.IsNullOrEmpty(set1.actionSetPath) || set1.GetActionSetData() == null);
  286. bool set2null = (ReferenceEquals(null, set2) || string.IsNullOrEmpty(set2.actionSetPath) || set2.GetActionSetData() == null);
  287. if (set1null && set2null)
  288. return true;
  289. else if (set1null != set2null)
  290. return false;
  291. return set1.Equals(set2);
  292. }
  293. void ISerializationCallbackReceiver.OnBeforeSerialize()
  294. {
  295. }
  296. void ISerializationCallbackReceiver.OnAfterDeserialize()
  297. {
  298. if (setData != null)
  299. {
  300. if (setData.fullPath != actionSetPath)
  301. {
  302. setData = SteamVR_Input.GetActionSetDataFromPath(actionSetPath);
  303. }
  304. }
  305. if (initialized == false)
  306. Initialize(false, false);
  307. }
  308. }
  309. /// <summary>
  310. /// Action sets are logical groupings of actions. Multiple sets can be active at one time.
  311. /// </summary>
  312. public class SteamVR_ActionSet_Data : ISteamVR_ActionSet
  313. {
  314. public SteamVR_ActionSet_Data() { }
  315. /// <summary>All actions within this set (including out actions)</summary>
  316. public SteamVR_Action[] allActions { get; set; }
  317. /// <summary>All IN actions within this set that are NOT pose or skeleton actions</summary>
  318. public ISteamVR_Action_In[] nonVisualInActions { get; set; }
  319. /// <summary>All pose and skeleton actions within this set</summary>
  320. public ISteamVR_Action_In[] visualActions { get; set; }
  321. /// <summary>All pose actions within this set</summary>
  322. public SteamVR_Action_Pose[] poseActions { get; set; }
  323. /// <summary>All skeleton actions within this set</summary>
  324. public SteamVR_Action_Skeleton[] skeletonActions { get; set; }
  325. /// <summary>All out actions within this set</summary>
  326. public ISteamVR_Action_Out[] outActionArray { get; set; }
  327. /// <summary>The full path to this action set (ex: /actions/in/default)</summary>
  328. public string fullPath { get; set; }
  329. public string usage { get; set; }
  330. public ulong handle { get; set; }
  331. protected bool[] rawSetActive = new bool[SteamVR_Input_Source.numSources];
  332. protected float[] rawSetLastChanged = new float[SteamVR_Input_Source.numSources];
  333. protected int[] rawSetPriority = new int[SteamVR_Input_Source.numSources];
  334. protected bool initialized = false;
  335. public void PreInitialize()
  336. {
  337. }
  338. public void FinishPreInitialize()
  339. {
  340. List<SteamVR_Action> allActionsList = new List<SteamVR_Action>();
  341. List<ISteamVR_Action_In> nonVisualInActionsList = new List<ISteamVR_Action_In>();
  342. List<ISteamVR_Action_In> visualActionsList = new List<ISteamVR_Action_In>();
  343. List<SteamVR_Action_Pose> poseActionsList = new List<SteamVR_Action_Pose>();
  344. List<SteamVR_Action_Skeleton> skeletonActionsList = new List<SteamVR_Action_Skeleton>();
  345. List<ISteamVR_Action_Out> outActionList = new List<ISteamVR_Action_Out>();
  346. if (SteamVR_Input.actions == null)
  347. {
  348. Debug.LogError("<b>[SteamVR Input]</b> Actions not initialized!");
  349. return;
  350. }
  351. for (int actionIndex = 0; actionIndex < SteamVR_Input.actions.Length; actionIndex++)
  352. {
  353. SteamVR_Action action = SteamVR_Input.actions[actionIndex];
  354. if (action.actionSet.GetActionSetData() == this)
  355. {
  356. allActionsList.Add(action);
  357. if (action is ISteamVR_Action_Boolean || action is ISteamVR_Action_Single || action is ISteamVR_Action_Vector2 || action is ISteamVR_Action_Vector3)
  358. {
  359. nonVisualInActionsList.Add((ISteamVR_Action_In)action);
  360. }
  361. else if (action is SteamVR_Action_Pose)
  362. {
  363. visualActionsList.Add((ISteamVR_Action_In)action);
  364. poseActionsList.Add((SteamVR_Action_Pose)action);
  365. }
  366. else if (action is SteamVR_Action_Skeleton)
  367. {
  368. visualActionsList.Add((ISteamVR_Action_In)action);
  369. skeletonActionsList.Add((SteamVR_Action_Skeleton)action);
  370. }
  371. else if (action is ISteamVR_Action_Out)
  372. {
  373. outActionList.Add((ISteamVR_Action_Out)action);
  374. }
  375. else
  376. {
  377. Debug.LogError("<b>[SteamVR Input]</b> Action doesn't implement known interface: " + action.fullPath);
  378. }
  379. }
  380. }
  381. allActions = allActionsList.ToArray();
  382. nonVisualInActions = nonVisualInActionsList.ToArray();
  383. visualActions = visualActionsList.ToArray();
  384. poseActions = poseActionsList.ToArray();
  385. skeletonActions = skeletonActionsList.ToArray();
  386. outActionArray = outActionList.ToArray();
  387. }
  388. public void Initialize()
  389. {
  390. ulong newHandle = 0;
  391. EVRInputError err = OpenVR.Input.GetActionSetHandle(fullPath.ToLower(), ref newHandle);
  392. handle = newHandle;
  393. if (err != EVRInputError.None)
  394. Debug.LogError("<b>[SteamVR]</b> GetActionSetHandle (" + fullPath + ") error: " + err.ToString());
  395. initialized = true;
  396. }
  397. /// <summary>
  398. /// Returns whether the set is currently active or not.
  399. /// </summary>
  400. /// <param name="source">The device to check. Any means all devices here (not left or right, but all)</param>
  401. public bool IsActive(SteamVR_Input_Sources source = SteamVR_Input_Sources.Any)
  402. {
  403. int sourceIndex = (int)source;
  404. if (initialized)
  405. return rawSetActive[sourceIndex] || rawSetActive[0];
  406. return false;
  407. }
  408. /// <summary>
  409. /// Returns the last time this action set was changed (set to active or inactive)
  410. /// </summary>
  411. /// <param name="source">The device to check. Any means all devices here (not left or right, but all)</param>
  412. public float GetTimeLastChanged(SteamVR_Input_Sources source = SteamVR_Input_Sources.Any)
  413. {
  414. int sourceIndex = (int)source;
  415. if (initialized)
  416. return rawSetLastChanged[sourceIndex];
  417. return 0;
  418. }
  419. /// <summary>
  420. /// Activate this set so its actions can be called
  421. /// </summary>
  422. /// <param name="disableAllOtherActionSets">Disable all other action sets at the same time</param>
  423. /// <param name="priority">The priority of this action set. If you have two actions bound to the same input (button) the higher priority set will override the lower priority. If they are the same priority both will execute.</param>
  424. /// <param name="activateForSource">Will activate this action set only for the specified source. Any if you want to activate for everything</param>
  425. public void Activate(SteamVR_Input_Sources activateForSource = SteamVR_Input_Sources.Any, int priority = 0, bool disableAllOtherActionSets = false)
  426. {
  427. int sourceIndex = (int)activateForSource;
  428. if (disableAllOtherActionSets)
  429. SteamVR_ActionSet_Manager.DisableAllActionSets();
  430. if (rawSetActive[sourceIndex] == false)
  431. {
  432. rawSetActive[sourceIndex] = true;
  433. SteamVR_ActionSet_Manager.SetChanged();
  434. rawSetLastChanged[sourceIndex] = Time.realtimeSinceStartup;
  435. }
  436. if (rawSetPriority[sourceIndex] != priority)
  437. {
  438. rawSetPriority[sourceIndex] = priority;
  439. SteamVR_ActionSet_Manager.SetChanged();
  440. rawSetLastChanged[sourceIndex] = Time.realtimeSinceStartup;
  441. }
  442. }
  443. /// <summary>
  444. /// Deactivate the action set so its actions can no longer be called
  445. /// </summary>
  446. public void Deactivate(SteamVR_Input_Sources forSource = SteamVR_Input_Sources.Any)
  447. {
  448. int sourceIndex = (int)forSource;
  449. if (rawSetActive[sourceIndex] != false)
  450. {
  451. rawSetLastChanged[sourceIndex] = Time.realtimeSinceStartup;
  452. SteamVR_ActionSet_Manager.SetChanged();
  453. }
  454. rawSetActive[sourceIndex] = false;
  455. rawSetPriority[sourceIndex] = 0;
  456. }
  457. private string cachedShortName;
  458. /// <summary>Gets the last part of the path for this action. Removes "actions" and direction.</summary>
  459. public string GetShortName()
  460. {
  461. if (cachedShortName == null)
  462. {
  463. cachedShortName = SteamVR_Input_ActionFile.GetShortName(fullPath);
  464. }
  465. return cachedShortName;
  466. }
  467. public bool ReadRawSetActive(SteamVR_Input_Sources inputSource)
  468. {
  469. int sourceIndex = (int)inputSource;
  470. return rawSetActive[sourceIndex];
  471. }
  472. public float ReadRawSetLastChanged(SteamVR_Input_Sources inputSource)
  473. {
  474. int sourceIndex = (int)inputSource;
  475. return rawSetLastChanged[sourceIndex];
  476. }
  477. public int ReadRawSetPriority(SteamVR_Input_Sources inputSource)
  478. {
  479. int sourceIndex = (int)inputSource;
  480. return rawSetPriority[sourceIndex];
  481. }
  482. }
  483. /// <summary>
  484. /// Action sets are logical groupings of actions. Multiple sets can be active at one time.
  485. /// </summary>
  486. public interface ISteamVR_ActionSet
  487. {
  488. /// <summary>All actions within this set (including out actions)</summary>
  489. SteamVR_Action[] allActions { get; }
  490. /// <summary>All IN actions within this set that are NOT pose or skeleton actions</summary>
  491. ISteamVR_Action_In[] nonVisualInActions { get; }
  492. /// <summary>All pose and skeleton actions within this set</summary>
  493. ISteamVR_Action_In[] visualActions { get; }
  494. /// <summary>All pose actions within this set</summary>
  495. SteamVR_Action_Pose[] poseActions { get; }
  496. /// <summary>All skeleton actions within this set</summary>
  497. SteamVR_Action_Skeleton[] skeletonActions { get; }
  498. /// <summary>All out actions within this set</summary>
  499. ISteamVR_Action_Out[] outActionArray { get; }
  500. /// <summary>The full path to this action set (ex: /actions/in/default)</summary>
  501. string fullPath { get; }
  502. /// <summary>How the binding UI should display this set</summary>
  503. string usage { get; }
  504. ulong handle { get; }
  505. bool ReadRawSetActive(SteamVR_Input_Sources inputSource);
  506. float ReadRawSetLastChanged(SteamVR_Input_Sources inputSource);
  507. int ReadRawSetPriority(SteamVR_Input_Sources inputSource);
  508. /// <summary>
  509. /// Returns whether the set is currently active or not.
  510. /// </summary>
  511. /// <param name="source">The device to check. Any means all devices here (not left or right, but all)</param>
  512. bool IsActive(SteamVR_Input_Sources source = SteamVR_Input_Sources.Any);
  513. /// <summary>
  514. /// Returns the last time this action set was changed (set to active or inactive)
  515. /// </summary>
  516. /// <param name="source">The device to check. Any means all devices here (not left or right, but all)</param>
  517. float GetTimeLastChanged(SteamVR_Input_Sources source = SteamVR_Input_Sources.Any);
  518. /// <summary>
  519. /// Activate this set so its actions can be called
  520. /// </summary>
  521. /// <param name="disableAllOtherActionSets">Disable all other action sets at the same time</param>
  522. /// <param name="priority">The priority of this action set. If you have two actions bound to the same input (button) the higher priority set will override the lower priority. If they are the same priority both will execute.</param>
  523. /// <param name="activateForSource">Will activate this action set only for the specified source. Any if you want to activate for everything</param>
  524. void Activate(SteamVR_Input_Sources activateForSource = SteamVR_Input_Sources.Any, int priority = 0, bool disableAllOtherActionSets = false);
  525. /// <summary>Deactivate the action set so its actions can no longer be called</summary>
  526. void Deactivate(SteamVR_Input_Sources forSource = SteamVR_Input_Sources.Any);
  527. /// <summary>Gets the last part of the path for this action. Removes "actions" and direction.</summary>
  528. string GetShortName();
  529. }
  530. }