MotionTrackerWindow.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.Linq;
  2. using UnityEngine;
  3. using UnityEditor;
  4. namespace LitMotion.Editor
  5. {
  6. /// <summary>
  7. /// Editor window that displays a list of motions being tracked.
  8. /// </summary>
  9. public class MotionTrackerWindow : EditorWindow
  10. {
  11. static MotionTrackerWindow instance;
  12. [MenuItem("Window/LitMotion/Motion Tracker")]
  13. public static void OpenWindow()
  14. {
  15. if (instance != null) instance.Close();
  16. GetWindow<MotionTrackerWindow>("Motion Tracker").Show();
  17. }
  18. static readonly GUILayoutOption[] EmptyLayoutOption = new GUILayoutOption[0];
  19. MotionTrackerTreeView treeView;
  20. object splitterState;
  21. const string EnableTrackingPrefsKey = "LitMotion-MotionTracker-EnableTracking";
  22. const string EnableStackTracePrefsKey = "LitMotion-MotionTracker-EnableStackTrace";
  23. void OnEnable()
  24. {
  25. instance = this;
  26. splitterState = SplitterGUILayout.CreateSplitterState(new float[] { 75f, 25f }, new int[] { 32, 32 }, null);
  27. treeView = new MotionTrackerTreeView();
  28. MotionTracker.EnableTracking = EditorPrefs.GetBool(EnableTrackingPrefsKey, false);
  29. MotionTracker.EnableStackTrace = EditorPrefs.GetBool(EnableStackTracePrefsKey, false);
  30. }
  31. void OnGUI()
  32. {
  33. RenderHeadPanel();
  34. SplitterGUILayout.BeginVerticalSplit(this.splitterState, EmptyLayoutOption);
  35. RenderTable();
  36. RenderDetailsPanel();
  37. SplitterGUILayout.EndVerticalSplit();
  38. }
  39. static readonly GUIContent ClearHeadContent = EditorGUIUtility.TrTextContent(" Clear ");
  40. static readonly GUIContent EnableTrackingHeadContent = EditorGUIUtility.TrTextContent("Enable Tracking");
  41. static readonly GUIContent EnableStackTraceHeadContent = EditorGUIUtility.TrTextContent("Enable Stack Trace");
  42. void RenderHeadPanel()
  43. {
  44. EditorGUILayout.BeginVertical(EmptyLayoutOption);
  45. EditorGUILayout.BeginHorizontal(EditorStyles.toolbar, EmptyLayoutOption);
  46. if (GUILayout.Toggle(MotionTracker.EnableTracking, EnableTrackingHeadContent, EditorStyles.toolbarButton, EmptyLayoutOption) != MotionTracker.EnableTracking)
  47. {
  48. MotionTracker.EnableTracking = !MotionTracker.EnableTracking;
  49. EditorPrefs.SetBool(EnableTrackingPrefsKey, MotionTracker.EnableTracking);
  50. }
  51. if (GUILayout.Toggle(MotionTracker.EnableStackTrace, EnableStackTraceHeadContent, EditorStyles.toolbarButton, EmptyLayoutOption) != MotionTracker.EnableStackTrace)
  52. {
  53. MotionTracker.EnableStackTrace = !MotionTracker.EnableStackTrace;
  54. EditorPrefs.SetBool(EnableStackTracePrefsKey, MotionTracker.EnableStackTrace);
  55. }
  56. GUILayout.FlexibleSpace();
  57. if (GUILayout.Button(ClearHeadContent, EditorStyles.toolbarButton, EmptyLayoutOption))
  58. {
  59. MotionTracker.Clear();
  60. treeView.ReloadAndSort();
  61. Repaint();
  62. }
  63. EditorGUILayout.EndHorizontal();
  64. EditorGUILayout.EndVertical();
  65. }
  66. Vector2 tableScroll;
  67. GUIStyle tableListStyle;
  68. void RenderTable()
  69. {
  70. if (tableListStyle == null)
  71. {
  72. tableListStyle = new GUIStyle("CN Box");
  73. tableListStyle.margin.top = 0;
  74. tableListStyle.padding.left = 3;
  75. }
  76. EditorGUILayout.BeginVertical(tableListStyle, EmptyLayoutOption);
  77. tableScroll = EditorGUILayout.BeginScrollView(this.tableScroll, new GUILayoutOption[]
  78. {
  79. GUILayout.ExpandWidth(true),
  80. GUILayout.MaxWidth(2000f)
  81. });
  82. var controlRect = EditorGUILayout.GetControlRect(new GUILayoutOption[]
  83. {
  84. GUILayout.ExpandHeight(true),
  85. GUILayout.ExpandWidth(true)
  86. });
  87. treeView?.OnGUI(controlRect);
  88. EditorGUILayout.EndScrollView();
  89. EditorGUILayout.EndVertical();
  90. }
  91. static int interval;
  92. void Update()
  93. {
  94. if (interval++ % 120 == 0)
  95. {
  96. treeView.ReloadAndSort();
  97. Repaint();
  98. }
  99. }
  100. static GUIStyle detailsStyle;
  101. Vector2 detailsScroll;
  102. void RenderDetailsPanel()
  103. {
  104. if (detailsStyle == null)
  105. {
  106. detailsStyle = new GUIStyle("CN Message")
  107. {
  108. wordWrap = false,
  109. stretchHeight = true
  110. };
  111. detailsStyle.margin.right = 15;
  112. }
  113. string message = "";
  114. var selected = treeView.state.selectedIDs;
  115. if (selected.Count > 0)
  116. {
  117. var first = selected[0];
  118. if (treeView.CurrentBindingItems.FirstOrDefault(x => x.id == first) is MotionTrackerViewItem item)
  119. {
  120. message = item.Position;
  121. }
  122. }
  123. detailsScroll = EditorGUILayout.BeginScrollView(this.detailsScroll, EmptyLayoutOption);
  124. var vector = detailsStyle.CalcSize(new GUIContent(message));
  125. EditorGUILayout.SelectableLabel(message, detailsStyle, new GUILayoutOption[]
  126. {
  127. GUILayout.ExpandHeight(true),
  128. GUILayout.ExpandWidth(true),
  129. GUILayout.MinWidth(vector.x),
  130. GUILayout.MinHeight(vector.y)
  131. });
  132. EditorGUILayout.EndScrollView();
  133. }
  134. }
  135. }