1
0

SteamVR_Behaviour_Vector3.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. public class SteamVR_Behaviour_Vector3 : MonoBehaviour
  11. {
  12. /// <summary>The vector3 action to get data from</summary>
  13. public SteamVR_Action_Vector3 vector3Action;
  14. /// <summary>The device this action applies to. Any if the action is not device specific.</summary>
  15. [Tooltip("The device this action should apply to. Any if the action is not device specific.")]
  16. public SteamVR_Input_Sources inputSource;
  17. /// <summary>Unity event that fires whenever the action's value has changed since the last update.</summary>
  18. [Tooltip("Fires whenever the action's value has changed since the last update.")]
  19. public SteamVR_Behaviour_Vector3Event onChange;
  20. /// <summary>Unity event that fires whenever the action's value has been updated</summary>
  21. [Tooltip("Fires whenever the action's value has been updated.")]
  22. public SteamVR_Behaviour_Vector3Event onUpdate;
  23. /// <summary>Unity event that fires whenever the action's value has been updated and is non-zero</summary>
  24. [Tooltip("Fires whenever the action's value has been updated and is non-zero.")]
  25. public SteamVR_Behaviour_Vector3Event onAxis;
  26. /// <summary>C# event that fires whenever the action's value has changed since the last update.</summary>
  27. public ChangeHandler onChangeEvent;
  28. /// <summary>C# event that fires whenever the action's value has been updated</summary>
  29. public UpdateHandler onUpdateEvent;
  30. /// <summary>C# event that fires whenever the action's value has been updated and is non-zero</summary>
  31. public AxisHandler onAxisEvent;
  32. /// <summary>Returns whether this action is bound and the action set is active</summary>
  33. public bool isActive { get { return vector3Action.GetActive(inputSource); } }
  34. protected virtual void OnEnable()
  35. {
  36. if (vector3Action == null)
  37. {
  38. Debug.LogError("[SteamVR] Vector3 action not set.", this);
  39. return;
  40. }
  41. AddHandlers();
  42. }
  43. protected virtual void OnDisable()
  44. {
  45. RemoveHandlers();
  46. }
  47. protected void AddHandlers()
  48. {
  49. vector3Action[inputSource].onUpdate += SteamVR_Behaviour_Vector3_OnUpdate;
  50. vector3Action[inputSource].onChange += SteamVR_Behaviour_Vector3_OnChange;
  51. vector3Action[inputSource].onAxis += SteamVR_Behaviour_Vector3_OnAxis;
  52. }
  53. protected void RemoveHandlers()
  54. {
  55. if (vector3Action != null)
  56. {
  57. vector3Action[inputSource].onUpdate -= SteamVR_Behaviour_Vector3_OnUpdate;
  58. vector3Action[inputSource].onChange -= SteamVR_Behaviour_Vector3_OnChange;
  59. vector3Action[inputSource].onAxis -= SteamVR_Behaviour_Vector3_OnAxis;
  60. }
  61. }
  62. private void SteamVR_Behaviour_Vector3_OnUpdate(SteamVR_Action_Vector3 fromAction, SteamVR_Input_Sources fromSource, Vector3 newAxis, Vector3 newDelta)
  63. {
  64. if (onUpdate != null)
  65. {
  66. onUpdate.Invoke(this, fromSource, newAxis, newDelta);
  67. }
  68. if (onUpdateEvent != null)
  69. {
  70. onUpdateEvent.Invoke(this, fromSource, newAxis, newDelta);
  71. }
  72. }
  73. private void SteamVR_Behaviour_Vector3_OnChange(SteamVR_Action_Vector3 fromAction, SteamVR_Input_Sources fromSource, Vector3 newAxis, Vector3 newDelta)
  74. {
  75. if (onChange != null)
  76. {
  77. onChange.Invoke(this, fromSource, newAxis, newDelta);
  78. }
  79. if (onChangeEvent != null)
  80. {
  81. onChangeEvent.Invoke(this, fromSource, newAxis, newDelta);
  82. }
  83. }
  84. private void SteamVR_Behaviour_Vector3_OnAxis(SteamVR_Action_Vector3 fromAction, SteamVR_Input_Sources fromSource, Vector3 newAxis, Vector3 newDelta)
  85. {
  86. if (onAxis != null)
  87. {
  88. onAxis.Invoke(this, fromSource, newAxis, newDelta);
  89. }
  90. if (onAxisEvent != null)
  91. {
  92. onAxisEvent.Invoke(this, fromSource, newAxis, newDelta);
  93. }
  94. }
  95. /// <summary>
  96. /// Gets the localized name of the device that the action corresponds to.
  97. /// </summary>
  98. /// <param name="localizedParts">
  99. /// <list type="bullet">
  100. /// <item><description>VRInputString_Hand - Which hand the origin is in. E.g. "Left Hand"</description></item>
  101. /// <item><description>VRInputString_ControllerType - What kind of controller the user has in that hand.E.g. "Vive Controller"</description></item>
  102. /// <item><description>VRInputString_InputSource - What part of that controller is the origin. E.g. "Trackpad"</description></item>
  103. /// <item><description>VRInputString_All - All of the above. E.g. "Left Hand Vive Controller Trackpad"</description></item>
  104. /// </list>
  105. /// </param>
  106. public string GetLocalizedName(params EVRInputStringBits[] localizedParts)
  107. {
  108. if (vector3Action != null)
  109. return vector3Action.GetLocalizedOriginPart(inputSource, localizedParts);
  110. return null;
  111. }
  112. public delegate void AxisHandler(SteamVR_Behaviour_Vector3 fromAction, SteamVR_Input_Sources fromSource, Vector3 newAxis, Vector3 newDelta);
  113. public delegate void ChangeHandler(SteamVR_Behaviour_Vector3 fromAction, SteamVR_Input_Sources fromSource, Vector3 newAxis, Vector3 newDelta);
  114. public delegate void UpdateHandler(SteamVR_Behaviour_Vector3 fromAction, SteamVR_Input_Sources fromSource, Vector3 newAxis, Vector3 newDelta);
  115. }
  116. }