DragonECS-Unity/src/Debug/Systems/PipelineDebugSystem.cs

338 lines
14 KiB
C#
Raw Normal View History

2023-05-07 00:50:44 +08:00
using DCFApixels.DragonECS.Unity.Debug;
using System.Reflection;
using UnityEngine;
2023-03-29 19:58:58 +08:00
namespace DCFApixels.DragonECS
{
2023-03-29 15:48:18 +08:00
[DebugHide, DebugColor(DebugColor.Gray)]
2023-05-07 00:50:44 +08:00
public class PipelineDebugSystem : IEcsPreInitProcess
{
private string _monitorName;
public PipelineDebugSystem(string monitorName = "Pipeline")
{
_monitorName = monitorName;
}
2023-05-07 00:50:44 +08:00
void IEcsPreInitProcess.PreInit(EcsPipeline pipeline)
{
PipelineDebugMonitor monitor = new GameObject(EcsConsts.DEBUG_PREFIX + _monitorName).AddComponent<PipelineDebugMonitor>();
monitor.source = this;
2023-03-30 05:33:35 +08:00
monitor.pipeline = pipeline;
monitor.monitorName = _monitorName;
2023-05-07 00:50:44 +08:00
PipelineProcessesDebugMonitor processesMonitor = new GameObject(EcsConsts.DEBUG_PREFIX + "Processes Matrix").AddComponent<PipelineProcessesDebugMonitor>();
processesMonitor.transform.parent = monitor.transform;
processesMonitor.source = this;
processesMonitor.pipeline = pipeline;
processesMonitor.monitorName = "Processes Matrix";
//foreach (var item in pipeline.AllSystems) //Вырезано пока не сделаю TODO в SystemDebugMonitor
//{
// DebugNameAttribute debugName = item.GetType().GetCustomAttribute<DebugNameAttribute>();
// string name = debugName == null ? item.GetType().Name : debugName.name;
// SystemDebugMonitor.CreateMonitor(monitor.transform, item, name);
//}
}
}
public class PipelineDebugMonitor : DebugMonitorBase
{
2023-03-30 05:33:35 +08:00
internal PipelineDebugSystem source;
internal EcsPipeline pipeline;
}
2023-05-07 00:50:44 +08:00
public class PipelineProcessesDebugMonitor : DebugMonitorBase
{
internal PipelineDebugSystem source;
internal EcsPipeline pipeline;
}
#if UNITY_EDITOR
namespace Editors
{
2023-05-26 23:32:05 +08:00
using DCFApixels.DragonECS.Internal;
2023-04-24 16:48:26 +08:00
using DCFApixels.DragonECS.RunnersCore;
using System;
2023-05-07 00:50:44 +08:00
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
[CustomEditor(typeof(PipelineDebugMonitor))]
2023-03-30 05:33:35 +08:00
public class PipelineDebugMonitorEditor : Editor
{
2023-05-23 01:48:54 +08:00
private DebugColorAttribute _fakeDebugColorAttribute = new DebugColorAttribute(190, 190, 190);
private Type _debugColorAttributeType = typeof(DebugColorAttribute);
private GUIStyle _headerStyle;
private GUIStyle _interfacesStyle;
private Color _interfaceColor = new Color(0.96f, 1f, 0.16f);
private PipelineDebugMonitor Target => (PipelineDebugMonitor)target;
2023-04-26 16:54:27 +08:00
private GUIStyle systemsListStyle;
public override void OnInspectorGUI()
{
2023-04-26 16:54:27 +08:00
systemsListStyle = new GUIStyle(EditorStyles.miniLabel);
systemsListStyle.wordWrap = true;
if (Target.source == null)
return;
2023-03-27 20:49:27 +08:00
if (_headerStyle == null)
{
_headerStyle = new GUIStyle(EditorStyles.boldLabel);
2023-03-29 15:48:18 +08:00
_interfacesStyle = new GUIStyle(EditorStyles.miniLabel);
_interfacesStyle.hover.textColor = _interfaceColor;
_interfacesStyle.focused.textColor = _interfaceColor;
_interfacesStyle.active.textColor = _interfaceColor;
_interfacesStyle.normal.textColor = _interfaceColor;
2023-04-26 16:54:27 +08:00
_interfacesStyle.wordWrap = true;
_headerStyle.fontSize = 28;
}
GUILayout.Label("[Systems]", _headerStyle);
DebugMonitorPrefs.instance.IsShowInterfaces = EditorGUILayout.Toggle("Show Interfaces", DebugMonitorPrefs.instance.IsShowInterfaces);
2023-03-29 19:58:58 +08:00
DebugMonitorPrefs.instance.IsShowHidden = EditorGUILayout.Toggle("Show Hidden", DebugMonitorPrefs.instance.IsShowHidden);
2023-03-27 20:49:27 +08:00
GUILayout.BeginVertical();
2023-03-30 05:33:35 +08:00
foreach (var item in Target.pipeline.AllSystems)
{
DrawSystem(item);
}
2023-03-27 20:49:27 +08:00
GUILayout.EndVertical();
2023-03-29 15:48:18 +08:00
GUILayout.Label("[Runners]", _headerStyle);
GUILayout.BeginVertical(EcsEditor.GetStyle(Color.black, 0.2f));
2023-03-30 05:33:35 +08:00
foreach (var item in Target.pipeline.AllRunners)
{
DrawRunner(item.Value);
}
2023-03-29 15:48:18 +08:00
GUILayout.EndVertical();
}
2023-05-30 18:31:03 +08:00
private void DrawSystem(IEcsProcess system)
{
2023-05-28 05:55:39 +08:00
if (system is SystemsLayerMarkerSystem markerSystem)
2023-03-27 20:49:27 +08:00
{
GUILayout.EndVertical();
GUILayout.BeginVertical(EcsEditor.GetStyle(Color.black, 0.2f));
2023-03-27 20:49:27 +08:00
GUILayout.BeginHorizontal();
GUILayout.Label("<");
GUILayout.Label($"{markerSystem.name}", EditorStyles.boldLabel);
GUILayout.Label(">", GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
return;
}
Type type = system.GetType();
2023-03-29 15:48:18 +08:00
if (CheckIsHidden(type))
return;
2023-05-07 00:50:44 +08:00
string name = EcsEditor.GetGenericName(type);
Color color = (GetAttribute<DebugColorAttribute>(type) ?? _fakeDebugColorAttribute).GetUnityColor();
GUILayout.BeginVertical(EcsEditor.GetStyle(color, 0.2f));
if (DebugMonitorPrefs.instance.IsShowInterfaces)
{
GUILayout.Label(string.Join(", ", type.GetInterfaces().Select(o => o.Name)), _interfacesStyle);
}
GUILayout.Label(name, EditorStyles.boldLabel);
GUILayout.EndVertical();
}
private void DrawRunner(IEcsRunner runner)
{
Type type = runner.GetType();
2023-03-29 15:48:18 +08:00
if (CheckIsHidden(type))
return;
Color color = (GetAttribute<DebugColorAttribute>(type) ?? _fakeDebugColorAttribute).GetUnityColor();
GUILayout.BeginVertical(EcsEditor.GetStyle(color, 0.2f));
2023-05-07 00:50:44 +08:00
GUILayout.Label(EcsEditor.GetGenericName(type), EditorStyles.boldLabel);
2023-04-26 16:54:27 +08:00
GUILayout.Label(string.Join(", ", runner.Targets.Cast<object>().Select(o => o.GetType().Name)), systemsListStyle);
GUILayout.EndVertical();
}
private TAttribute GetAttribute<TAttribute>(Type target) where TAttribute : Attribute
{
var result = target.GetCustomAttributes(_debugColorAttributeType, false);
if (result.Length > 0)
return (TAttribute)result[0];
return null;
}
2023-03-29 15:48:18 +08:00
private bool CheckIsHidden(Type target)
{
2023-03-29 19:58:58 +08:00
if (DebugMonitorPrefs.instance.IsShowHidden)
2023-03-29 15:48:18 +08:00
return false;
return target.GetCustomAttribute<DebugHideAttribute>() != null;
}
}
2023-05-07 00:50:44 +08:00
[CustomEditor(typeof(PipelineProcessesDebugMonitor))]
public class PipelineProcessesDebugMonitorEditor : Editor
{
private bool _isInit = false;
private List<ProcessData> _processesList = new List<ProcessData>();
private Dictionary<Type, int> _processeIndexes = new Dictionary<Type, int>();
private PipelineProcessesDebugMonitor Target => (PipelineProcessesDebugMonitor)target;
2023-05-30 18:31:03 +08:00
private Type systemInterfaceType = typeof(IEcsProcess);
2023-05-07 00:50:44 +08:00
2023-05-30 18:31:03 +08:00
private IEcsProcess[] _systems;
2023-05-07 00:50:44 +08:00
private void Init()
{
if (_isInit)
return;
bool showHidden = DebugMonitorPrefs.instance.IsShowHidden;
_processesList.Clear();
_processeIndexes.Clear();
if (showHidden)
2023-05-28 05:55:39 +08:00
_systems = Target.pipeline.AllSystems.Where(o => o is SystemsLayerMarkerSystem == false).ToArray();
2023-05-07 00:50:44 +08:00
else
_systems = Target.pipeline.AllSystems.Where(o => o.GetType().GetCustomAttribute<DebugHideAttribute>() == null).ToArray();
int i = 0;
foreach (var system in _systems)
{
foreach (var intr in system.GetType().GetInterfaces())
{
if(systemInterfaceType.IsAssignableFrom(intr) && systemInterfaceType != intr && (showHidden || intr.GetCustomAttribute<DebugHideAttribute>() == null))
{
ProcessData data;
if (!_processeIndexes.TryGetValue(intr, out int index))
{
index = _processesList.Count;
_processeIndexes.Add(intr, index);
data = new ProcessData();
_processesList.Add(data);
data.name = EcsEditor.GetGenericName(intr);
data.interfaceType = intr;
data.systemsBitMask = new BitMask(_systems.Length);
}
data = _processesList[index];
data.systemsBitMask[i] = true;
}
}
i++;
}
_isInit = true;
}
private Vector2 _position;
private Vector2 _cellsize = new Vector2(EditorGUIUtility.singleLineHeight, EditorGUIUtility.singleLineHeight);
private Vector2 _nameCellSize = new Vector2(200f, 200f);
public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
DebugMonitorPrefs.instance.IsShowHidden = EditorGUILayout.Toggle("Show Hidden", DebugMonitorPrefs.instance.IsShowHidden);
if (EditorGUI.EndChangeCheck())
{
_isInit = false;
}
Init();
Rect rect;
Rect lineRect;
GUILayout.Label("", GUILayout.ExpandWidth(true), GUILayout.Height(400f));
rect = GUILayoutUtility.GetLastRect();
rect.height = 400f;
Rect rectView = new Rect(0f, 0f, _nameCellSize.x + _cellsize.x * _processesList.Count, _nameCellSize.y + _cellsize.y * _systems.Length);
_position = GUI.BeginScrollView(rect, _position, rectView, true, true);
List<string> systeNames = new List<string>();
var blackStyle = EcsEditor.GetStyle(Color.black, 0.04f);
var whiteStyle = EcsEditor.GetStyle(Color.white, 0.04f);
GUIContent label = new GUIContent();
Vector2 pivod = _nameCellSize;
rect = new Rect();
rect.y = _nameCellSize.y;
rect.width = _nameCellSize.x;
rect.height = _cellsize.x;
rect.y -= _cellsize.y;
for (int i = 0; i < _processesList.Count; i++)
{
lineRect = rect;
lineRect.y = 0f;
lineRect.x = _nameCellSize.x + _cellsize.x * i;
lineRect.width = _cellsize.x;
lineRect.height = rectView.height;
GUI.Label(lineRect, "", i % 2 == 1 ? whiteStyle : blackStyle);
GUIUtility.RotateAroundPivot(90, pivod);
//GUIContent label = new GUIContent(_processesList[i].name, "." + _processesList[i].name);
label.text = _processesList[i].name;
label.tooltip = "." + _processesList[i].name;
GUI.Label(rect, label, EditorStyles.miniBoldLabel);
GUIUtility.RotateAroundPivot(-90, pivod);
pivod.x += _cellsize.x;
rect.x += _cellsize.x;
}
//GUIUtility.RotateAroundPivot(-90, _nameCellSize);
rect = new Rect();
rect.y = _nameCellSize.y;
rect.width = _nameCellSize.x;
rect.height = _cellsize.x;
for (int i = 0; i < _systems.Length; i++)
{
string name = EcsEditor.GetGenericName(_systems[i].GetType());
systeNames.Add(name);
lineRect = rect;
lineRect.width = rectView.width;
GUI.Label(lineRect, "", i % 2 == 1 ? whiteStyle : blackStyle);
// GUIContent label = new GUIContent(name, i + " " + name);
label.text = name;
label.tooltip = i + " " + name;
GUI.Label(rect, label, EditorStyles.miniBoldLabel);
rect.y += _cellsize.y;
}
for (int x = 0; x < _processesList.Count; x++)
{
var process = _processesList[x];
for (int y = 0; y < _systems.Length; y++)
{
string systemName = systeNames[x];
rect = new Rect(x * _cellsize.x + _nameCellSize.x, y * _cellsize.y + _nameCellSize.y, _cellsize.x, _cellsize.y);
bool flag = process.systemsBitMask[y];
string labeltext = flag ? "^" : " ";
label.text = labeltext;
label.tooltip = $"{process.name}-{systemName}";
GUI.Label(rect, label);
//GUI.Label(rect, lable, flag ? whiteStyle : blackStyle);
// GUI.Label(rect, label, EditorStyles.helpBox);
}
}
GUI.EndScrollView();
}
private class ProcessData
{
public Type interfaceType;
public string name;
public BitMask systemsBitMask;
}
}
}
#endif
}