SteamVR_Action_In.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. /// In actions are all input type actions. Boolean, Single, Vector2, Vector3, Skeleton, and Pose.
  13. /// </summary>
  14. public abstract class SteamVR_Action_In<SourceMap, SourceElement> : SteamVR_Action<SourceMap, SourceElement>, ISteamVR_Action_In
  15. where SourceMap : SteamVR_Action_In_Source_Map<SourceElement>, new()
  16. where SourceElement : SteamVR_Action_In_Source, new()
  17. {
  18. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Returns true if the action has been changed since the previous update</summary>
  19. public bool changed { get { return sourceMap[SteamVR_Input_Sources.Any].changed; } }
  20. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Returns true if the action was changed for the previous update cycle</summary>
  21. public bool lastChanged { get { return sourceMap[SteamVR_Input_Sources.Any].changed; } }
  22. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The time the action was changed (Time.realtimeSinceStartup)</summary>
  23. public float changedTime { get { return sourceMap[SteamVR_Input_Sources.Any].changedTime; } }
  24. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The time the action was updated (Time.realtimeSinceStartup)</summary>
  25. public float updateTime { get { return sourceMap[SteamVR_Input_Sources.Any].updateTime; } }
  26. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The handle to the component that triggered the action to be changed</summary>
  27. public ulong activeOrigin { get { return sourceMap[SteamVR_Input_Sources.Any].activeOrigin; } }
  28. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The handle to the component that triggered the action to be changed in the previous update</summary>
  29. public ulong lastActiveOrigin { get { return sourceMap[SteamVR_Input_Sources.Any].lastActiveOrigin; } }
  30. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The input source that triggered the action to be changed</summary>
  31. public SteamVR_Input_Sources activeDevice { get { return sourceMap[SteamVR_Input_Sources.Any].activeDevice; } }
  32. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The device index (used by Render Models) used by the device that triggered the action to be changed</summary>
  33. public uint trackedDeviceIndex { get { return sourceMap[SteamVR_Input_Sources.Any].trackedDeviceIndex; } }
  34. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The name of the component on the render model that caused the action to be changed (not localized)</summary>
  35. public string renderModelComponentName { get { return sourceMap[SteamVR_Input_Sources.Any].renderModelComponentName; } }
  36. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The full localized name for the component, controller, and hand that caused the action to be changed</summary>
  37. public string localizedOriginName { get { return sourceMap[SteamVR_Input_Sources.Any].localizedOriginName; } }
  38. /// <summary>
  39. /// <strong>[Should not be called by user code]</strong>
  40. /// Updates the data for all the input sources the system has detected need to be updated.
  41. /// </summary>
  42. public virtual void UpdateValues()
  43. {
  44. sourceMap.UpdateValues();
  45. }
  46. /// <summary>
  47. /// The name of the component on the render model that caused the action to be updated (not localized)
  48. /// </summary>
  49. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  50. public virtual string GetRenderModelComponentName(SteamVR_Input_Sources inputSource)
  51. {
  52. return sourceMap[inputSource].renderModelComponentName;
  53. }
  54. /// <summary>
  55. /// The input source that triggered the action to be updated last
  56. /// </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 virtual SteamVR_Input_Sources GetActiveDevice(SteamVR_Input_Sources inputSource)
  59. {
  60. return sourceMap[inputSource].activeDevice;
  61. }
  62. /// <summary>
  63. /// Gets the device index for the controller this action is bound to. This can be used for render models or the pose tracking system.
  64. /// </summary>
  65. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  66. public virtual uint GetDeviceIndex(SteamVR_Input_Sources inputSource)
  67. {
  68. return sourceMap[inputSource].trackedDeviceIndex;
  69. }
  70. /// <summary>
  71. /// Indicates whether or not the data for this action and specified input source has changed since the last update. Determined by SteamVR or 'changeTolerance'.
  72. /// </summary>
  73. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  74. public virtual bool GetChanged(SteamVR_Input_Sources inputSource)
  75. {
  76. return sourceMap[inputSource].changed;
  77. }
  78. /// <summary>
  79. /// The time the action was changed (Time.realtimeSinceStartup)
  80. /// </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 override float GetTimeLastChanged(SteamVR_Input_Sources inputSource)
  83. {
  84. return sourceMap[inputSource].changedTime;
  85. }
  86. /// <summary>
  87. /// Gets the localized name of the device that the action corresponds to. Include as many EVRInputStringBits as you want to add to the localized string
  88. /// </summary>
  89. /// <param name="inputSource"></param>
  90. /// <param name="localizedParts">
  91. /// <list type="bullet">
  92. /// <item><description>VRInputString_Hand - Which hand the origin is in. ex: "Left Hand". </description></item>
  93. /// <item><description>VRInputString_ControllerType - What kind of controller the user has in that hand. ex: "Vive Controller". </description></item>
  94. /// <item><description>VRInputString_InputSource - What part of that controller is the origin. ex: "Trackpad". </description></item>
  95. /// <item><description>VRInputString_All - All of the above. ex: "Left Hand Vive Controller Trackpad". </description></item>
  96. /// </list>
  97. /// </param>
  98. public string GetLocalizedOriginPart(SteamVR_Input_Sources inputSource, params EVRInputStringBits[] localizedParts)
  99. {
  100. return sourceMap[inputSource].GetLocalizedOriginPart(localizedParts);
  101. }
  102. /// <summary>
  103. /// Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"
  104. /// </summary>
  105. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  106. public string GetLocalizedOrigin(SteamVR_Input_Sources inputSource)
  107. {
  108. return sourceMap[inputSource].GetLocalizedOrigin();
  109. }
  110. /// <summary>
  111. /// <strong>[Should not be called by user code]</strong>
  112. /// Returns whether the system has determined this source should be updated (based on code calls)
  113. /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
  114. /// </summary>
  115. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  116. public override bool IsUpdating(SteamVR_Input_Sources inputSource)
  117. {
  118. return sourceMap.IsUpdating(inputSource);
  119. }
  120. /// <summary>
  121. /// <strong>[Should not be called by user code]</strong>
  122. /// Forces the system to start updating the data for this action and the specified input source.
  123. /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
  124. /// </summary>
  125. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  126. public void ForceAddSourceToUpdateList(SteamVR_Input_Sources inputSource)
  127. {
  128. sourceMap.ForceAddSourceToUpdateList(inputSource);
  129. }
  130. }
  131. public class SteamVR_Action_In_Source_Map<SourceElement> : SteamVR_Action_Source_Map<SourceElement>
  132. where SourceElement : SteamVR_Action_In_Source, new()
  133. {
  134. protected List<SteamVR_Input_Sources> updatingSources = new List<SteamVR_Input_Sources>();
  135. /// <summary>
  136. /// <strong>[Should not be called by user code]</strong>
  137. /// Returns whether the system has determined this source should be updated (based on code calls)
  138. /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
  139. /// </summary>
  140. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  141. public bool IsUpdating(SteamVR_Input_Sources inputSource)
  142. {
  143. for (int sourceIndex = 0; sourceIndex < updatingSources.Count; sourceIndex++)
  144. {
  145. if (inputSource == updatingSources[sourceIndex])
  146. return true;
  147. }
  148. return false;
  149. }
  150. protected override void OnAccessSource(SteamVR_Input_Sources inputSource)
  151. {
  152. if (SteamVR_Action.startUpdatingSourceOnAccess)
  153. {
  154. ForceAddSourceToUpdateList(inputSource);
  155. }
  156. }
  157. /// <summary>
  158. /// <strong>[Should not be called by user code]</strong>
  159. /// Forces the system to start updating the data for this action and the specified input source.
  160. /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
  161. /// </summary>
  162. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  163. public void ForceAddSourceToUpdateList(SteamVR_Input_Sources inputSource)
  164. {
  165. if (sources[inputSource].isUpdating == false)
  166. {
  167. updatingSources.Add(inputSource);
  168. sources[inputSource].isUpdating = true;
  169. if (SteamVR_Input.isStartupFrame == false)
  170. sources[inputSource].UpdateValue();
  171. }
  172. }
  173. /// <summary>
  174. /// <strong>[Should not be called by user code]</strong>
  175. /// Updates the data for all the input sources the system has detected need to be updated.
  176. /// </summary>
  177. public void UpdateValues()
  178. {
  179. for (int sourceIndex = 0; sourceIndex < updatingSources.Count; sourceIndex++)
  180. {
  181. sources[updatingSources[sourceIndex]].UpdateValue();
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// In actions are all input type actions. Boolean, Single, Vector2, Vector3, Skeleton, and Pose.
  187. /// This class fires onChange and onUpdate events.
  188. /// </summary>
  189. public abstract class SteamVR_Action_In_Source : SteamVR_Action_Source, ISteamVR_Action_In_Source
  190. {
  191. protected static uint inputOriginInfo_size = 0;
  192. /// <summary>
  193. /// <strong>[Should not be called by user code]</strong>
  194. /// Forces the system to start updating the data for this action and the specified input source.
  195. /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
  196. /// </summary>
  197. public bool isUpdating { get; set; }
  198. /// <summary>The time the action was updated (Time.realtimeSinceStartup)</summary>
  199. public float updateTime { get; protected set; }
  200. /// <summary>The handle to the component that triggered the action to be changed</summary>
  201. public abstract ulong activeOrigin { get; }
  202. /// <summary>The handle to the component that triggered the action to be changed in the previous update</summary>
  203. public abstract ulong lastActiveOrigin { get; }
  204. /// <summary>Returns true if the action has been changed since the previous update</summary>
  205. public abstract bool changed { get; protected set; }
  206. /// <summary>Returns true if the action was changed for the previous update cycle</summary>
  207. public abstract bool lastChanged { get; protected set; }
  208. /// <summary>The input source that triggered the action to be updated</summary>
  209. public SteamVR_Input_Sources activeDevice { get { UpdateOriginTrackedDeviceInfo(); return SteamVR_Input_Source.GetSource(inputOriginInfo.devicePath); } }
  210. /// <summary>The device index (used by Render Models) used by the device that triggered the action to be updated</summary>
  211. public uint trackedDeviceIndex { get { UpdateOriginTrackedDeviceInfo(); return inputOriginInfo.trackedDeviceIndex; } }
  212. /// <summary>The name of the component on the render model that caused the action to be updated (not localized)</summary>
  213. public string renderModelComponentName { get { UpdateOriginTrackedDeviceInfo(); return inputOriginInfo.rchRenderModelComponentName; } }
  214. /// <summary>
  215. /// Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"
  216. /// </summary>
  217. public string localizedOriginName { get { UpdateOriginTrackedDeviceInfo(); return GetLocalizedOrigin(); } }
  218. /// <summary>The Time.realtimeSinceStartup that this action was last changed.</summary>
  219. public float changedTime { get; protected set; }
  220. protected int lastOriginGetFrame { get; set; }
  221. protected InputOriginInfo_t inputOriginInfo = new InputOriginInfo_t();
  222. protected InputOriginInfo_t lastInputOriginInfo = new InputOriginInfo_t();
  223. /// <summary><strong>[Should not be called by user code]</strong> Updates the data for this action and this input source</summary>
  224. public abstract void UpdateValue();
  225. /// <summary>
  226. /// <strong>[Should not be called by user code]</strong> Initializes the handle for the action, the size of the InputOriginInfo struct, and any other related SteamVR data.
  227. /// </summary>
  228. public override void Initialize()
  229. {
  230. base.Initialize();
  231. if (inputOriginInfo_size == 0)
  232. inputOriginInfo_size = (uint)Marshal.SizeOf(typeof(InputOriginInfo_t));
  233. }
  234. protected void UpdateOriginTrackedDeviceInfo()
  235. {
  236. if (lastOriginGetFrame != Time.frameCount) //only get once per frame
  237. {
  238. EVRInputError err = OpenVR.Input.GetOriginTrackedDeviceInfo(activeOrigin, ref inputOriginInfo, inputOriginInfo_size);
  239. if (err != EVRInputError.None)
  240. Debug.LogError("<b>[SteamVR]</b> GetOriginTrackedDeviceInfo error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString() + " activeOrigin: " + activeOrigin.ToString() + " active: " + active);
  241. lastInputOriginInfo = inputOriginInfo;
  242. lastOriginGetFrame = Time.frameCount;
  243. }
  244. }
  245. /// <summary>
  246. /// Gets the localized name of the device that the action corresponds to. Include as many EVRInputStringBits as you want to add to the localized string
  247. /// </summary>
  248. /// <param name="inputSource"></param>
  249. /// <param name="localizedParts">
  250. /// <list type="bullet">
  251. /// <item><description>VRInputString_Hand - Which hand the origin is in. ex: "Left Hand". </description></item>
  252. /// <item><description>VRInputString_ControllerType - What kind of controller the user has in that hand. ex: "Vive Controller". </description></item>
  253. /// <item><description>VRInputString_InputSource - What part of that controller is the origin. ex: "Trackpad". </description></item>
  254. /// <item><description>VRInputString_All - All of the above. ex: "Left Hand Vive Controller Trackpad". </description></item>
  255. /// </list>
  256. /// </param>
  257. public string GetLocalizedOriginPart(params EVRInputStringBits[] localizedParts)
  258. {
  259. UpdateOriginTrackedDeviceInfo();
  260. if (active)
  261. {
  262. return SteamVR_Input.GetLocalizedName(activeOrigin, localizedParts);
  263. }
  264. return null;
  265. }
  266. /// <summary>
  267. /// Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"
  268. /// </summary>
  269. public string GetLocalizedOrigin()
  270. {
  271. UpdateOriginTrackedDeviceInfo();
  272. if (active)
  273. {
  274. return SteamVR_Input.GetLocalizedName(activeOrigin, EVRInputStringBits.VRInputString_All);
  275. }
  276. return null;
  277. }
  278. }
  279. public interface ISteamVR_Action_In : ISteamVR_Action, ISteamVR_Action_In_Source
  280. {
  281. /// <summary>
  282. /// <strong>[Should not be called by user code]</strong>
  283. /// Updates the data for all the input sources the system has detected need to be updated.
  284. /// </summary>
  285. void UpdateValues();
  286. /// <summary>
  287. /// The name of the component on the render model that caused the action to be updated (not localized)
  288. /// </summary>
  289. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  290. string GetRenderModelComponentName(SteamVR_Input_Sources inputSource);
  291. /// <summary>
  292. /// The input source that triggered the action to be updated last
  293. /// </summary>
  294. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  295. SteamVR_Input_Sources GetActiveDevice(SteamVR_Input_Sources inputSource);
  296. /// <summary>
  297. /// Gets the device index for the controller this action is bound to. This can be used for render models or the pose tracking system.
  298. /// </summary>
  299. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  300. uint GetDeviceIndex(SteamVR_Input_Sources inputSource);
  301. /// <summary>
  302. /// Indicates whether or not the data for this action and specified input source has changed since the last update. Determined by SteamVR or 'changeTolerance'.
  303. /// </summary>
  304. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  305. bool GetChanged(SteamVR_Input_Sources inputSource);
  306. /// <summary>
  307. /// Gets the localized name of the device that the action corresponds to. Include as many EVRInputStringBits as you want to add to the localized string
  308. /// </summary>
  309. /// <param name="inputSource"></param>
  310. /// <param name="localizedParts">
  311. /// <list type="bullet">
  312. /// <item><description>VRInputString_Hand - Which hand the origin is in. ex: "Left Hand". </description></item>
  313. /// <item><description>VRInputString_ControllerType - What kind of controller the user has in that hand. ex: "Vive Controller". </description></item>
  314. /// <item><description>VRInputString_InputSource - What part of that controller is the origin. ex: "Trackpad". </description></item>
  315. /// <item><description>VRInputString_All - All of the above. ex: "Left Hand Vive Controller Trackpad". </description></item>
  316. /// </list>
  317. /// </param>
  318. string GetLocalizedOriginPart(SteamVR_Input_Sources inputSource, params EVRInputStringBits[] localizedParts);
  319. /// <summary>
  320. /// Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"
  321. /// </summary>
  322. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  323. string GetLocalizedOrigin(SteamVR_Input_Sources inputSource);
  324. }
  325. public interface ISteamVR_Action_In_Source : ISteamVR_Action_Source
  326. {
  327. /// <summary>Returns true if the action has been changed in the most recent update</summary>
  328. bool changed { get; }
  329. /// <summary>Returns true if the action was changed for the previous update cycle</summary>
  330. bool lastChanged { get; }
  331. /// <summary>The Time.realtimeSinceStartup that this action was last changed.</summary>
  332. float changedTime { get; }
  333. /// <summary>The time the action was updated (Time.realtimeSinceStartup)</summary>
  334. float updateTime { get; }
  335. /// <summary>The handle to the component that triggered the action to be changed</summary>
  336. ulong activeOrigin { get; }
  337. /// <summary>The handle to the component that triggered the action to be changed in the previous update</summary>
  338. ulong lastActiveOrigin { get; }
  339. /// <summary>The input source that triggered the action to be updated</summary>
  340. SteamVR_Input_Sources activeDevice { get; }
  341. /// <summary>The device index (used by Render Models) used by the device that triggered the action to be updated</summary>
  342. uint trackedDeviceIndex { get; }
  343. /// <summary>The name of the component on the render model that caused the action to be updated (not localized)</summary>
  344. string renderModelComponentName { get; }
  345. /// <summary>Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"</summary>
  346. string localizedOriginName { get; }
  347. }
  348. }