MotionDebuggerWindow.cs
Editor/Diagnostics/MotionDebuggerWindow.cs
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using UnityEditor;
5 using UnityEngine;
6
7 namespace MeatMotion.Editor
8 {
9 public class MotionDebuggerWindow : EditorWindow
10 {
11 const string EnableKey = "MeatMotion.Debugger.Enabled", StackKey = "MeatMotion.Debugger.StackTrace";
12 readonly List<MotionDebugger.TrackingState> rows = new List<MotionDebugger.TrackingState>();
13 MotionDebugger.TrackingState selected;
14 string filter = "";
15 Vector2 listScroll, detailsScroll;
16
17 [MenuItem("Window/MeatMotion Debugger")]
18 public static void OpenWindow() => GetWindow<MotionDebuggerWindow>("MeatMotion Debugger").Show();
19
20 void OnEnable()
21 {
22 MotionDebugger.Enabled = EditorPrefs.GetBool(EnableKey, MotionDebugger.Enabled);
23 MotionDebugger.EnableStackTrace = EditorPrefs.GetBool(StackKey, MotionDebugger.EnableStackTrace);
24 }
25
26 void OnInspectorUpdate() => Repaint();
27
28 void OnGUI()
29 {
30 using (new GUILayout.HorizontalScope(EditorStyles.toolbar))
31 {
32 Preference("Track motions", EnableKey, ref MotionDebugger.Enabled);
33 Preference("Capture stack", StackKey, ref MotionDebugger.EnableStackTrace);
34 if (GUILayout.Button("Clear", EditorStyles.toolbarButton)) MotionDebugger.Clear();
35 filter = EditorGUILayout.TextField(filter, EditorStyles.toolbarSearchField);
36 }
37 if (Event.current.type == EventType.Layout) CollectRows();
38 EditorGUILayout.LabelField($"Motions ({rows.Count})", EditorStyles.boldLabel);
39 Rect area = GUILayoutUtility.GetRect(0, 0, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
40 const float height = 20;
41 var content = new Rect(0, 0, Mathf.Max(1, area.width - 18), rows.Count * height);
42 listScroll = GUI.BeginScrollView(area, listScroll, content);
43 int first = Mathf.Max(0, Mathf.FloorToInt(listScroll.y / height));
44 int end = Mathf.Min(rows.Count, first + Mathf.CeilToInt(area.height / height) + 1);
45 for (int i = first; i < end; i++)
46 {
47 var item = rows[i];
48 if (!item.Handle.IsActive()) continue;
49 var rect = new Rect(0, i * height, content.width, height);
50 if (GUI.Toggle(rect, item == selected, $"{item.Handle.GetDebugName()} {item.ValueType.Name}", "Button"))
51 selected = item;
52 }
53 GUI.EndScrollView();
54
55 using var details = new EditorGUILayout.ScrollViewScope(detailsScroll, GUILayout.Height(Mathf.Min(210, position.height * .38f)));
56 detailsScroll = details.scrollPosition;
57 if (selected == null || !selected.Handle.IsActive()) return;
58 EditorGUILayout.LabelField(selected.Handle.GetDebugName(), EditorStyles.boldLabel);
59 EditorGUILayout.LabelField("Value / Options / Adapter", $"{selected.ValueType.Name} / {selected.OptionsType.Name} / {selected.AdapterType.Name}");
60 EditorGUILayout.LabelField("Scheduler", selected.Scheduler?.ToString() ?? "Default");
61 EditorGUILayout.LabelField("Time", $"{selected.Handle.Time:G4} / {selected.Handle.TotalDuration:G4} s");
62 if (selected.StackTrace == null) return;
63 foreach (var frame in selected.StackTrace.GetFrames() ?? Array.Empty<System.Diagnostics.StackFrame>())
64 {
65 var method = frame.GetMethod();
66 string path = frame.GetFileName();
67 int line = frame.GetFileLineNumber();
68 string description = $"{method?.DeclaringType?.Name}.{method?.Name}";
69 if (string.IsNullOrEmpty(path)) EditorGUILayout.LabelField(description);
70 else if (GUILayout.Button($"{description} {Path.GetFileName(path)}:{line}", EditorStyles.linkLabel))
71 UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(path, line);
72 }
73 }
74
75 void CollectRows()
76 {
77 rows.Clear();
78 var items = MotionDebugger.Items;
79 bool found = false;
80 for (int i = 0; i < items.Count; i++)
81 {
82 var item = items[i];
83 if (filter.Length != 0 && !Contains(item.ValueType.Name) && !Contains(item.AdapterType.Name) &&
84 !Contains(item.Handle.GetDebugName())) continue;
85 rows.Add(item);
86 found |= item == selected;
87 }
88 if (!found) selected = null;
89 }
90
91 bool Contains(string text) => text.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0;
92
93 static void Preference(string label, string key, ref bool current)
94 {
95 bool value = GUILayout.Toggle(current, label, EditorStyles.toolbarButton);
96 if (value == current) return;
97 current = value;
98 EditorPrefs.SetBool(key, value);
99 }
100 }
101 }