GvrTrackedController.cs
10.8 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
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
//-----------------------------------------------------------------------
// <copyright file="GvrTrackedController.cs" company="Google Inc.">
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-----------------------------------------------------------------------
using System.Collections;
using Gvr.Internal;
using UnityEngine;
/// <summary>Represents an object tracked by controller input.</summary>
/// <remarks>
/// Manages the active status of the tracked controller based on controller connection status.
/// <para>
/// Fetches a `GvrControllerInputDevice` for the configured `GvrControllerHand` and propagates
/// the device instance to all `IGvrControllerInputDeviceReceiver`s underneath this object on
/// Start and if the controller handedness changes. If the controller is not positionally
/// tracked, position of the object is updated to approximate arm mechanics by using a
/// `GvrBaseArmModel`. `GvrBaseArmModel`s are also propagated to all `IGvrArmModelReceiver`s
/// underneath this object.
/// </para></remarks>
[HelpURL("https://developers.google.com/vr/reference/unity/class/GvrTrackedController")]
public class GvrTrackedController : MonoBehaviour
{
[SerializeField]
[Tooltip("Arm model used to control the pose (position and rotation) of the object, " +
"and to propagate to children that implement IGvrArmModelReceiver.")]
private GvrBaseArmModel armModel;
private GvrControllerInputDevice controllerInputDevice;
[SerializeField]
[Tooltip("Is the object's active status determined by the controller connection status.")]
private bool isDeactivatedWhenDisconnected = true;
[SerializeField]
[Tooltip("Controller Hand")]
private GvrControllerHand controllerHand = GvrControllerHand.Dominant;
/// <summary>Gets the controller input device for this tracked object.</summary>
/// <value>The controller input device for this tracked object.</value>
public GvrControllerInputDevice ControllerInputDevice
{
get
{
return controllerInputDevice;
}
}
/// <summary>Gets or sets the controller hand used for this tracked object.</summary>
/// <value>The controller hand used for this tracked object.</value>
public GvrControllerHand ControllerHand
{
get
{
return controllerHand;
}
set
{
if (value != controllerHand)
{
controllerHand = value;
SetupControllerInputDevice();
}
}
}
/// <summary>Gets or sets an arm model.</summary>
/// <remarks>
/// Used to control the pose (position and rotation) of the object and to propagate to children
/// that implement `IGvrArmModelReceiver`.
/// </remarks>
/// <value>
/// An arm model used to control the pose (position and rotation) of the object, and to
/// propagate to children that implement `IGvrArmModelReceiver`.
/// </value>
public GvrBaseArmModel ArmModel
{
get
{
return armModel;
}
set
{
if (armModel == value)
{
return;
}
armModel = value;
PropagateControllerInputDeviceToArmModel();
PropagateArmModel();
}
}
/// <summary>
/// Gets or sets a value indicating whether the object's active status is determined by the
/// controller connection status.
/// </summary>
/// <value>
/// Value `true` if the object's active status is determined by the controller connection
/// status, `false` otherwise.
/// </value>
public bool IsDeactivatedWhenDisconnected
{
get
{
return isDeactivatedWhenDisconnected;
}
set
{
if (isDeactivatedWhenDisconnected == value)
{
return;
}
isDeactivatedWhenDisconnected = value;
if (isDeactivatedWhenDisconnected)
{
OnControllerStateChanged(controllerInputDevice.State, controllerInputDevice.State);
}
}
}
/// <summary>Propagates the arm model to all `IGvrArmModelReceiver`s.</summary>
/// <remarks>Should only called when ArmModel is instantiated or changed.</remarks>
[SuppressMemoryAllocationError(
IsWarning = false,
Reason = "Only called when ArmModel is instantiated or changed.")]
public void PropagateArmModel()
{
IGvrArmModelReceiver[] receivers =
GetComponentsInChildren<IGvrArmModelReceiver>(true);
for (int i = 0; i < receivers.Length; i++)
{
IGvrArmModelReceiver receiver = receivers[i];
receiver.ArmModel = armModel;
}
}
private void Awake()
{
// Adding this event handler calls it immediately.
GvrControllerInput.OnDevicesChanged += SetupControllerInputDevice;
}
private void OnEnable()
{
// Print an error to console if no GvrControllerInput is found.
if (controllerInputDevice.State == GvrConnectionState.Error)
{
Debug.LogWarning(controllerInputDevice.ErrorDetails);
}
// Update the position using OnPostControllerInputUpdated.
// This way, the position and rotation will be correct for the entire frame
// so that it doesn't matter what order Updates get called in.
GvrControllerInput.OnPostControllerInputUpdated += OnPostControllerInputUpdated;
// Force the pose to update immediately in case the controller isn't updated before the next
// time a frame is rendered.
UpdatePose();
// Check the controller state immediately whenever this script is enabled.
OnControllerStateChanged(controllerInputDevice.State, controllerInputDevice.State);
}
private void OnDisable()
{
GvrControllerInput.OnPostControllerInputUpdated -= OnPostControllerInputUpdated;
}
private void Start()
{
PropagateArmModel();
if (controllerInputDevice != null)
{
PropagateControllerInputDevice();
OnControllerStateChanged(controllerInputDevice.State, controllerInputDevice.State);
}
}
private void OnDestroy()
{
GvrControllerInput.OnDevicesChanged -= SetupControllerInputDevice;
if (controllerInputDevice != null)
{
controllerInputDevice.OnStateChanged -= OnControllerStateChanged;
controllerInputDevice = null;
PropagateControllerInputDevice();
}
}
private void PropagateControllerInputDevice()
{
IGvrControllerInputDeviceReceiver[] receivers =
GetComponentsInChildren<IGvrControllerInputDeviceReceiver>(true);
foreach (var receiver in receivers)
{
receiver.ControllerInputDevice = controllerInputDevice;
}
PropagateControllerInputDeviceToArmModel();
}
private void PropagateControllerInputDeviceToArmModel()
{
// Propagate the controller input device to everything in the arm model's object's
// hierarchy in case it is not a child of the tracked controller.
if (armModel != null)
{
IGvrControllerInputDeviceReceiver[] receivers =
armModel.GetComponentsInChildren<IGvrControllerInputDeviceReceiver>(true);
foreach (var receiver in receivers)
{
receiver.ControllerInputDevice = controllerInputDevice;
}
}
}
private void SetupControllerInputDevice()
{
GvrControllerInputDevice newDevice = GvrControllerInput.GetDevice(controllerHand);
if (controllerInputDevice == newDevice)
{
return;
}
if (controllerInputDevice != null)
{
controllerInputDevice.OnStateChanged -= OnControllerStateChanged;
controllerInputDevice = null;
}
controllerInputDevice = newDevice;
if (controllerInputDevice != null)
{
controllerInputDevice.OnStateChanged += OnControllerStateChanged;
OnControllerStateChanged(controllerInputDevice.State, controllerInputDevice.State);
}
else
{
OnControllerStateChanged(GvrConnectionState.Disconnected,
GvrConnectionState.Disconnected);
}
PropagateControllerInputDevice();
}
private void OnPostControllerInputUpdated()
{
UpdatePose();
}
private void OnControllerStateChanged(GvrConnectionState state, GvrConnectionState oldState)
{
if (isDeactivatedWhenDisconnected && enabled)
{
gameObject.SetActive(state == GvrConnectionState.Connected);
}
}
private void UpdatePose()
{
if (controllerInputDevice == null)
{
return;
}
// Disable arm model if the device supports 6DoF.
if (controllerInputDevice.SupportsPositionalTracking)
{
transform.localPosition = controllerInputDevice.Position;
transform.localRotation = controllerInputDevice.Orientation;
}
else
{
if (armModel == null || !controllerInputDevice.IsDominantHand)
{
return;
}
transform.localPosition = ArmModel.ControllerPositionFromHead;
transform.localRotation = ArmModel.ControllerRotationFromHead;
}
}
#if UNITY_EDITOR
/// <summary>This MonoBehavior's `OnValidate` override.</summary>
/// <remarks>
/// If the `armModel` serialized field is changed while the application is playing by using the
/// inspector in the editor, then we need to call `PropagateArmModel` to ensure all children
/// `IGvrArmModelReceiver` are updated.
/// <para>
/// Outside of the editor, this can't happen because the arm model can only change when a Setter
/// is called that automatically calls `PropagateArmModel`.
/// </para></remarks>
private void OnValidate()
{
if (Application.isPlaying && isActiveAndEnabled)
{
PropagateArmModel();
if (controllerInputDevice != null)
{
OnControllerStateChanged(controllerInputDevice.State, controllerInputDevice.State);
}
}
}
#endif // UNITY_EDITOR
}