SteamController.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #if (UNITY_STANDALONE || UNITY_EDITOR) && UNITY_ENABLE_STEAM_CONTROLLER_SUPPORT
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Security.Cryptography.X509Certificates;
  5. using UnityEngine.InputSystem.Utilities;
  6. #if UNITY_EDITOR
  7. using UnityEngine.InputSystem.Steam.Editor;
  8. #endif
  9. ////TODO: support action set layers
  10. namespace UnityEngine.InputSystem.Steam
  11. {
  12. /// <summary>
  13. /// Base class for controllers made available through the Steam controller API.
  14. /// </summary>
  15. /// <remarks>
  16. /// Unlike other controllers, the Steam controller is somewhat of an amorphous input
  17. /// device which gains specific shape only in combination with an "In-Game Action File"
  18. /// in VDF format installed alongside the application. These files specify the actions
  19. /// that are supported by the application and are internally bound to specific controls
  20. /// on a controller inside the Steam runtime. The bindings are set up in the Steam client
  21. /// through its own binding UI.
  22. ///
  23. /// Note that as the Steam controller API supports PS4 and Xbox controllers as well,
  24. /// the actual hardware device behind a SteamController instance may not be a
  25. /// Steam Controller. The <see cref="steamControllerType"/> property can be used what kind
  26. /// of controller the Steam runtime is talking to internally.
  27. ///
  28. /// This class is abstract. Specific Steam controller interfaces can either be implemented
  29. /// manually based on this class or generated automatically from Steam IGA files using
  30. /// <see cref="SteamIGAConverter"/>. This can be done in the editor by right-clicking
  31. /// a .VDF file containing the actions and then selecting "Steam >> Generate Unity Input Device...".
  32. /// The result is a newly generated device layout that will automatically register itself
  33. /// with the input system and will represent a Steam controller with the specific action
  34. /// sets and actions found in the .VDF file. The Steam handles for sets and actions will
  35. /// automatically be exposed from the device and controls will be set up that correspond
  36. /// to each action defined in the .VDF file.
  37. ///
  38. /// Devices based on SteamController can be used in one of two ways.
  39. ///
  40. /// The first method is by manually managing active action sets on a controller. This is done by
  41. /// calling the various APIs (such as <see cref="ActivateSteamActionSet"/>) that correspond
  42. /// to the methods in the <see cref="ISteamControllerAPI">Steam controller API</see>. The
  43. /// controller handle is implicit in this case and corresponds to the <see cref="steamControllerHandle"/>
  44. /// of the controller the methods are called on.
  45. ///
  46. /// The second method is by using
  47. ///
  48. ///
  49. ///
  50. ///
  51. ///
  52. /// By default, Steam controllers will automatically activate action set layers in
  53. /// response to action maps being enabled and disabled. The correlation between Unity
  54. /// actions and action maps and Steam actions and action sets happens entirely by name.
  55. /// E.g. a Unity action map called "gameplay" will be looked up as a Steam action set
  56. /// using the same name "gameplay".
  57. /// </remarks>
  58. [Scripting.Preserve]
  59. public abstract class SteamController : InputDevice
  60. {
  61. internal const string kSteamInterface = "Steam";
  62. /// <summary>
  63. /// Handle in the <see cref="ISteamControllerAPI">Steam API</see> for the controller.
  64. /// </summary>
  65. public SteamHandle<SteamController> steamControllerHandle { get; internal set; }
  66. /*
  67. * TODO
  68. public SteamControllerType steamControllerType
  69. {
  70. get { throw new NotImplementedException(); }
  71. }*/
  72. /// <summary>
  73. /// The list of Steam action sets supported by this controller.
  74. /// </summary>
  75. /// <remarks>
  76. /// Steam action sets are implicitly supplied to the Steam runtime rather than explicitly configured
  77. /// by the application. ...
  78. /// </remarks>
  79. public abstract ReadOnlyArray<SteamActionSetInfo> steamActionSets { get; }
  80. /// <summary>
  81. /// Determine whether the controller automatically activates and deactivates action set
  82. /// layers in response to <see cref="InputActionMap">input action map</see> being enabled
  83. /// and disabled.
  84. /// </summary>
  85. /// <remarks>
  86. /// This is on by default.
  87. ///
  88. /// When on, if an <see cref="InputActionMap">action map</see> has bindings to a SteamController
  89. /// and is enabled or disabled, the SteamController will automatically enable or disable
  90. /// the correspondingly named Steam action set.
  91. /// </remarks>
  92. public bool autoActivateSets { get; set; }
  93. protected SteamController()
  94. {
  95. autoActivateSets = true;
  96. }
  97. public void ActivateSteamActionSet(SteamHandle<InputActionMap> actionSet)
  98. {
  99. SteamSupport.GetAPIAndRequireItToBeSet().ActivateActionSet(steamControllerHandle, actionSet);
  100. }
  101. public SteamHandle<InputActionMap> currentSteamActionSet
  102. {
  103. get { return SteamSupport.GetAPIAndRequireItToBeSet().GetCurrentActionSet(steamControllerHandle); }
  104. }
  105. protected abstract void ResolveSteamActions(ISteamControllerAPI api);
  106. protected abstract void Update(ISteamControllerAPI api);
  107. // These methods avoid having an 'internal' modifier that every override needs to carry along.
  108. internal void InvokeResolveSteamActions()
  109. {
  110. ResolveSteamActions(SteamSupport.GetAPIAndRequireItToBeSet());
  111. }
  112. internal void InvokeUpdate()
  113. {
  114. Update(SteamSupport.GetAPIAndRequireItToBeSet());
  115. }
  116. public struct SteamActionSetInfo
  117. {
  118. public string name { get; set; }
  119. public SteamHandle<InputActionMap> handle { get; set; }
  120. }
  121. }
  122. }
  123. #endif // (UNITY_STANDALONE || UNITY_EDITOR) && UNITY_ENABLE_STEAM_CONTROLLER_SUPPORT