2
0

SteamVR_Behaviour_Vector2.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /// Simplifies the use of the Vector2 action. Provides an onChange event that fires whenever the vector2 changes.
  12. /// </summary>
  13. public class SteamVR_Behaviour_Vector2 : MonoBehaviour
  14. {
  15. /// <summary>The vector2 action to get data from</summary>
  16. public SteamVR_Action_Vector2 vector2Action;
  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_Vector2Event 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_Vector2Event 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_Vector2Event 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 vector2Action.GetActive(inputSource); } }
  37. protected virtual void OnEnable()
  38. {
  39. if (vector2Action == null)
  40. {
  41. Debug.LogError("[SteamVR] Vector2 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. vector2Action[inputSource].onUpdate += SteamVR_Behaviour_Vector2_OnUpdate;
  53. vector2Action[inputSource].onChange += SteamVR_Behaviour_Vector2_OnChange;
  54. vector2Action[inputSource].onAxis += SteamVR_Behaviour_Vector2_OnAxis;
  55. }
  56. protected void RemoveHandlers()
  57. {
  58. if (vector2Action != null)
  59. {
  60. vector2Action[inputSource].onUpdate -= SteamVR_Behaviour_Vector2_OnUpdate;
  61. vector2Action[inputSource].onChange -= SteamVR_Behaviour_Vector2_OnChange;
  62. vector2Action[inputSource].onAxis -= SteamVR_Behaviour_Vector2_OnAxis;
  63. }
  64. }
  65. private void SteamVR_Behaviour_Vector2_OnUpdate(SteamVR_Action_Vector2 fromAction, SteamVR_Input_Sources fromSource, Vector2 newAxis, Vector2 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_Vector2_OnChange(SteamVR_Action_Vector2 fromAction, SteamVR_Input_Sources fromSource, Vector2 newAxis, Vector2 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_Vector2_OnAxis(SteamVR_Action_Vector2 fromAction, SteamVR_Input_Sources fromSource, Vector2 newAxis, Vector2 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 (vector2Action != null)
  112. return vector2Action.GetLocalizedOriginPart(inputSource, localizedParts);
  113. return null;
  114. }
  115. public delegate void AxisHandler(SteamVR_Behaviour_Vector2 fromAction, SteamVR_Input_Sources fromSource, Vector2 newAxis, Vector2 newDelta);
  116. public delegate void ChangeHandler(SteamVR_Behaviour_Vector2 fromAction, SteamVR_Input_Sources fromSource, Vector2 newAxis, Vector2 newDelta);
  117. public delegate void UpdateHandler(SteamVR_Behaviour_Vector2 fromAction, SteamVR_Input_Sources fromSource, Vector2 newAxis, Vector2 newDelta);
  118. }
  119. }