Spelunx Cavern SDK
 
Loading...
Searching...
No Matches
ViveDebugKeys.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.Linq;
3using UnityEngine;
4using UnityEngine.InputSystem;
5
6namespace Spelunx.Vive
7{
8 /// <summary>
9 /// Helpful debug information (both keyboard shortcuts and GUI) for Vive Trackers.
10 /// This also adds the ability to calibrate the rotation of all active Vive Trackers.
11 /// </summary>
12 [DisallowMultipleComponent]
13 public class ViveDebugKeys : MonoBehaviour, IDebugKeys
14 {
15 [Header("Input Actions")]
16 [SerializeField, Tooltip("Calibrate the rotations of all vive trackers. Hold them upright in the center of the CAVERN and pointed towards the center of the screen.")]
17 private InputAction calibrate = new("Calibrate", InputActionType.Value, "<Keyboard>/c");
18
19 // display the number of vive trackers in the debug GUI
20 private int numViveTrackers = 0;
21 private int numViveControllers = 0;
22 private readonly List<string> trackerRoles = new();
23 private readonly List<string> controllerRoles = new();
24 public List<(string Key, string Description)> KeyDescriptions()
25 {
26 return new(){
27 (calibrate.GetBindingDisplayString(), "Calibrate the rotation of all vive trackers."),
28 };
29 }
30
31 // Render information about the currently bound Vive Trackers in the Debug UI
32 public void DoExtraGUI()
33 {
34 if (numViveTrackers > 0)
35 {
36 GUILayout.Label($"Vive Trackers: {numViveTrackers}");
37 GUILayout.Label($"Tracker roles: {string.Join(", ", trackerRoles)}");
38 }
39 if (numViveControllers > 0)
40 {
41 GUILayout.Label($"Vive Controllers: {numViveControllers}");
42 GUILayout.Label($"Controller roles: {string.Join(", ", controllerRoles)}");
43 }
44 }
45
46 // enable the input actions on play mode start
47 void OnEnable()
48 {
49 calibrate.Enable();
50 }
51
52
53 // disable the input actions on play mode stop
54 void OnDisable()
55 {
56 calibrate.Disable();
57 }
58
59 // bind the proper callbacks to each action.performed
60 // using the saved key managers
61 // This must happen in play mode, not in edit mode, or it won't work.
62 void Awake()
63 {
64 calibrate.performed += CalibrateAction;
65
66 // add the vive tracker info to the GUI
67 foreach (ViveTracker tracker in FindObjectsByType<ViveTracker>(FindObjectsSortMode.None))
68 {
69 numViveTrackers++;
70 ViveTracker.SteamVRPoseBindings binding = tracker.binding;
71 trackerRoles.Add(ViveTracker.GetReadableName(binding));
72 }
73 foreach (ViveController controller in FindObjectsByType<ViveController>(FindObjectsSortMode.None))
74 {
75 numViveControllers++;
76 ViveController.Role role = controller.role;
77 controllerRoles.Add(ViveController.GetReadableRoleName(role));
78 }
79 }
80
81 void CalibrateAction(InputAction.CallbackContext ctx)
82 {
83 foreach (GameObject tracker in GameObject.FindGameObjectsWithTag("ViveTracker"))
84 {
85 tracker.GetComponent<ViveTracker>().Calibrate();
86 }
87 }
88
89 }
90}
Helpful debug information (both keyboard shortcuts and GUI) for Vive Trackers. This also adds the abi...
List<(string Key, string Description)> KeyDescriptions()