From ea7ba809181d1d443ffa8ce1abd942d956bfa91e Mon Sep 17 00:00:00 2001 From: "Theston E. Fox" Date: Sat, 1 Apr 2017 23:20:19 +0100 Subject: [PATCH] feat(ControllerEvents): provide alternative events for obsolete aliases The Controller Events button alias events were deprecated but there were no alternatives for these events. The relevant scripts now emit a controller event which will replace these obsolete alias events. Any public bool that was used for an alias state has also been replicated in the relevant script. All obsolete messages now provide a message which denotes what the alternative method or parameter should be used. --- .../Scripts/Controller_Hand.cs | 16 +-- .../Interactions/VRTK_ControllerEvents.cs | 42 +++++--- .../Scripts/Interactions/VRTK_InteractGrab.cs | 29 ++++- .../Scripts/Interactions/VRTK_InteractUse.cs | 29 ++++- .../Scripts/Internal/VRTK_VRInputModule.cs | 2 +- Assets/VRTK/Scripts/Pointers/VRTK_Pointer.cs | 98 +++++++++++++++-- Assets/VRTK/Scripts/UI/VRTK_UIPointer.cs | 100 +++++++++++++++++- .../VRTK_InteractGrab_UnityEvents.cs | 16 +++ .../VRTK_InteractUse_UnityEvents.cs | 16 +++ .../UnityEvents/VRTK_Pointer_UnityEvents.cs | 46 ++++++++ .../VRTK_Pointer_UnityEvents.cs.meta | 12 +++ .../UnityEvents/VRTK_UIPointer_UnityEvents.cs | 32 ++++++ DOCUMENTATION.md | 70 +++++++++--- 13 files changed, 456 insertions(+), 52 deletions(-) create mode 100644 Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_Pointer_UnityEvents.cs create mode 100644 Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_Pointer_UnityEvents.cs.meta diff --git a/Assets/VRTK/Examples/ExampleResources/Scripts/Controller_Hand.cs b/Assets/VRTK/Examples/ExampleResources/Scripts/Controller_Hand.cs index afe6f722c..d5d29345b 100644 --- a/Assets/VRTK/Examples/ExampleResources/Scripts/Controller_Hand.cs +++ b/Assets/VRTK/Examples/ExampleResources/Scripts/Controller_Hand.cs @@ -22,10 +22,10 @@ public enum Hands private void Start() { - GetComponentInParent().ControllerGrabInteractableObject += DoGrabOn; - GetComponentInParent().ControllerUngrabInteractableObject += DoGrabOff; - GetComponentInParent().ControllerUseInteractableObject += DoUseOn; - GetComponentInParent().ControllerUnuseInteractableObject += DoUseOff; + GetComponentInParent().GrabButtonPressed += DoGrabOn; + GetComponentInParent().GrabButtonReleased += DoGrabOff; + GetComponentInParent().UseButtonPressed += DoUseOn; + GetComponentInParent().UseButtonReleased += DoUseOff; var handContainer = "ModelPieces"; pointerFinger = transform.Find(handContainer + "/PointerFingerContainer"); @@ -52,22 +52,22 @@ private void InversePosition(Transform givenTransform) givenTransform.localEulerAngles = new Vector3(givenTransform.localEulerAngles.x, givenTransform.localEulerAngles.y * -1, givenTransform.localEulerAngles.z); } - private void DoGrabOn(object sender, ObjectInteractEventArgs e) + private void DoGrabOn(object sender, ControllerInteractionEventArgs e) { targetGripRotation = maxRotation; } - private void DoGrabOff(object sender, ObjectInteractEventArgs e) + private void DoGrabOff(object sender, ControllerInteractionEventArgs e) { targetGripRotation = originalGripRotation; } - private void DoUseOn(object sender, ObjectInteractEventArgs e) + private void DoUseOn(object sender, ControllerInteractionEventArgs e) { targetPointerRotation = maxRotation; } - private void DoUseOff(object sender, ObjectInteractEventArgs e) + private void DoUseOff(object sender, ControllerInteractionEventArgs e) { targetPointerRotation = originalPointerRotation; } diff --git a/Assets/VRTK/Scripts/Interactions/VRTK_ControllerEvents.cs b/Assets/VRTK/Scripts/Interactions/VRTK_ControllerEvents.cs index b19f82a4e..9d3411de1 100644 --- a/Assets/VRTK/Scripts/Interactions/VRTK_ControllerEvents.cs +++ b/Assets/VRTK/Scripts/Interactions/VRTK_ControllerEvents.cs @@ -218,30 +218,31 @@ public enum ButtonAlias /// This will be true if the button aliased to the pointer is held down. /// [HideInInspector] - [Obsolete("`VRTK_ControllerEvents.pointerPressed` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.pointerPressed` is no longer used, use `VRTK_Pointer.IsActivationButtonPressed()` instead. This parameter will be removed in a future version of VRTK.")] public bool pointerPressed = false; /// /// This will be true if the button aliased to the grab is held down. /// [HideInInspector] - [Obsolete("`VRTK_ControllerEvents.grabPressed` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.grabPressed` is no longer used, use `VRTK_InteractGrab.IsGrabButtonPressed()` instead. This parameter will be removed in a future version of VRTK.")] public bool grabPressed = false; /// /// This will be true if the button aliased to the use is held down. /// [HideInInspector] - [Obsolete("`VRTK_ControllerEvents.usePressed` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.usePressed` is no longer used, use `VRTK_InteractUse.IsUseButtonPressed()` instead. This parameter will be removed in a future version of VRTK.")] public bool usePressed = false; /// /// This will be true if the button aliased to the UI click is held down. /// [HideInInspector] - [Obsolete("`VRTK_ControllerEvents.uiClickPressed` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.uiClickPressed` is no longer used, use `VRTK_UIPointer.IsSelectionButtonPressed()` instead. This parameter will be removed in a future version of VRTK.")] public bool uiClickPressed = false; /// /// This will be true if the button aliased to the menu is held down. /// [HideInInspector] + [Obsolete("`VRTK_ControllerEvents.menuPressed` is no longer used, use `VRTK_ControllerEvents.buttonTwoPressed` instead. This parameter will be removed in a future version of VRTK.")] public bool menuPressed = false; /// @@ -401,61 +402,61 @@ public enum ButtonAlias /// /// Emitted when the pointer toggle alias button is pressed. /// - [Obsolete("`VRTK_ControllerEvents.AliasPointerOn` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.AliasPointerOn` has been replaced with `VRTK_Pointer.ActivationButtonPressed`. This parameter will be removed in a future version of VRTK.")] public event ControllerInteractionEventHandler AliasPointerOn; /// /// Emitted when the pointer toggle alias button is released. /// - [Obsolete("`VRTK_ControllerEvents.AliasPointerOff` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.AliasPointerOff` has been replaced with `VRTK_Pointer.ActivationButtonReleased`. This parameter will be removed in a future version of VRTK.")] public event ControllerInteractionEventHandler AliasPointerOff; /// /// Emitted when the pointer set alias button is released. /// - [Obsolete("`VRTK_ControllerEvents.AliasPointerSet` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.AliasPointerSet` has been replaced with `VRTK_Pointer.SelectionButtonReleased`. This parameter will be removed in a future version of VRTK.")] public event ControllerInteractionEventHandler AliasPointerSet; /// /// Emitted when the grab toggle alias button is pressed. /// - [Obsolete("`VRTK_ControllerEvents.AliasGrabOn` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.AliasGrabOn` has been replaced with `VRTK_InteractGrab.GrabButtonPressed`. This parameter will be removed in a future version of VRTK.")] public event ControllerInteractionEventHandler AliasGrabOn; /// /// Emitted when the grab toggle alias button is released. /// - [Obsolete("`VRTK_ControllerEvents.AliasGrabOff` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.AliasGrabOff` has been replaced with `VRTK_InteractGrab.GrabButtonReleased`. This parameter will be removed in a future version of VRTK.")] public event ControllerInteractionEventHandler AliasGrabOff; /// /// Emitted when the use toggle alias button is pressed. /// - [Obsolete("`VRTK_ControllerEvents.AliasUseOn` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.AliasUseOn` has been replaced with `VRTK_InteractUse.UseButtonPressed`. This parameter will be removed in a future version of VRTK.")] public event ControllerInteractionEventHandler AliasUseOn; /// /// Emitted when the use toggle alias button is released. /// - [Obsolete("`VRTK_ControllerEvents.AliasUseOff` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.AliasUseOff` has been replaced with `VRTK_InteractUse.UseButtonReleased`. This parameter will be removed in a future version of VRTK.")] public event ControllerInteractionEventHandler AliasUseOff; /// /// Emitted when the menu toggle alias button is pressed. /// - [Obsolete("`VRTK_ControllerEvents.AliasMenuOn` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.AliasMenuOn` is no longer used, use `VRTK_ControllerEvents.ButtonTwoPressed` instead. This parameter will be removed in a future version of VRTK.")] public event ControllerInteractionEventHandler AliasMenuOn; /// /// Emitted when the menu toggle alias button is released. /// - [Obsolete("`VRTK_ControllerEvents.AliasMenuOff` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.AliasMenuOff` is no longer used, use `VRTK_ControllerEvents.ButtonTwoReleased` instead. This parameter will be removed in a future version of VRTK.")] public event ControllerInteractionEventHandler AliasMenuOff; /// /// Emitted when the UI click alias button is pressed. /// - [Obsolete("`VRTK_ControllerEvents.AliasUIClickOn` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.AliasUIClickOn` has been replaced with `VRTK_UIPointer.SelectionButtonPressed`. This parameter will be removed in a future version of VRTK.")] public event ControllerInteractionEventHandler AliasUIClickOn; /// /// Emitted when the UI click alias button is released. /// - [Obsolete("`VRTK_ControllerEvents.AliasUIClickOff` is no longer used. This parameter will be removed in a future version of VRTK.")] + [Obsolete("`VRTK_ControllerEvents.AliasUIClickOff` has been replaced with `VRTK_UIPointer.SelectionButtonReleased`. This parameter will be removed in a future version of VRTK.")] public event ControllerInteractionEventHandler AliasUIClickOff; /// @@ -750,6 +751,7 @@ public virtual void OnStartMenuReleased(ControllerInteractionEventArgs e) } } + [Obsolete("`VRTK_ControllerEvents.OnAliasPointerOn` has been replaced with `VRTK_Pointer.OnActivationButtonPressed`. This method will be removed in a future version of VRTK.")] public virtual void OnAliasPointerOn(ControllerInteractionEventArgs e) { if (AliasPointerOn != null) @@ -758,6 +760,7 @@ public virtual void OnAliasPointerOn(ControllerInteractionEventArgs e) } } + [Obsolete("`VRTK_ControllerEvents.OnAliasPointerOff` has been replaced with `VRTK_Pointer.OnActivationButtonReleased`. This method will be removed in a future version of VRTK.")] public virtual void OnAliasPointerOff(ControllerInteractionEventArgs e) { if (AliasPointerOff != null) @@ -766,6 +769,7 @@ public virtual void OnAliasPointerOff(ControllerInteractionEventArgs e) } } + [Obsolete("`VRTK_ControllerEvents.OnAliasPointerSet` has been replaced with `VRTK_Pointer.OnSelectionButtonReleased`. This method will be removed in a future version of VRTK.")] public virtual void OnAliasPointerSet(ControllerInteractionEventArgs e) { if (AliasPointerSet != null) @@ -774,6 +778,7 @@ public virtual void OnAliasPointerSet(ControllerInteractionEventArgs e) } } + [Obsolete("`VRTK_ControllerEvents.OnAliasGrabOn` has been replaced with `VRTK_InteractGrab.OnGrabButtonPressed`. This method will be removed in a future version of VRTK.")] public virtual void OnAliasGrabOn(ControllerInteractionEventArgs e) { if (AliasGrabOn != null) @@ -782,6 +787,7 @@ public virtual void OnAliasGrabOn(ControllerInteractionEventArgs e) } } + [Obsolete("`VRTK_ControllerEvents.OnAliasGrabOff` has been replaced with `VRTK_InteractGrab.OnGrabButtonReleased`. This method will be removed in a future version of VRTK.")] public virtual void OnAliasGrabOff(ControllerInteractionEventArgs e) { if (AliasGrabOff != null) @@ -790,6 +796,7 @@ public virtual void OnAliasGrabOff(ControllerInteractionEventArgs e) } } + [Obsolete("`VRTK_ControllerEvents.OnAliasUseOn` has been replaced with `VRTK_InteractUse.OnUseButtonPressed`. This method will be removed in a future version of VRTK.")] public virtual void OnAliasUseOn(ControllerInteractionEventArgs e) { if (AliasUseOn != null) @@ -798,6 +805,7 @@ public virtual void OnAliasUseOn(ControllerInteractionEventArgs e) } } + [Obsolete("`VRTK_ControllerEvents.OnAliasUseOff` has been replaced with `VRTK_InteractUse.OnUseButtonReleased`. This method will be removed in a future version of VRTK.")] public virtual void OnAliasUseOff(ControllerInteractionEventArgs e) { if (AliasUseOff != null) @@ -806,6 +814,7 @@ public virtual void OnAliasUseOff(ControllerInteractionEventArgs e) } } + [Obsolete("`VRTK_ControllerEvents.OnAliasUIClickOn` has been replaced with `VRTK_UIPointer.OnSelectionButtonPressed`. This method will be removed in a future version of VRTK.")] public virtual void OnAliasUIClickOn(ControllerInteractionEventArgs e) { if (AliasUIClickOn != null) @@ -814,6 +823,7 @@ public virtual void OnAliasUIClickOn(ControllerInteractionEventArgs e) } } + [Obsolete("`VRTK_ControllerEvents.OnAliasUIClickOff` has been replaced with `VRTK_UIPointer.OnSelectionButtonReleased`. This method will be removed in a future version of VRTK.")] public virtual void OnAliasUIClickOff(ControllerInteractionEventArgs e) { if (AliasUIClickOff != null) @@ -822,6 +832,7 @@ public virtual void OnAliasUIClickOff(ControllerInteractionEventArgs e) } } + [Obsolete("`VRTK_ControllerEvents.OnAliasMenuOn` has been replaced with `VRTK_ControllerEvents.OnButtonTwoPressed`. This method will be removed in a future version of VRTK.")] public virtual void OnAliasMenuOn(ControllerInteractionEventArgs e) { if (AliasMenuOn != null) @@ -830,6 +841,7 @@ public virtual void OnAliasMenuOn(ControllerInteractionEventArgs e) } } + [Obsolete("`VRTK_ControllerEvents.OnAliasMenuOff` has been replaced with `VRTK_ControllerEvents.OnButtonTwoReleased`. This method will be removed in a future version of VRTK.")] public virtual void OnAliasMenuOff(ControllerInteractionEventArgs e) { if (AliasMenuOff != null) diff --git a/Assets/VRTK/Scripts/Interactions/VRTK_InteractGrab.cs b/Assets/VRTK/Scripts/Interactions/VRTK_InteractGrab.cs index a0d52d1cf..a79635bfa 100644 --- a/Assets/VRTK/Scripts/Interactions/VRTK_InteractGrab.cs +++ b/Assets/VRTK/Scripts/Interactions/VRTK_InteractGrab.cs @@ -48,6 +48,15 @@ public class VRTK_InteractGrab : MonoBehaviour [Tooltip("The Interact Touch to listen for touches on. If the script is being applied onto a controller then this parameter can be left blank as it will be auto populated by the controller the script is on at runtime.")] public VRTK_InteractTouch interactTouch; + /// + /// Emitted when the grab button is pressed. + /// + public event ControllerInteractionEventHandler GrabButtonPressed; + /// + /// Emitted when the grab button is released. + /// + public event ControllerInteractionEventHandler GrabButtonReleased; + /// /// Emitted when a valid object is grabbed. /// @@ -83,6 +92,22 @@ public virtual void OnControllerUngrabInteractableObject(ObjectInteractEventArgs } } + public virtual void OnGrabButtonPressed(ControllerInteractionEventArgs e) + { + if (GrabButtonPressed != null) + { + GrabButtonPressed(this, e); + } + } + + public virtual void OnGrabButtonReleased(ControllerInteractionEventArgs e) + { + if (GrabButtonReleased != null) + { + GrabButtonReleased(this, e); + } + } + /// /// The IsGrabButtonPressed method determines whether the current grab alias button is being pressed down. /// @@ -479,14 +504,14 @@ protected virtual void AttemptReleaseObject() protected virtual void DoGrabObject(object sender, ControllerInteractionEventArgs e) { - grabPressed = true; + OnGrabButtonPressed(controllerEvents.SetControllerEvent(ref grabPressed, true)); AttemptGrabObject(); } protected virtual void DoReleaseObject(object sender, ControllerInteractionEventArgs e) { AttemptReleaseObject(); - grabPressed = false; + OnGrabButtonReleased(controllerEvents.SetControllerEvent(ref grabPressed, false)); } protected virtual void CheckControllerAttachPointSet() diff --git a/Assets/VRTK/Scripts/Interactions/VRTK_InteractUse.cs b/Assets/VRTK/Scripts/Interactions/VRTK_InteractUse.cs index dd440b8a4..4805889a9 100644 --- a/Assets/VRTK/Scripts/Interactions/VRTK_InteractUse.cs +++ b/Assets/VRTK/Scripts/Interactions/VRTK_InteractUse.cs @@ -36,6 +36,15 @@ public class VRTK_InteractUse : MonoBehaviour [Tooltip("The Interact Grab to listen for grab actions on. If the script is being applied onto a controller then this parameter can be left blank as it will be auto populated by the controller the script is on at runtime.")] public VRTK_InteractGrab interactGrab; + /// + /// Emitted when the use toggle alias button is pressed. + /// + public event ControllerInteractionEventHandler UseButtonPressed; + /// + /// Emitted when the use toggle alias button is released. + /// + public event ControllerInteractionEventHandler UseButtonReleased; + /// /// Emitted when a valid object starts being used. /// @@ -67,6 +76,22 @@ public virtual void OnControllerUnuseInteractableObject(ObjectInteractEventArgs } } + public virtual void OnUseButtonPressed(ControllerInteractionEventArgs e) + { + if (UseButtonPressed != null) + { + UseButtonPressed(this, e); + } + } + + public virtual void OnUseButtonReleased(ControllerInteractionEventArgs e) + { + if (UseButtonReleased != null) + { + UseButtonReleased(this, e); + } + } + /// /// The IsUsebuttonPressed method determines whether the current use alias button is being pressed down. /// @@ -340,7 +365,7 @@ protected virtual void AttemptUseObject() protected virtual void DoStartUseObject(object sender, ControllerInteractionEventArgs e) { - usePressed = true; + OnUseButtonPressed(controllerEvents.SetControllerEvent(ref usePressed, true)); AttemptUseObject(); } @@ -350,7 +375,7 @@ protected virtual void DoStopUseObject(object sender, ControllerInteractionEvent { StopUsing(); } - usePressed = false; + OnUseButtonReleased(controllerEvents.SetControllerEvent(ref usePressed, false)); } } } \ No newline at end of file diff --git a/Assets/VRTK/Scripts/Internal/VRTK_VRInputModule.cs b/Assets/VRTK/Scripts/Internal/VRTK_VRInputModule.cs index 8dbc1642f..f8defa0b0 100644 --- a/Assets/VRTK/Scripts/Internal/VRTK_VRInputModule.cs +++ b/Assets/VRTK/Scripts/Internal/VRTK_VRInputModule.cs @@ -260,7 +260,7 @@ protected virtual bool AttemptClick(VRTK_UIPointer pointer) protected virtual void Drag(VRTK_UIPointer pointer, List results) { - pointer.pointerEventData.dragging = pointer.SelectionButtonActive() && pointer.pointerEventData.delta != Vector2.zero; + pointer.pointerEventData.dragging = pointer.IsSelectionButtonPressed() && pointer.pointerEventData.delta != Vector2.zero; if (pointer.pointerEventData.pointerDrag) { diff --git a/Assets/VRTK/Scripts/Pointers/VRTK_Pointer.cs b/Assets/VRTK/Scripts/Pointers/VRTK_Pointer.cs index 2459268e9..735c2e45c 100644 --- a/Assets/VRTK/Scripts/Pointers/VRTK_Pointer.cs +++ b/Assets/VRTK/Scripts/Pointers/VRTK_Pointer.cs @@ -62,6 +62,24 @@ public class VRTK_Pointer : VRTK_DestinationMarker [Tooltip("A custom VRTK_PointerDirectionIndicator to use to determine the rotation given to the destination set event.")] public VRTK_PointerDirectionIndicator directionIndicator; + /// + /// Emitted when the pointer activation button is pressed. + /// + public event ControllerInteractionEventHandler ActivationButtonPressed; + /// + /// Emitted when the pointer activation button is released. + /// + public event ControllerInteractionEventHandler ActivationButtonReleased; + /// + /// Emitted when the pointer selection button is pressed. + /// + public event ControllerInteractionEventHandler SelectionButtonPressed; + /// + /// Emitted when the pointer selection button is released. + /// + public event ControllerInteractionEventHandler SelectionButtonReleased; + + protected VRTK_ControllerEvents.ButtonAlias subscribedActivationButton = VRTK_ControllerEvents.ButtonAlias.Undefined; protected VRTK_ControllerEvents.ButtonAlias subscribedSelectionButton = VRTK_ControllerEvents.ButtonAlias.Undefined; protected bool currentSelectOnPress; @@ -75,6 +93,58 @@ public class VRTK_Pointer : VRTK_DestinationMarker protected VRTK_InteractableObject pointerInteractableObject = null; protected Collider currentCollider; protected bool canClickOnHover; + protected bool activationButtonPressed; + protected bool selectionButtonPressed; + + public virtual void OnActivationButtonPressed(ControllerInteractionEventArgs e) + { + if (ActivationButtonPressed != null) + { + ActivationButtonPressed(this, e); + } + } + + public virtual void OnActivationButtonReleased(ControllerInteractionEventArgs e) + { + if (ActivationButtonReleased != null) + { + ActivationButtonReleased(this, e); + } + } + + public virtual void OnSelectionButtonPressed(ControllerInteractionEventArgs e) + { + if (SelectionButtonPressed != null) + { + SelectionButtonPressed(this, e); + } + } + + public virtual void OnSelectionButtonReleased(ControllerInteractionEventArgs e) + { + if (SelectionButtonReleased != null) + { + SelectionButtonReleased(this, e); + } + } + + /// + /// The IsActivationButtonPressed method returns whether the configured activation button is being pressed. + /// + /// Returns true if the activationButton is being pressed. + public virtual bool IsActivationButtonPressed() + { + return activationButtonPressed; + } + + /// + /// The IsSelectionButtonPressed method returns whether the configured activation button is being pressed. + /// + /// Returns true if the selectionButton is being pressed. + public virtual bool IsSelectionButtonPressed() + { + return selectionButtonPressed; + } /// /// The PointerEnter method emits a DestinationMarkerEnter event when the pointer enters a valid object. @@ -356,8 +426,8 @@ protected virtual void SubscribeActivationButton() if (controller) { - controller.SubscribeToButtonAliasEvent(activationButton, true, ActivationButtonPressed); - controller.SubscribeToButtonAliasEvent(activationButton, false, ActivationButtonReleased); + controller.SubscribeToButtonAliasEvent(activationButton, true, DoActivationButtonPressed); + controller.SubscribeToButtonAliasEvent(activationButton, false, DoActivationButtonReleased); subscribedActivationButton = activationButton; } } @@ -366,14 +436,15 @@ protected virtual void UnsubscribeActivationButton() { if (controller && subscribedActivationButton != VRTK_ControllerEvents.ButtonAlias.Undefined) { - controller.UnsubscribeToButtonAliasEvent(subscribedActivationButton, true, ActivationButtonPressed); - controller.UnsubscribeToButtonAliasEvent(subscribedActivationButton, false, ActivationButtonReleased); + controller.UnsubscribeToButtonAliasEvent(subscribedActivationButton, true, DoActivationButtonPressed); + controller.UnsubscribeToButtonAliasEvent(subscribedActivationButton, false, DoActivationButtonReleased); subscribedActivationButton = VRTK_ControllerEvents.ButtonAlias.Undefined; } } - protected virtual void ActivationButtonPressed(object sender, ControllerInteractionEventArgs e) + protected virtual void DoActivationButtonPressed(object sender, ControllerInteractionEventArgs e) { + OnActivationButtonPressed(controller.SetControllerEvent(ref activationButtonPressed, true)); if (EnabledPointerRenderer()) { controllerIndex = e.controllerIndex; @@ -381,7 +452,7 @@ protected virtual void ActivationButtonPressed(object sender, ControllerInteract } } - protected virtual void ActivationButtonReleased(object sender, ControllerInteractionEventArgs e) + protected virtual void DoActivationButtonReleased(object sender, ControllerInteractionEventArgs e) { if (EnabledPointerRenderer()) { @@ -391,6 +462,7 @@ protected virtual void ActivationButtonReleased(object sender, ControllerInterac Toggle(false); } } + OnActivationButtonReleased(controller.SetControllerEvent(ref activationButtonPressed, false)); } protected virtual void SubscribeSelectionButton() @@ -402,6 +474,8 @@ protected virtual void SubscribeSelectionButton() if (controller) { + controller.SubscribeToButtonAliasEvent(selectionButton, true, DoSelectionButtonPressed); + controller.SubscribeToButtonAliasEvent(selectionButton, false, DoSelectionButtonReleased); controller.SubscribeToButtonAliasEvent(selectionButton, selectOnPress, SelectionButtonAction); subscribedSelectionButton = selectionButton; currentSelectOnPress = selectOnPress; @@ -412,11 +486,23 @@ protected virtual void UnsubscribeSelectionButton() { if (controller && subscribedSelectionButton != VRTK_ControllerEvents.ButtonAlias.Undefined) { + controller.UnsubscribeToButtonAliasEvent(selectionButton, true, DoSelectionButtonPressed); + controller.UnsubscribeToButtonAliasEvent(selectionButton, false, DoSelectionButtonReleased); controller.UnsubscribeToButtonAliasEvent(subscribedSelectionButton, currentSelectOnPress, SelectionButtonAction); subscribedSelectionButton = VRTK_ControllerEvents.ButtonAlias.Undefined; } } + protected virtual void DoSelectionButtonPressed(object sender, ControllerInteractionEventArgs e) + { + OnSelectionButtonPressed(controller.SetControllerEvent(ref selectionButtonPressed, true)); + } + + protected virtual void DoSelectionButtonReleased(object sender, ControllerInteractionEventArgs e) + { + OnSelectionButtonReleased(controller.SetControllerEvent(ref selectionButtonPressed, false)); + } + protected virtual void SelectionButtonAction(object sender, ControllerInteractionEventArgs e) { controllerIndex = e.controllerIndex; diff --git a/Assets/VRTK/Scripts/UI/VRTK_UIPointer.cs b/Assets/VRTK/Scripts/UI/VRTK_UIPointer.cs index b11b84ceb..ab8d36af9 100644 --- a/Assets/VRTK/Scripts/UI/VRTK_UIPointer.cs +++ b/Assets/VRTK/Scripts/UI/VRTK_UIPointer.cs @@ -112,6 +112,23 @@ public enum ClickMethods [HideInInspector] public bool collisionClick = false; + /// + /// Emitted when the UI activation button is pressed. + /// + public event ControllerInteractionEventHandler ActivationButtonPressed; + /// + /// Emitted when the UI activation button is released. + /// + public event ControllerInteractionEventHandler ActivationButtonReleased; + /// + /// Emitted when the UI selection button is pressed. + /// + public event ControllerInteractionEventHandler SelectionButtonPressed; + /// + /// Emitted when the UI selection button is released. + /// + public event ControllerInteractionEventHandler SelectionButtonReleased; + /// /// Emitted when the UI Pointer is colliding with a valid UI element. /// @@ -208,6 +225,38 @@ public virtual void OnUIPointerElementDragEnd(UIPointerEventArgs e) } } + public virtual void OnActivationButtonPressed(ControllerInteractionEventArgs e) + { + if (ActivationButtonPressed != null) + { + ActivationButtonPressed(this, e); + } + } + + public virtual void OnActivationButtonReleased(ControllerInteractionEventArgs e) + { + if (ActivationButtonReleased != null) + { + ActivationButtonReleased(this, e); + } + } + + public virtual void OnSelectionButtonPressed(ControllerInteractionEventArgs e) + { + if (SelectionButtonPressed != null) + { + SelectionButtonPressed(this, e); + } + } + + public virtual void OnSelectionButtonReleased(ControllerInteractionEventArgs e) + { + if (SelectionButtonReleased != null) + { + SelectionButtonReleased(this, e); + } + } + public virtual UIPointerEventArgs SetUIPointerEvent(RaycastResult currentRaycastResult, GameObject currentTarget, GameObject lastTarget = null) { UIPointerEventArgs e; @@ -268,12 +317,12 @@ public virtual bool PointerActive() } else if (activationMode == ActivationMethods.HoldButton) { - return (controller != null ? controller.IsButtonPressed(activationButton) : false); + return IsActivationButtonPressed(); } else { pointerClicked = false; - if (controller != null && controller.IsButtonPressed(activationButton) && !lastPointerPressState) + if (IsActivationButtonPressed() && !lastPointerPressState) { pointerClicked = true; } @@ -289,10 +338,19 @@ public virtual bool PointerActive() } /// - /// The SelectionButtonActive method is used to determine if the configured selection button is currently in the active state. + /// The IsActivationButtonPressed method is used to determine if the configured activation button is currently in the active state. + /// + /// Returns true if the activation button is active. + public virtual bool IsActivationButtonPressed() + { + return (controller != null ? controller.IsButtonPressed(activationButton) : false); + } + + /// + /// The IsSelectionButtonPressed method is used to determine if the configured selection button is currently in the active state. /// /// Returns true if the selection button is active. - public virtual bool SelectionButtonActive() + public virtual bool IsSelectionButtonPressed() { return (controller != null ? controller.IsButtonPressed(selectionButton) : false); } @@ -305,7 +363,7 @@ public virtual bool SelectionButtonActive() /// Returns true if the UI Click button is in a valid state to action a click, returns false if it is not in a valid state. public virtual bool ValidClick(bool checkLastClick, bool lastClickState = false) { - var controllerClicked = (collisionClick ? collisionClick : SelectionButtonActive()); + var controllerClicked = (collisionClick ? collisionClick : IsSelectionButtonPressed()); var result = (checkLastClick ? controllerClicked && lastPointerClickState == lastClickState : controllerClicked); lastPointerClickState = controllerClicked; return result; @@ -343,6 +401,10 @@ protected virtual void OnEnable() if (controller != null) { controllerRenderModel = VRTK_SDK_Bridge.GetControllerRenderModel(controller.gameObject); + controller.SubscribeToButtonAliasEvent(activationButton, true, DoActivationButtonPressed); + controller.SubscribeToButtonAliasEvent(activationButton, false, DoActivationButtonReleased); + controller.SubscribeToButtonAliasEvent(selectionButton, true, DoSelectionButtonPressed); + controller.SubscribeToButtonAliasEvent(selectionButton, false, DoSelectionButtonReleased); } } @@ -352,6 +414,14 @@ protected virtual void OnDisable() { cachedVRInputModule.pointers.Remove(this); } + + if (controller != null) + { + controller.UnsubscribeToButtonAliasEvent(activationButton, true, DoActivationButtonPressed); + controller.UnsubscribeToButtonAliasEvent(activationButton, false, DoActivationButtonReleased); + controller.UnsubscribeToButtonAliasEvent(selectionButton, true, DoSelectionButtonPressed); + controller.UnsubscribeToButtonAliasEvent(selectionButton, false, DoSelectionButtonReleased); + } } protected virtual void LateUpdate() @@ -362,6 +432,26 @@ protected virtual void LateUpdate() } } + protected virtual void DoActivationButtonPressed(object sender, ControllerInteractionEventArgs e) + { + OnActivationButtonPressed(controller.SetControllerEvent()); + } + + protected virtual void DoActivationButtonReleased(object sender, ControllerInteractionEventArgs e) + { + OnActivationButtonReleased(controller.SetControllerEvent()); + } + + protected virtual void DoSelectionButtonPressed(object sender, ControllerInteractionEventArgs e) + { + OnSelectionButtonPressed(controller.SetControllerEvent()); + } + + protected virtual void DoSelectionButtonReleased(object sender, ControllerInteractionEventArgs e) + { + OnSelectionButtonReleased(controller.SetControllerEvent()); + } + protected virtual void ResetHoverTimer() { hoverDurationTimer = 0f; diff --git a/Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_InteractGrab_UnityEvents.cs b/Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_InteractGrab_UnityEvents.cs index 96860964f..422e4a723 100644 --- a/Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_InteractGrab_UnityEvents.cs +++ b/Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_InteractGrab_UnityEvents.cs @@ -10,17 +10,23 @@ public sealed class ObjectInteractEvent : UnityEvent + { + public VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent OnActivationButtonPressed = new VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent(); + public VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent OnActivationButtonReleased = new VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent(); + public VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent OnSelectionButtonPressed = new VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent(); + public VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent OnSelectionButtonReleased = new VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent(); + + protected override void AddListeners(VRTK_Pointer component) + { + component.ActivationButtonPressed += ActivationButtonPressed; + component.ActivationButtonReleased += ActivationButtonReleased; + component.SelectionButtonPressed += SelectionButtonPressed; + component.SelectionButtonReleased += SelectionButtonReleased; + } + + protected override void RemoveListeners(VRTK_Pointer component) + { + component.ActivationButtonPressed -= ActivationButtonPressed; + component.ActivationButtonReleased -= ActivationButtonReleased; + component.SelectionButtonPressed -= SelectionButtonPressed; + component.SelectionButtonReleased -= SelectionButtonReleased; + } + + private void ActivationButtonPressed(object o, ControllerInteractionEventArgs e) + { + OnActivationButtonPressed.Invoke(o, e); + } + + private void ActivationButtonReleased(object o, ControllerInteractionEventArgs e) + { + OnActivationButtonReleased.Invoke(o, e); + } + + private void SelectionButtonPressed(object o, ControllerInteractionEventArgs e) + { + OnSelectionButtonPressed.Invoke(o, e); + } + + private void SelectionButtonReleased(object o, ControllerInteractionEventArgs e) + { + OnSelectionButtonReleased.Invoke(o, e); + } + } +} \ No newline at end of file diff --git a/Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_Pointer_UnityEvents.cs.meta b/Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_Pointer_UnityEvents.cs.meta new file mode 100644 index 000000000..3abb69b56 --- /dev/null +++ b/Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_Pointer_UnityEvents.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: c0e3ee07f9d2ebf4f8fa7d1bee001d55 +timeCreated: 1491081941 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_UIPointer_UnityEvents.cs b/Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_UIPointer_UnityEvents.cs index cf9c372d6..3e383c487 100644 --- a/Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_UIPointer_UnityEvents.cs +++ b/Assets/VRTK/Scripts/Utilities/UnityEvents/VRTK_UIPointer_UnityEvents.cs @@ -13,6 +13,10 @@ public sealed class UIPointerEvent : UnityEvent { } public UIPointerEvent OnUIPointerElementClick = new UIPointerEvent(); public UIPointerEvent OnUIPointerElementDragStart = new UIPointerEvent(); public UIPointerEvent OnUIPointerElementDragEnd = new UIPointerEvent(); + public VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent OnActivationButtonPressed = new VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent(); + public VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent OnActivationButtonReleased = new VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent(); + public VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent OnSelectionButtonPressed = new VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent(); + public VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent OnSelectionButtonReleased = new VRTK_ControllerEvents_UnityEvents.ControllerInteractionEvent(); protected override void AddListeners(VRTK_UIPointer component) { @@ -21,6 +25,10 @@ protected override void AddListeners(VRTK_UIPointer component) component.UIPointerElementClick += UIPointerElementClick; component.UIPointerElementDragStart += UIPointerElementDragStart; component.UIPointerElementDragEnd += UIPointerElementDragEnd; + component.ActivationButtonPressed += ActivationButtonPressed; + component.ActivationButtonReleased += ActivationButtonReleased; + component.SelectionButtonPressed += SelectionButtonPressed; + component.SelectionButtonReleased += SelectionButtonReleased; } protected override void RemoveListeners(VRTK_UIPointer component) @@ -30,6 +38,10 @@ protected override void RemoveListeners(VRTK_UIPointer component) component.UIPointerElementClick -= UIPointerElementClick; component.UIPointerElementDragStart -= UIPointerElementDragStart; component.UIPointerElementDragEnd -= UIPointerElementDragEnd; + component.ActivationButtonPressed -= ActivationButtonPressed; + component.ActivationButtonReleased -= ActivationButtonReleased; + component.SelectionButtonPressed -= SelectionButtonPressed; + component.SelectionButtonReleased -= SelectionButtonReleased; } private void UIPointerElementEnter(object o, UIPointerEventArgs e) @@ -56,5 +68,25 @@ private void UIPointerElementDragEnd(object o, UIPointerEventArgs e) { OnUIPointerElementDragEnd.Invoke(o, e); } + + private void ActivationButtonPressed(object o, ControllerInteractionEventArgs e) + { + OnActivationButtonPressed.Invoke(o, e); + } + + private void ActivationButtonReleased(object o, ControllerInteractionEventArgs e) + { + OnActivationButtonReleased.Invoke(o, e); + } + + private void SelectionButtonPressed(object o, ControllerInteractionEventArgs e) + { + OnSelectionButtonPressed.Invoke(o, e); + } + + private void SelectionButtonReleased(object o, ControllerInteractionEventArgs e) + { + OnSelectionButtonReleased.Invoke(o, e); + } } } \ No newline at end of file diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 4a38320a0..d62c10f0c 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -1006,8 +1006,43 @@ It extends the `VRTK_DestinationMarker` to allow for destination events to be em * **Custom Origin:** A custom transform to use as the origin of the pointer. If no pointer origin transform is provided then the transform the script is attached to is used. * **Direction Indicator:** A custom VRTK_PointerDirectionIndicator to use to determine the rotation given to the destination set event. +### Class Events + + * `ActivationButtonPressed` - Emitted when the pointer activation button is pressed. + * `ActivationButtonReleased` - Emitted when the pointer activation button is released. + * `SelectionButtonPressed` - Emitted when the pointer selection button is pressed. + * `SelectionButtonReleased` - Emitted when the pointer selection button is released. + +### Unity Events + +Adding the `VRTK_Pointer_UnityEvents` component to `VRTK_Pointer` object allows access to `UnityEvents` that will react identically to the Class Events. + + * All C# delegate events are mapped to a Unity Event with the `On` prefix. e.g. `MyEvent` -> `OnMyEvent`. + ### Class Methods +#### IsActivationButtonPressed/0 + + > `public virtual bool IsActivationButtonPressed()` + + * Parameters + * _none_ + * Returns + * `bool` - Returns true if the activationButton is being pressed. + +The IsActivationButtonPressed method returns whether the configured activation button is being pressed. + +#### IsSelectionButtonPressed/0 + + > `public virtual bool IsSelectionButtonPressed()` + + * Parameters + * _none_ + * Returns + * `bool` - Returns true if the selectionButton is being pressed. + +The IsSelectionButtonPressed method returns whether the configured activation button is being pressed. + #### PointerEnter/1 > `public virtual void PointerEnter(RaycastHit givenHit)` @@ -2992,6 +3027,8 @@ The interactable objects require a collider to activate the trigger and a rigidb ### Class Events + * `GrabButtonPressed` - Emitted when the grab button is pressed. + * `GrabButtonReleased` - Emitted when the grab button is released. * `ControllerGrabInteractableObject` - Emitted when a valid object is grabbed. * `ControllerUngrabInteractableObject` - Emitted when a valid object is released from being grabbed. @@ -3001,11 +3038,6 @@ Adding the `VRTK_InteractGrab_UnityEvents` component to `VRTK_InteractGrab` obje * All C# delegate events are mapped to a Unity Event with the `On` prefix. e.g. `MyEvent` -> `OnMyEvent`. -### Event Payload - - * `uint controllerIndex` - The index of the controller doing the interaction. - * `GameObject target` - The GameObject of the interactable object that is being interacted with by the controller. - ### Class Methods #### IsGrabButtonPressed/0 @@ -3085,6 +3117,8 @@ If a valid interactable object is usable then pressing the set `Use` button on t ### Class Events + * `UseButtonPressed` - Emitted when the use toggle alias button is pressed. + * `UseButtonReleased` - Emitted when the use toggle alias button is released. * `ControllerUseInteractableObject` - Emitted when a valid object starts being used. * `ControllerUnuseInteractableObject` - Emitted when a valid object stops being used. @@ -3094,11 +3128,6 @@ Adding the `VRTK_InteractUse_UnityEvents` component to `VRTK_InteractUse` object * All C# delegate events are mapped to a Unity Event with the `On` prefix. e.g. `MyEvent` -> `OnMyEvent`. -### Event Payload - - * `uint controllerIndex` - The index of the controller doing the interaction. - * `GameObject target` - The GameObject of the interactable object that is being interacted with by the controller. - ### Class Methods #### IsUseButtonPressed/0 @@ -4856,6 +4885,10 @@ The UI pointer is activated via the `Pointer` alias on the `Controller Events` a ### Class Events + * `ActivationButtonPressed` - Emitted when the UI activation button is pressed. + * `ActivationButtonReleased` - Emitted when the UI activation button is released. + * `SelectionButtonPressed` - Emitted when the UI selection button is pressed. + * `SelectionButtonReleased` - Emitted when the UI selection button is released. * `UIPointerElementEnter` - Emitted when the UI Pointer is colliding with a valid UI element. * `UIPointerElementExit` - Emitted when the UI Pointer is no longer colliding with any valid UI elements. * `UIPointerElementClick` - Emitted when the UI Pointer has clicked the currently collided UI element. @@ -4911,16 +4944,27 @@ The RemoveEventSystem resets the Unity EventSystem back to the original state be The PointerActive method determines if the ui pointer beam should be active based on whether the pointer alias is being held and whether the Hold Button To Use parameter is checked. -#### SelectionButtonActive/0 +#### IsActivationButtonPressed/0 + + > `public virtual bool IsActivationButtonPressed()` + + * Parameters + * _none_ + * Returns + * `bool` - Returns true if the activation button is active. + +The IsActivationButtonPressed method is used to determine if the configured activation button is currently in the active state. + +#### IsSelectionButtonPressed/0 - > `public virtual bool SelectionButtonActive()` + > `public virtual bool IsSelectionButtonPressed()` * Parameters * _none_ * Returns * `bool` - Returns true if the selection button is active. -The SelectionButtonActive method is used to determine if the configured selection button is currently in the active state. +The IsSelectionButtonPressed method is used to determine if the configured selection button is currently in the active state. #### ValidClick/2