Spelunx Cavern SDK
 
Loading...
Searching...
No Matches
CavernDebugKeys.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using UnityEngine;
6using UnityEngine.Events;
7using UnityEngine.InputSystem;
8
9namespace Spelunx
10{
11 [DisallowMultipleComponent]
12 public class CavernDebugKeys : MonoBehaviour, IDebugKeys
13 {
14 [Header("Input Actions")]
15 [SerializeField, Tooltip("Quits the game or play mode")]
16 private InputAction quit = new("Quit", InputActionType.Value, "<Keyboard>/Escape");
17 [SerializeField, Tooltip("Opens the help debug window")]
18 private InputAction help = new("Help", InputActionType.Value, "<Keyboard>/h");
19 [SerializeField, Tooltip("Swaps the eyes on the stereoscopic glasses")]
20 private InputAction swapEyes = new("Swap Eyes", InputActionType.Value, "<Keyboard>/e");
21 [SerializeField, Tooltip("Toggles rendering between stereo and mono")]
22 private InputAction stereoMonoToggle = new("Stereo/Mono Toggle", InputActionType.Value, "<Keyboard>/t");
23 [SerializeField, Tooltip("Toggles muting all sounds")]
24 private InputAction muteToggle = new("Mute Toggle", InputActionType.Value, "<Keyboard>/m");
25 [SerializeField, Tooltip("Hides the mouse when it doesn't move for a few seconds")]
26 private InputAction mouseMove = new("Mouse Move", InputActionType.Value, "<Mouse>/delta");
27 [SerializeField, Tooltip("Increase the interpupillary distance")]
28 private InputAction increaseIPD = new("Increase IPD", InputActionType.Value, "<Keyboard>/rightArrow");
29 [SerializeField, Tooltip("Decreases the interpupillary distance")]
30 private InputAction decreaseIPD = new("Decrease IPD", InputActionType.Value, "<Keyboard>/leftArrow");
31 [SerializeField, Tooltip("Increase the camera height")]
32 private InputAction increaseCameraHeight = new("Increase Camera Height", InputActionType.Value, "<Keyboard>/upArrow");
33 [SerializeField, Tooltip("Decreases the camera height")]
34 private InputAction decreaseCameraHeight = new("Decrease Camera Height", InputActionType.Value, "<Keyboard>/downArrow");
35
36 [Header("Settings")]
37 [SerializeField, Range(0, 0.01f), Tooltip("Amount to adjust interpupillary distance for stereo rendering")]
38 private float IPD_CHANGE = 0.001f;
39 [SerializeField, Range(0, 0.5f), Tooltip("Amount to adjust camera height")]
40 private float CAMERA_HEIGHT_CHANGE = 0.0254f; // one inch
41 [SerializeField, Tooltip("The Head object that gets repositioned")]
42 private Transform headTrackingCamera;
43
44 [SerializeField, Tooltip("Skin for the GUI")]
45 private GUISkin guiSkin;
46
47 // used to render the help debug window
48 private List<string> helpKeys = new();
49 private List<string> helpDescriptions = new();
50 private UnityAction extraGUICalls;
51
52 private enum HelpDisplay
53 {
54 Off = 0,
55 PC = 1,
56 CAVERN = 2
57 }
58 private HelpDisplay showHelp = HelpDisplay.Off;
59
60 // running average of framerates
61 private readonly int[] framerates = new int[100];
62 private int framerateIndex = 0;
63
64 public List<(string Key, string Description)> KeyDescriptions()
65 {
66 return new(){
67 (quit.GetBindingDisplayString(), "Quit the game or exit play mode"),
68 (help.GetBindingDisplayString(), "Open this help window"),
69 (swapEyes.GetBindingDisplayString(), "Swap the eyes on the stereoscopic glasses"),
70 (stereoMonoToggle.GetBindingDisplayString(), "Toggle rendering between stereo and mono"),
71 (muteToggle.GetBindingDisplayString(), "Mute all sounds"),
72 (increaseIPD.GetBindingDisplayString(), "Increase IPD"),
73 (decreaseIPD.GetBindingDisplayString(), "Decrease IPD"),
74 (increaseCameraHeight.GetBindingDisplayString(), "Increase Camera Height"),
75 (decreaseCameraHeight.GetBindingDisplayString(), "Decrease Camera Height")
76 };
77 }
78
79 public void DoExtraGUI()
80 {
81 framerates[framerateIndex] = (int)(1 / Time.unscaledDeltaTime);
82 framerateIndex = (framerateIndex + 1) % framerates.Length;
83 int currentFramerate = (int)framerates.Average();
84
85 CavernRenderer cavern = GetComponentInChildren<CavernRenderer>();
86 GUILayout.Label($"Framerate: {currentFramerate} fps");
87 float ipd = cavern.IPD * 1000;
88 GUILayout.Label($"Head height: {headTrackingCamera.localPosition.y} meters\t\tIPD: {ipd:F1} mm");
89 }
90
91 // enable the input actions on play mode start
92 void OnEnable()
93 {
94 quit.Enable();
95 help.Enable();
96 swapEyes.Enable();
97 stereoMonoToggle.Enable();
98 muteToggle.Enable();
99 mouseMove.Enable();
100 increaseIPD.Enable();
101 decreaseIPD.Enable();
102 increaseCameraHeight.Enable();
103 decreaseCameraHeight.Enable();
104 }
105
106
107 // disable the input actions on play mode stop
108 void OnDisable()
109 {
110 quit.Disable();
111 help.Disable();
112 swapEyes.Disable();
113 stereoMonoToggle.Disable();
114 muteToggle.Disable();
115 mouseMove.Disable();
116 increaseIPD.Disable();
117 decreaseIPD.Disable();
118 increaseCameraHeight.Disable();
119 decreaseCameraHeight.Disable();
120 }
121
122 // bind the proper callbacks to each action.performed
123 // using the saved key managers
124 // This must happen in play mode, not in edit mode, or it won't work.
125 void Awake()
126 {
127 quit.performed += QuitAction;
128 help.performed += HelpAction;
129 swapEyes.performed += SwapEyesAction;
130 stereoMonoToggle.performed += MonoStereoAction;
131 muteToggle.performed += MuteToggleAction;
132 mouseMove.performed += OnMouseMove;
133 increaseIPD.performed += IncreaseIPDAction;
134 decreaseIPD.performed += DecreaseIPDAction;
135 increaseCameraHeight.performed += IncreaseCameraHeightAction;
136 decreaseCameraHeight.performed += DecreaseCameraHeightAction;
137 }
138
139 void Start()
140 {
141 // Start the coroutine for hiding the mouse if it doesn't move
142 hideMouseCoroutine = HideMouse();
143 StartCoroutine(hideMouseCoroutine);
144 // find all help descriptions and add them to list
145 foreach (IDebugKeys manager in GetComponents<IDebugKeys>())
146 {
147 foreach ((string Key, string Description) d in manager.KeyDescriptions())
148 {
149 helpKeys.Add(d.Key);
150 helpDescriptions.Add(d.Description);
151 }
152 extraGUICalls += manager.DoExtraGUI;
153 }
154 }
155
156 public void QuitAction(InputAction.CallbackContext ctx)
157 {
158#if UNITY_EDITOR
159 // UnityEditor.EditorApplication.isPlaying = false;
160 UnityEditor.EditorApplication.ExitPlaymode();
161#else
162 Application.Quit();
163#endif
164 }
165
166 void SwapEyesAction(InputAction.CallbackContext ctx)
167 {
168 CavernRenderer cavern = GetComponentInChildren<CavernRenderer>();
169 cavern.SwapEyes = !cavern.SwapEyes;
170 }
171
172 void MonoStereoAction(InputAction.CallbackContext ctx)
173 {
174 CavernRenderer cavern = GetComponentInChildren<CavernRenderer>();
175 switch (cavern.GetStereoscopicMode())
176 {
177 case CavernRenderer.StereoscopicMode.Mono:
178 cavern.SetStereoscopicMode(CavernRenderer.StereoscopicMode.Stereo);
179 break;
180 case CavernRenderer.StereoscopicMode.Stereo:
181 cavern.SetStereoscopicMode(CavernRenderer.StereoscopicMode.Mono);
182 break;
183 }
184
185 }
186
187 // void HeadtrackingToggleAction(InputAction.CallbackContext ctx){
188
189 // }
190
191 void MuteToggleAction(InputAction.CallbackContext ctx)
192 {
193 AudioListener l = GetComponentInChildren<AudioListener>();
194 l.enabled = !l.enabled;
195 }
196
197 void HelpAction(InputAction.CallbackContext ctx)
198 {
199 // cycle through the different help display types
200 switch (showHelp)
201 {
202 case HelpDisplay.Off:
203 showHelp = HelpDisplay.PC;
204 break;
205 case HelpDisplay.PC:
206 showHelp = HelpDisplay.Off;
207 break;
208 case HelpDisplay.CAVERN:
209 showHelp = HelpDisplay.Off;
210 break;
211 }
212 // showHelp = (HelpDisplay)(((int)showHelp + 1) % typeof(HelpDisplay).GetEnumValues().Length);
213 }
214
215 void IncreaseIPDAction(InputAction.CallbackContext ctx)
216 {
217 CavernRenderer cavern = GetComponentInChildren<CavernRenderer>();
218 cavern.IPD += IPD_CHANGE;
219 }
220
221 void DecreaseIPDAction(InputAction.CallbackContext ctx)
222 {
223 CavernRenderer cavern = GetComponentInChildren<CavernRenderer>();
224 cavern.IPD -= IPD_CHANGE;
225 }
226
227 void IncreaseCameraHeightAction(InputAction.CallbackContext ctx)
228 {
229 headTrackingCamera.localPosition = new(headTrackingCamera.localPosition.x, headTrackingCamera.localPosition.y + CAMERA_HEIGHT_CHANGE, headTrackingCamera.localPosition.z);
230 }
231
232 void DecreaseCameraHeightAction(InputAction.CallbackContext ctx)
233 {
234 headTrackingCamera.localPosition = new(headTrackingCamera.localPosition.x, headTrackingCamera.localPosition.y - CAMERA_HEIGHT_CHANGE, headTrackingCamera.localPosition.z);
235 }
236
237
238 #region Cursor Hiding
239
240 // We hide the cursor when it's not moving.
241 // We use coroutines instead of an update loop because most of the time the mouse isn't going to be moving
242 // And this saves on compute cost in that case (although it's slightly worse if the mouse is moving often)
243 IEnumerator hideMouseCoroutine = null;
244 IEnumerator HideMouse()
245 {
246 yield return new WaitForSeconds(3); // after three seconds, hide the mouse
247 Cursor.visible = false;
248 }
249
250 void OnMouseMove(InputAction.CallbackContext context)
251 {
252 StopCoroutine(hideMouseCoroutine);
253
254 Cursor.visible = true;
255 hideMouseCoroutine = HideMouse();
256 StartCoroutine(hideMouseCoroutine);
257 }
258 #endregion
259
260 #region Debug GUI
261 void OnGUI()
262 {
263 switch (showHelp)
264 {
265 case HelpDisplay.Off:
266 return;
267 case HelpDisplay.PC:
268 PCGui();
269 break;
270 case HelpDisplay.CAVERN:
271
272 break;
273 }
274
275 }
276
277 void PCGui()
278 {
279 GUI.skin = guiSkin;
280 GUILayout.BeginArea(new Rect(40, 40, 1000, 1000), GUI.skin.box);
281 // GUILayout.Box("Debug Info");
282 GUILayout.BeginVertical();
283 GUILayout.BeginHorizontal();
284 GUILayout.BeginVertical();
285 foreach (string key in helpKeys)
286 {
287 GUILayout.Label(key);
288 }
289 GUILayout.EndVertical();
290
291 GUILayout.BeginVertical();
292 foreach (string description in helpDescriptions)
293 {
294 GUILayout.Label(description);
295 }
296 GUILayout.EndVertical();
297 GUILayout.EndHorizontal();
298 extraGUICalls.Invoke();
299 GUILayout.EndVertical();
300 GUILayout.EndArea();
301 }
302
303 void CAVERNGui()
304 {
305
306 }
307
308 #endregion
309 }
310}
List<(string Key, string Description)> KeyDescriptions()
void QuitAction(InputAction.CallbackContext ctx)