SteamVR_Action_Vibration.cs
12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//======= 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]
/// <summary>
/// Vibration actions are used to trigger haptic feedback in vr controllers.
/// </summary>
public class SteamVR_Action_Vibration : SteamVR_Action_Out<SteamVR_Action_Vibration_Source_Map, SteamVR_Action_Vibration_Source>, 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);
/// <summary><strong>[SteamVR_Input_Sources.Any]</strong> This event fires whenever a change happens in the action</summary>
public event ActiveChangeHandler onActiveChange
{ add { sourceMap[SteamVR_Input_Sources.Any].onActiveChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveChange -= value; } }
/// <summary><strong>[SteamVR_Input_Sources.Any]</strong> This event fires whenever a change happens in the action</summary>
public event ActiveChangeHandler onActiveBindingChange
{ add { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange -= value; } }
/// <summary><strong>[SteamVR_Input_Sources.Any]</strong> This event fires whenever this action is executed</summary>
public event ExecuteHandler onExecute
{ add { sourceMap[SteamVR_Input_Sources.Any].onExecute += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onExecute -= value; } }
public SteamVR_Action_Vibration() { }
/// <summary>
/// Trigger the haptics at a certain time for a certain length
/// </summary>
/// <param name="secondsFromNow">How long from the current time to execute the action (in seconds - can be 0)</param>
/// <param name="durationSeconds">How long the haptic action should last (in seconds)</param>
/// <param name="frequency">How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful)</param>
/// <param name="amplitude">How intense the haptic action should be (0 - 1)</param>
/// <param name="inputSource">The device you would like to execute the haptic action. Any if the action is not device specific.</param>
public void Execute(float secondsFromNow, float durationSeconds, float frequency, float amplitude, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].Execute(secondsFromNow, durationSeconds, frequency, amplitude);
}
/// <summary>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.</summary>
/// <param name="functionToCall">A local function that receives the boolean action who's active state changes and the corresponding input source</param>
/// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
public void AddOnActiveChangeListener(ActiveChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onActiveChange += functionToCall;
}
/// <summary>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.</summary>
/// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
/// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
public void RemoveOnActiveChangeListener(ActiveChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onActiveChange -= functionToStopCalling;
}
/// <summary>Executes a function when the active state of this action (with the specified inputSource) changes. This happens when the action is bound or unbound</summary>
/// <param name="functionToCall">A local function that receives the boolean action who's active state changes and the corresponding input source</param>
/// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
public void AddOnActiveBindingChangeListener(ActiveChangeHandler functionToCall, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onActiveBindingChange += functionToCall;
}
/// <summary>Stops executing the function setup by the corresponding AddListener</summary>
/// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
/// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
public void RemoveOnActiveBindingChangeListener(ActiveChangeHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onActiveBindingChange -= functionToStopCalling;
}
/// <summary>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</summary>
/// <param name="functionToCall">A local function that receives the boolean action who's active state changes and the corresponding input source</param>
/// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
public void AddOnExecuteListener(ExecuteHandler functionToCall, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onExecute += functionToCall;
}
/// <summary>Stops executing the function setup by the corresponding AddListener</summary>
/// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
/// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
public void RemoveOnExecuteListener(ExecuteHandler functionToStopCalling, SteamVR_Input_Sources inputSource)
{
sourceMap[inputSource].onExecute -= functionToStopCalling;
}
/// <summary>
/// Returns the last time this action was executed
/// </summary>
/// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
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<SteamVR_Action_Vibration_Source>
{
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
{
/// <summary>Event fires when the active state (ActionSet active and binding active) changes</summary>
public event SteamVR_Action_Vibration.ActiveChangeHandler onActiveChange;
/// <summary>Event fires when the active state of the binding changes</summary>
public event SteamVR_Action_Vibration.ActiveChangeHandler onActiveBindingChange;
/// <summary>Event fires whenever this action is executed</summary>
public event SteamVR_Action_Vibration.ExecuteHandler onExecute;
//todo: fix the active state of out actions
/// <summary>Returns true if this action is bound and the ActionSet is active</summary>
public override bool active { get { return activeBinding && setActive; } }
/// <summary>Returns true if the action is bound</summary>
public override bool activeBinding { get { return true; } }
/// <summary>Returns true if the action was bound and the ActionSet was active during the previous update</summary>
public override bool lastActive { get; protected set; }
/// <summary>Returns true if the action was bound during the previous update</summary>
public override bool lastActiveBinding { get { return true; } }
/// <summary>The last time the execute method was called on this action</summary>
public float timeLastExecuted { get; protected set; }
protected SteamVR_Action_Vibration vibrationAction;
/// <summary>
/// <strong>[Should not be called by user code]</strong>
/// Initializes the handle for the inputSource, and any other related SteamVR data.
/// </summary>
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;
}
/// <summary>
/// Trigger the haptics at a certain time for a certain length
/// </summary>
/// <param name="secondsFromNow">How long from the current time to execute the action (in seconds - can be 0)</param>
/// <param name="durationSeconds">How long the haptic action should last (in seconds)</param>
/// <param name="frequency">How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful)</param>
/// <param name="amplitude">How intense the haptic action should be (0 - 1)</param>
/// <param name="inputSource">The device you would like to execute the haptic action. Any if the action is not device specific.</param>
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("<b>[SteamVR]</b> TriggerHapticVibrationAction (" + fullPath + ") error: " + err.ToString() + " handle: " + handle.ToString());
if (onExecute != null)
onExecute.Invoke(vibrationAction, inputSource, secondsFromNow, durationSeconds, frequency, amplitude);
}
}
/// <summary>
/// Vibration actions are used to trigger haptic feedback in vr controllers.
/// </summary>
public interface ISteamVR_Action_Vibration : ISteamVR_Action_Out
{
/// <summary>
/// Trigger the haptics at a certain time for a certain length
/// </summary>
/// <param name="secondsFromNow">How long from the current time to execute the action (in seconds - can be 0)</param>
/// <param name="durationSeconds">How long the haptic action should last (in seconds)</param>
/// <param name="frequency">How often the haptic motor should bounce (0 - 320 in hz. The lower end being more useful)</param>
/// <param name="amplitude">How intense the haptic action should be (0 - 1)</param>
/// <param name="inputSource">The device you would like to execute the haptic action. Any if the action is not device specific.</param>
void Execute(float secondsFromNow, float durationSeconds, float frequency, float amplitude, SteamVR_Input_Sources inputSource);
}
}
#pragma warning restore 0067