2
0

SteamVR_Behaviour_Boolean.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. namespace Valve.VR
  9. {
  10. /// <summary>
  11. /// This component simplifies using boolean actions.
  12. /// <para>Provides editor accessible events: OnPress, OnPressDown, OnPressUp, OnChange, and OnUpdate.</para>
  13. /// <para>Provides script accessible events: OnPressEvent, OnPressDownEvent, OnPressUpEvent, OnChangeEvent, and OnUpdateEvent.</para>
  14. /// </summary>
  15. public class SteamVR_Behaviour_Boolean : MonoBehaviour
  16. {
  17. [Tooltip("The SteamVR boolean action that this component should use")]
  18. public SteamVR_Action_Boolean booleanAction;
  19. [Tooltip("The device this action should apply to. Any if the action is not device specific.")]
  20. public SteamVR_Input_Sources inputSource;
  21. /// <summary>This UnityEvent fires whenever a change happens in the action</summary>
  22. public SteamVR_Behaviour_BooleanEvent onChange;
  23. /// <summary>This C# event fires whenever a change happens in the action</summary>
  24. public event ChangeHandler onChangeEvent;
  25. /// <summary>This UnityEvent fires whenever the action is updated</summary>
  26. public SteamVR_Behaviour_BooleanEvent onUpdate;
  27. /// <summary>This C# event fires whenever the action is updated</summary>
  28. public event UpdateHandler onUpdateEvent;
  29. /// <summary>This UnityEvent will fire whenever the boolean action is true and gets updated</summary>
  30. public SteamVR_Behaviour_BooleanEvent onPress;
  31. /// <summary>This C# event will fire whenever the boolean action is true and gets updated</summary>
  32. public event StateHandler onPressEvent;
  33. /// <summary>This UnityEvent will fire whenever the boolean action has changed from false to true in the last update</summary>
  34. public SteamVR_Behaviour_BooleanEvent onPressDown;
  35. /// <summary>This C# event will fire whenever the boolean action has changed from false to true in the last update</summary>
  36. public event StateDownHandler onPressDownEvent;
  37. /// <summary>This UnityEvent will fire whenever the boolean action has changed from true to false in the last update</summary>
  38. public SteamVR_Behaviour_BooleanEvent onPressUp;
  39. /// <summary>This C# event will fire whenever the boolean action has changed from true to false in the last update</summary>
  40. public event StateUpHandler onPressUpEvent;
  41. /// <summary>Returns true if this action is currently bound and its action set is active</summary>
  42. public bool isActive { get { return booleanAction[inputSource].active; } }
  43. /// <summary>Returns the action set that this action is in.</summary>
  44. public SteamVR_ActionSet actionSet { get { if (booleanAction != null) return booleanAction.actionSet; else return null; } }
  45. protected virtual void OnEnable()
  46. {
  47. if (booleanAction == null)
  48. {
  49. Debug.LogError("[SteamVR] Boolean action not set.", this);
  50. return;
  51. }
  52. AddHandlers();
  53. }
  54. protected virtual void OnDisable()
  55. {
  56. RemoveHandlers();
  57. }
  58. protected void AddHandlers()
  59. {
  60. booleanAction[inputSource].onUpdate += SteamVR_Behaviour_Boolean_OnUpdate;
  61. booleanAction[inputSource].onChange += SteamVR_Behaviour_Boolean_OnChange;
  62. booleanAction[inputSource].onState += SteamVR_Behaviour_Boolean_OnState;
  63. booleanAction[inputSource].onStateDown += SteamVR_Behaviour_Boolean_OnStateDown;
  64. booleanAction[inputSource].onStateUp += SteamVR_Behaviour_Boolean_OnStateUp;
  65. }
  66. protected void RemoveHandlers()
  67. {
  68. if (booleanAction != null)
  69. {
  70. booleanAction[inputSource].onUpdate -= SteamVR_Behaviour_Boolean_OnUpdate;
  71. booleanAction[inputSource].onChange -= SteamVR_Behaviour_Boolean_OnChange;
  72. booleanAction[inputSource].onState -= SteamVR_Behaviour_Boolean_OnState;
  73. booleanAction[inputSource].onStateDown -= SteamVR_Behaviour_Boolean_OnStateDown;
  74. booleanAction[inputSource].onStateUp -= SteamVR_Behaviour_Boolean_OnStateUp;
  75. }
  76. }
  77. private void SteamVR_Behaviour_Boolean_OnStateUp(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  78. {
  79. if (onPressUp != null)
  80. {
  81. onPressUp.Invoke(this, fromSource, false);
  82. }
  83. if (onPressUpEvent != null)
  84. {
  85. onPressUpEvent.Invoke(this, fromSource);
  86. }
  87. }
  88. private void SteamVR_Behaviour_Boolean_OnStateDown(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  89. {
  90. if (onPressDown != null)
  91. {
  92. onPressDown.Invoke(this, fromSource, true);
  93. }
  94. if (onPressDownEvent != null)
  95. {
  96. onPressDownEvent.Invoke(this, fromSource);
  97. }
  98. }
  99. private void SteamVR_Behaviour_Boolean_OnState(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  100. {
  101. if (onPress != null)
  102. {
  103. onPress.Invoke(this, fromSource, true);
  104. }
  105. if (onPressEvent != null)
  106. {
  107. onPressEvent.Invoke(this, fromSource);
  108. }
  109. }
  110. private void SteamVR_Behaviour_Boolean_OnUpdate(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource, bool newState)
  111. {
  112. if (onUpdate != null)
  113. {
  114. onUpdate.Invoke(this, fromSource, newState);
  115. }
  116. if (onUpdateEvent != null)
  117. {
  118. onUpdateEvent.Invoke(this, fromSource, newState);
  119. }
  120. }
  121. private void SteamVR_Behaviour_Boolean_OnChange(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource, bool newState)
  122. {
  123. if (onChange != null)
  124. {
  125. onChange.Invoke(this, fromSource, newState);
  126. }
  127. if (onChangeEvent != null)
  128. {
  129. onChangeEvent.Invoke(this, fromSource, newState);
  130. }
  131. }
  132. /// <summary>
  133. /// Gets the localized name of the device that the action corresponds to.
  134. /// </summary>
  135. /// <param name="localizedParts">
  136. /// <list type="bullet">
  137. /// <item><description>VRInputString_Hand - Which hand the origin is in. E.g. "Left Hand"</description></item>
  138. /// <item><description>VRInputString_ControllerType - What kind of controller the user has in that hand.E.g. "Vive Controller"</description></item>
  139. /// <item><description>VRInputString_InputSource - What part of that controller is the origin. E.g. "Trackpad"</description></item>
  140. /// <item><description>VRInputString_All - All of the above. E.g. "Left Hand Vive Controller Trackpad"</description></item>
  141. /// </list>
  142. /// </param>
  143. public string GetLocalizedName(params EVRInputStringBits[] localizedParts)
  144. {
  145. if (booleanAction != null)
  146. return booleanAction.GetLocalizedOriginPart(inputSource, localizedParts);
  147. return null;
  148. }
  149. public delegate void StateDownHandler(SteamVR_Behaviour_Boolean fromAction, SteamVR_Input_Sources fromSource);
  150. public delegate void StateUpHandler(SteamVR_Behaviour_Boolean fromAction, SteamVR_Input_Sources fromSource);
  151. public delegate void StateHandler(SteamVR_Behaviour_Boolean fromAction, SteamVR_Input_Sources fromSource);
  152. public delegate void ActiveChangeHandler(SteamVR_Behaviour_Boolean fromAction, SteamVR_Input_Sources fromSource, bool active);
  153. public delegate void ChangeHandler(SteamVR_Behaviour_Boolean fromAction, SteamVR_Input_Sources fromSource, bool newState);
  154. public delegate void UpdateHandler(SteamVR_Behaviour_Boolean fromAction, SteamVR_Input_Sources fromSource, bool newState);
  155. }
  156. }