SteamVR_Action_In.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. /// <summary>
  131. /// Returns a string for the type of controller that was being used the last time the action was triggered. Common types:
  132. /// vive_controller, oculus_touch, knuckles, vive_cosmos_controller, logitech_stylus
  133. /// </summary>
  134. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  135. public string GetControllerType(SteamVR_Input_Sources inputSource)
  136. {
  137. return SteamVR.instance.GetStringProperty(ETrackedDeviceProperty.Prop_ControllerType_String, GetDeviceIndex(inputSource));
  138. }
  139. }
  140. public class SteamVR_Action_In_Source_Map<SourceElement> : SteamVR_Action_Source_Map<SourceElement>
  141. where SourceElement : SteamVR_Action_In_Source, new()
  142. {
  143. protected List<int> updatingSources = new List<int>();
  144. /// <summary>
  145. /// <strong>[Should not be called by user code]</strong>
  146. /// Returns whether the system has determined this source should be updated (based on code calls)
  147. /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
  148. /// </summary>
  149. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  150. public bool IsUpdating(SteamVR_Input_Sources inputSource)
  151. {
  152. int isUpdatingSourceIndex = (int)inputSource;
  153. for (int sourceIndex = 0; sourceIndex < updatingSources.Count; sourceIndex++)
  154. {
  155. if (isUpdatingSourceIndex == updatingSources[sourceIndex])
  156. return true;
  157. }
  158. return false;
  159. }
  160. protected override void OnAccessSource(SteamVR_Input_Sources inputSource)
  161. {
  162. if (SteamVR_Action.startUpdatingSourceOnAccess)
  163. {
  164. ForceAddSourceToUpdateList(inputSource);
  165. }
  166. }
  167. /// <summary>
  168. /// <strong>[Should not be called by user code]</strong>
  169. /// Forces the system to start updating the data for this action and the specified input source.
  170. /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
  171. /// </summary>
  172. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  173. public void ForceAddSourceToUpdateList(SteamVR_Input_Sources inputSource)
  174. {
  175. int sourceIndex = (int)inputSource;
  176. if (sources[sourceIndex] == null)
  177. {
  178. sources[sourceIndex] = new SourceElement();
  179. }
  180. if (sources[sourceIndex].isUpdating == false)
  181. {
  182. updatingSources.Add(sourceIndex);
  183. sources[sourceIndex].isUpdating = true;
  184. if (SteamVR_Input.isStartupFrame == false)
  185. sources[sourceIndex].UpdateValue();
  186. }
  187. }
  188. /// <summary>
  189. /// <strong>[Should not be called by user code]</strong>
  190. /// Updates the data for all the input sources the system has detected need to be updated.
  191. /// </summary>
  192. public void UpdateValues()
  193. {
  194. for (int sourceIndex = 0; sourceIndex < updatingSources.Count; sourceIndex++)
  195. {
  196. sources[updatingSources[sourceIndex]].UpdateValue();
  197. }
  198. }
  199. }
  200. /// <summary>
  201. /// In actions are all input type actions. Boolean, Single, Vector2, Vector3, Skeleton, and Pose.
  202. /// This class fires onChange and onUpdate events.
  203. /// </summary>
  204. public abstract class SteamVR_Action_In_Source : SteamVR_Action_Source, ISteamVR_Action_In_Source
  205. {
  206. protected static uint inputOriginInfo_size = 0;
  207. /// <summary>
  208. /// <strong>[Should not be called by user code]</strong>
  209. /// Forces the system to start updating the data for this action and the specified input source.
  210. /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
  211. /// </summary>
  212. public bool isUpdating { get; set; }
  213. /// <summary>The time the action was updated (Time.realtimeSinceStartup)</summary>
  214. public float updateTime { get; protected set; }
  215. /// <summary>The handle to the component that triggered the action to be changed</summary>
  216. public abstract ulong activeOrigin { get; }
  217. /// <summary>The handle to the component that triggered the action to be changed in the previous update</summary>
  218. public abstract ulong lastActiveOrigin { get; }
  219. /// <summary>Returns true if the action has been changed since the previous update</summary>
  220. public abstract bool changed { get; protected set; }
  221. /// <summary>Returns true if the action was changed for the previous update cycle</summary>
  222. public abstract bool lastChanged { get; protected set; }
  223. /// <summary>The input source that triggered the action to be updated</summary>
  224. public SteamVR_Input_Sources activeDevice { get { UpdateOriginTrackedDeviceInfo(); return SteamVR_Input_Source.GetSource(inputOriginInfo.devicePath); } }
  225. /// <summary>The device index (used by Render Models) used by the device that triggered the action to be updated</summary>
  226. public uint trackedDeviceIndex { get { UpdateOriginTrackedDeviceInfo(); return inputOriginInfo.trackedDeviceIndex; } }
  227. /// <summary>The name of the component on the render model that caused the action to be updated (not localized)</summary>
  228. public string renderModelComponentName { get { UpdateOriginTrackedDeviceInfo(); return inputOriginInfo.rchRenderModelComponentName; } }
  229. /// <summary>
  230. /// Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"
  231. /// </summary>
  232. public string localizedOriginName { get { UpdateOriginTrackedDeviceInfo(); return GetLocalizedOrigin(); } }
  233. /// <summary>The Time.realtimeSinceStartup that this action was last changed.</summary>
  234. public float changedTime { get; protected set; }
  235. protected int lastOriginGetFrame { get; set; }
  236. protected InputOriginInfo_t inputOriginInfo = new InputOriginInfo_t();
  237. protected InputOriginInfo_t lastInputOriginInfo = new InputOriginInfo_t();
  238. /// <summary><strong>[Should not be called by user code]</strong> Updates the data for this action and this input source</summary>
  239. public abstract void UpdateValue();
  240. /// <summary>
  241. /// <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.
  242. /// </summary>
  243. public override void Initialize()
  244. {
  245. base.Initialize();
  246. if (inputOriginInfo_size == 0)
  247. inputOriginInfo_size = (uint)Marshal.SizeOf(typeof(InputOriginInfo_t));
  248. }
  249. protected void UpdateOriginTrackedDeviceInfo()
  250. {
  251. if (lastOriginGetFrame != Time.frameCount) //only get once per frame
  252. {
  253. EVRInputError err = OpenVR.Input.GetOriginTrackedDeviceInfo(activeOrigin, ref inputOriginInfo, inputOriginInfo_size);
  254. if (err != EVRInputError.None)
  255. Debug.LogError("<b>[SteamVR]</b> GetOriginTrackedDeviceInfo error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString() + " activeOrigin: " + activeOrigin.ToString() + " active: " + active);
  256. lastInputOriginInfo = inputOriginInfo;
  257. lastOriginGetFrame = Time.frameCount;
  258. }
  259. }
  260. /// <summary>
  261. /// 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
  262. /// </summary>
  263. /// <param name="inputSource"></param>
  264. /// <param name="localizedParts">
  265. /// <list type="bullet">
  266. /// <item><description>VRInputString_Hand - Which hand the origin is in. ex: "Left Hand". </description></item>
  267. /// <item><description>VRInputString_ControllerType - What kind of controller the user has in that hand. ex: "Vive Controller". </description></item>
  268. /// <item><description>VRInputString_InputSource - What part of that controller is the origin. ex: "Trackpad". </description></item>
  269. /// <item><description>VRInputString_All - All of the above. ex: "Left Hand Vive Controller Trackpad". </description></item>
  270. /// </list>
  271. /// </param>
  272. public string GetLocalizedOriginPart(params EVRInputStringBits[] localizedParts)
  273. {
  274. UpdateOriginTrackedDeviceInfo();
  275. if (active)
  276. {
  277. return SteamVR_Input.GetLocalizedName(activeOrigin, localizedParts);
  278. }
  279. return null;
  280. }
  281. /// <summary>
  282. /// Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"
  283. /// </summary>
  284. public string GetLocalizedOrigin()
  285. {
  286. UpdateOriginTrackedDeviceInfo();
  287. if (active)
  288. {
  289. return SteamVR_Input.GetLocalizedName(activeOrigin, EVRInputStringBits.VRInputString_All);
  290. }
  291. return null;
  292. }
  293. }
  294. public interface ISteamVR_Action_In : ISteamVR_Action, ISteamVR_Action_In_Source
  295. {
  296. /// <summary>
  297. /// <strong>[Should not be called by user code]</strong>
  298. /// Updates the data for all the input sources the system has detected need to be updated.
  299. /// </summary>
  300. void UpdateValues();
  301. /// <summary>
  302. /// The name of the component on the render model that caused the action to be updated (not localized)
  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. string GetRenderModelComponentName(SteamVR_Input_Sources inputSource);
  306. /// <summary>
  307. /// The input source that triggered the action to be updated last
  308. /// </summary>
  309. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  310. SteamVR_Input_Sources GetActiveDevice(SteamVR_Input_Sources inputSource);
  311. /// <summary>
  312. /// Gets the device index for the controller this action is bound to. This can be used for render models or the pose tracking system.
  313. /// </summary>
  314. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  315. uint GetDeviceIndex(SteamVR_Input_Sources inputSource);
  316. /// <summary>
  317. /// Indicates whether or not the data for this action and specified input source has changed since the last update. Determined by SteamVR or 'changeTolerance'.
  318. /// </summary>
  319. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  320. bool GetChanged(SteamVR_Input_Sources inputSource);
  321. /// <summary>
  322. /// 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
  323. /// </summary>
  324. /// <param name="inputSource"></param>
  325. /// <param name="localizedParts">
  326. /// <list type="bullet">
  327. /// <item><description>VRInputString_Hand - Which hand the origin is in. ex: "Left Hand". </description></item>
  328. /// <item><description>VRInputString_ControllerType - What kind of controller the user has in that hand. ex: "Vive Controller". </description></item>
  329. /// <item><description>VRInputString_InputSource - What part of that controller is the origin. ex: "Trackpad". </description></item>
  330. /// <item><description>VRInputString_All - All of the above. ex: "Left Hand Vive Controller Trackpad". </description></item>
  331. /// </list>
  332. /// </param>
  333. string GetLocalizedOriginPart(SteamVR_Input_Sources inputSource, params EVRInputStringBits[] localizedParts);
  334. /// <summary>
  335. /// Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"
  336. /// </summary>
  337. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  338. string GetLocalizedOrigin(SteamVR_Input_Sources inputSource);
  339. }
  340. public interface ISteamVR_Action_In_Source : ISteamVR_Action_Source
  341. {
  342. /// <summary>Returns true if the action has been changed in the most recent update</summary>
  343. bool changed { get; }
  344. /// <summary>Returns true if the action was changed for the previous update cycle</summary>
  345. bool lastChanged { get; }
  346. /// <summary>The Time.realtimeSinceStartup that this action was last changed.</summary>
  347. float changedTime { get; }
  348. /// <summary>The time the action was updated (Time.realtimeSinceStartup)</summary>
  349. float updateTime { get; }
  350. /// <summary>The handle to the component that triggered the action to be changed</summary>
  351. ulong activeOrigin { get; }
  352. /// <summary>The handle to the component that triggered the action to be changed in the previous update</summary>
  353. ulong lastActiveOrigin { get; }
  354. /// <summary>The input source that triggered the action to be updated</summary>
  355. SteamVR_Input_Sources activeDevice { get; }
  356. /// <summary>The device index (used by Render Models) used by the device that triggered the action to be updated</summary>
  357. uint trackedDeviceIndex { get; }
  358. /// <summary>The name of the component on the render model that caused the action to be updated (not localized)</summary>
  359. string renderModelComponentName { get; }
  360. /// <summary>Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"</summary>
  361. string localizedOriginName { get; }
  362. }
  363. }