-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputController.cs
342 lines (289 loc) · 13.4 KB
/
InputController.cs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
// Cuboid (http://github.com/arjonagelhout/cuboid)
// Copyright (c) 2023 Arjo Nagelhout
//
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR;
namespace Cuboid.Input
{
/// <summary>
/// The InputController is responsible for getting all input from the
/// XRControllerInput action maps and allows other classes to listen to
/// changes in input.
///
/// This is done via code instead of individual action bindings because
/// this gives errors when the bindings are lost / changed. So there's less
/// room for someone to accidentally forget binding.
///
/// As a tradeoff, this approach is more verbose.
/// </summary>
public class InputController : MonoBehaviour,
XRControllerInput.ILeftHandActions,
XRControllerInput.ILeftHandTrackingActions,
XRControllerInput.ILeftHandVisualsActions,
XRControllerInput.IRightHandActions,
XRControllerInput.IRightHandTrackingActions,
XRControllerInput.IRightHandVisualsActions
{
private static InputController _instance;
public static InputController Instance { get { return _instance; } }
private XRControllerInput _xrControllerInput;
[NonSerialized] public Binding<bool> NonDominantHandPrimaryButton = new(false);
[NonSerialized] public Binding<bool> NonDominantHandSecondaryButton = new(false);
[NonSerialized] public Binding<Vector2> NonDominantHandScroll2D = new(Vector2.zero);
[NonSerialized] public Handedness Handedness;
public XRController LeftHandXRController;
public XRController RightHandXRController;
public XRController GetXRController(Handedness.Hand hand) => hand == Handedness.Hand.LeftHand ?
LeftHandXRController : RightHandXRController;
private class ButtonData
{
public Binding<bool> Binding;
public int Count = 0;
}
public class ControllerInputData
{
public Binding<bool> PrimaryButton = new();
public Binding<bool> GripButton = new();
public Binding<Vector2> Scroll2D = new();
public Action JoystickButtonPressed;
// tracking
public Vector3 Position = Vector3.zero;
public Quaternion Rotation = Quaternion.identity;
public InputTrackingState TrackingState = InputTrackingState.All;
// visuals
public Action<float> OnPrimaryButtonValueChanged;
public Action<float> OnSecondaryButtonValueChanged;
public Action<float> OnTriggerValueChanged;
public Action<float> OnGripValueChanged;
}
public ControllerInputData LeftInput => ControllersInputData[Handedness.Hand.LeftHand];
public ControllerInputData RightInput => ControllersInputData[Handedness.Hand.RightHand];
public Dictionary<Handedness.Hand, ControllerInputData> ControllersInputData = new Dictionary<Handedness.Hand, ControllerInputData>()
{
{
Handedness.Hand.LeftHand,
new ControllerInputData()
},
{
Handedness.Hand.RightHand,
new ControllerInputData()
}
};
private Dictionary<Binding<bool>, ButtonData> _buttonData = new Dictionary<Binding<bool>, ButtonData>();
private void Awake()
{
// Singleton implementation
if (_instance != null && _instance != this) { Destroy(this); } else { _instance = this; }
Handedness = new();
}
private void Start()
{
// need to be registered because there are multiple buttons that can activate / deactivate the
// value and we only want to fire the events once.
RegisterButton(LeftInput.PrimaryButton);
RegisterButton(RightInput.PrimaryButton);
RegisterActions();
}
private void RegisterButton(Binding<bool> binding)
{
_buttonData.Add(binding, new ButtonData()
{
Binding = binding,
Count = 0
});
}
#region Left Hand actions
void XRControllerInput.ILeftHandActions.OnPrimaryButton(InputAction.CallbackContext context)
{
if (!Handedness.IsLeftHandEnabled)
{
ProcessButtonAction(context, NonDominantHandPrimaryButton);
}
ProcessButtonAction(context, LeftInput.PrimaryButton);
}
void XRControllerInput.ILeftHandActions.OnSecondaryButton(InputAction.CallbackContext context)
{
if (!Handedness.IsLeftHandEnabled)
{
ProcessButtonAction(context, NonDominantHandSecondaryButton);
}
ProcessButtonAction(context, LeftInput.PrimaryButton);
}
void XRControllerInput.ILeftHandActions.OnGripButton(InputAction.CallbackContext context)
{
ProcessButtonAction(context, LeftInput.PrimaryButton);
}
void XRControllerInput.ILeftHandActions.OnTriggerButton(InputAction.CallbackContext context)
{
ProcessButtonAction(context, LeftInput.PrimaryButton);
}
void XRControllerInput.ILeftHandActions.OnScroll2D(InputAction.CallbackContext context)
{
if (!Handedness.IsLeftHandEnabled)
{
NonDominantHandScroll2D.Value = context.ReadValue<Vector2>();
}
LeftInput.Scroll2D.Value = context.ReadValue<Vector2>();
}
void XRControllerInput.ILeftHandActions.OnJoystickButtonPressed(InputAction.CallbackContext context)
{
if (context.performed)
{
LeftInput.JoystickButtonPressed?.Invoke();
}
}
#endregion
#region Right hand actions
void XRControllerInput.IRightHandActions.OnPrimaryButton(InputAction.CallbackContext context)
{
if (!Handedness.IsRightHandEnabled)
{
ProcessButtonAction(context, NonDominantHandPrimaryButton);
}
ProcessButtonAction(context, RightInput.PrimaryButton);
}
void XRControllerInput.IRightHandActions.OnSecondaryButton(InputAction.CallbackContext context)
{
if (!Handedness.IsRightHandEnabled)
{
ProcessButtonAction(context, NonDominantHandSecondaryButton);
}
ProcessButtonAction(context, RightInput.PrimaryButton);
}
void XRControllerInput.IRightHandActions.OnGripButton(InputAction.CallbackContext context)
{
ProcessButtonAction(context, RightInput.PrimaryButton);
}
void XRControllerInput.IRightHandActions.OnTriggerButton(InputAction.CallbackContext context)
{
ProcessButtonAction(context, RightInput.PrimaryButton);
}
void XRControllerInput.IRightHandActions.OnScroll2D(InputAction.CallbackContext context)
{
if (!Handedness.IsRightHandEnabled)
{
NonDominantHandScroll2D.Value = context.ReadValue<Vector2>();
}
RightInput.Scroll2D.Value = context.ReadValue<Vector2>();
}
void XRControllerInput.IRightHandActions.OnJoystickButtonPressed(InputAction.CallbackContext context)
{
if (context.performed)
{
RightInput.JoystickButtonPressed?.Invoke();
}
}
#endregion
/// <summary>
/// Calls the down or up actions based on whether the button was
/// pressed or released
/// </summary>
private void ProcessButtonAction(InputAction.CallbackContext context, Binding<bool> binding)
{
if (_buttonData.TryGetValue(binding, out ButtonData buttonData))
{
ref int count = ref _buttonData[binding].Count;
switch (context.phase)
{
case InputActionPhase.Started:
if (count++ == 0)
{
// means that this is the first time it tried to press
binding.Value = true;
}
break;
case InputActionPhase.Canceled:
if (--count == 0)
{
// means that this is the last button that was released
binding.Value = false;
}
break;
}
}
else
{
switch (context.phase)
{
case InputActionPhase.Started:
binding.Value = true;
break;
case InputActionPhase.Canceled:
binding.Value = false;
break;
}
}
}
#region Action registration
private void OnEnable()
{
RegisterActions();
}
private void OnDisable()
{
UnregisterActions();
}
private void OnDestroy()
{
UnregisterActions();
}
private void RegisterActions()
{
if (_xrControllerInput == null)
{
_xrControllerInput = new XRControllerInput();
}
_xrControllerInput.LeftHand.SetCallbacks(this);
_xrControllerInput.LeftHandTracking.SetCallbacks(this);
_xrControllerInput.LeftHandVisuals.SetCallbacks(this);
_xrControllerInput.RightHand.SetCallbacks(this);
_xrControllerInput.RightHandTracking.SetCallbacks(this);
_xrControllerInput.RightHandVisuals.SetCallbacks(this);
// always call enable! otherwise it won't get any actions
_xrControllerInput.LeftHand.Enable();
_xrControllerInput.LeftHandTracking.Enable();
_xrControllerInput.LeftHandVisuals.Enable();
_xrControllerInput.RightHand.Enable();
_xrControllerInput.RightHandTracking.Enable();
_xrControllerInput.RightHandVisuals.Enable();
}
private void UnregisterActions()
{
if (_xrControllerInput != null)
{
_xrControllerInput.LeftHand.Disable();
_xrControllerInput.LeftHandTracking.Disable();
_xrControllerInput.LeftHandVisuals.Disable();
_xrControllerInput.RightHand.Disable();
_xrControllerInput.RightHandTracking.Disable();
_xrControllerInput.RightHandVisuals.Disable();
}
}
// left hand action binding
// tracking
void XRControllerInput.ILeftHandTrackingActions.OnPosition(InputAction.CallbackContext context) => LeftInput.Position = context.ReadValue<Vector3>();
void XRControllerInput.ILeftHandTrackingActions.OnRotation(InputAction.CallbackContext context) => LeftInput.Rotation = context.ReadValue<Quaternion>();
void XRControllerInput.ILeftHandTrackingActions.OnTrackingState(InputAction.CallbackContext context) => LeftInput.TrackingState = (InputTrackingState)context.ReadValue<int>();
// visuals
void XRControllerInput.ILeftHandVisualsActions.OnPrimaryButtonValue(InputAction.CallbackContext context) => LeftInput.OnPrimaryButtonValueChanged?.Invoke(context.ReadValue<float>());
void XRControllerInput.ILeftHandVisualsActions.OnSecondaryButtonValue(InputAction.CallbackContext context) => LeftInput.OnSecondaryButtonValueChanged?.Invoke(context.ReadValue<float>());
void XRControllerInput.ILeftHandVisualsActions.OnGripValue(InputAction.CallbackContext context) => LeftInput.OnGripValueChanged?.Invoke(context.ReadValue<float>());
void XRControllerInput.ILeftHandVisualsActions.OnTriggerValue(InputAction.CallbackContext context) => LeftInput.OnTriggerValueChanged?.Invoke(context.ReadValue<float>());
// right hand action binding
// tracking
void XRControllerInput.IRightHandTrackingActions.OnPosition(InputAction.CallbackContext context) => RightInput.Position = context.ReadValue<Vector3>();
void XRControllerInput.IRightHandTrackingActions.OnRotation(InputAction.CallbackContext context) => RightInput.Rotation = context.ReadValue<Quaternion>();
void XRControllerInput.IRightHandTrackingActions.OnTrackingState(InputAction.CallbackContext context) => RightInput.TrackingState = (InputTrackingState)context.ReadValue<int>();
// visuals
void XRControllerInput.IRightHandVisualsActions.OnPrimaryButtonValue(InputAction.CallbackContext context) => RightInput.OnPrimaryButtonValueChanged?.Invoke(context.ReadValue<float>());
void XRControllerInput.IRightHandVisualsActions.OnSecondaryButtonValue(InputAction.CallbackContext context) => RightInput.OnSecondaryButtonValueChanged?.Invoke(context.ReadValue<float>());
void XRControllerInput.IRightHandVisualsActions.OnGripValue(InputAction.CallbackContext context) => RightInput.OnGripValueChanged?.Invoke(context.ReadValue<float>());
void XRControllerInput.IRightHandVisualsActions.OnTriggerValue(InputAction.CallbackContext context) => RightInput.OnTriggerValueChanged?.Invoke(context.ReadValue<float>());
#endregion
}
}