Skip to content

Commit

Permalink
feat(Controller): add event for controller enable and disable state
Browse files Browse the repository at this point in the history
The Controller Events script will now throw an event when the
controller is enabled and when the controller is disabled.
  • Loading branch information
thestonefox committed Sep 8, 2016
1 parent dcb8f0f commit 36e2ae7
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
Expand Up @@ -40,6 +40,9 @@ private void Start()
GetComponent<VRTK_ControllerEvents>().TouchpadTouchEnd += new ControllerInteractionEventHandler(DoTouchpadTouchEnd);

GetComponent<VRTK_ControllerEvents>().TouchpadAxisChanged += new ControllerInteractionEventHandler(DoTouchpadAxisChanged);

GetComponent<VRTK_ControllerEvents>().ControllerEnabled += new ControllerInteractionEventHandler(DoControllerEnabled);
GetComponent<VRTK_ControllerEvents>().ControllerDisabled += new ControllerInteractionEventHandler(DoControllerDisabled);
}

private void DebugLogger(uint index, string button, string action, ControllerInteractionEventArgs e)
Expand Down Expand Up @@ -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);
}
}
}
Expand Up @@ -127,6 +127,14 @@ public class UnityObjectEvent : UnityEvent<ControllerInteractionEventArgs> { };
/// Emits the AliasUIClickOff class event.
/// </summary>
public UnityObjectEvent OnAliasMenuOff;
/// <summary>
/// Emits the ControllerEnabled class event.
/// </summary>
public UnityObjectEvent OnControllerEnabled;
/// <summary>
/// Emits the ControllerDisabled class event.
/// </summary>
public UnityObjectEvent OnControllerDisabled;

private void SetControllerEvents()
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -357,6 +377,8 @@ private void OnDisable()
ce.AliasUIClickOff -= AliasUIClickOff;
ce.AliasMenuOn -= AliasMenuOn;
ce.AliasMenuOff -= AliasMenuOff;
ce.ControllerEnabled -= ControllerEnabled;
ce.ControllerDisabled -= ControllerDisabled;
}
}
}
43 changes: 41 additions & 2 deletions Assets/VRTK/Scripts/VRTK_ControllerEvents.cs
Expand Up @@ -290,6 +290,15 @@ public enum ButtonAlias
/// </summary>
public event ControllerInteractionEventHandler AliasUIClickOff;

/// <summary>
/// Emitted when the controller is enabled.
/// </summary>
public event ControllerInteractionEventHandler ControllerEnabled;
/// <summary>
/// Emitted when the controller is disabled.
/// </summary>
public event ControllerInteractionEventHandler ControllerDisabled;

private uint controllerIndex;
private Vector2 touchpadAxis = Vector2.zero;
private Vector2 triggerAxis = Vector2.zero;
Expand Down Expand Up @@ -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);
}
}

/// <summary>
/// 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.
/// </summary>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
4 changes: 4 additions & 0 deletions DOCUMENTATION.md
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 36e2ae7

Please sign in to comment.