-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathControllerWrapper.cs
More file actions
192 lines (159 loc) · 7.65 KB
/
Copy pathControllerWrapper.cs
File metadata and controls
192 lines (159 loc) · 7.65 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
using UnityEngine;
using System.Collections;
using Valve.VR;
/// <summary>
/// This script must be on a controller to function
///
/// The purpose of this is to send events using the SteamVR.Event system.
/// When using this script game objects do not need a reference to the controllers, they only need
/// to listen for the event. (SteamVR.Event.Listen("controllerEvent", OnControllerEvent)
///
/// The listening object will need to recast the (params ojbect[] args) to ControllerEventArgs
/// ControllerEventArgs eventStruct = (ControllerEventArgs)args[0];
/// And the parameters can be accessed from there.
///
/// Credit: This script structure as taken from "SteamVR_TrackedController" in the Steam Unity VR package
/// And was made working with the help from Lee Wasilenko at www.vrdevschool.com
///
/// Extensions: This class can be extended to get the controller rotation and axis information.
/// The hair trigger method can be added as well
///
/// Notes: The SteamVR event system uses the following pattern to check for null.
/// The EtrackingResults are stored in the Valve name space.
/// If this script needs to check for events then what are they?
///
/// static public bool initializing { get; private set; }
///
/// var initializing = result == ETrackingResult.Uninitialized;
/// if (initializing != SteamVR.initializing)
/// {
/// SteamVR_Utils.Event.Send("initializing", initializing);
/// }
/// </summary>
///
//All parameters here will be passed on the event trigger
public struct ControllerEventArgs {
public uint controllerIndex;
public uint flags;
public float padX, padY;
}
public class ControllerWrapper : MonoBehaviour {
public uint controllerIndex;
public VRControllerState_t controllerState;
public bool triggerPressed = false;
public bool steamPressed = false;
public bool menuPressed = false;
public bool padPressed = false;
public bool padTouched = false;
public bool gripped = false;
// Use this for initialization
void Start() {
//Get a reference to the tracked object class
if (this.GetComponent<SteamVR_TrackedObject>() == null) {
gameObject.AddComponent<SteamVR_TrackedObject>();
}
//Get the index this controller is
controllerIndex = (uint)this.GetComponent<SteamVR_TrackedObject>().index;
if (this.GetComponent<SteamVR_RenderModel>() != null) {
this.GetComponent<SteamVR_RenderModel>().index = (SteamVR_TrackedObject.EIndex)controllerIndex;
}
}
// Update is called once per frame
void Update() {
var system = OpenVR.System;
if (system != null && system.GetControllerState(controllerIndex, ref controllerState)) {
ulong trigger = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_SteamVR_Trigger));
if (trigger > 0L && !triggerPressed) {
triggerPressed = true;
ControllerEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (uint)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
//I beleive the null check is handled in the SteamVR_Utils before the event is sent
//TODO see if this good enough null checking
SteamVR_Utils.Event.Send("triggerClicked", e);
} else if (trigger == 0L && triggerPressed) {
triggerPressed = false;
ControllerEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (uint)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
SteamVR_Utils.Event.Send("triggerUnclicked", e);
}
ulong grip = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_Grip));
if (grip > 0L && !gripped) {
gripped = true;
ControllerEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (uint)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
SteamVR_Utils.Event.Send("gripClicked", e);
} else if (grip == 0L && gripped) {
gripped = false;
ControllerEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (uint)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
SteamVR_Utils.Event.Send("gripUnclicked", e);
}
ulong pad = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_SteamVR_Touchpad));
if (pad > 0L && !padPressed) {
padPressed = true;
ControllerEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (uint)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
SteamVR_Utils.Event.Send("padClicked", e);
} else if (pad == 0L && padPressed) {
padPressed = false;
ControllerEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (uint)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
SteamVR_Utils.Event.Send("padUnclicked", e);
}
ulong menu = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_ApplicationMenu));
if (menu > 0L && !menuPressed) {
menuPressed = true;
ControllerEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (uint)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
SteamVR_Utils.Event.Send("menuClicked", e);
} else if (menu == 0L && menuPressed) {
menuPressed = false;
ControllerEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (uint)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
SteamVR_Utils.Event.Send("menuUnclicked", e);
}
pad = controllerState.ulButtonTouched & (1UL << ((int)EVRButtonId.k_EButton_SteamVR_Touchpad));
if (pad > 0L && !padTouched) {
padTouched = true;
ControllerEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (uint)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
SteamVR_Utils.Event.Send("padTouched", e);
} else if (pad == 0L && padTouched) {
padTouched = false;
ControllerEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (uint)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
SteamVR_Utils.Event.Send("padUntouched", e);
}
}
}
}