@@ -10,10 +10,30 @@ namespace WebXR
10
10
11
11
public class WebXRController : MonoBehaviour
12
12
{
13
+ public enum ButtonTypes
14
+ {
15
+ Trigger = 0 ,
16
+ Grip = 1 ,
17
+ Thumbstick = 2 ,
18
+ Touchpad = 3 ,
19
+ ButtonA = 4 ,
20
+ ButtonB = 5
21
+ }
22
+
23
+ public enum AxisTypes
24
+ {
25
+ Trigger ,
26
+ Grip
27
+ }
28
+
29
+ public enum Axis2DTypes
30
+ {
31
+ Thumbstick , // primary2DAxis
32
+ Touchpad // secondary2DAxis
33
+ }
34
+
13
35
[ Tooltip ( "Controller hand to use." ) ]
14
36
public WebXRControllerHand hand = WebXRControllerHand . NONE ;
15
- [ Tooltip ( "Controller input settings." ) ]
16
- public WebXRControllerInputMap inputMap ;
17
37
[ Tooltip ( "Simulate 3dof controller" ) ]
18
38
public bool simulate3dof = false ;
19
39
[ Tooltip ( "Vector from head to elbow" ) ]
@@ -41,7 +61,7 @@ public class WebXRController : MonoBehaviour
41
61
42
62
private Quaternion headRotation ;
43
63
private Vector3 headPosition ;
44
- private Dictionary < string , WebXRControllerButton > buttonStates = new Dictionary < string , WebXRControllerButton > ( ) ;
64
+ private Dictionary < ButtonTypes , WebXRControllerButton > buttonStates = new Dictionary < ButtonTypes , WebXRControllerButton > ( ) ;
45
65
46
66
private Dictionary < int , Transform > handJoints = new Dictionary < int , Transform > ( ) ;
47
67
private bool handJointsVisible = false ;
@@ -107,12 +127,12 @@ public void TryUpdateButtons()
107
127
}
108
128
109
129
WebXRControllerButton [ ] buttons = new WebXRControllerButton [ 6 ] ;
110
- buttons [ 0 ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
111
- buttons [ 1 ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
112
- buttons [ 2 ] = new WebXRControllerButton ( thumbstick == 1 , thumbstick ) ;
113
- buttons [ 3 ] = new WebXRControllerButton ( touchpad == 1 , touchpad ) ;
114
- buttons [ 4 ] = new WebXRControllerButton ( buttonA == 1 , buttonA ) ;
115
- buttons [ 5 ] = new WebXRControllerButton ( buttonB == 1 , buttonB ) ;
130
+ buttons [ ( int ) ButtonTypes . Trigger ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
131
+ buttons [ ( int ) ButtonTypes . Grip ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
132
+ buttons [ ( int ) ButtonTypes . Thumbstick ] = new WebXRControllerButton ( thumbstick == 1 , thumbstick ) ;
133
+ buttons [ ( int ) ButtonTypes . Touchpad ] = new WebXRControllerButton ( touchpad == 1 , touchpad ) ;
134
+ buttons [ ( int ) ButtonTypes . ButtonA ] = new WebXRControllerButton ( buttonA == 1 , buttonA ) ;
135
+ buttons [ ( int ) ButtonTypes . ButtonB ] = new WebXRControllerButton ( buttonB == 1 , buttonB ) ;
116
136
UpdateButtons ( buttons ) ;
117
137
}
118
138
#endif
@@ -124,27 +144,35 @@ private void UpdateButtons(WebXRControllerButton[] buttons)
124
144
for ( int i = 0 ; i < buttons . Length ; i ++ )
125
145
{
126
146
WebXRControllerButton button = buttons [ i ] ;
127
- foreach ( WebXRControllerInput input in inputMap . inputs )
128
- {
129
- if ( input . gamepadId == i )
130
- SetButtonState ( input . actionName , button . pressed , button . value ) ;
131
- }
147
+ SetButtonState ( ( ButtonTypes ) i , button . pressed , button . value ) ;
132
148
}
133
149
}
134
150
135
- public float GetAxis ( string action )
151
+ public float GetAxis ( AxisTypes action )
136
152
{
137
153
switch ( action )
138
154
{
139
- case " Grip" :
155
+ case AxisTypes . Grip :
140
156
return squeeze ;
141
- case " Trigger" :
157
+ case AxisTypes . Trigger :
142
158
return trigger ;
143
159
}
144
160
return 0 ;
145
161
}
146
162
147
- public bool GetButton ( string action )
163
+ public Vector2 GetAxis2D ( Axis2DTypes action )
164
+ {
165
+ switch ( action )
166
+ {
167
+ case Axis2DTypes . Thumbstick :
168
+ return new Vector2 ( thumbstickX , thumbstickY ) ;
169
+ case Axis2DTypes . Touchpad :
170
+ return new Vector2 ( touchpadX , touchpadY ) ;
171
+ }
172
+ return Vector2 . zero ;
173
+ }
174
+
175
+ public bool GetButton ( ButtonTypes action )
148
176
{
149
177
if ( ! buttonStates . ContainsKey ( action ) )
150
178
{
@@ -153,14 +181,14 @@ public bool GetButton(string action)
153
181
return buttonStates [ action ] . pressed ;
154
182
}
155
183
156
- private bool GetPastButtonState ( string action )
184
+ private bool GetPastButtonState ( ButtonTypes action )
157
185
{
158
186
if ( ! buttonStates . ContainsKey ( action ) )
159
187
return false ;
160
188
return buttonStates [ action ] . prevPressedState ;
161
189
}
162
190
163
- private void SetButtonState ( string action , bool isPressed , float value )
191
+ private void SetButtonState ( ButtonTypes action , bool isPressed , float value )
164
192
{
165
193
if ( buttonStates . ContainsKey ( action ) )
166
194
{
@@ -171,14 +199,14 @@ private void SetButtonState(string action, bool isPressed, float value)
171
199
buttonStates . Add ( action , new WebXRControllerButton ( isPressed , value ) ) ;
172
200
}
173
201
174
- private void SetPastButtonState ( string action , bool isPressed )
202
+ private void SetPastButtonState ( ButtonTypes action , bool isPressed )
175
203
{
176
204
if ( ! buttonStates . ContainsKey ( action ) )
177
205
return ;
178
206
buttonStates [ action ] . prevPressedState = isPressed ;
179
207
}
180
208
181
- public bool GetButtonDown ( string action )
209
+ public bool GetButtonDown ( ButtonTypes action )
182
210
{
183
211
if ( GetButton ( action ) && ! GetPastButtonState ( action ) )
184
212
{
@@ -188,7 +216,7 @@ public bool GetButtonDown(string action)
188
216
return false ;
189
217
}
190
218
191
- public bool GetButtonUp ( string action )
219
+ public bool GetButtonUp ( ButtonTypes action )
192
220
{
193
221
if ( ! GetButton ( action ) && GetPastButtonState ( action ) )
194
222
{
@@ -236,12 +264,12 @@ private void OnControllerUpdate(WebXRControllerData controllerData)
236
264
buttonB = controllerData . buttonB ;
237
265
238
266
WebXRControllerButton [ ] buttons = new WebXRControllerButton [ 6 ] ;
239
- buttons [ 0 ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
240
- buttons [ 1 ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
241
- buttons [ 2 ] = new WebXRControllerButton ( thumbstick == 1 , thumbstick ) ;
242
- buttons [ 3 ] = new WebXRControllerButton ( touchpad == 1 , touchpad ) ;
243
- buttons [ 4 ] = new WebXRControllerButton ( buttonA == 1 , buttonA ) ;
244
- buttons [ 5 ] = new WebXRControllerButton ( buttonB == 1 , buttonB ) ;
267
+ buttons [ ( int ) ButtonTypes . Trigger ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
268
+ buttons [ ( int ) ButtonTypes . Grip ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
269
+ buttons [ ( int ) ButtonTypes . Thumbstick ] = new WebXRControllerButton ( thumbstick == 1 , thumbstick ) ;
270
+ buttons [ ( int ) ButtonTypes . Touchpad ] = new WebXRControllerButton ( touchpad == 1 , touchpad ) ;
271
+ buttons [ ( int ) ButtonTypes . ButtonA ] = new WebXRControllerButton ( buttonA == 1 , buttonA ) ;
272
+ buttons [ ( int ) ButtonTypes . ButtonB ] = new WebXRControllerButton ( buttonB == 1 , buttonB ) ;
245
273
UpdateButtons ( buttons ) ;
246
274
}
247
275
}
@@ -295,8 +323,8 @@ private void OnHandUpdate(WebXRHandData handData)
295
323
squeeze = handData . squeeze ;
296
324
297
325
WebXRControllerButton [ ] buttons = new WebXRControllerButton [ 2 ] ;
298
- buttons [ 0 ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
299
- buttons [ 1 ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
326
+ buttons [ ( int ) ButtonTypes . Trigger ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
327
+ buttons [ ( int ) ButtonTypes . Grip ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
300
328
UpdateButtons ( buttons ) ;
301
329
}
302
330
}
@@ -360,11 +388,6 @@ public void Pulse(float intensity, float duration)
360
388
361
389
void OnEnable ( )
362
390
{
363
- if ( inputMap == null )
364
- {
365
- Debug . LogError ( "A Input Map must be assigned to WebXRController!" ) ;
366
- return ;
367
- }
368
391
WebXRManager . OnControllerUpdate += OnControllerUpdate ;
369
392
WebXRManager . OnHandUpdate += OnHandUpdate ;
370
393
WebXRManager . OnHeadsetUpdate += onHeadsetUpdate ;
0 commit comments