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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
|
using UnityEngine.UI;
using UnityEngine.Assertions;
using UnityEngine.InputSystem;
#if XR_HANDS_1_1_OR_NEWER
using UnityEngine.XR.Hands;
#endif
using UnityEngine.XR.Interaction.Toolkit.Inputs;
using UnityEngine.XR.Interaction.Toolkit.Inputs.Readers;
using UnityEngine.XR.Interaction.Toolkit.Inputs.Simulation;
using UnityEngine.XR.Interaction.Toolkit.Utilities;
namespace UnityEngine.XR.Interaction.Toolkit.Samples.DeviceSimulator
{
class XRInteractionSimulatorPlayModeMenu : MonoBehaviour
{
[Header("Menus")]
[SerializeField]
GameObject m_InputSelectionMenu;
[SerializeField]
GameObject m_ClosedInputSelectionMenu;
[SerializeField]
GameObject m_ControllerActionsMenu;
[SerializeField]
GameObject m_ClosedControllerActionsMenu;
[SerializeField]
GameObject m_HandActionsMenu;
[SerializeField]
GameObject m_ClosedHandActionsMenu;
[Header("Input Readers")]
[SerializeField]
XRInputButtonReader m_ToggleActionMenu;
[SerializeField]
XRInputButtonReader m_ToggleInputSelectionMenu;
[Header("Device Highlight Panels")]
[SerializeField]
GameObject m_HighlightFullBodyPanel;
[SerializeField]
GameObject m_HighlightLeftControllerPanel;
[SerializeField]
GameObject m_HighlightRightControllerPanel;
[SerializeField]
GameObject m_HighlightLeftHandPanel;
[SerializeField]
GameObject m_HighlightRightHandPanel;
[SerializeField]
GameObject m_HighlightHeadPanel;
[Header("Controller Action Panels")]
[SerializeField]
GameObject m_ControllerActionHighlightPanel;
[SerializeField]
Text m_FirstControllerActionText;
[SerializeField]
Text m_SecondControllerActionText;
[SerializeField]
Text m_ThirdControllerActionText;
[SerializeField]
Text m_FourthControllerActionText;
[SerializeField]
Text m_FirstControllerBindingText;
[SerializeField]
Text m_SecondControllerBindingText;
[SerializeField]
Text m_ThirdControllerBindingText;
[SerializeField]
Text m_FourthControllerBindingText;
[SerializeField]
GameObject m_FirstControllerBindingGO;
[SerializeField]
GameObject m_SecondControllerBindingGO;
[SerializeField]
GameObject m_ThirdControllerBindingGO;
[SerializeField]
GameObject m_FourthControllerBindingGO;
[Header("Hand Action Panels")]
[SerializeField]
GameObject m_HandActionHighlightPanel;
[SerializeField]
Text m_FirstHandActionText;
[SerializeField]
Text m_SecondHandActionText;
[SerializeField]
Text m_ThirdHandActionText;
[SerializeField]
Text m_FourthHandActionText;
[SerializeField]
Text m_FirstHandBindingText;
[SerializeField]
Text m_SecondHandBindingText;
[SerializeField]
Text m_ThirdHandBindingText;
[SerializeField]
Text m_FourthHandBindingText;
[SerializeField]
GameObject m_FirstHandBindingGO;
[SerializeField]
GameObject m_SecondHandBindingGO;
[SerializeField]
GameObject m_ThirdHandBindingGO;
[SerializeField]
GameObject m_FourthHandBindingGO;
[Header("Hand UI")]
[SerializeField]
Image m_LeftHandIcon;
[SerializeField]
Image m_RightHandIcon;
[SerializeField]
GameObject m_HandPackageWarningPanel;
[SerializeField]
GameObject m_InputModalityManagerWarningPanel;
[SerializeField]
GameObject m_InputMenuHandVisualizerWarningPanel;
[SerializeField]
GameObject m_HandMenuHandVisualizerWarningPanel;
XRInteractionSimulator m_Simulator;
SimulatedDeviceLifecycleManager m_DeviceLifecycleManager;
SimulatedHandExpressionManager m_HandExpressionManager;
SimulatedDeviceLifecycleManager.DeviceMode m_PreviousDeviceMode = SimulatedDeviceLifecycleManager.DeviceMode.None;
TargetedDevices m_PreviousTargetedDeviceInput = TargetedDevices.None;
ControllerInputMode m_PreviousControllerInputMode = ControllerInputMode.None;
SimulatedHandExpression m_PreviousHandExpression = new SimulatedHandExpression();
int m_ControllerActionIndex = -1;
int m_HandActionIndex = -1;
int m_QuickActionHandExpressionLength;
int[] m_HandExpressionIndices = { -1, -1, -1, -1 };
bool m_PreviousControllerMenuState;
bool m_PreviousHandMenuState;
static readonly Color k_DisabledColor = new Color(0x70 / 255f, 0x70 / 255f, 0x70 / 255f);
/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
protected void Start()
{
#if HAS_FIND_FIRST_OBJECT_BY_TYPE
var simulator = Object.FindFirstObjectByType<XRInteractionSimulator>();
#else
var simulator = Object.FindObjectOfType<XRInteractionSimulator>();
#endif
if (simulator != null)
{
m_Simulator = simulator;
}
else
{
Debug.LogError($"Could not find the XRInteractionSimulator component, disabling simulator UI.", this);
gameObject.SetActive(false);
return;
}
if (!m_Simulator.gameObject.TryGetComponent(out m_DeviceLifecycleManager))
{
Debug.LogError($"Could not find SimulatedDeviceLifecycleManager component on {m_Simulator.name}, disabling simulator UI.", this);
gameObject.SetActive(false);
return;
}
if (!m_Simulator.gameObject.TryGetComponent(out m_HandExpressionManager))
{
Debug.LogError($"Could not find SimulatedHandExpressionManager component on {m_Simulator.name}, disabling simulator UI.", this);
gameObject.SetActive(false);
return;
}
InitializeQuickActionPanels();
#if XR_HANDS_1_1_OR_NEWER
CheckInputModalityManager();
#else
m_HandPackageWarningPanel.SetActive(true);
m_LeftHandIcon.color = k_DisabledColor;
m_RightHandIcon.color = k_DisabledColor;
#endif
#if XR_HANDS_1_2_OR_NEWER
if (!m_HandPackageWarningPanel.activeSelf && !m_InputModalityManagerWarningPanel.activeSelf)
CheckHandVisualizer();
#endif
}
void CheckInputModalityManager()
{
if (ComponentLocatorUtility<XRInputModalityManager>.TryFindComponent(out var inputModalityManager) &&
inputModalityManager.leftHand == null && inputModalityManager.rightHand == null)
{
m_InputModalityManagerWarningPanel.SetActive(true);
m_LeftHandIcon.color = k_DisabledColor;
m_RightHandIcon.color = k_DisabledColor;
}
}
#if XR_HANDS_1_2_OR_NEWER
void CheckHandVisualizer()
{
if (ComponentLocatorUtility<XRInputModalityManager>.TryFindComponent(out var inputModalityManager))
{
if (inputModalityManager.leftHand == null && inputModalityManager.rightHand == null)
return;
if ((inputModalityManager.leftHand != null &&
inputModalityManager.leftHand.GetComponentInChildren<XRHandMeshController>() != null) ||
(inputModalityManager.rightHand != null &&
inputModalityManager.rightHand.GetComponentInChildren<XRHandMeshController>() != null))
{
return;
}
m_InputMenuHandVisualizerWarningPanel.SetActive(true);
m_HandMenuHandVisualizerWarningPanel.SetActive(true);
}
}
#endif
void InitializeQuickActionPanels()
{
InitializeControllerQuickActionPanels();
InitializeHandQuickActionPanels();
}
void InitializeControllerQuickActionPanels()
{
var inputModesLength = m_Simulator.quickActionControllerInputModes.Count;
if (inputModesLength > 0)
{
GetControllerQuickActionNames(m_Simulator.quickActionControllerInputModes[0], m_FirstControllerActionText, m_FirstControllerBindingText);
}
else
{
m_FirstControllerActionText.gameObject.SetActive(false);
m_FirstControllerBindingGO.SetActive(false);
}
if (inputModesLength > 1)
{
GetControllerQuickActionNames(m_Simulator.quickActionControllerInputModes[1], m_SecondControllerActionText, m_SecondControllerBindingText);
}
else
{
m_SecondControllerActionText.gameObject.SetActive(false);
m_SecondControllerBindingGO.SetActive(false);
}
if (inputModesLength > 2)
{
GetControllerQuickActionNames(m_Simulator.quickActionControllerInputModes[2], m_ThirdControllerActionText, m_ThirdControllerBindingText);
}
else
{
m_ThirdControllerActionText.gameObject.SetActive(false);
m_ThirdControllerBindingGO.SetActive(false);
}
if (inputModesLength > 3)
{
GetControllerQuickActionNames(m_Simulator.quickActionControllerInputModes[3], m_FourthControllerActionText, m_FourthControllerBindingText);
}
else
{
m_FourthControllerActionText.gameObject.SetActive(false);
m_FourthControllerBindingGO.SetActive(false);
}
}
void InitializeHandQuickActionPanels()
{
for (int i = 0; i < m_HandExpressionManager.simulatedHandExpressions.Count; i++)
{
if (m_HandExpressionManager.simulatedHandExpressions[i].isQuickAction)
{
if (m_QuickActionHandExpressionLength < 4)
m_HandExpressionIndices[m_QuickActionHandExpressionLength] = i;
m_QuickActionHandExpressionLength++;
}
}
if (m_QuickActionHandExpressionLength > 0)
{
var handExpression = m_HandExpressionManager.simulatedHandExpressions[m_HandExpressionIndices[0]];
m_FirstHandActionText.text = handExpression.name;
m_FirstHandBindingText.text = GetBindingString(handExpression.toggleInput);
}
else
{
m_FirstHandActionText.gameObject.SetActive(false);
m_FirstHandBindingGO.SetActive(false);
}
if (m_QuickActionHandExpressionLength > 1)
{
var handExpression = m_HandExpressionManager.simulatedHandExpressions[m_HandExpressionIndices[1]];
m_SecondHandActionText.text = handExpression.name;
m_SecondHandBindingText.text = GetBindingString(handExpression.toggleInput);
}
else
{
m_SecondHandActionText.gameObject.SetActive(false);
m_SecondHandBindingGO.SetActive(false);
}
if (m_QuickActionHandExpressionLength > 2)
{
var handExpression = m_HandExpressionManager.simulatedHandExpressions[m_HandExpressionIndices[2]];
m_ThirdHandActionText.text = handExpression.name;
m_ThirdHandBindingText.text = GetBindingString(handExpression.toggleInput);
}
else
{
m_ThirdHandActionText.gameObject.SetActive(false);
m_ThirdHandBindingGO.SetActive(false);
}
if (m_QuickActionHandExpressionLength > 3)
{
var handExpression = m_HandExpressionManager.simulatedHandExpressions[m_HandExpressionIndices[3]];
m_FourthHandActionText.text = handExpression.name;
m_FourthHandBindingText.text = GetBindingString(handExpression.toggleInput);
}
else
{
m_FourthHandActionText.gameObject.SetActive(false);
m_FourthHandBindingGO.SetActive(false);
}
}
void GetControllerQuickActionNames(ControllerInputMode inputMode, Text actionText, Text bindingText)
{
switch (inputMode)
{
case ControllerInputMode.None:
actionText.text = "None";
bindingText.text = "?";
break;
case ControllerInputMode.Trigger:
actionText.text = "Trigger";
bindingText.text = GetBindingString(m_Simulator.triggerInput);
break;
case ControllerInputMode.Grip:
actionText.text = "Grip";
bindingText.text = GetBindingString(m_Simulator.gripInput);
break;
case ControllerInputMode.PrimaryButton:
actionText.text = "Primary";
bindingText.text = GetBindingString(m_Simulator.primaryButtonInput);
break;
case ControllerInputMode.SecondaryButton:
actionText.text = "Secondary";
bindingText.text = GetBindingString(m_Simulator.secondaryButtonInput);
break;
case ControllerInputMode.Menu:
actionText.text = "Menu";
bindingText.text = GetBindingString(m_Simulator.menuInput);
break;
case ControllerInputMode.Primary2DAxisClick:
actionText.text = "Prim2DClick";
bindingText.text = GetBindingString(m_Simulator.primary2DAxisClickInput);
break;
case ControllerInputMode.Secondary2DAxisClick:
actionText.text = "Sec2DClick";
bindingText.text = GetBindingString(m_Simulator.secondary2DAxisClickInput);
break;
case ControllerInputMode.Primary2DAxisTouch:
actionText.text = "Prim2DTouch";
bindingText.text = GetBindingString(m_Simulator.primary2DAxisTouchInput);
break;
case ControllerInputMode.Secondary2DAxisTouch:
actionText.text = "Sec2DTouch";
bindingText.text = GetBindingString(m_Simulator.secondary2DAxisTouchInput);
break;
case ControllerInputMode.PrimaryTouch:
actionText.text = "PrimTouch";
bindingText.text = GetBindingString(m_Simulator.primaryTouchInput);
break;
case ControllerInputMode.SecondaryTouch:
actionText.text = "SecTouch";
bindingText.text = GetBindingString(m_Simulator.secondaryTouchInput);
break;
default:
Assert.IsTrue(false, $"Unhandled {nameof(inputMode)}={inputMode}.");
break;
}
}
static string GetBindingString(XRInputButtonReader reader)
{
if (reader == null)
return string.Empty;
InputAction action;
switch (reader.inputSourceMode)
{
case XRInputButtonReader.InputSourceMode.InputActionReference:
action = reader.inputActionReferencePerformed != null ? reader.inputActionReferencePerformed.action : null;
break;
case XRInputButtonReader.InputSourceMode.InputAction:
action = reader.inputActionPerformed;
break;
default:
action = null;
break;
}
return action != null ? action.GetBindingDisplayString(0) : string.Empty;
}
/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
protected void Update()
{
HandleHighlightedDevicePanels();
HandleHighlightedControllerActionPanels();
HandleHighlightedHandActionPanels();
HandleActiveMenus();
}
/// <summary>
/// Toggles the visibility of the input selection menu.
/// </summary>
public void OpenCloseInputSelectionMenu()
{
if (m_InputSelectionMenu.activeSelf)
{
m_ClosedInputSelectionMenu.SetActive(true);
m_InputSelectionMenu.SetActive(false);
}
else
{
m_ClosedInputSelectionMenu.SetActive(false);
m_InputSelectionMenu.SetActive(true);
}
}
/// <summary>
/// Toggles the visibility of for the controller actions menu.
/// </summary>
public void OpenCloseControllerActionsMenu()
{
if (m_ControllerActionsMenu.activeSelf)
{
m_ClosedControllerActionsMenu.SetActive(true);
m_ControllerActionsMenu.SetActive(false);
}
else
{
m_ClosedControllerActionsMenu.SetActive(false);
m_ControllerActionsMenu.SetActive(true);
}
}
/// <summary>
/// Toggles the visibility of for the hand actions menu.
/// </summary>
public void OpenCloseHandActionsMenu()
{
if (m_HandActionsMenu.activeSelf)
{
m_ClosedHandActionsMenu.SetActive(true);
m_HandActionsMenu.SetActive(false);
}
else
{
m_ClosedHandActionsMenu.SetActive(false);
m_HandActionsMenu.SetActive(true);
}
}
void HandleActiveMenus()
{
if (m_PreviousDeviceMode != m_DeviceLifecycleManager.deviceMode && !m_Simulator.manipulatingFPS)
{
if (m_Simulator.manipulatingLeftController || m_Simulator.manipulatingRightController)
{
m_PreviousHandMenuState = m_HandActionsMenu.activeSelf;
m_HandActionsMenu.SetActive(false);
m_ClosedHandActionsMenu.SetActive(false);
if (m_PreviousControllerMenuState)
m_ControllerActionsMenu.SetActive(true);
else
m_ClosedControllerActionsMenu.SetActive(true);
}
else if (m_Simulator.manipulatingLeftHand || m_Simulator.manipulatingRightHand)
{
m_PreviousControllerMenuState = m_ControllerActionsMenu.activeSelf;
m_ControllerActionsMenu.SetActive(false);
m_ClosedControllerActionsMenu.SetActive(false);
if (m_PreviousHandMenuState)
m_HandActionsMenu.SetActive(true);
else
m_ClosedHandActionsMenu.SetActive(true);
}
m_PreviousDeviceMode = m_DeviceLifecycleManager.deviceMode;
}
if (m_Simulator.manipulatingFPS && m_PreviousDeviceMode != SimulatedDeviceLifecycleManager.DeviceMode.None)
{
if (m_PreviousDeviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Controller)
m_PreviousControllerMenuState = m_ControllerActionsMenu.activeSelf;
else if (m_PreviousDeviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Hand)
m_PreviousHandMenuState = m_HandActionsMenu.activeSelf;
m_HandActionsMenu.SetActive(false);
m_ClosedHandActionsMenu.SetActive(false);
m_ControllerActionsMenu.SetActive(false);
m_ClosedControllerActionsMenu.SetActive(false);
m_PreviousDeviceMode = SimulatedDeviceLifecycleManager.DeviceMode.None;
}
if (m_ToggleActionMenu.ReadWasPerformedThisFrame())
{
if (m_Simulator.manipulatingLeftController || m_Simulator.manipulatingRightController)
OpenCloseControllerActionsMenu();
else if (m_Simulator.manipulatingLeftHand || m_Simulator.manipulatingRightHand)
OpenCloseHandActionsMenu();
}
if (m_ToggleInputSelectionMenu.ReadWasPerformedThisFrame())
OpenCloseInputSelectionMenu();
}
void HandleHighlightedDevicePanels()
{
if (m_Simulator.targetedDeviceInput != m_PreviousTargetedDeviceInput || m_PreviousDeviceMode != m_DeviceLifecycleManager.deviceMode)
{
ClearHighlightedDevicePanels();
if (m_Simulator.manipulatingFPS)
{
m_HighlightFullBodyPanel.SetActive(true);
return;
}
if (m_Simulator.manipulatingLeftController)
{
m_HighlightLeftControllerPanel.SetActive(true);
}
if (m_Simulator.manipulatingRightController)
{
m_HighlightRightControllerPanel.SetActive(true);
}
if (m_Simulator.manipulatingLeftHand)
{
m_HighlightLeftHandPanel.SetActive(true);
}
if (m_Simulator.manipulatingRightHand)
{
m_HighlightRightHandPanel.SetActive(true);
}
if (m_Simulator.manipulatingHMD)
{
m_HighlightHeadPanel.SetActive(true);
}
m_PreviousTargetedDeviceInput = m_Simulator.targetedDeviceInput;
}
}
void HandleHighlightedControllerActionPanels()
{
if (m_Simulator.controllerInputMode != m_PreviousControllerInputMode)
{
m_ControllerActionIndex = m_ControllerActionIndex < m_Simulator.quickActionControllerInputModes.Count - 1 ? m_ControllerActionIndex + 1 : 0;
m_ControllerActionHighlightPanel.SetActive(true);
if (m_ControllerActionIndex == 0)
{
m_ControllerActionHighlightPanel.transform.position = m_FirstControllerActionText.transform.position;
}
else if (m_ControllerActionIndex == 1)
{
m_ControllerActionHighlightPanel.transform.position = m_SecondControllerActionText.transform.position;
}
else if (m_ControllerActionIndex == 2)
{
m_ControllerActionHighlightPanel.transform.position = m_ThirdControllerActionText.transform.position;
}
else if (m_ControllerActionIndex == 3)
{
m_ControllerActionHighlightPanel.transform.position = m_FourthControllerActionText.transform.position;
}
else
{
m_ControllerActionHighlightPanel.SetActive(false);
}
m_PreviousControllerInputMode = m_Simulator.controllerInputMode;
}
}
void HandleHighlightedHandActionPanels()
{
if (m_Simulator.currentHandExpression != m_PreviousHandExpression)
{
m_HandActionIndex = m_HandActionIndex < m_QuickActionHandExpressionLength - 1 ? m_HandActionIndex + 1 : 0;
m_HandActionHighlightPanel.SetActive(true);
if (m_HandActionIndex == 0)
{
m_HandActionHighlightPanel.transform.position = m_FirstHandActionText.transform.position;
}
else if (m_HandActionIndex == 1)
{
m_HandActionHighlightPanel.transform.position = m_SecondHandActionText.transform.position;
}
else if (m_HandActionIndex == 2)
{
m_HandActionHighlightPanel.transform.position = m_ThirdHandActionText.transform.position;
}
else if (m_HandActionIndex == 3)
{
m_HandActionHighlightPanel.transform.position = m_FourthHandActionText.transform.position;
}
else
{
m_HandActionHighlightPanel.SetActive(false);
}
m_PreviousHandExpression = m_Simulator.currentHandExpression;
}
}
void ClearHighlightedDevicePanels()
{
m_HighlightFullBodyPanel.SetActive(false);
m_HighlightLeftControllerPanel.SetActive(false);
m_HighlightRightControllerPanel.SetActive(false);
m_HighlightLeftHandPanel.SetActive(false);
m_HighlightRightHandPanel.SetActive(false);
m_HighlightHeadPanel.SetActive(false);
}
}
}
|