//======= 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]
///
/// An analog action with two values generally from -1 to 1. Also provides a delta since the last update.
///
public class SteamVR_Action_Vector2 : SteamVR_Action_In, ISteamVR_Action_Vector2, ISerializationCallbackReceiver
{
public delegate void AxisHandler(SteamVR_Action_Vector2 fromAction, SteamVR_Input_Sources fromSource, Vector2 axis, Vector2 delta);
public delegate void ActiveChangeHandler(SteamVR_Action_Vector2 fromAction, SteamVR_Input_Sources fromSource, bool active);
public delegate void ChangeHandler(SteamVR_Action_Vector2 fromAction, SteamVR_Input_Sources fromSource, Vector2 axis, Vector2 delta);
public delegate void UpdateHandler(SteamVR_Action_Vector2 fromAction, SteamVR_Input_Sources fromSource, Vector2 axis, Vector2 delta);
/// [Shortcut to: SteamVR_Input_Sources.Any] This event fires whenever the axis changes by more than the specified changeTolerance
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 will fire whenever the Vector2 value of the action is non-zero
public event AxisHandler onAxis
{ add { sourceMap[SteamVR_Input_Sources.Any].onAxis += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onAxis -= 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 active 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] The current Vector2 value of the action.
/// Note: Will only return non-zero if the action is also active.
public Vector2 axis { get { return sourceMap[SteamVR_Input_Sources.Any].axis; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] The Vector2 value of the action from the previous update.
/// Note: Will only return non-zero if the action is also active.
public Vector2 lastAxis { get { return sourceMap[SteamVR_Input_Sources.Any].lastAxis; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] The Vector2 value difference between this update and the previous update.
/// Note: Will only return non-zero if the action is also active.
public Vector2 delta { get { return sourceMap[SteamVR_Input_Sources.Any].delta; } }
/// [Shortcut to: SteamVR_Input_Sources.Any] The Vector2 value difference between the previous update and update before that.
/// Note: Will only return non-zero if the action is also active.
public Vector2 lastDelta { get { return sourceMap[SteamVR_Input_Sources.Any].lastDelta; } }
public SteamVR_Action_Vector2() { }
/// The current Vector2 value of the action
/// The device you would like to get data from. Any if the action is not device specific.
public Vector2 GetAxis(SteamVR_Input_Sources inputSource)
{
return sourceMap[inputSource].axis;
}
/// The Vector2 value difference between this update and the previous update.
/// The device you would like to get data from. Any if the action is not device specific.
public Vector2 GetAxisDelta(SteamVR_Input_Sources inputSource)
{
return sourceMap[inputSource].delta;
}
/// The Vector2 value of the action from the previous update.
/// The device you would like to get data from. Any if the action is not device specific.
public Vector2 GetLastAxis(SteamVR_Input_Sources inputSource)
{
return sourceMap[inputSource].lastAxis;
}
/// The Vector2 value difference between the previous update and update before that.
/// The device you would like to get data from. Any if the action is not device specific.
public Vector2 GetLastAxisDelta(SteamVR_Input_Sources inputSource)
{
return sourceMap[inputSource].lastDelta;
}
/// 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 axis changes by more than the specified changeTolerance
/// 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 Vector2 value of the action is non-zero.
/// 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 AddOnAxisListener(AxisHandler functionToCall, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onAxis += 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 RemoveOnAxisListener(AxisHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onAxis -= functionToStopCalling;
}
///
/// Removes all listeners, useful for dispose pattern
///
public void RemoveAllListeners(SteamVR_Input_Sources input_Sources)
{
sourceMap[input_Sources].RemoveAllListeners();
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
InitAfterDeserialize();
}
}
///
/// Boolean actions are either true or false. There is an onStateUp and onStateDown event for the rising and falling edge.
///
public class SteamVR_Action_Vector2_Source_Map : SteamVR_Action_In_Source_Map
{
}
public class SteamVR_Action_Vector2_Source : SteamVR_Action_In_Source, ISteamVR_Action_Vector2
{
protected static uint actionData_size = 0;
/// The amount the axis needs to change before a change is detected
public float changeTolerance = Mathf.Epsilon;
/// Event fires when the value of the action is non-zero
public event SteamVR_Action_Vector2.AxisHandler onAxis;
/// Event fires when the active state (ActionSet active and binding active) changes
public event SteamVR_Action_Vector2.ActiveChangeHandler onActiveChange;
/// Event fires when the active state of the binding changes
public event SteamVR_Action_Vector2.ActiveChangeHandler onActiveBindingChange;
/// This event fires whenever the axis changes by more than the specified changeTolerance
public event SteamVR_Action_Vector2.ChangeHandler onChange;
/// Event fires when the action is updated
public event SteamVR_Action_Vector2.UpdateHandler onUpdate;
/// The current Vector2 value of the action.
/// Note: Will only return non-zero if the action is also active.
public Vector2 axis { get; protected set; }
/// The Vector2 value of the action from the previous update.
/// Note: Will only return non-zero if the action is also active.
public Vector2 lastAxis { get; protected set; }
/// The Vector2 value difference between this update and the previous update.
/// Note: Will only return non-zero if the action is also active.
public Vector2 delta { get; protected set; }
/// The Vector2 value difference between the previous update and update before that.
/// Note: Will only return non-zero if the action is also active.
public Vector2 lastDelta { get; protected set; }
/// If the Vector2 value of this action has changed more than the changeTolerance since the last update
public override bool changed { get; protected set; }
/// If the Vector2 value of this action has changed more than the changeTolerance between the previous update and the update before that
public override bool lastChanged { get; 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 InputAnalogActionData_t actionData = new InputAnalogActionData_t();
protected InputAnalogActionData_t lastActionData = new InputAnalogActionData_t();
protected SteamVR_Action_Vector2 vector2Action;
///
/// [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);
vector2Action = (SteamVR_Action_Vector2)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(InputAnalogActionData_t));
}
///
/// Removes all listeners, useful for dispose pattern
///
public void RemoveAllListeners()
{
Delegate[] delegates;
if (onAxis != null)
{
delegates = onAxis.GetInvocationList();
if (delegates != null)
foreach (Delegate existingDelegate in delegates)
onAxis -= (SteamVR_Action_Vector2.AxisHandler)existingDelegate;
}
if (onUpdate != null)
{
delegates = onUpdate.GetInvocationList();
if (delegates != null)
foreach (Delegate existingDelegate in delegates)
onUpdate -= (SteamVR_Action_Vector2.UpdateHandler)existingDelegate;
}
if (onChange != null)
{
delegates = onChange.GetInvocationList();
if (delegates != null)
foreach (Delegate existingDelegate in delegates)
onChange -= (SteamVR_Action_Vector2.ChangeHandler)existingDelegate;
}
if (onActiveChange != null)
{
delegates = onActiveChange.GetInvocationList();
if (delegates != null)
foreach (Delegate existingDelegate in delegates)
onActiveChange -= (SteamVR_Action_Vector2.ActiveChangeHandler)existingDelegate;
}
if (onActiveBindingChange != null)
{
delegates = onActiveBindingChange.GetInvocationList();
if (delegates != null)
foreach (Delegate existingDelegate in delegates)
onActiveBindingChange -= (SteamVR_Action_Vector2.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;
lastAxis = axis;
lastDelta = delta;
EVRInputError err = OpenVR.Input.GetAnalogActionData(handle, ref actionData, actionData_size, SteamVR_Input_Source.GetHandle(inputSource));
if (err != EVRInputError.None)
Debug.LogError("[SteamVR] GetAnalogActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString());
updateTime = Time.realtimeSinceStartup;
axis = new Vector2(actionData.x, actionData.y);
delta = new Vector2(actionData.deltaX, actionData.deltaY);
changed = false;
if (active)
{
if (delta.magnitude > changeTolerance)
{
changed = true;
changedTime = Time.realtimeSinceStartup + actionData.fUpdateTime; //fUpdateTime is the time from the time the action was called that the action changed
if (onChange != null)
onChange.Invoke(vector2Action, inputSource, axis, delta);
}
if (axis != Vector2.zero)
{
if (onAxis != null)
onAxis.Invoke(vector2Action, inputSource, axis, delta);
}
if (onUpdate != null)
{
onUpdate.Invoke(vector2Action, inputSource, axis, delta);
}
}
if (onActiveBindingChange != null && lastActiveBinding != activeBinding)
onActiveBindingChange.Invoke(vector2Action, inputSource, activeBinding);
if (onActiveChange != null && lastActive != active)
onActiveChange.Invoke(vector2Action, inputSource, activeBinding);
}
}
public interface ISteamVR_Action_Vector2 : ISteamVR_Action_In_Source
{
/// The current float value of the action.
/// Note: Will only return non-zero if the action is also active.
Vector2 axis { get; }
/// The float value of the action from the previous update.
/// Note: Will only return non-zero if the action is also active.
Vector2 lastAxis { get; }
/// The float value difference between this update and the previous update.
/// Note: Will only return non-zero if the action is also active.
Vector2 delta { get; }
/// The float value difference between the previous update and update before that.
/// Note: Will only return non-zero if the action is also active.
Vector2 lastDelta { get; }
}
}