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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
|
using System;
using UnityEngine.InputSystem;
using UnityEngine.Serialization;
using UnityEngine.UI;
using UnityEngine.XR.Interaction.Toolkit.Inputs.Simulation;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.XR.Interaction.Toolkit.Samples.DeviceSimulator
{
class XRDeviceSimulatorUI : MonoBehaviour
{
XRDeviceSimulator m_Simulator;
SimulatedDeviceLifecycleManager m_DeviceLifecycleManager;
SimulatedHandExpressionManager m_HandExpressionManager;
const string k_MouseDeviceType = "Mouse";
const string k_TranslateLookText = "Move";
const string k_RotateLookText = "Look";
#if UNITY_EDITOR
const string k_MenuOpenStateKey = "XRI." + nameof(XRDeviceSimulatorUI) + ".MenuOpenState";
#endif
[SerializeField]
[HideInInspector]
bool m_IsMenuOpen = true;
bool isMenuOpen
{
get
{
#if UNITY_EDITOR
m_IsMenuOpen = EditorPrefs.GetBool(k_MenuOpenStateKey, m_IsMenuOpen);
#endif
return m_IsMenuOpen;
}
set
{
m_IsMenuOpen = value;
#if UNITY_EDITOR
EditorPrefs.SetBool(k_MenuOpenStateKey, m_IsMenuOpen);
#endif
}
}
[Header("Open/Close Panels")]
[SerializeField]
GameObject m_XRDeviceSimulatorMainPanel;
[SerializeField]
GameObject m_XRDeviceSimulatorCollapsedPanel;
[Header("Sprites")]
[SerializeField]
Sprite m_HmdSpriteDark;
[SerializeField]
Sprite m_HmdSpriteLight;
[SerializeField]
Sprite m_KeyboardSprite;
internal Sprite keyboardSprite => m_KeyboardSprite;
[SerializeField]
Sprite m_MouseSprite;
internal Sprite mouseSprite => m_MouseSprite;
[SerializeField]
Sprite m_RMouseSpriteDark;
internal Sprite rMouseSpriteDark => m_RMouseSpriteDark;
[SerializeField]
Sprite m_RMouseSpriteLight;
internal Sprite rMouseSpriteLight => m_RMouseSpriteLight;
[HideInInspector]
[SerializeField]
Sprite m_RMouseSprite;
internal Sprite rMouseSprite
{
get
{
#if !UNITY_EDITOR
if (m_RMouseSprite == null)
m_RMouseSprite = m_RMouseSpriteDark;
#endif
return m_RMouseSprite;
}
}
[SerializeField]
Sprite m_RoundedRectangle;
[Header("General")]
[SerializeField]
Text m_CycleDevicesText;
[SerializeField]
Text m_CurrentSelectedDeviceText;
[Header("Headset Device")]
[SerializeField]
Image m_HeadsetImage;
[Space]
[SerializeField]
Image m_HeadsetMoveButton;
[SerializeField]
Image m_HeadsetMoveButtonIcon;
[SerializeField]
Text m_HeadsetMoveButtonText;
[SerializeField]
Image m_HeadsetMoveValueIcon;
[SerializeField]
Text m_HeadsetMoveValueText;
[Space]
[SerializeField]
Image m_HeadsetLookButton;
[SerializeField]
Text m_HeadsetLookButtonText;
[SerializeField]
Image m_HeadsetLookValueIcon;
[SerializeField]
Text m_HeadsetLookValueText;
[Space]
[SerializeField]
[FormerlySerializedAs("m_ShowCursorButton")]
Image m_CursorLockButton;
[SerializeField]
[FormerlySerializedAs("m_ShowCursorValueText")]
Text m_CursorLockValueText;
[Space]
[SerializeField]
Text m_MouseModeButtonText;
[SerializeField]
Text m_MouseModeValueText;
[Space]
[SerializeField]
Image m_HeadsetSelectedButton;
[SerializeField]
Text m_HeadsetSelectedValueText;
[Header("Controllers")]
[SerializeField]
Image m_ControllerSelectedButton;
[SerializeField]
Image m_ControllerSelectedIcon;
[SerializeField]
Text m_ControllerSelectedText;
[SerializeField]
Text m_ControllersSelectedValueText;
[SerializeField]
CanvasGroup m_ControllersCanvasGroup;
[Header("Left Controller")]
[SerializeField]
XRDeviceSimulatorControllerUI m_LeftController;
[SerializeField]
Text m_LeftControllerButtonText;
[Header("Right Controller")]
[SerializeField]
XRDeviceSimulatorControllerUI m_RightController;
[SerializeField]
Text m_RightControllerButtonText;
[Header("Hands")]
[SerializeField]
Image m_HandsSelectedButton;
[SerializeField]
Image m_HandsSelectedIcon;
[SerializeField]
Text m_HandsSelectedText;
[SerializeField]
Image m_HandsSelectedValueIcon;
[SerializeField]
Text m_HandsSelectedValueText;
[SerializeField]
CanvasGroup m_HandsCanvasGroup;
[Header("Left Hand")]
[SerializeField]
XRDeviceSimulatorHandsUI m_LeftHand;
[SerializeField]
Text m_LeftHandButtonText;
[Header("Right Hand")]
[SerializeField]
XRDeviceSimulatorHandsUI m_RightHand;
[SerializeField]
Text m_RightHandButtonText;
static readonly Color k_EnabledColorDark = new Color(0xC4 / 255f, 0xC4 / 255f, 0xC4 / 255f);
static readonly Color k_EnabledColorLight = new Color(0x55 / 255f, 0x55 / 255f, 0x55 / 255f);
[HideInInspector]
[SerializeField]
Color m_EnabledColor = Color.clear;
internal Color enabledColor
{
get
{
#if !UNITY_EDITOR
if (m_EnabledColor == Color.clear)
m_EnabledColor = k_EnabledColorDark;
#endif
return m_EnabledColor;
}
}
static readonly Color k_DisabledColorDark = new Color(0xC4 / 255f, 0xC4 / 255f, 0xC4 / 255f, 0.5f);
static readonly Color k_DisabledColorLight = new Color(0x55 / 255f, 0x55 / 255f, 0x55 / 255f, 0.5f);
[HideInInspector]
[SerializeField]
Color m_DisabledColor = Color.clear;
internal Color disabledColor
{
get
{
#if !UNITY_EDITOR
if (m_DisabledColor == Color.clear)
m_DisabledColor = k_DisabledColorDark;
#endif
return m_DisabledColor;
}
}
static readonly Color k_ButtonColorDark = new Color(0x55 / 255f, 0x55 / 255f, 0x55 / 255f);
static readonly Color k_ButtonColorLight = new Color(0xE4 / 255f, 0xE4 / 255f, 0xE4 / 255f);
[HideInInspector]
[SerializeField]
Color m_ButtonColor = Color.clear;
internal Color buttonColor
{
get
{
#if !UNITY_EDITOR
if (m_ButtonColor == Color.clear)
m_ButtonColor = k_ButtonColorDark;
#endif
return m_ButtonColor;
}
}
static readonly Color k_DisabledButtonColorDark = new Color(0x55 / 255f, 0x55 / 255f, 0x55 / 255f, 0.5f);
static readonly Color k_DisabledButtonColorLight = new Color(0xE4 / 255f, 0xE4 / 255f, 0xE4 / 255f, 0.5f);
[HideInInspector]
[SerializeField]
Color m_DisabledButtonColor = Color.clear;
internal Color disabledButtonColor
{
get
{
#if !UNITY_EDITOR
if (m_DisabledButtonColor == Color.clear)
m_DisabledButtonColor = k_DisabledButtonColorDark;
#endif
return m_DisabledButtonColor;
}
}
static readonly Color k_SelectedColorDark = new Color(0x4F / 255f, 0x65 / 255f, 0x7F / 255f);
static readonly Color k_SelectedColorLight = new Color(0x96 / 255f, 0xC3 / 255f, 0xFB / 255f);
[HideInInspector]
[SerializeField]
Color m_SelectedColor = Color.clear;
internal Color selectedColor
{
get
{
#if !UNITY_EDITOR
if (m_SelectedColor == Color.clear)
m_SelectedColor = k_SelectedColorDark;
#endif
return m_SelectedColor;
}
}
static readonly Color k_BackgroundColorDark = Color.black;
static readonly Color k_BackgroundColorLight = new Color(0xB6 / 255f, 0xB6 / 255f, 0xB6 / 255f);
[HideInInspector]
[SerializeField]
Color m_BackgroundColor = Color.clear;
internal Color backgroundColor
{
get
{
#if !UNITY_EDITOR
if (m_BackgroundColor == Color.clear)
m_BackgroundColor = k_BackgroundColorDark;
#endif
return m_BackgroundColor;
}
}
static readonly Color k_DeviceColorDark = new Color(0x6E / 255f, 0x6E / 255f, 0x6E / 255f);
static readonly Color k_DeviceColorLight = new Color(0xE4 / 255f, 0xE4 / 255f, 0xE4 / 255f);
[HideInInspector]
[SerializeField]
Color m_DeviceColor = Color.clear;
internal Color deviceColor
{
get
{
#if !UNITY_EDITOR
if (m_DeviceColor == Color.clear)
m_DeviceColor = k_DeviceColorDark;
#endif
return m_DeviceColor;
}
}
static readonly Color k_DisabledDeviceColorDark = new Color(0x58 / 255f, 0x58 / 255f, 0x58 / 255f);
static readonly Color k_DisabledDeviceColorLight = new Color(0xA2 / 255f, 0xA2 / 255f, 0xA2 / 255f, 0.5f);
[HideInInspector]
[SerializeField]
Color m_DisabledDeviceColor = Color.clear;
internal Color disabledDeviceColor
{
get
{
#if !UNITY_EDITOR
if (m_DisabledDeviceColor == Color.clear)
m_DisabledDeviceColor = k_DisabledDeviceColorDark;
#endif
return m_DisabledDeviceColor;
}
}
// Handles 2 axis activation for 1 UI Button
bool m_XAxisActivated;
bool m_ZAxisActivated;
/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
protected void Start()
{
var simulator = GetComponentInParent<XRDeviceSimulator>();
if (simulator != null)
Initialize(simulator);
}
/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
protected void Update()
{
#if XR_HANDS_1_1_OR_NEWER
if (m_DeviceLifecycleManager.deviceMode != SimulatedDeviceLifecycleManager.DeviceMode.Hand)
return;
for (var index = 0; index < m_HandExpressionManager.simulatedHandExpressions.Count; ++index)
{
var simulatedExpression = m_HandExpressionManager.simulatedHandExpressions[index];
if (simulatedExpression.toggleInput.ReadWasPerformedThisFrame())
{
if (m_Simulator.manipulatingLeftHand)
m_LeftHand.ToggleExpression(simulatedExpression, m_Simulator);
if (m_Simulator.manipulatingRightHand)
m_RightHand.ToggleExpression(simulatedExpression, m_Simulator);
}
}
#endif
}
/// <summary>
/// See <see cref="MonoBehaviour"/>.
/// </summary>
protected void OnDestroy()
{
if (m_Simulator != null)
{
Unsubscribe(m_Simulator.manipulateLeftAction, OnManipulateLeftAction);
Unsubscribe(m_Simulator.manipulateRightAction, OnManipulateRightAction);
Unsubscribe(m_Simulator.toggleManipulateLeftAction, OnToggleManipulateLeftAction);
Unsubscribe(m_Simulator.toggleManipulateRightAction, OnToggleManipulateRightAction);
Unsubscribe(m_Simulator.toggleManipulateBodyAction, OnToggleManipulateBodyAction);
Unsubscribe(m_Simulator.manipulateHeadAction, OnManipulateHeadAction);
Unsubscribe(m_Simulator.handControllerModeAction, OnHandControllerModeAction);
Unsubscribe(m_Simulator.cycleDevicesAction, OnCycleDevicesAction);
Unsubscribe(m_Simulator.stopManipulationAction, OnStopManipulationAction);
Unsubscribe(m_Simulator.toggleMouseTransformationModeAction, OnToggleMouseTransformationModeAction);
Unsubscribe(m_Simulator.negateModeAction, OnNegateModeAction);
Unsubscribe(m_Simulator.toggleCursorLockAction, OnToggleCursorLockAction);
Unsubscribe(m_Simulator.keyboardXTranslateAction, OnKeyboardXTranslateAction);
Unsubscribe(m_Simulator.keyboardYTranslateAction, OnKeyboardYTranslateAction);
Unsubscribe(m_Simulator.keyboardZTranslateAction, OnKeyboardZTranslateAction);
Unsubscribe(m_Simulator.restingHandAxis2DAction, OnRestingHandAxis2DAction);
Unsubscribe(m_Simulator.gripAction, OnGripAction);
Unsubscribe(m_Simulator.triggerAction, OnTriggerAction);
Unsubscribe(m_Simulator.menuAction, OnMenuAction);
Unsubscribe(m_Simulator.primaryButtonAction, OnPrimaryButtonAction);
Unsubscribe(m_Simulator.secondaryButtonAction, OnSecondaryButtonAction);
}
}
void Initialize(XRDeviceSimulator simulator)
{
m_Simulator = simulator;
if (!m_Simulator.gameObject.TryGetComponent(out m_DeviceLifecycleManager))
{
Debug.LogError($"Could not find SimulatedDeviceLifecycleManager component on {m_Simulator.name}, disabling simulator UI.");
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.");
gameObject.SetActive(false);
return;
}
InitColorTheme();
Initialize();
// Start with the headset enabled
OnSetMouseMode();
OnActivateHeadsetDevice();
}
void InitColorTheme()
{
#if UNITY_EDITOR
var isEditorPro = EditorGUIUtility.isProSkin;
m_EnabledColor = isEditorPro ? k_EnabledColorDark : k_EnabledColorLight;
m_DisabledColor = isEditorPro ? k_DisabledColorDark : k_DisabledColorLight;
m_ButtonColor = isEditorPro ? k_ButtonColorDark : k_ButtonColorLight;
m_DisabledButtonColor = isEditorPro ? k_DisabledButtonColorDark : k_DisabledButtonColorLight;
m_SelectedColor = isEditorPro ? k_SelectedColorDark : k_SelectedColorLight;
m_BackgroundColor = isEditorPro ? k_BackgroundColorDark : k_BackgroundColorLight;
m_DeviceColor = isEditorPro ? k_DeviceColorDark : k_DeviceColorLight;
m_DisabledDeviceColor = isEditorPro ? k_DisabledDeviceColorDark : k_DisabledDeviceColorLight;
m_HeadsetImage.sprite = isEditorPro ? m_HmdSpriteDark : m_HmdSpriteLight;
m_RMouseSprite = isEditorPro ? m_RMouseSpriteDark : m_RMouseSpriteLight;
#endif
}
void Initialize()
{
var bckgrdAlpha = m_XRDeviceSimulatorMainPanel.GetComponent<Image>().color.a;
foreach (var image in GetComponentsInChildren<Image>(true))
image.color = image.sprite == null || image.sprite == m_RoundedRectangle ? buttonColor : enabledColor;
foreach (var text in GetComponentsInChildren<Text>(true))
text.color = enabledColor;
m_HeadsetImage.color = Color.white;
var bckgrdColor = backgroundColor;
bckgrdColor.a = bckgrdAlpha;
m_XRDeviceSimulatorMainPanel.GetComponent<Image>().color = bckgrdColor;
m_XRDeviceSimulatorCollapsedPanel.GetComponent<Image>().color = bckgrdColor;
m_CycleDevicesText.text = m_Simulator.cycleDevicesAction.action.controls[0].displayName;
// Headset
var toggleManipulateBodyActionControl = m_Simulator.toggleManipulateBodyAction.action.controls[0];
m_HeadsetSelectedValueText.text = $"{toggleManipulateBodyActionControl.displayName}";
var ctrlsBinding1 = m_Simulator.axis2DAction.action.controls;
var ctrlsBinding2 = m_Simulator.keyboardYTranslateAction.action.controls;
m_HeadsetMoveValueText.text = $"{ctrlsBinding1[0].displayName},{ctrlsBinding1[1].displayName},{ctrlsBinding1[2].displayName},{ctrlsBinding1[3].displayName} + " +
$"{ctrlsBinding2[0].displayName},{ctrlsBinding2[1].displayName}";
m_CursorLockValueText.text = m_Simulator.toggleCursorLockAction.action.controls[0].displayName;
m_CursorLockButton.color = Cursor.lockState == CursorLockMode.Locked ? selectedColor : buttonColor;
m_HeadsetLookButtonText.text = m_Simulator.mouseTransformationMode == XRDeviceSimulator.TransformationMode.Translate ? k_TranslateLookText : k_RotateLookText;
m_MouseModeValueText.text = m_Simulator.toggleMouseTransformationModeAction.action.controls[0].displayName;
var manipulateHeadActionControl = m_Simulator.manipulateHeadAction.action.controls[0];
m_HeadsetLookValueIcon.sprite = GetInputIcon(manipulateHeadActionControl);
if (manipulateHeadActionControl.name.Equals("leftButton") || manipulateHeadActionControl.name.Equals("rightButton"))
{
m_HeadsetLookValueIcon.color = Color.white;
// If the binding is using the left button, mirror the MouseR image
if (manipulateHeadActionControl.name.Equals("leftButton"))
m_HeadsetLookValueIcon.transform.localScale = new Vector3(-1f, 1f, 1f);
}
m_HeadsetLookValueText.text = manipulateHeadActionControl.device.name == k_MouseDeviceType ? k_MouseDeviceType : manipulateHeadActionControl.displayName;
m_LeftController.Initialize(m_Simulator);
m_RightController.Initialize(m_Simulator);
var toggleSlashHoldLeftText = $"{m_Simulator.toggleManipulateLeftAction.action.controls[0].displayName} / {m_Simulator.manipulateLeftAction.action.controls[0].displayName} [Hold]";
var toggleSlashHoldRightText = $"{m_Simulator.toggleManipulateRightAction.action.controls[0].displayName} / {m_Simulator.manipulateRightAction.action.controls[0].displayName} [Hold]";
m_LeftControllerButtonText.text = toggleSlashHoldLeftText;
m_RightControllerButtonText.text = toggleSlashHoldRightText;
m_LeftHand.Initialize(m_Simulator);
m_RightHand.Initialize(m_Simulator);
m_LeftHandButtonText.text = toggleSlashHoldLeftText;
m_RightHandButtonText.text = toggleSlashHoldRightText;
UpdateDeviceInputMethod();
HandsSetActive(false);
#if XR_HANDS_1_1_OR_NEWER
m_HandsSelectedValueIcon.color = enabledColor;
m_HandsSelectedValueText.color = enabledColor;
#else
m_HandsSelectedValueIcon.color = disabledColor;
m_HandsSelectedValueText.color = disabledColor;
#endif
m_HeadsetMoveButtonIcon.color = enabledColor;
// Update OnDestroy with corresponding Unsubscribe call when adding here
Subscribe(m_Simulator.manipulateLeftAction, OnManipulateLeftAction);
Subscribe(m_Simulator.manipulateRightAction, OnManipulateRightAction);
Subscribe(m_Simulator.toggleManipulateLeftAction, OnToggleManipulateLeftAction);
Subscribe(m_Simulator.toggleManipulateRightAction, OnToggleManipulateRightAction);
Subscribe(m_Simulator.toggleManipulateBodyAction, OnToggleManipulateBodyAction);
Subscribe(m_Simulator.manipulateHeadAction, OnManipulateHeadAction);
Subscribe(m_Simulator.handControllerModeAction, OnHandControllerModeAction);
Subscribe(m_Simulator.cycleDevicesAction, OnCycleDevicesAction);
Subscribe(m_Simulator.stopManipulationAction, OnStopManipulationAction);
Subscribe(m_Simulator.toggleMouseTransformationModeAction, OnToggleMouseTransformationModeAction);
Subscribe(m_Simulator.negateModeAction, OnNegateModeAction);
Subscribe(m_Simulator.toggleCursorLockAction, OnToggleCursorLockAction);
Subscribe(m_Simulator.keyboardXTranslateAction, OnKeyboardXTranslateAction);
Subscribe(m_Simulator.keyboardYTranslateAction, OnKeyboardYTranslateAction);
Subscribe(m_Simulator.keyboardZTranslateAction, OnKeyboardZTranslateAction);
Subscribe(m_Simulator.restingHandAxis2DAction, OnRestingHandAxis2DAction);
Subscribe(m_Simulator.gripAction, OnGripAction);
Subscribe(m_Simulator.triggerAction, OnTriggerAction);
Subscribe(m_Simulator.menuAction, OnMenuAction);
Subscribe(m_Simulator.primaryButtonAction, OnPrimaryButtonAction);
Subscribe(m_Simulator.secondaryButtonAction, OnSecondaryButtonAction);
m_XRDeviceSimulatorMainPanel.SetActive(isMenuOpen);
m_XRDeviceSimulatorCollapsedPanel.SetActive(!isMenuOpen);
}
void UpdateDeviceInputMethod()
{
var toggleManipulateText = $"{m_Simulator.toggleManipulateLeftAction.action.controls[0].displayName}, {m_Simulator.toggleManipulateRightAction.action.controls[0].displayName} [Toggle]";
#if XR_HANDS_1_1_OR_NEWER
var modeChangeText = m_Simulator.handControllerModeAction != null ? $"{m_Simulator.handControllerModeAction.action.controls[0].displayName}" : string.Empty;
#else
var modeChangeText = string.Empty;
#endif
m_ControllersSelectedValueText.text = m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Controller ? toggleManipulateText : modeChangeText;
m_HandsSelectedValueText.text = m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Hand ? toggleManipulateText : modeChangeText;
var modeColor = m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Controller ? enabledColor : disabledColor;
m_ControllerSelectedIcon.color = modeColor;
m_ControllerSelectedText.color = modeColor;
modeColor = m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Hand ? enabledColor : disabledColor;
m_HandsSelectedIcon.color = modeColor;
m_HandsSelectedText.color = modeColor;
}
internal Sprite GetInputIcon(InputControl control)
{
if (control == null)
return null;
var icon = keyboardSprite;
if (control.device.name == k_MouseDeviceType)
{
switch (control.name)
{
case "leftButton":
case "rightButton":
icon = rMouseSprite;
break;
default:
icon = mouseSprite;
break;
}
}
return icon;
}
/// <summary>
/// Hides the simulator UI panel when called while displaying the simulator button.
/// </summary>
public void OnClickCloseSimulatorUIPanel()
{
isMenuOpen = false;
m_XRDeviceSimulatorMainPanel.SetActive(false);
m_XRDeviceSimulatorCollapsedPanel.SetActive(true);
}
/// <summary>
/// Displays the simulator UI panel when called while hiding the simulator button.
/// </summary>
public void OnClickOpenSimulatorUIPanel()
{
isMenuOpen = true;
m_XRDeviceSimulatorMainPanel.SetActive(true);
m_XRDeviceSimulatorCollapsedPanel.SetActive(false);
}
void OnActivateLeftDevice()
{
if (m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Controller)
OnActivateLeftController();
else if (m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Hand)
OnActivateLeftHand();
}
void OnActivateRightDevice()
{
if (m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Controller)
OnActivateRightController();
else if (m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Hand)
OnActivateRightHand();
}
void OnActivateBothDevices()
{
if (m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Controller)
OnActivateBothControllers();
else if (m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Hand)
OnActivateBothHands();
}
/// <summary>
/// Sets the Left Controller device as active to receive input.
/// </summary>
void OnActivateLeftController()
{
m_CurrentSelectedDeviceText.text = "Left Controller";
OnActivateController(m_LeftController);
}
/// <summary>
/// Sets the Right Controller device as active to receive input.
/// </summary>
void OnActivateRightController()
{
m_CurrentSelectedDeviceText.text = "Right Controller";
OnActivateController(m_RightController);
}
void OnActivateController(XRDeviceSimulatorControllerUI controller)
{
ControllersSetActive(true);
PushCurrentButtonState(controller);
controller.SetAsActiveController(true, m_Simulator);
var other = controller == m_LeftController ? m_RightController : m_LeftController;
other.SetAsActiveController(false, m_Simulator, true);
HeadDeviceSetActive(false);
HandsSetActive(false);
}
/// <summary>
/// Sets both Left & Right Controller devices as active to receive input.
/// </summary>
void OnActivateBothControllers()
{
ControllersSetActive(true);
m_CurrentSelectedDeviceText.text = "Left & Right Controllers";
PushCurrentButtonState(m_LeftController);
PushCurrentButtonState(m_RightController);
m_LeftController.SetAsActiveController(true, m_Simulator);
m_RightController.SetAsActiveController(true, m_Simulator);
HeadDeviceSetActive(false);
HandsSetActive(false);
}
void PushCurrentButtonState(XRDeviceSimulatorControllerUI controller)
{
controller.OnGrip(m_Simulator.gripAction.action.inProgress);
controller.OnTrigger(m_Simulator.triggerAction.action.inProgress);
controller.OnMenu(m_Simulator.menuAction.action.inProgress);
controller.OnPrimaryButton(m_Simulator.primaryButtonAction.action.inProgress);
controller.OnSecondaryButton(m_Simulator.secondaryButtonAction.action.inProgress);
controller.OnXAxisTranslatePerformed(m_Simulator.keyboardXTranslateAction.action.inProgress);
controller.OnZAxisTranslatePerformed(m_Simulator.keyboardZTranslateAction.action.inProgress);
}
/// <summary>
/// Sets the Left Hand device as active to receive input.
/// </summary>
void OnActivateLeftHand()
{
m_CurrentSelectedDeviceText.text = "Left Hand";
OnActivateHand(m_LeftHand);
}
/// <summary>
/// Sets the Right Hand device as active to receive input.
/// </summary>
void OnActivateRightHand()
{
m_CurrentSelectedDeviceText.text = "Right Hand";
OnActivateHand(m_RightHand);
}
void OnActivateHand(XRDeviceSimulatorHandsUI hand)
{
HandsSetActive(true);
hand.SetActive(true, m_Simulator);
XRDeviceSimulatorHandsUI otherHand = hand == m_LeftHand ? m_RightHand : m_LeftHand;
otherHand.SetActive(false, m_Simulator);
HeadDeviceSetActive(false);
ControllersSetActive(false);
}
/// <summary>
/// Sets both Left & Right Hand devices as active to receive input.
/// </summary>
void OnActivateBothHands()
{
HandsSetActive(true);
m_CurrentSelectedDeviceText.text = "Left & Right Hands";
m_LeftHand.SetActive(true, m_Simulator);
m_RightHand.SetActive(true, m_Simulator);
HeadDeviceSetActive(false);
ControllersSetActive(false);
}
/// <summary>
/// Sets the headset device as active to receive input.
/// </summary>
void OnActivateHeadsetDevice(bool activated = true)
{
m_LeftController.SetAsActiveController(false, m_Simulator);
m_RightController.SetAsActiveController(false, m_Simulator);
m_LeftHand.SetActive(false, m_Simulator);
m_RightHand.SetActive(false, m_Simulator);
m_CurrentSelectedDeviceText.text = activated ? "Head Mounted Display (HMD)" : "None";
m_HeadsetImage.gameObject.SetActive(activated);
HeadDeviceSetActive(activated);
if (m_Simulator.manipulatingFPS)
{
ControllersSetActive(false, m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Controller);
HandsSetActive(false, m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Hand);
}
else
{
HandsSetActive(false, Mathf.Approximately(m_HandsCanvasGroup.alpha, 1f));
ControllersSetActive(false, Mathf.Approximately(m_ControllersCanvasGroup.alpha, 1f));
}
}
/// <summary>
/// Updates all the UI associated the the Headset.
/// </summary>
/// <param name="active">Whether the headset is the active device or not.</param>
void HeadDeviceSetActive(bool active)
{
m_HeadsetImage.gameObject.SetActive(active);
m_HeadsetSelectedButton.color = active ? selectedColor : buttonColor;
var currentColor = active ? enabledColor : disabledColor;
m_HeadsetMoveButtonIcon.color = currentColor;
m_HeadsetMoveButtonText.color = currentColor;
m_HeadsetMoveValueIcon.color = currentColor;
m_HeadsetMoveValueText.color = currentColor;
m_HeadsetMoveButton.color = active ? buttonColor : disabledButtonColor;
}
void HandsSetActive(bool isActive, bool showCanvasGroup = false)
{
m_HandsCanvasGroup.alpha = isActive || showCanvasGroup ? 1f : 0f;
#if XR_HANDS_1_1_OR_NEWER
m_HandsSelectedButton.color = isActive ? selectedColor : buttonColor;
#else
m_HandsSelectedButton.color = disabledButtonColor;
#endif
}
void ControllersSetActive(bool isActive, bool showCanvasGroup = false)
{
m_ControllersCanvasGroup.alpha = isActive || showCanvasGroup ? 1f : 0f;
m_ControllerSelectedButton.color = isActive ? selectedColor : buttonColor;
}
static void Subscribe(InputActionReference reference, Action<InputAction.CallbackContext> performedOrCanceled)
{
var action = reference != null ? reference.action : null;
if (action != null && performedOrCanceled != null)
{
action.performed += performedOrCanceled;
action.canceled += performedOrCanceled;
}
}
static void Unsubscribe(InputActionReference reference, Action<InputAction.CallbackContext> performedOrCanceled)
{
var action = reference != null ? reference.action : null;
if (action != null && performedOrCanceled != null)
{
action.performed -= performedOrCanceled;
action.canceled -= performedOrCanceled;
}
}
void OnManipulateLeftAction(InputAction.CallbackContext context)
{
if (context.phase.IsInProgress())
{
if (m_Simulator.manipulatingLeftDevice && m_Simulator.manipulatingRightDevice)
OnActivateBothDevices();
else if (m_Simulator.manipulatingLeftDevice)
OnActivateLeftDevice();
}
else
{
if (m_Simulator.manipulatingRightDevice)
OnActivateRightDevice();
else
OnActivateHeadsetDevice(m_Simulator.manipulatingFPS);
}
}
void OnManipulateRightAction(InputAction.CallbackContext context)
{
if (context.phase.IsInProgress())
{
if (m_Simulator.manipulatingLeftDevice && m_Simulator.manipulatingRightDevice)
OnActivateBothDevices();
else if (m_Simulator.manipulatingRightDevice)
OnActivateRightDevice();
}
else
{
if (m_Simulator.manipulatingLeftDevice)
OnActivateLeftDevice();
else
OnActivateHeadsetDevice(m_Simulator.manipulatingFPS);
}
}
void OnToggleManipulateLeftAction(InputAction.CallbackContext context)
{
if (context.phase.IsInProgress())
{
if (m_Simulator.manipulatingLeftDevice)
OnActivateLeftDevice();
else
OnActivateHeadsetDevice();
}
}
void OnToggleManipulateRightAction(InputAction.CallbackContext context)
{
if (context.phase.IsInProgress())
{
if (m_Simulator.manipulatingRightDevice)
OnActivateRightDevice();
else
OnActivateHeadsetDevice();
}
}
void OnToggleManipulateBodyAction(InputAction.CallbackContext context)
{
if (context.phase.IsInProgress())
{
OnActivateHeadsetDevice();
}
}
void OnManipulateHeadAction(InputAction.CallbackContext context)
{
var isInProgress = context.phase.IsInProgress();
var noDevices = !m_Simulator.manipulatingLeftDevice && !m_Simulator.manipulatingRightDevice;
if (isInProgress)
{
if (m_Simulator.manipulatingFPS || noDevices)
OnActivateHeadsetDevice();
}
else if (noDevices)
{
OnActivateHeadsetDevice(m_Simulator.manipulatingFPS);
}
m_HeadsetLookButton.color = isInProgress ? selectedColor : buttonColor;
}
void OnHandControllerModeAction(InputAction.CallbackContext context)
{
#if XR_HANDS_1_1_OR_NEWER
if (context.phase.IsInProgress())
{
if (m_Simulator.manipulatingLeftDevice && m_Simulator.manipulatingRightDevice)
OnActivateBothDevices();
else if (m_Simulator.manipulatingLeftDevice)
OnActivateLeftDevice();
else if (m_Simulator.manipulatingRightDevice)
OnActivateRightDevice();
else if (m_Simulator.manipulatingFPS)
OnActivateHeadsetDevice();
else
{
ControllersSetActive(false, m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Controller);
HandsSetActive(false, m_DeviceLifecycleManager.deviceMode == SimulatedDeviceLifecycleManager.DeviceMode.Hand);
}
}
UpdateDeviceInputMethod();
#endif
}
void OnCycleDevicesAction(InputAction.CallbackContext context)
{
if (context.phase.IsInProgress())
{
if (m_Simulator.manipulatingFPS)
OnActivateHeadsetDevice();
if (m_Simulator.manipulatingLeftDevice)
OnActivateLeftDevice();
if (m_Simulator.manipulatingRightDevice)
OnActivateRightDevice();
}
}
void OnStopManipulationAction(InputAction.CallbackContext context)
{
if (context.phase.IsInProgress())
OnActivateHeadsetDevice(m_Simulator.manipulatingFPS);
}
void OnToggleMouseTransformationModeAction(InputAction.CallbackContext context)
{
if (context.phase.IsInProgress())
OnSetMouseMode();
}
void OnNegateModeAction(InputAction.CallbackContext context)
{
OnSetMouseMode();
}
void OnToggleCursorLockAction(InputAction.CallbackContext context)
{
if (context.phase.IsInProgress())
OnCursorLockChanged();
}
void OnKeyboardXTranslateAction(InputAction.CallbackContext context)
{
OnXAxisTranslatePerformed(context.phase.IsInProgress(), false);
}
void OnKeyboardYTranslateAction(InputAction.CallbackContext context)
{
OnYAxisTranslatePerformed(context.phase.IsInProgress());
}
void OnKeyboardZTranslateAction(InputAction.CallbackContext context)
{
OnZAxisTranslatePerformed(context.phase.IsInProgress(), false);
}
void OnRestingHandAxis2DAction(InputAction.CallbackContext context)
{
var restingHandAxis2DInput = Vector2.ClampMagnitude(context.ReadValue<Vector2>(), 1f);
if (context.phase.IsInProgress())
{
if (restingHandAxis2DInput.x != 0f)
OnXAxisTranslatePerformed(true, true);
if (restingHandAxis2DInput.y != 0f)
OnZAxisTranslatePerformed(true, true);
}
else
{
if (restingHandAxis2DInput.x == 0f)
OnXAxisTranslatePerformed(false, true);
if (restingHandAxis2DInput.y == 0f)
OnZAxisTranslatePerformed(false, true);
}
}
void OnGripAction(InputAction.CallbackContext context)
{
OnGripPerformed(context.phase.IsInProgress());
}
void OnTriggerAction(InputAction.CallbackContext context)
{
OnTriggerPerformed(context.phase.IsInProgress());
}
void OnMenuAction(InputAction.CallbackContext context)
{
OnMenuPerformed(context.phase.IsInProgress());
}
void OnPrimaryButtonAction(InputAction.CallbackContext context)
{
OnPrimaryButtonPerformed(context.phase.IsInProgress());
}
void OnSecondaryButtonAction(InputAction.CallbackContext context)
{
OnSecondaryButtonPerformed(context.phase.IsInProgress());
}
void OnSetMouseMode()
{
// Translate/Rotate
m_MouseModeButtonText.text = m_Simulator.negateMode
? XRDeviceSimulator.Negate(m_Simulator.mouseTransformationMode).ToString()
: m_Simulator.mouseTransformationMode.ToString();
// Move/Look
m_HeadsetLookButtonText.text =
m_Simulator.mouseTransformationMode == XRDeviceSimulator.TransformationMode.Translate
? k_TranslateLookText
: k_RotateLookText;
}
void OnCursorLockChanged()
{
m_CursorLockButton.color = Cursor.lockState == CursorLockMode.Locked ? selectedColor : buttonColor;
}
// x-axis [A-D]
void OnXAxisTranslatePerformed(bool activated, bool restingHand)
{
var active = activated;
if (!restingHand)
{
m_XAxisActivated = activated;
active |= m_ZAxisActivated;
}
if (m_Simulator.manipulatingLeftController)
{
var lController = restingHand ? m_RightController : m_LeftController;
lController.OnXAxisTranslatePerformed(active);
}
if (m_Simulator.manipulatingRightController)
{
var rController = restingHand ? m_LeftController : m_RightController;
rController.OnXAxisTranslatePerformed(active);
}
if (m_Simulator.manipulatingFPS)
m_HeadsetMoveButton.color = active ? selectedColor : buttonColor;
}
// y-axis [Q-E]
void OnYAxisTranslatePerformed(bool activated)
{
if (m_Simulator.manipulatingFPS)
m_HeadsetMoveButton.color = activated ? selectedColor : buttonColor;
}
// z-axis [W-S]
void OnZAxisTranslatePerformed(bool activated, bool restingHand)
{
var active = activated;
if (!restingHand)
{
m_ZAxisActivated = activated;
active |= m_XAxisActivated;
}
if (m_Simulator.manipulatingLeftController)
{
var lController = restingHand ? m_RightController : m_LeftController;
lController.OnZAxisTranslatePerformed(active);
}
if (m_Simulator.manipulatingRightController)
{
var rController = restingHand ? m_LeftController : m_RightController;
rController.OnZAxisTranslatePerformed(active);
}
if (m_Simulator.manipulatingFPS)
m_HeadsetMoveButton.color = active ? selectedColor : buttonColor;
}
void OnMenuPerformed(bool activated)
{
if (m_Simulator.manipulatingLeftController)
m_LeftController.OnMenu(activated);
if (m_Simulator.manipulatingRightController)
m_RightController.OnMenu(activated);
}
void OnGripPerformed(bool activated)
{
if (m_Simulator.manipulatingLeftController)
m_LeftController.OnGrip(activated);
if (m_Simulator.manipulatingRightController)
m_RightController.OnGrip(activated);
}
void OnTriggerPerformed(bool activated)
{
if (m_Simulator.manipulatingLeftController)
m_LeftController.OnTrigger(activated);
if (m_Simulator.manipulatingRightController)
m_RightController.OnTrigger(activated);
}
void OnPrimaryButtonPerformed(bool activated)
{
if (m_Simulator.manipulatingLeftController)
m_LeftController.OnPrimaryButton(activated);
if (m_Simulator.manipulatingRightController)
m_RightController.OnPrimaryButton(activated);
}
void OnSecondaryButtonPerformed(bool activated)
{
if (m_Simulator.manipulatingLeftController)
m_LeftController.OnSecondaryButton(activated);
if (m_Simulator.manipulatingRightController)
m_RightController.OnSecondaryButton(activated);
}
}
}
|