SteamVR_Behaviour_Single.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. namespace Valve.VR
  9. {
  10. /// <summary>
  11. /// SteamVR_Behaviour_Single simplifies the use of single actions. It gives an event to subscribe to for when the action has changed.
  12. /// </summary>
  13. public class SteamVR_Behaviour_Single : MonoBehaviour
  14. {
  15. /// <summary>The single action to get data from.</summary>
  16. public SteamVR_Action_Single singleAction;
  17. /// <summary>The device this action applies to. Any if the action is not device specific.</summary>
  18. [Tooltip("The device this action should apply to. Any if the action is not device specific.")]
  19. public SteamVR_Input_Sources inputSource;
  20. /// <summary>Unity event that Fires whenever the action's value has changed since the last update.</summary>
  21. [Tooltip("Fires whenever the action's value has changed since the last update.")]
  22. public SteamVR_Behaviour_SingleEvent onChange;
  23. /// <summary>Unity event that Fires whenever the action's value has been updated</summary>
  24. [Tooltip("Fires whenever the action's value has been updated.")]
  25. public SteamVR_Behaviour_SingleEvent onUpdate;
  26. /// <summary>Unity event that Fires whenever the action's value has been updated and is non-zero</summary>
  27. [Tooltip("Fires whenever the action's value has been updated and is non-zero.")]
  28. public SteamVR_Behaviour_SingleEvent onAxis;
  29. /// <summary>C# event that fires whenever the action's value has changed since the last update.</summary>
  30. public ChangeHandler onChangeEvent;
  31. /// <summary>C# event that fires whenever the action's value has been updated</summary>
  32. public UpdateHandler onUpdateEvent;
  33. /// <summary>C# event that fires whenever the action's value has been updated and is non-zero</summary>
  34. public AxisHandler onAxisEvent;
  35. /// <summary>Returns whether this action is bound and the action set is active</summary>
  36. public bool isActive { get { return singleAction.GetActive(inputSource); } }
  37. protected virtual void OnEnable()
  38. {
  39. if (singleAction == null)
  40. {
  41. Debug.LogError("[SteamVR] Single action not set.", this);
  42. return;
  43. }
  44. AddHandlers();
  45. }
  46. protected virtual void OnDisable()
  47. {
  48. RemoveHandlers();
  49. }
  50. protected void AddHandlers()
  51. {
  52. singleAction[inputSource].onUpdate += SteamVR_Behaviour_Single_OnUpdate;
  53. singleAction[inputSource].onChange += SteamVR_Behaviour_Single_OnChange;
  54. singleAction[inputSource].onAxis += SteamVR_Behaviour_Single_OnAxis;
  55. }
  56. protected void RemoveHandlers()
  57. {
  58. if (singleAction != null)
  59. {
  60. singleAction[inputSource].onUpdate -= SteamVR_Behaviour_Single_OnUpdate;
  61. singleAction[inputSource].onChange -= SteamVR_Behaviour_Single_OnChange;
  62. singleAction[inputSource].onAxis -= SteamVR_Behaviour_Single_OnAxis;
  63. }
  64. }
  65. private void SteamVR_Behaviour_Single_OnUpdate(SteamVR_Action_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta)
  66. {
  67. if (onUpdate != null)
  68. {
  69. onUpdate.Invoke(this, fromSource, newAxis, newDelta);
  70. }
  71. if (onUpdateEvent != null)
  72. {
  73. onUpdateEvent.Invoke(this, fromSource, newAxis, newDelta);
  74. }
  75. }
  76. private void SteamVR_Behaviour_Single_OnChange(SteamVR_Action_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta)
  77. {
  78. if (onChange != null)
  79. {
  80. onChange.Invoke(this, fromSource, newAxis, newDelta);
  81. }
  82. if (onChangeEvent != null)
  83. {
  84. onChangeEvent.Invoke(this, fromSource, newAxis, newDelta);
  85. }
  86. }
  87. private void SteamVR_Behaviour_Single_OnAxis(SteamVR_Action_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta)
  88. {
  89. if (onAxis != null)
  90. {
  91. onAxis.Invoke(this, fromSource, newAxis, newDelta);
  92. }
  93. if (onAxisEvent != null)
  94. {
  95. onAxisEvent.Invoke(this, fromSource, newAxis, newDelta);
  96. }
  97. }
  98. /// <summary>
  99. /// Gets the localized name of the device that the action corresponds to.
  100. /// </summary>
  101. /// <param name="localizedParts">
  102. /// <list type="bullet">
  103. /// <item><description>VRInputString_Hand - Which hand the origin is in. E.g. "Left Hand"</description></item>
  104. /// <item><description>VRInputString_ControllerType - What kind of controller the user has in that hand.E.g. "Vive Controller"</description></item>
  105. /// <item><description>VRInputString_InputSource - What part of that controller is the origin. E.g. "Trackpad"</description></item>
  106. /// <item><description>VRInputString_All - All of the above. E.g. "Left Hand Vive Controller Trackpad"</description></item>
  107. /// </list>
  108. /// </param>
  109. public string GetLocalizedName(params EVRInputStringBits[] localizedParts)
  110. {
  111. if (singleAction != null)
  112. return singleAction.GetLocalizedOriginPart(inputSource, localizedParts);
  113. return null;
  114. }
  115. public delegate void AxisHandler(SteamVR_Behaviour_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta);
  116. public delegate void ChangeHandler(SteamVR_Behaviour_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta);
  117. public delegate void UpdateHandler(SteamVR_Behaviour_Single fromAction, SteamVR_Input_Sources fromSource, float newAxis, float newDelta);
  118. }
  119. }