//======= 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; #pragma warning disable 0067 namespace Valve.VR { [Serializable] /// /// Vibration actions are used to trigger haptic feedback in vr controllers. /// public class SteamVR_Action_Vibration : SteamVR_Action_Out, ISerializationCallbackReceiver { public delegate void ActiveChangeHandler(SteamVR_Action_Vibration fromAction, SteamVR_Input_Sources fromSource, bool active); public delegate void ExecuteHandler(SteamVR_Action_Vibration fromAction, SteamVR_Input_Sources fromSource, float secondsFromNow, float durationSeconds, float frequency, float amplitude); /// [SteamVR_Input_Sources.Any] This event fires whenever a change happens in the action public event ActiveChangeHandler onActiveChange { add { sourceMap[SteamVR_Input_Sources.Any].onActiveChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveChange -= value; } } /// [SteamVR_Input_Sources.Any] This event fires whenever a change happens in the action public event ActiveChangeHandler onActiveBindingChange { add { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange -= value; } } /// [SteamVR_Input_Sources.Any] This event fires whenever this action is executed public event ExecuteHandler onExecute { add { sourceMap[SteamVR_Input_Sources.Any].onExecute += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onExecute -= value; } } public SteamVR_Action_Vibration() { } /// /// Trigger the haptics at a certain time for a certain length /// /// How long from the current time to execute the action (in seconds - can be 0) /// How long the haptic action should last (in seconds) /// How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful) /// How intense the haptic action should be (0 - 1) /// The device you would like to execute the haptic action. Any if the action is not device specific. public void Execute(float secondsFromNow, float durationSeconds, float frequency, float amplitude, SteamVR_Input_Sources inputSource) { sourceMap[inputSource].Execute(secondsFromNow, durationSeconds, frequency, amplitude); } /// 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 execute method of this action (with the specified inputSource) is called. 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 AddOnExecuteListener(ExecuteHandler functionToCall, SteamVR_Input_Sources inputSource) { sourceMap[inputSource].onExecute += 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 RemoveOnExecuteListener(ExecuteHandler functionToStopCalling, SteamVR_Input_Sources inputSource) { sourceMap[inputSource].onExecute -= functionToStopCalling; } /// /// Removes all listeners, useful for dispose pattern /// public void RemoveAllListeners(SteamVR_Input_Sources input_Sources) { sourceMap[input_Sources].RemoveAllListeners(); } /// /// Returns the last time this action was executed /// /// The device you would like to get data from. Any if the action is not device specific. public override float GetTimeLastChanged(SteamVR_Input_Sources inputSource) { return sourceMap[inputSource].timeLastExecuted; } void ISerializationCallbackReceiver.OnBeforeSerialize() { } void ISerializationCallbackReceiver.OnAfterDeserialize() { InitAfterDeserialize(); } public override bool IsUpdating(SteamVR_Input_Sources inputSource) { return sourceMap.IsUpdating(inputSource); } } public class SteamVR_Action_Vibration_Source_Map : SteamVR_Action_Source_Map { public bool IsUpdating(SteamVR_Input_Sources inputSource) { int sourceIndex = (int)inputSource; return sources[sourceIndex].timeLastExecuted != 0; } } public class SteamVR_Action_Vibration_Source : SteamVR_Action_Out_Source { /// Event fires when the active state (ActionSet active and binding active) changes public event SteamVR_Action_Vibration.ActiveChangeHandler onActiveChange; /// Event fires when the active state of the binding changes public event SteamVR_Action_Vibration.ActiveChangeHandler onActiveBindingChange; /// Event fires whenever this action is executed public event SteamVR_Action_Vibration.ExecuteHandler onExecute; //todo: fix the active state of out actions /// Returns true if this action is bound and the ActionSet is active public override bool active { get { return activeBinding && setActive; } } /// Returns true if the action is bound public override bool activeBinding { get { return true; } } /// 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 true; } } /// The last time the execute method was called on this action public float timeLastExecuted { get; protected set; } protected SteamVR_Action_Vibration vibrationAction; /// /// [Should not be called by user code] /// Initializes the handle for the inputSource, and any other related SteamVR data. /// public override void Initialize() { base.Initialize(); lastActive = true; } public override void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource) { base.Preinitialize(wrappingAction, forInputSource); vibrationAction = (SteamVR_Action_Vibration)wrappingAction; } /// /// Removes all listeners, useful for dispose pattern /// public void RemoveAllListeners() { Delegate[] delegates; if (onActiveBindingChange != null) { delegates = onActiveBindingChange.GetInvocationList(); if (delegates != null) foreach (Delegate existingDelegate in delegates) onActiveBindingChange -= (SteamVR_Action_Vibration.ActiveChangeHandler)existingDelegate; } if (onActiveChange != null) { delegates = onActiveChange.GetInvocationList(); if (delegates != null) foreach (Delegate existingDelegate in delegates) onActiveChange -= (SteamVR_Action_Vibration.ActiveChangeHandler)existingDelegate; } if (onExecute != null) { delegates = onExecute.GetInvocationList(); if (delegates != null) foreach (Delegate existingDelegate in delegates) onExecute -= (SteamVR_Action_Vibration.ExecuteHandler)existingDelegate; } } /// /// Trigger the haptics at a certain time for a certain length /// /// How long from the current time to execute the action (in seconds - can be 0) /// How long the haptic action should last (in seconds) /// How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful) /// How intense the haptic action should be (0 - 1) /// The device you would like to execute the haptic action. Any if the action is not device specific. public void Execute(float secondsFromNow, float durationSeconds, float frequency, float amplitude) { if (SteamVR_Input.isStartupFrame) return; timeLastExecuted = Time.realtimeSinceStartup; EVRInputError err = OpenVR.Input.TriggerHapticVibrationAction(handle, secondsFromNow, durationSeconds, frequency, amplitude, inputSourceHandle); //Debug.Log(string.Format("[{5}: haptic] secondsFromNow({0}), durationSeconds({1}), frequency({2}), amplitude({3}), inputSource({4})", secondsFromNow, durationSeconds, frequency, amplitude, inputSource, this.GetShortName())); if (err != EVRInputError.None) Debug.LogError("[SteamVR] TriggerHapticVibrationAction (" + fullPath + ") error: " + err.ToString() + " handle: " + handle.ToString()); if (onExecute != null) onExecute.Invoke(vibrationAction, inputSource, secondsFromNow, durationSeconds, frequency, amplitude); } } /// /// Vibration actions are used to trigger haptic feedback in vr controllers. /// public interface ISteamVR_Action_Vibration : ISteamVR_Action_Out { /// /// Trigger the haptics at a certain time for a certain length /// /// How long from the current time to execute the action (in seconds - can be 0) /// How long the haptic action should last (in seconds) /// How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful) /// How intense the haptic action should be (0 - 1) /// The device you would like to execute the haptic action. Any if the action is not device specific. void Execute(float secondsFromNow, float durationSeconds, float frequency, float amplitude, SteamVR_Input_Sources inputSource); } } #pragma warning restore 0067