1
0

SteamVR_Action.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. // Action implementation overview:
  3. // Actions are split into three parts:
  4. // * Action: The user-accessible class that is the interface for accessing action data.
  5. // There may be many Action instances per Actual SteamVR Action, but these instances are just interfaces to the data and should have virtually no overhead.
  6. // * Action Map: This is basically a wrapper for a list of Action_Source instances.
  7. // The idea being there is one Map per Actual SteamVR Action.
  8. // These maps can be retrieved from a static store in SteamVR_Input so we're not duplicating data.
  9. // * Action Source: This is a collection of cached data retrieved by calls to the underlying SteamVR Input system.
  10. // Each Action Source has an inputSource that it is associated with.
  11. using UnityEngine;
  12. using System.Collections;
  13. using System;
  14. using Valve.VR;
  15. using System.Runtime.InteropServices;
  16. using System.Collections.Generic;
  17. namespace Valve.VR
  18. {
  19. [Serializable]
  20. /// <summary>
  21. /// This is the base level action for SteamVR Input Actions. All SteamVR_Action_In and SteamVR_Action_Out inherit from this.
  22. /// Initializes the ulong handle for the action, has some helper references that all actions will have.
  23. /// </summary>
  24. public abstract class SteamVR_Action<SourceMap, SourceElement> : SteamVR_Action, ISteamVR_Action where SourceMap : SteamVR_Action_Source_Map<SourceElement>, new() where SourceElement : SteamVR_Action_Source, new()
  25. {
  26. /// <summary>
  27. /// The map to the source elements, a dictionary of source elements. Should be accessed through the action indexer
  28. /// </summary>
  29. [NonSerialized]
  30. protected SourceMap sourceMap;
  31. /// <summary>
  32. /// Access this action restricted to individual input sources.
  33. /// </summary>
  34. /// <param name="inputSource">The input source to access for this action</param>
  35. public virtual SourceElement this[SteamVR_Input_Sources inputSource]
  36. {
  37. get
  38. {
  39. return sourceMap[inputSource];
  40. }
  41. }
  42. /// <summary>The full string path for this action</summary>
  43. public override string fullPath
  44. {
  45. get
  46. {
  47. return sourceMap.fullPath;
  48. }
  49. }
  50. /// <summary>The underlying handle for this action used for native SteamVR Input calls</summary>
  51. public override ulong handle { get { return sourceMap.handle; } }
  52. /// <summary>The actionset this action is contained within</summary>
  53. public override SteamVR_ActionSet actionSet
  54. {
  55. get
  56. {
  57. return sourceMap.actionSet;
  58. }
  59. }
  60. /// <summary>The action direction of this action (in for input - most actions, out for output - mainly haptics)</summary>
  61. public override SteamVR_ActionDirections direction
  62. {
  63. get
  64. {
  65. return sourceMap.direction;
  66. }
  67. }
  68. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Returns true if the action is bound and the actionset is active</summary>
  69. public override bool active { get { return sourceMap[SteamVR_Input_Sources.Any].active; } }
  70. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Returns true if the action was bound and the ActionSet was active during the previous update</summary>
  71. public override bool lastActive { get { return sourceMap[SteamVR_Input_Sources.Any].lastActive; } }
  72. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Returns true if the action is bound</summary>
  73. public override bool activeBinding { get { return sourceMap[SteamVR_Input_Sources.Any].activeBinding; } }
  74. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Returns true if the action was bound at the previous update</summary>
  75. public override bool lastActiveBinding { get { return sourceMap[SteamVR_Input_Sources.Any].lastActiveBinding; } }
  76. [NonSerialized]
  77. protected bool initialized = false;
  78. /// <summary>
  79. /// Prepares the action to be initialized. Creating dictionaries, finding the right existing action, etc.
  80. /// </summary>
  81. public override void PreInitialize(string newActionPath)
  82. {
  83. actionPath = newActionPath;
  84. sourceMap = new SourceMap();
  85. sourceMap.PreInitialize(this, actionPath);
  86. initialized = true;
  87. }
  88. protected override void CreateUninitialized(string newActionPath, bool caseSensitive)
  89. {
  90. actionPath = newActionPath;
  91. sourceMap = new SourceMap();
  92. sourceMap.PreInitialize(this, actionPath, false);
  93. needsReinit = true;
  94. initialized = false;
  95. }
  96. protected override void CreateUninitialized(string newActionSet, SteamVR_ActionDirections direction, string newAction, bool caseSensitive)
  97. {
  98. actionPath = SteamVR_Input_ActionFile_Action.CreateNewName(newActionSet, direction, newAction);
  99. sourceMap = new SourceMap();
  100. sourceMap.PreInitialize(this, actionPath, false);
  101. needsReinit = true;
  102. initialized = false;
  103. }
  104. /// <summary>
  105. /// <strong>[Should not be called by user code]</strong> If it looks like we aren't attached to a real action then try and find the existing action for our given path.
  106. /// </summary>
  107. public override string TryNeedsInitData()
  108. {
  109. if (needsReinit && actionPath != null)
  110. {
  111. SteamVR_Action existingAction = FindExistingActionForPartialPath(actionPath);
  112. if (existingAction == null)
  113. {
  114. this.sourceMap = null;
  115. }
  116. else
  117. {
  118. this.actionPath = existingAction.fullPath;
  119. this.sourceMap = (SourceMap)existingAction.GetSourceMap();
  120. initialized = true;
  121. needsReinit = false;
  122. return actionPath;
  123. }
  124. }
  125. return null;
  126. }
  127. /// <summary>
  128. /// <strong>[Should not be called by user code]</strong> Initializes the individual sources as well as the base map itself.
  129. /// Gets the handle for the action from SteamVR and does any other SteamVR related setup that needs to be done
  130. /// </summary>
  131. public override void Initialize(bool createNew = false, bool throwErrors = true)
  132. {
  133. if (needsReinit)
  134. {
  135. TryNeedsInitData();
  136. }
  137. if (createNew)
  138. {
  139. sourceMap.Initialize();
  140. }
  141. else
  142. {
  143. sourceMap = SteamVR_Input.GetActionDataFromPath<SourceMap>(actionPath);
  144. if (sourceMap == null)
  145. {
  146. #if UNITY_EDITOR
  147. if (throwErrors)
  148. {
  149. if (string.IsNullOrEmpty(actionPath))
  150. {
  151. Debug.LogError("<b>[SteamVR]</b> Action has not been assigned.");
  152. }
  153. else
  154. {
  155. Debug.LogError("<b>[SteamVR]</b> Could not find action with path: " + actionPath);
  156. }
  157. }
  158. #endif
  159. }
  160. }
  161. initialized = true;
  162. }
  163. /// <summary>
  164. /// <strong>[Should not be called by user code]</strong> Returns the underlying source map for the action.
  165. /// <strong>[Should not be called by user code]</strong> Returns the underlying source map for the action.
  166. /// </summary>
  167. public override SteamVR_Action_Source_Map GetSourceMap()
  168. {
  169. return sourceMap;
  170. }
  171. protected override void InitializeCopy(string newActionPath, SteamVR_Action_Source_Map newData)
  172. {
  173. this.actionPath = newActionPath;
  174. this.sourceMap = (SourceMap)newData;
  175. initialized = true;
  176. }
  177. protected void InitAfterDeserialize()
  178. {
  179. if (sourceMap != null)
  180. {
  181. if (sourceMap.fullPath != actionPath)
  182. {
  183. needsReinit = true;
  184. TryNeedsInitData();
  185. }
  186. if (string.IsNullOrEmpty(actionPath))
  187. sourceMap = null;
  188. }
  189. if (initialized == false)
  190. {
  191. Initialize(false, false);
  192. }
  193. }
  194. /// <summary>
  195. /// Gets a value indicating whether or not the action is currently bound and if the containing action set is active
  196. /// </summary>
  197. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  198. public override bool GetActive(SteamVR_Input_Sources inputSource)
  199. {
  200. return sourceMap[inputSource].active;
  201. }
  202. /// <summary>
  203. /// Gets a value indicating whether or not the action is currently bound
  204. /// </summary>
  205. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  206. public override bool GetActiveBinding(SteamVR_Input_Sources inputSource)
  207. {
  208. return sourceMap[inputSource].activeBinding;
  209. }
  210. /// <summary>
  211. /// Gets the value from the previous update indicating whether or not the action was currently bound and if the containing action set was active
  212. /// </summary>
  213. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  214. public override bool GetLastActive(SteamVR_Input_Sources inputSource)
  215. {
  216. return sourceMap[inputSource].lastActive;
  217. }
  218. /// <summary>
  219. /// Gets the value from the previous update indicating whether or not the action is currently bound
  220. /// </summary>
  221. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  222. public override bool GetLastActiveBinding(SteamVR_Input_Sources inputSource)
  223. {
  224. return sourceMap[inputSource].lastActiveBinding;
  225. }
  226. }
  227. [Serializable]
  228. public abstract class SteamVR_Action : IEquatable<SteamVR_Action>, ISteamVR_Action
  229. {
  230. public SteamVR_Action() { }
  231. [SerializeField]
  232. protected string actionPath;
  233. [SerializeField]
  234. protected bool needsReinit;
  235. /// <summary>
  236. /// <strong>Not recommended.</strong> Determines if we should do a lazy-loading style of updating actions where we don't check for their data until the code asks for it. Note: You will have to manually activate actions otherwise. Not recommended.
  237. /// </summary>
  238. public static bool startUpdatingSourceOnAccess = true;
  239. /// <summary>
  240. /// <strong>[Should not be called by user code]</strong> Creates an actual action that will later be called by user code.
  241. /// </summary>
  242. public static CreateType Create<CreateType>(string newActionPath) where CreateType : SteamVR_Action, new()
  243. {
  244. CreateType action = new CreateType();
  245. action.PreInitialize(newActionPath);
  246. return action;
  247. }
  248. /// <summary>
  249. /// <strong>[Should not be called by user code]</strong> Creates an uninitialized action that can be saved without being attached to a real action
  250. /// </summary>
  251. public static CreateType CreateUninitialized<CreateType>(string setName, SteamVR_ActionDirections direction, string newActionName, bool caseSensitive) where CreateType : SteamVR_Action, new()
  252. {
  253. CreateType action = new CreateType();
  254. action.CreateUninitialized(setName, direction, newActionName, caseSensitive);
  255. return action;
  256. }
  257. /// <summary>
  258. /// <strong>[Should not be called by user code]</strong> Creates an uninitialized action that can be saved without being attached to a real action
  259. /// </summary>
  260. public static CreateType CreateUninitialized<CreateType>(string actionPath, bool caseSensitive) where CreateType : SteamVR_Action, new()
  261. {
  262. CreateType action = new CreateType();
  263. action.CreateUninitialized(actionPath, caseSensitive);
  264. return action;
  265. }
  266. /// <summary>
  267. /// <strong>[Should not be called by user code]</strong> Gets a copy of the underlying source map so we're always using the same underlying event data
  268. /// </summary>
  269. public CreateType GetCopy<CreateType>() where CreateType : SteamVR_Action, new()
  270. {
  271. if (SteamVR_Input.ShouldMakeCopy()) //no need to make copies at runtime
  272. {
  273. CreateType action = new CreateType();
  274. action.InitializeCopy(this.actionPath, this.GetSourceMap());
  275. return action;
  276. }
  277. else
  278. {
  279. return (CreateType)this;
  280. }
  281. }
  282. public abstract string TryNeedsInitData();
  283. protected abstract void InitializeCopy(string newActionPath, SteamVR_Action_Source_Map newData);
  284. /// <summary>The full string path for this action</summary>
  285. public abstract string fullPath { get; }
  286. /// <summary>The underlying handle for this action used for native SteamVR Input calls</summary>
  287. public abstract ulong handle { get; }
  288. /// <summary>The actionset this action is contained within</summary>
  289. public abstract SteamVR_ActionSet actionSet { get; }
  290. /// <summary>The action direction of this action (in for input - most actions, out for output - mainly haptics)</summary>
  291. public abstract SteamVR_ActionDirections direction { get; }
  292. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Returns true if the action set that contains this action is active for Any input source.</summary>
  293. public bool setActive { get { return actionSet.IsActive(SteamVR_Input_Sources.Any); } }
  294. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Returns true if the action is bound and the actionset is active</summary>
  295. public abstract bool active { get; }
  296. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Returns true if the action is bound</summary>
  297. public abstract bool activeBinding { get; }
  298. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Returns true if the action was bound and the actionset was active at the previous update</summary>
  299. public abstract bool lastActive { get; }
  300. /// <summary>
  301. ///
  302. /// </summary>
  303. public abstract bool lastActiveBinding { get; }
  304. /// <summary>
  305. /// Prepares the action to be initialized. Creating dictionaries, finding the right existing action, etc.
  306. /// </summary>
  307. public abstract void PreInitialize(string newActionPath);
  308. protected abstract void CreateUninitialized(string newActionPath, bool caseSensitive);
  309. protected abstract void CreateUninitialized(string newActionSet, SteamVR_ActionDirections direction, string newAction, bool caseSensitive);
  310. /// <summary>
  311. /// Initializes the individual sources as well as the base map itself. Gets the handle for the action from SteamVR and does any other SteamVR related setup that needs to be done
  312. /// </summary>
  313. public abstract void Initialize(bool createNew = false, bool throwNotSetError = true);
  314. /// <summary>Gets the last timestamp this action was changed. (by Time.realtimeSinceStartup)</summary>
  315. /// <param name="inputSource">The input source to use to select the last changed time</param>
  316. public abstract float GetTimeLastChanged(SteamVR_Input_Sources inputSource);
  317. public abstract SteamVR_Action_Source_Map GetSourceMap();
  318. /// <summary>
  319. /// Gets a value indicating whether or not the action is currently bound and if the containing action set is active
  320. /// </summary>
  321. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  322. public abstract bool GetActive(SteamVR_Input_Sources inputSource);
  323. /// <summary>
  324. /// Gets a value indicating whether or not the containing action set is active
  325. /// </summary>
  326. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  327. public bool GetSetActive(SteamVR_Input_Sources inputSource)
  328. {
  329. return actionSet.IsActive(inputSource);
  330. }
  331. /// <summary>
  332. /// Gets a value indicating whether or not the action is currently bound
  333. /// </summary>
  334. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  335. public abstract bool GetActiveBinding(SteamVR_Input_Sources inputSource);
  336. /// <summary>
  337. /// Gets the value from the previous update indicating whether or not the action is currently bound and if the containing action set is active
  338. /// </summary>
  339. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  340. public abstract bool GetLastActive(SteamVR_Input_Sources inputSource);
  341. /// <summary>
  342. /// Gets the value from the previous update indicating whether or not the action is currently bound
  343. /// </summary>
  344. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  345. public abstract bool GetLastActiveBinding(SteamVR_Input_Sources inputSource);
  346. /// <summary>Returns the full action path for this action.</summary>
  347. public string GetPath()
  348. {
  349. return actionPath;
  350. }
  351. /// <summary>
  352. /// Returns true if the data for this action is being updated for the specified input source. This can be triggered by querying the data
  353. /// </summary>
  354. public abstract bool IsUpdating(SteamVR_Input_Sources inputSource);
  355. /// <summary>
  356. /// Creates a hashcode from the full action path of this action
  357. /// </summary>
  358. public override int GetHashCode()
  359. {
  360. if (actionPath == null)
  361. return 0;
  362. else
  363. return actionPath.GetHashCode();
  364. }
  365. /// <summary>
  366. /// Compares two SteamVR_Actions by their action path instead of references
  367. /// </summary>
  368. public bool Equals(SteamVR_Action other)
  369. {
  370. if (ReferenceEquals(null, other))
  371. return false;
  372. //SteamVR_Action_Source_Map thisMap = this.GetSourceMap();
  373. //SteamVR_Action_Source_Map otherMap = other.GetSourceMap();
  374. //return this.actionPath == other.actionPath && thisMap.fullPath == otherMap.fullPath;
  375. return this.actionPath == other.actionPath;
  376. }
  377. /// <summary>
  378. /// Compares two SteamVR_Actions by their action path instead of references
  379. /// </summary>
  380. public override bool Equals(object other)
  381. {
  382. if (ReferenceEquals(null, other))
  383. {
  384. if (string.IsNullOrEmpty(this.actionPath)) //if we haven't set a path, say this action is equal to null
  385. return true;
  386. if (this.GetSourceMap() == null)
  387. return true;
  388. return false;
  389. }
  390. if (ReferenceEquals(this, other))
  391. return true;
  392. if (other is SteamVR_Action)
  393. return this.Equals((SteamVR_Action)other);
  394. return false;
  395. }
  396. /// <summary>
  397. /// Compares two SteamVR_Actions by their action path.
  398. /// </summary>
  399. public static bool operator !=(SteamVR_Action action1, SteamVR_Action action2)
  400. {
  401. return !(action1 == action2);
  402. }
  403. /// <summary>
  404. /// Compares two SteamVR_Actions by their action path.
  405. /// </summary>
  406. public static bool operator ==(SteamVR_Action action1, SteamVR_Action action2)
  407. {
  408. bool action1null = (ReferenceEquals(null, action1) || string.IsNullOrEmpty(action1.actionPath) || action1.GetSourceMap() == null);
  409. bool action2null = (ReferenceEquals(null, action2) || string.IsNullOrEmpty(action2.actionPath) || action2.GetSourceMap() == null);
  410. if (action1null && action2null)
  411. return true;
  412. else if (action1null != action2null)
  413. return false;
  414. return action1.Equals(action2);
  415. }
  416. /// <summary>
  417. /// Tries to find an existing action matching some subsection of an action path. More useful functions in SteamVR_Input.
  418. /// </summary>
  419. public static SteamVR_Action FindExistingActionForPartialPath(string path)
  420. {
  421. if (string.IsNullOrEmpty(path) || path.IndexOf('/') == -1)
  422. return null;
  423. // 0 1 2 3 4
  424. // /actions/default/in/foobar
  425. string[] pathParts = path.Split('/');
  426. SteamVR_Action existingAction;
  427. if (pathParts.Length >= 5 && string.IsNullOrEmpty(pathParts[2]))
  428. {
  429. string set = pathParts[2];
  430. string name = pathParts[4];
  431. existingAction = SteamVR_Input.GetBaseAction(set, name);
  432. }
  433. else
  434. {
  435. existingAction = SteamVR_Input.GetBaseActionFromPath(path);
  436. }
  437. return existingAction;
  438. }
  439. [NonSerialized]
  440. private string cachedShortName;
  441. /// <summary>Gets just the name of this action. The last part of the path for this action. Removes action set, and direction.</summary>
  442. public string GetShortName()
  443. {
  444. if (cachedShortName == null)
  445. {
  446. cachedShortName = SteamVR_Input_ActionFile.GetShortName(fullPath);
  447. }
  448. return cachedShortName;
  449. }
  450. public void ShowOrigins()
  451. {
  452. OpenVR.Input.ShowActionOrigins(actionSet.handle, handle);
  453. }
  454. public void HideOrigins()
  455. {
  456. OpenVR.Input.ShowActionOrigins(0,0);
  457. }
  458. }
  459. public abstract class SteamVR_Action_Source_Map<SourceElement> : SteamVR_Action_Source_Map where SourceElement : SteamVR_Action_Source, new()
  460. {
  461. /// <summary>
  462. /// Gets a reference to the action restricted to a certain input source. LeftHand or RightHand for example.
  463. /// </summary>
  464. /// <param name="inputSource">The device you would like data from</param>
  465. public SourceElement this[SteamVR_Input_Sources inputSource]
  466. {
  467. get
  468. {
  469. return GetSourceElementForIndexer(inputSource);
  470. }
  471. }
  472. protected virtual void OnAccessSource(SteamVR_Input_Sources inputSource) { }
  473. protected SourceElement[] sources = new SourceElement[SteamVR_Input_Source.numSources];
  474. /// <summary>
  475. /// <strong>[Should not be called by user code]</strong> Initializes the individual sources as well as the base map itself. Gets the handle for the action from SteamVR and does any other SteamVR related setup that needs to be done
  476. /// </summary>
  477. public override void Initialize()
  478. {
  479. base.Initialize();
  480. for (int sourceIndex = 0; sourceIndex < sources.Length; sourceIndex++)
  481. {
  482. if (sources[sourceIndex] != null)
  483. sources[sourceIndex].Initialize();
  484. }
  485. }
  486. protected override void PreinitializeMap(SteamVR_Input_Sources inputSource, SteamVR_Action wrappingAction)
  487. {
  488. int sourceIndex = (int)inputSource;
  489. sources[sourceIndex] = new SourceElement();
  490. sources[sourceIndex].Preinitialize(wrappingAction, inputSource);
  491. }
  492. // Normally I'd just make the indexer virtual and override that but some unity versions don't like that
  493. protected virtual SourceElement GetSourceElementForIndexer(SteamVR_Input_Sources inputSource)
  494. {
  495. int sourceIndex = (int)inputSource;
  496. OnAccessSource(inputSource);
  497. return sources[sourceIndex];
  498. }
  499. }
  500. public abstract class SteamVR_Action_Source_Map
  501. {
  502. /// <summary>The full string path for this action (from the action manifest)</summary>
  503. public string fullPath { get; protected set; }
  504. /// <summary>The underlying handle for this action used for native SteamVR Input calls. Retrieved on Initialization from SteamVR.</summary>
  505. public ulong handle { get; protected set; }
  506. /// <summary>The ActionSet this action is contained within</summary>
  507. public SteamVR_ActionSet actionSet { get; protected set; }
  508. /// <summary>The action direction of this action (in for input - most actions, out for output - haptics)</summary>
  509. public SteamVR_ActionDirections direction { get; protected set; }
  510. /// <summary>The base SteamVR_Action this map corresponds to</summary>
  511. public SteamVR_Action action;
  512. public virtual void PreInitialize(SteamVR_Action wrappingAction, string actionPath, bool throwErrors = true)
  513. {
  514. fullPath = actionPath;
  515. action = wrappingAction;
  516. actionSet = SteamVR_Input.GetActionSetFromPath(GetActionSetPath());
  517. direction = GetActionDirection();
  518. SteamVR_Input_Sources[] sources = SteamVR_Input_Source.GetAllSources();
  519. for (int sourceIndex = 0; sourceIndex < sources.Length; sourceIndex++)
  520. {
  521. PreinitializeMap(sources[sourceIndex], wrappingAction);
  522. }
  523. }
  524. /// <summary>
  525. /// <strong>[Should not be called by user code]</strong> Sets up the internals of the action source before SteamVR has been initialized.
  526. /// </summary>
  527. protected abstract void PreinitializeMap(SteamVR_Input_Sources inputSource, SteamVR_Action wrappingAction);
  528. /// <summary>
  529. /// <strong>[Should not be called by user code]</strong> Initializes the handle for the action and any other related SteamVR data.
  530. /// </summary>
  531. public virtual void Initialize()
  532. {
  533. ulong newHandle = 0;
  534. EVRInputError err = OpenVR.Input.GetActionHandle(fullPath.ToLowerInvariant(), ref newHandle);
  535. handle = newHandle;
  536. if (err != EVRInputError.None)
  537. Debug.LogError("<b>[SteamVR]</b> GetActionHandle (" + fullPath.ToLowerInvariant() + ") error: " + err.ToString());
  538. }
  539. private string GetActionSetPath()
  540. {
  541. int actionsEndIndex = fullPath.IndexOf('/', 1);
  542. int setStartIndex = actionsEndIndex + 1;
  543. int setEndIndex = fullPath.IndexOf('/', setStartIndex);
  544. int count = setEndIndex;
  545. return fullPath.Substring(0, count);
  546. }
  547. private static string inLowered = "IN".ToLower(System.Globalization.CultureInfo.CurrentCulture);
  548. private static string outLowered = "OUT".ToLower(System.Globalization.CultureInfo.CurrentCulture);
  549. private SteamVR_ActionDirections GetActionDirection()
  550. {
  551. int actionsEndIndex = fullPath.IndexOf('/', 1);
  552. int setStartIndex = actionsEndIndex + 1;
  553. int setEndIndex = fullPath.IndexOf('/', setStartIndex);
  554. int directionEndIndex = fullPath.IndexOf('/', setEndIndex + 1);
  555. int count = directionEndIndex - setEndIndex - 1;
  556. string direction = fullPath.Substring(setEndIndex + 1, count);
  557. if (direction == inLowered)
  558. return SteamVR_ActionDirections.In;
  559. else if (direction == outLowered)
  560. return SteamVR_ActionDirections.Out;
  561. else
  562. Debug.LogError("Could not find match for direction: " + direction);
  563. return SteamVR_ActionDirections.In;
  564. }
  565. }
  566. public abstract class SteamVR_Action_Source : ISteamVR_Action_Source
  567. {
  568. /// <summary>The full string path for this action (from the action manifest)</summary>
  569. public string fullPath { get { return action.fullPath; } }
  570. /// <summary>The underlying handle for this action used for native SteamVR Input calls. Retrieved on Initialization from SteamVR.</summary>
  571. public ulong handle { get { return action.handle; } }
  572. /// <summary>The ActionSet this action is contained within</summary>
  573. public SteamVR_ActionSet actionSet { get { return action.actionSet; } }
  574. /// <summary>The action direction of this action (in for input - most actions, out for output - haptics)</summary>
  575. public SteamVR_ActionDirections direction { get { return action.direction; } }
  576. /// <summary>The input source that this instance corresponds to. ex. LeftHand, RightHand</summary>
  577. public SteamVR_Input_Sources inputSource { get; protected set; }
  578. /// <summary>Returns true if the action set this is contained in is active for this input source (or Any)</summary>
  579. public bool setActive { get { return actionSet.IsActive(inputSource); } }
  580. /// <summary>Returns true if this action is bound and the ActionSet is active</summary>
  581. public abstract bool active { get; }
  582. /// <summary>Returns true if the action is bound</summary>
  583. public abstract bool activeBinding { get; }
  584. /// <summary>Returns true if the action was bound and the ActionSet was active during the previous update</summary>
  585. public abstract bool lastActive { get; protected set; }
  586. /// <summary>Returns true if the action was bound during the previous update</summary>
  587. public abstract bool lastActiveBinding { get; }
  588. protected ulong inputSourceHandle;
  589. protected SteamVR_Action action;
  590. /// <summary>
  591. /// <strong>[Should not be called by user code]</strong> Sets up the internals of the action source before SteamVR has been initialized.
  592. /// </summary>
  593. public virtual void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource)
  594. {
  595. action = wrappingAction;
  596. inputSource = forInputSource;
  597. }
  598. public SteamVR_Action_Source() { }
  599. /// <summary>
  600. /// <strong>[Should not be called by user code]</strong>
  601. /// Initializes the handle for the inputSource, and any other related SteamVR data.
  602. /// </summary>
  603. public virtual void Initialize()
  604. {
  605. inputSourceHandle = SteamVR_Input_Source.GetHandle(inputSource);
  606. }
  607. }
  608. public interface ISteamVR_Action : ISteamVR_Action_Source
  609. {
  610. /// <summary>Returns the active state of the action for the specified Input Source</summary>
  611. /// <param name="inputSource">The input source to check</param>
  612. bool GetActive(SteamVR_Input_Sources inputSource);
  613. /// <summary>Returns the name of the action without the action set or direction</summary>
  614. string GetShortName();
  615. }
  616. public interface ISteamVR_Action_Source
  617. {
  618. /// <summary>Returns true if this action is bound and the ActionSet is active</summary>
  619. bool active { get; }
  620. /// <summary>Returns true if the action is bound</summary>
  621. bool activeBinding { get; }
  622. /// <summary>Returns true if the action was bound and the ActionSet was active during the previous update</summary>
  623. bool lastActive { get; }
  624. /// <summary>Returns true if the action was bound last update</summary>
  625. bool lastActiveBinding { get; }
  626. /// <summary>The full string path for this action (from the action manifest)</summary>
  627. string fullPath { get; }
  628. /// <summary>The underlying handle for this action used for native SteamVR Input calls. Retrieved on Initialization from SteamVR.</summary>
  629. ulong handle { get; }
  630. /// <summary>The ActionSet this action is contained within</summary>
  631. SteamVR_ActionSet actionSet { get; }
  632. /// <summary>The action direction of this action (in for input, out for output)</summary>
  633. SteamVR_ActionDirections direction { get; }
  634. }
  635. }