From 36e2ae7fc62af490244f9af59307efb6536e7d73 Mon Sep 17 00:00:00 2001 From: "Theston E. Fox" Date: Thu, 8 Sep 2016 22:56:00 +0100 Subject: [PATCH] feat(Controller): add event for controller enable and disable state The Controller Events script will now throw an event when the controller is enabled and when the controller is disabled. --- .../VRTK_ControllerEvents_ListenerExample.cs | 13 ++++++ .../VRTK_ControllerEvents_UnityEvents.cs | 22 ++++++++++ Assets/VRTK/Scripts/VRTK_ControllerEvents.cs | 43 ++++++++++++++++++- DOCUMENTATION.md | 4 ++ 4 files changed, 80 insertions(+), 2 deletions(-) diff --git a/Assets/VRTK/Examples/Resources/Scripts/VRTK_ControllerEvents_ListenerExample.cs b/Assets/VRTK/Examples/Resources/Scripts/VRTK_ControllerEvents_ListenerExample.cs index 9d7404eae..adb8c64d9 100644 --- a/Assets/VRTK/Examples/Resources/Scripts/VRTK_ControllerEvents_ListenerExample.cs +++ b/Assets/VRTK/Examples/Resources/Scripts/VRTK_ControllerEvents_ListenerExample.cs @@ -40,6 +40,9 @@ private void Start() GetComponent().TouchpadTouchEnd += new ControllerInteractionEventHandler(DoTouchpadTouchEnd); GetComponent().TouchpadAxisChanged += new ControllerInteractionEventHandler(DoTouchpadAxisChanged); + + GetComponent().ControllerEnabled += new ControllerInteractionEventHandler(DoControllerEnabled); + GetComponent().ControllerDisabled += new ControllerInteractionEventHandler(DoControllerDisabled); } private void DebugLogger(uint index, string button, string action, ControllerInteractionEventArgs e) @@ -137,5 +140,15 @@ private void DoTouchpadAxisChanged(object sender, ControllerInteractionEventArgs { DebugLogger(e.controllerIndex, "TOUCHPAD", "axis changed", e); } + + private void DoControllerEnabled(object sender, ControllerInteractionEventArgs e) + { + DebugLogger(e.controllerIndex, "CONTROLLER STATE", "ENABLED", e); + } + + private void DoControllerDisabled(object sender, ControllerInteractionEventArgs e) + { + DebugLogger(e.controllerIndex, "CONTROLLER STATE", "DISABLED", e); + } } } \ No newline at end of file diff --git a/Assets/VRTK/Scripts/Helper/UnityEvents/VRTK_ControllerEvents_UnityEvents.cs b/Assets/VRTK/Scripts/Helper/UnityEvents/VRTK_ControllerEvents_UnityEvents.cs index 705ed9248..c4acb4336 100644 --- a/Assets/VRTK/Scripts/Helper/UnityEvents/VRTK_ControllerEvents_UnityEvents.cs +++ b/Assets/VRTK/Scripts/Helper/UnityEvents/VRTK_ControllerEvents_UnityEvents.cs @@ -127,6 +127,14 @@ public class UnityObjectEvent : UnityEvent { }; /// Emits the AliasUIClickOff class event. /// public UnityObjectEvent OnAliasMenuOff; + /// + /// Emits the ControllerEnabled class event. + /// + public UnityObjectEvent OnControllerEnabled; + /// + /// Emits the ControllerDisabled class event. + /// + public UnityObjectEvent OnControllerDisabled; private void SetControllerEvents() { @@ -174,6 +182,8 @@ private void OnEnable() ce.AliasUIClickOff += AliasUIClickOff; ce.AliasMenuOn += AliasMenuOn; ce.AliasMenuOff += AliasMenuOff; + ce.ControllerEnabled += ControllerEnabled; + ce.ControllerDisabled += ControllerDisabled; } private void TriggerPressed(object o, ControllerInteractionEventArgs e) @@ -321,6 +331,16 @@ private void AliasMenuOff(object o, ControllerInteractionEventArgs e) OnAliasMenuOff.Invoke(e); } + private void ControllerEnabled(object o, ControllerInteractionEventArgs e) + { + OnControllerEnabled.Invoke(e); + } + + private void ControllerDisabled(object o, ControllerInteractionEventArgs e) + { + OnControllerDisabled.Invoke(e); + } + private void OnDisable() { if (ce == null) @@ -357,6 +377,8 @@ private void OnDisable() ce.AliasUIClickOff -= AliasUIClickOff; ce.AliasMenuOn -= AliasMenuOn; ce.AliasMenuOff -= AliasMenuOff; + ce.ControllerEnabled -= ControllerEnabled; + ce.ControllerDisabled -= ControllerDisabled; } } } \ No newline at end of file diff --git a/Assets/VRTK/Scripts/VRTK_ControllerEvents.cs b/Assets/VRTK/Scripts/VRTK_ControllerEvents.cs index cc3c1795a..0516cdc39 100644 --- a/Assets/VRTK/Scripts/VRTK_ControllerEvents.cs +++ b/Assets/VRTK/Scripts/VRTK_ControllerEvents.cs @@ -290,6 +290,15 @@ public enum ButtonAlias /// public event ControllerInteractionEventHandler AliasUIClickOff; + /// + /// Emitted when the controller is enabled. + /// + public event ControllerInteractionEventHandler ControllerEnabled; + /// + /// Emitted when the controller is disabled. + /// + public event ControllerInteractionEventHandler ControllerDisabled; + private uint controllerIndex; private Vector2 touchpadAxis = Vector2.zero; private Vector2 triggerAxis = Vector2.zero; @@ -529,6 +538,22 @@ public virtual void OnAliasMenuOff(ControllerInteractionEventArgs e) } } + public virtual void OnControllerEnabled(ControllerInteractionEventArgs e) + { + if (ControllerEnabled != null) + { + ControllerEnabled(this, e); + } + } + + public virtual void OnControllerDisabled(ControllerInteractionEventArgs e) + { + if (ControllerDisabled != null) + { + ControllerDisabled(this, e); + } + } + /// /// The GetVelocity method is useful for getting the current velocity of the physical game controller. This can be useful to determine the speed at which the controller is being swung or the direction it is being moved in. /// @@ -615,6 +640,16 @@ private void Start() controllerIndex = VRTK_DeviceFinder.GetControllerIndex(gameObject); } + private void OnEnable() + { + Invoke("EnableEvents", 0f); + } + + private void OnDisable() + { + Invoke("DisableEvents", 0f); + } + private float CalculateTouchpadAxisAngle(Vector2 axis) { float angle = Mathf.Atan2(axis.y, axis.x) * Mathf.Rad2Deg; @@ -713,13 +748,17 @@ private bool Vector2ShallowEquals(Vector2 vectorA, Vector2 vectorB) vectorA.y.ToString("F" + axisFidelity) == vectorB.y.ToString("F" + axisFidelity)); } - private void OnDisable() + private void EnableEvents() { - Invoke("DisableEvents", 0.1f); + bool nullBool = false; + OnControllerEnabled(SetButtonEvent(ref nullBool, true, 0f)); } private void DisableEvents() { + bool nullBool = false; + OnControllerDisabled(SetButtonEvent(ref nullBool, false, 0f)); + if (triggerPressed) { OnTriggerReleased(SetButtonEvent(ref triggerPressed, false, 0f)); diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 65321f4ec..2ec95c00f 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -504,6 +504,8 @@ The script also has a public boolean pressed state for the buttons to allow the * `AliasMenuOff` - Emitted when the menu toggle alias button is released. * `AliasUIClickOn` - Emitted when the UI click alias button is pressed. * `AliasUIClickOff` - Emitted when the UI click alias button is released. + * `ControllerEnabled` - Emitted when the controller is enabled. + * `ControllerDisabled` - Emitted when the controller is disabled. ### Unity Events @@ -538,6 +540,8 @@ Adding the `VRTK_ControllerEvents_UnityEvents` component to `VRTK_ControllerEven * `OnAliasUIClickOff` - Emits the AliasMenuOff class event. * `OnAliasMenuOn` - Emits the AliasUIClickOn class event. * `OnAliasMenuOff` - Emits the AliasUIClickOff class event. + * `OnControllerEnabled` - Emits the ControllerEnabled class event. + * `OnControllerDisabled` - Emits the ControllerDisabled class event. ### Event Payload