SteamVR_Input_LiveWindow.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.CodeDom;
  4. using Microsoft.CSharp;
  5. using System.IO;
  6. using System.CodeDom.Compiler;
  7. using System.Linq;
  8. using System.Collections.Generic;
  9. using System.Reflection;
  10. using System.Linq.Expressions;
  11. using System;
  12. using UnityEditorInternal;
  13. namespace Valve.VR
  14. {
  15. public class SteamVR_Input_LiveWindow : EditorWindow
  16. {
  17. private GUIStyle labelStyle;
  18. private GUIStyle setLabelStyle;
  19. [MenuItem("Window/SteamVR Input Live View")]
  20. public static void ShowWindow()
  21. {
  22. GetWindow<SteamVR_Input_LiveWindow>(false, "SteamVR Input Live View", true);
  23. }
  24. private void OnInspectorUpdate()
  25. {
  26. Repaint();
  27. }
  28. private Vector2 scrollPosition;
  29. private Dictionary<SteamVR_Input_Sources, bool> sourceFoldouts = null;
  30. private Dictionary<SteamVR_Input_Sources, Dictionary<string, bool>> setFoldouts = null;
  31. Color inactiveSetColor = Color.Lerp(Color.red, Color.white, 0.5f);
  32. Color actionUnboundColor = Color.red;
  33. Color actionChangedColor = Color.green;
  34. Color actionNotUpdatingColor = Color.yellow;
  35. private void DrawMap()
  36. {
  37. EditorGUILayout.BeginHorizontal();
  38. //GUILayout.FlexibleSpace();
  39. GUI.backgroundColor = actionUnboundColor;
  40. EditorGUILayout.LabelField("Not Bound", labelStyle);
  41. GUILayout.FlexibleSpace();
  42. GUI.backgroundColor = inactiveSetColor;
  43. EditorGUILayout.LabelField("Inactive", labelStyle);
  44. GUILayout.FlexibleSpace();
  45. GUI.backgroundColor = actionNotUpdatingColor;
  46. EditorGUILayout.LabelField("Not Used Yet", labelStyle);
  47. GUILayout.FlexibleSpace();
  48. GUI.backgroundColor = actionChangedColor;
  49. EditorGUILayout.LabelField("Changed", labelStyle);
  50. //GUILayout.FlexibleSpace();
  51. EditorGUILayout.EndHorizontal();
  52. EditorGUILayout.Space();
  53. }
  54. private void OnGUI()
  55. {
  56. if (SteamVR_Input.actionSets == null)
  57. {
  58. EditorGUILayout.LabelField("Must first generate actions. Open SteamVR Input window.");
  59. return;
  60. }
  61. bool startUpdatingSourceOnAccess = SteamVR_Action.startUpdatingSourceOnAccess;
  62. SteamVR_Action.startUpdatingSourceOnAccess = false;
  63. if (labelStyle == null)
  64. {
  65. labelStyle = new GUIStyle(EditorStyles.textField);
  66. labelStyle.normal.background = Texture2D.whiteTexture;
  67. setLabelStyle = new GUIStyle(EditorStyles.label);
  68. setLabelStyle.wordWrap = true;
  69. setLabelStyle.normal.background = Texture2D.whiteTexture;
  70. }
  71. scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
  72. Color defaultColor = GUI.backgroundColor;
  73. SteamVR_ActionSet[] actionSets = SteamVR_Input.actionSets;
  74. SteamVR_Input_Sources[] sources = SteamVR_Input_Source.GetAllSources();
  75. if (sourceFoldouts == null)
  76. {
  77. sourceFoldouts = new Dictionary<SteamVR_Input_Sources, bool>();
  78. setFoldouts = new Dictionary<SteamVR_Input_Sources, Dictionary<string, bool>>();
  79. for (int sourceIndex = 0; sourceIndex < sources.Length; sourceIndex++)
  80. {
  81. sourceFoldouts.Add(sources[sourceIndex], false);
  82. setFoldouts.Add(sources[sourceIndex], new Dictionary<string, bool>());
  83. for (int actionSetIndex = 0; actionSetIndex < actionSets.Length; actionSetIndex++)
  84. {
  85. SteamVR_ActionSet set = actionSets[actionSetIndex];
  86. setFoldouts[sources[sourceIndex]].Add(set.GetShortName(), true);
  87. }
  88. }
  89. sourceFoldouts[SteamVR_Input_Sources.Any] = true;
  90. sourceFoldouts[SteamVR_Input_Sources.LeftHand] = true;
  91. sourceFoldouts[SteamVR_Input_Sources.RightHand] = true;
  92. }
  93. DrawMap();
  94. for (int sourceIndex = 0; sourceIndex < sources.Length; sourceIndex++)
  95. {
  96. SteamVR_Input_Sources source = sources[sourceIndex];
  97. sourceFoldouts[source] = EditorGUILayout.Foldout(sourceFoldouts[source], source.ToString());
  98. if (sourceFoldouts[source] == false)
  99. continue;
  100. EditorGUI.indentLevel++;
  101. for (int actionSetIndex = 0; actionSetIndex < actionSets.Length; actionSetIndex++)
  102. {
  103. SteamVR_ActionSet set = actionSets[actionSetIndex];
  104. bool setActive = set.IsActive(source);
  105. string activeText = setActive ? "Active" : "Inactive";
  106. float setLastChanged = set.GetTimeLastChanged();
  107. if (setLastChanged != -1)
  108. {
  109. float timeSinceLastChanged = Time.realtimeSinceStartup - setLastChanged;
  110. if (timeSinceLastChanged < 1)
  111. {
  112. Color blendColor = setActive ? Color.green : inactiveSetColor;
  113. Color setColor = Color.Lerp(blendColor, defaultColor, timeSinceLastChanged);
  114. GUI.backgroundColor = setColor;
  115. }
  116. }
  117. EditorGUILayout.BeginHorizontal();
  118. setFoldouts[source][set.GetShortName()] = EditorGUILayout.Foldout(setFoldouts[source][set.GetShortName()], set.GetShortName());
  119. EditorGUILayout.LabelField(activeText, labelStyle);
  120. GUI.backgroundColor = defaultColor;
  121. EditorGUILayout.EndHorizontal();
  122. if (setFoldouts[source][set.GetShortName()] == false)
  123. continue;
  124. EditorGUI.indentLevel++;
  125. for (int actionIndex = 0; actionIndex < set.allActions.Length; actionIndex++)
  126. {
  127. SteamVR_Action action = set.allActions[actionIndex];
  128. if (source != SteamVR_Input_Sources.Any && action is SteamVR_Action_Skeleton)
  129. continue;
  130. bool isUpdating = action.IsUpdating(source);
  131. bool inAction = action is ISteamVR_Action_In;
  132. bool noData = false;
  133. if (inAction && isUpdating == false)
  134. {
  135. GUI.backgroundColor = Color.yellow;
  136. noData = true;
  137. }
  138. else
  139. {
  140. bool actionBound = action.GetActiveBinding(source);
  141. if (setActive == false)
  142. {
  143. GUI.backgroundColor = inactiveSetColor;
  144. }
  145. else if (actionBound == false)
  146. {
  147. GUI.backgroundColor = Color.red;
  148. noData = true;
  149. }
  150. }
  151. if (noData)
  152. {
  153. EditorGUILayout.LabelField(action.GetShortName(), "-", labelStyle);
  154. GUI.backgroundColor = defaultColor;
  155. continue;
  156. }
  157. float actionLastChanged = action.GetTimeLastChanged(source);
  158. string actionText = "";
  159. float timeSinceLastChanged = -1;
  160. if (actionLastChanged != -1)
  161. {
  162. timeSinceLastChanged = Time.realtimeSinceStartup - actionLastChanged;
  163. if (timeSinceLastChanged < 1)
  164. {
  165. Color setColor = Color.Lerp(Color.green, defaultColor, timeSinceLastChanged);
  166. GUI.backgroundColor = setColor;
  167. }
  168. }
  169. if (action is SteamVR_Action_Boolean)
  170. {
  171. SteamVR_Action_Boolean actionBoolean = (SteamVR_Action_Boolean)action;
  172. actionText = actionBoolean.GetState(source).ToString();
  173. }
  174. else if (action is SteamVR_Action_Single)
  175. {
  176. SteamVR_Action_Single actionSingle = (SteamVR_Action_Single)action;
  177. actionText = actionSingle.GetAxis(source).ToString("0.0000");
  178. }
  179. else if (action is SteamVR_Action_Vector2)
  180. {
  181. SteamVR_Action_Vector2 actionVector2 = (SteamVR_Action_Vector2)action;
  182. actionText = string.Format("({0:0.0000}, {1:0.0000})", actionVector2.GetAxis(source).x, actionVector2.GetAxis(source).y);
  183. }
  184. else if (action is SteamVR_Action_Vector3)
  185. {
  186. SteamVR_Action_Vector3 actionVector3 = (SteamVR_Action_Vector3)action;
  187. Vector3 axis = actionVector3.GetAxis(source);
  188. actionText = string.Format("({0:0.0000}, {1:0.0000}, {2:0.0000})", axis.x, axis.y, axis.z);
  189. }
  190. else if (action is SteamVR_Action_Pose)
  191. {
  192. SteamVR_Action_Pose actionPose = (SteamVR_Action_Pose)action;
  193. Vector3 position = actionPose.GetLocalPosition(source);
  194. Quaternion rotation = actionPose.GetLocalRotation(source);
  195. actionText = string.Format("({0:0.0000}, {1:0.0000}, {2:0.0000}) : ({3:0.0000}, {4:0.0000}, {5:0.0000}, {6:0.0000})",
  196. position.x, position.y, position.z,
  197. rotation.x, rotation.y, rotation.z, rotation.w);
  198. }
  199. else if (action is SteamVR_Action_Skeleton)
  200. {
  201. SteamVR_Action_Skeleton actionSkeleton = (SteamVR_Action_Skeleton)action;
  202. Vector3 position = actionSkeleton.GetLocalPosition(source);
  203. Quaternion rotation = actionSkeleton.GetLocalRotation(source);
  204. actionText = string.Format("({0:0.0000}, {1:0.0000}, {2:0.0000}) : ({3:0.0000}, {4:0.0000}, {5:0.0000}, {6:0.0000})",
  205. position.x, position.y, position.z,
  206. rotation.x, rotation.y, rotation.z, rotation.w);
  207. }
  208. else if (action is SteamVR_Action_Vibration)
  209. {
  210. //SteamVR_Input_Action_Vibration actionVibration = (SteamVR_Input_Action_Vibration)action;
  211. if (timeSinceLastChanged == -1)
  212. actionText = "never used";
  213. actionText = string.Format("{0:0} seconds since last used", timeSinceLastChanged);
  214. }
  215. EditorGUILayout.LabelField(action.GetShortName(), actionText, labelStyle);
  216. GUI.backgroundColor = defaultColor;
  217. }
  218. EditorGUI.indentLevel--;
  219. EditorGUILayout.Space();
  220. }
  221. EditorGUI.indentLevel--;
  222. }
  223. EditorGUILayout.Space();
  224. EditorGUILayout.LabelField("Active Action Set List");
  225. EditorGUI.indentLevel++;
  226. EditorGUILayout.LabelField(SteamVR_ActionSet_Manager.debugActiveSetListText, setLabelStyle);
  227. EditorGUI.indentLevel--;
  228. EditorGUILayout.Space();
  229. EditorGUILayout.EndScrollView();
  230. SteamVR_Action.startUpdatingSourceOnAccess = startUpdatingSourceOnAccess;
  231. }
  232. }
  233. }