//======= Copyright (c) Valve Corporation, All rights reserved. ===============
using UnityEngine;
using System.Collections;
using System;
using Valve.VR;
using System.Runtime.InteropServices;
using System.Collections.Generic;
namespace Valve.VR
{
[Serializable]
///
/// Boolean actions are either true or false. There are a variety of helper events included that will fire for the given input source. They're prefixed with "on".
///
public class SteamVR_Action_Boolean : SteamVR_Action_In, ISteamVR_Action_Boolean, ISerializationCallbackReceiver
{
public delegate void StateDownHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource);
public delegate void StateUpHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource);
public delegate void StateHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource);
public delegate void ActiveChangeHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource, bool active);
public delegate void ChangeHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource, bool newState);
public delegate void UpdateHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource, bool newState);
/// [Shortcut to: SteamVR_Input_Sources.Any] This event fires whenever a state changes from false to true or true to false
public event ChangeHandler onChange
{ add { sourceMap[SteamVR_Input_Sources.Any].onChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onChange -= value; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] This event fires whenever the action is updated
public event UpdateHandler onUpdate
{ add { sourceMap[SteamVR_Input_Sources.Any].onUpdate += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onUpdate -= value; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] This event fires whenever the boolean action is true and gets updated
public event StateHandler onState
{ add { sourceMap[SteamVR_Input_Sources.Any].onState += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onState -= value; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] This event fires whenever the state of the boolean action has changed from false to true in the most recent update
public event StateDownHandler onStateDown
{ add { sourceMap[SteamVR_Input_Sources.Any].onStateDown += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onStateDown -= value; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] This event fires whenever the state of the boolean action has changed from true to false in the most recent update
public event StateUpHandler onStateUp
{ add { sourceMap[SteamVR_Input_Sources.Any].onStateUp += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onStateUp -= value; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] Event fires when the active state (ActionSet active and binding active) changes
public event ActiveChangeHandler onActiveChange
{ add { sourceMap[SteamVR_Input_Sources.Any].onActiveChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveChange -= value; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] Event fires when the bound state of the binding changes
public event ActiveChangeHandler onActiveBindingChange
{ add { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange -= value; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] True when the boolean action is true
public bool state { get { return sourceMap[SteamVR_Input_Sources.Any].state; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] True when the boolean action is true and the last state was false
public bool stateDown { get { return sourceMap[SteamVR_Input_Sources.Any].stateDown; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] True when the boolean action is false and the last state was true
public bool stateUp { get { return sourceMap[SteamVR_Input_Sources.Any].stateUp; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] (previous update) True when the boolean action is true
public bool lastState { get { return sourceMap[SteamVR_Input_Sources.Any].lastState; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] (previous update) True when the boolean action is true and the last state was false
public bool lastStateDown { get { return sourceMap[SteamVR_Input_Sources.Any].lastStateDown; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] (previous update) True when the boolean action is false and the last state was true
public bool lastStateUp { get { return sourceMap[SteamVR_Input_Sources.Any].lastStateUp; } }
public SteamVR_Action_Boolean() { }
/// Returns true if the value of the action has been changed to true (from false) in the most recent update.
/// The device you would like to get data from. Any if the action is not device specific.
public bool GetStateDown(SteamVR_Input_Sources inputSource)
{
return sourceMap[inputSource].stateDown;
}
/// Returns true if the value of the action has been changed to false (from true) in the most recent update.
/// The device you would like to get data from. Any if the action is not device specific.
public bool GetStateUp(SteamVR_Input_Sources inputSource)
{
return sourceMap[inputSource].stateUp;
}
/// Returns true if the value of the action (state) is currently true
/// The device you would like to get data from. Any if the action is not device specific.
public bool GetState(SteamVR_Input_Sources inputSource)
{
return sourceMap[inputSource].state;
}
/// [For the previous update] Returns true if the value of the action has been set to true (from false).
/// The device you would like to get data from. Any if the action is not device specific.
public bool GetLastStateDown(SteamVR_Input_Sources inputSource)
{
return sourceMap[inputSource].lastStateDown;
}
/// [For the previous update] Returns true if the value of the action has been set to false (from true).
/// The device you would like to get data from. Any if the action is not device specific.
public bool GetLastStateUp(SteamVR_Input_Sources inputSource)
{
return sourceMap[inputSource].lastStateUp;
}
/// [For the previous update] Returns true if the value of the action was true.
/// The device you would like to get data from. Any if the action is not device specific.
public bool GetLastState(SteamVR_Input_Sources inputSource)
{
return sourceMap[inputSource].lastState;
}
/// Executes a function when the *functional* active state of this action (with the specified inputSource) changes.
/// This happens when the action is bound or unbound, or when the ActionSet changes state.
/// A local function that receives the boolean action who's active state changes and the corresponding input source
/// The device you would like to get data from. Any if the action is not device specific.
public void AddOnActiveChangeListener(ActiveChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onActiveChange += functionToCall;
}
/// Stops executing a function when the *functional* active state of this action (with the specified inputSource) changes.
/// This happens when the action is bound or unbound, or when the ActionSet changes state.
/// The local function that you've setup to receive update events
/// The device you would like to get data from. Any if the action is not device specific.
public void RemoveOnActiveChangeListener(ActiveChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onActiveChange -= functionToStopCalling;
}
/// Executes a function when the active state of this action (with the specified inputSource) changes. This happens when the action is bound or unbound
/// A local function that receives the boolean action who's active state changes and the corresponding input source
/// The device you would like to get data from. Any if the action is not device specific.
public void AddOnActiveBindingChangeListener(ActiveChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onActiveBindingChange += functionToCall;
}
/// Stops executing the function setup by the corresponding AddListener
/// The local function that you've setup to receive update events
/// The device you would like to get data from. Any if the action is not device specific.
public void RemoveOnActiveBindingChangeListener(ActiveChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onActiveBindingChange -= functionToStopCalling;
}
/// Executes a function when the state of this action (with the specified inputSource) changes
/// A local function that receives the boolean action who's state has changed, the corresponding input source, and the new value
/// The device you would like to get data from. Any if the action is not device specific.
public void AddOnChangeListener(ChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onChange += functionToCall;
}
/// Stops executing the function setup by the corresponding AddListener
/// The local function that you've setup to receive on change events
/// The device you would like to get data from. Any if the action is not device specific.
public void RemoveOnChangeListener(ChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onChange -= functionToStopCalling;
}
/// Executes a function when the state of this action (with the specified inputSource) is updated.
/// A local function that receives the boolean action who's state has changed, the corresponding input source, and the new value
/// The device you would like to get data from. Any if the action is not device specific.
public void AddOnUpdateListener(UpdateHandler functionToCall, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onUpdate += functionToCall;
}
/// Stops executing the function setup by the corresponding AddListener
/// The local function that you've setup to receive update events
/// The device you would like to get data from. Any if the action is not device specific.
public void RemoveOnUpdateListener(UpdateHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onUpdate -= functionToStopCalling;
}
/// Executes a function when the state of this action (with the specified inputSource) changes to true (from false).
/// A local function that receives the boolean action who's state has changed, the corresponding input source, and the new value
/// The device you would like to get data from. Any if the action is not device specific.
public void AddOnStateDownListener(StateDownHandler functionToCall, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onStateDown += functionToCall;
}
/// Stops executing the function setup by the corresponding AddListener
/// The local function that you've setup to receive update events
/// The device you would like to get data from. Any if the action is not device specific.
public void RemoveOnStateDownListener(StateDownHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onStateDown -= functionToStopCalling;
}
/// Executes a function when the state of this action (with the specified inputSource) changes to false (from true).
/// A local function that receives the boolean action who's state has changed, the corresponding input source, and the new value
/// The device you would like to get data from. Any if the action is not device specific.
public void AddOnStateUpListener(StateUpHandler functionToCall, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onStateUp += functionToCall;
}
/// Stops executing the function setup by the corresponding AddListener
/// The local function that you've setup to receive events
/// The device you would like to get data from. Any if the action is not device specific.
public void RemoveOnStateUpListener(StateUpHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onStateUp -= functionToStopCalling;
}
///
/// Remove all listeners registered in the source, useful for Dispose pattern
///
public void RemoveAllListeners(SteamVR_Input_Sources input_Sources)
{
sourceMap[input_Sources].RemoveAllListeners();
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
InitAfterDeserialize();
}
}
public class SteamVR_Action_Boolean_Source_Map : SteamVR_Action_In_Source_Map
{
}
public class SteamVR_Action_Boolean_Source : SteamVR_Action_In_Source, ISteamVR_Action_Boolean
{
protected static uint actionData_size = 0;
/// Event fires when the state of the action changes from false to true
public event SteamVR_Action_Boolean.StateDownHandler onStateDown;
/// Event fires when the state of the action changes from true to false
public event SteamVR_Action_Boolean.StateUpHandler onStateUp;
/// Event fires when the state of the action is true and the action gets updated
public event SteamVR_Action_Boolean.StateHandler onState;
/// Event fires when the active state (ActionSet active and binding active) changes
public event SteamVR_Action_Boolean.ActiveChangeHandler onActiveChange;
/// Event fires when the active state of the binding changes
public event SteamVR_Action_Boolean.ActiveChangeHandler onActiveBindingChange;
/// Event fires when the state of the action changes from false to true or true to false
public event SteamVR_Action_Boolean.ChangeHandler onChange;
/// Event fires when the action is updated
public event SteamVR_Action_Boolean.UpdateHandler onUpdate;
/// The current value of the boolean action. Note: Will only return true if the action is also active.
public bool state { get { return active && actionData.bState; } }
/// True when the action's state changes from false to true. Note: Will only return true if the action is also active.
/// Will only return true if the action is also active.
public bool stateDown { get { return active && actionData.bState && actionData.bChanged; } }
/// True when the action's state changes from true to false. Note: Will only return true if the action is also active.
/// Will only return true if the action is also active.
public bool stateUp { get { return active && actionData.bState == false && actionData.bChanged; } }
/// True when the action's state changed during the most recent update. Note: Will only return true if the action is also active.
/// ActionSet is ignored since get is coming from the native struct.
public override bool changed { get { return active && actionData.bChanged; } protected set { } }
/// The value of the action's 'state' during the previous update
/// Always returns the previous update state
public bool lastState { get { return lastActionData.bState; } }
/// The value of the action's 'stateDown' during the previous update
/// Always returns the previous update state
public bool lastStateDown { get { return lastActionData.bState && lastActionData.bChanged; } }
/// The value of the action's 'stateUp' during the previous update
/// Always returns the previous update state
public bool lastStateUp { get { return lastActionData.bState == false && lastActionData.bChanged; } }
/// The value of the action's 'changed' during the previous update
/// Always returns the previous update state. Set is ignored since get is coming from the native struct.
public override bool lastChanged { get { return lastActionData.bChanged; } protected set { } }
/// The handle to the origin of the component that was used to update the value for this action
public override ulong activeOrigin
{
get
{
if (active)
return actionData.activeOrigin;
return 0;
}
}
/// The handle to the origin of the component that was used to update the value for this action (for the previous update)
public override ulong lastActiveOrigin { get { return lastActionData.activeOrigin; } }
/// Returns true if this action is bound and the ActionSet is active
public override bool active { get { return activeBinding && action.actionSet.IsActive(inputSource); } }
/// Returns true if the action is bound
public override bool activeBinding { get { return actionData.bActive; } }
/// Returns true if the action was bound and the ActionSet was active during the previous update
public override bool lastActive { get; protected set; }
/// Returns true if the action was bound during the previous update
public override bool lastActiveBinding { get { return lastActionData.bActive; } }
protected InputDigitalActionData_t actionData = new InputDigitalActionData_t();
protected InputDigitalActionData_t lastActionData = new InputDigitalActionData_t();
protected SteamVR_Action_Boolean booleanAction;
///
/// [Should not be called by user code] Sets up the internals of the action source before SteamVR has been initialized.
///
public override void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource)
{
base.Preinitialize(wrappingAction, forInputSource);
booleanAction = (SteamVR_Action_Boolean)wrappingAction;
}
///
/// [Should not be called by user code]
/// Initializes the handle for the inputSource, the action data size, and any other related SteamVR data.
///
public override void Initialize()
{
base.Initialize();
if (actionData_size == 0)
actionData_size = (uint)Marshal.SizeOf(typeof(InputDigitalActionData_t));
}
///
/// Remove all listeners, useful for Dispose pattern
///
public void RemoveAllListeners()
{
Delegate[] delegates;
if (onStateDown != null)
{
delegates = onStateDown.GetInvocationList();
if (delegates != null)
foreach (Delegate existingDelegate in delegates)
onStateDown -= (SteamVR_Action_Boolean.StateDownHandler)existingDelegate;
}
if (onStateUp != null)
{
delegates = onStateUp.GetInvocationList();
if (delegates != null)
foreach (Delegate existingDelegate in delegates)
onStateUp -= (SteamVR_Action_Boolean.StateUpHandler)existingDelegate;
}
if (onState != null)
{
delegates = onState.GetInvocationList();
if (delegates != null)
foreach (Delegate existingDelegate in delegates)
onState -= (SteamVR_Action_Boolean.StateHandler)existingDelegate;
}
if (onChange != null)
{
delegates = onChange.GetInvocationList();
if (delegates != null)
foreach (Delegate existingDelegate in delegates)
onChange -= (SteamVR_Action_Boolean.ChangeHandler)existingDelegate;
}
if (onUpdate != null)
{
delegates = onUpdate.GetInvocationList();
if (delegates != null)
foreach (Delegate existingDelegate in delegates)
onUpdate -= (SteamVR_Action_Boolean.UpdateHandler)existingDelegate;
}
if (onActiveChange != null)
{
delegates = onActiveChange.GetInvocationList();
if (delegates != null)
foreach (Delegate existingDelegate in delegates)
onActiveChange -= (SteamVR_Action_Boolean.ActiveChangeHandler)existingDelegate;
}
}
/// [Should not be called by user code]
/// Updates the data for this action and this input source. Sends related events.
///
public override void UpdateValue()
{
lastActionData = actionData;
lastActive = active;
EVRInputError err = OpenVR.Input.GetDigitalActionData(action.handle, ref actionData, actionData_size, inputSourceHandle);
if (err != EVRInputError.None)
Debug.LogError("[SteamVR] GetDigitalActionData error (" + action.fullPath + "): " + err.ToString() + " handle: " + action.handle.ToString());
if (changed)
changedTime = Time.realtimeSinceStartup + actionData.fUpdateTime;
updateTime = Time.realtimeSinceStartup;
if (active)
{
if (onStateDown != null && stateDown)
onStateDown.Invoke(booleanAction, inputSource);
if (onStateUp != null && stateUp)
onStateUp.Invoke(booleanAction, inputSource);
if (onState != null && state)
onState.Invoke(booleanAction, inputSource);
if (onChange != null && changed)
onChange.Invoke(booleanAction, inputSource, state);
if (onUpdate != null)
onUpdate.Invoke(booleanAction, inputSource, state);
}
if (onActiveBindingChange != null && lastActiveBinding != activeBinding)
onActiveBindingChange.Invoke(booleanAction, inputSource, activeBinding);
if (onActiveChange != null && lastActive != active)
onActiveChange.Invoke(booleanAction, inputSource, activeBinding);
}
}
public interface ISteamVR_Action_Boolean : ISteamVR_Action_In_Source
{
/// The current value of the boolean action. Note: Will only return true if the action is also active.
bool state { get; }
/// True when the action's state changes from false to true. Note: Will only return true if the action is also active.
bool stateDown { get; }
/// True when the action's state changes from true to false. Note: Will only return true if the action is also active.
bool stateUp { get; }
/// The value of the action's 'state' during the previous update
/// Always returns the previous update state
bool lastState { get; }
/// The value of the action's 'stateDown' during the previous update
/// Always returns the previous update state
bool lastStateDown { get; }
/// The value of the action's 'stateUp' during the previous update
/// Always returns the previous update state
bool lastStateUp { get; }
}
}