DragonECS-Unity/src/Debug/Monitors/Editor/PipelineProcessesMonitorEditor.cs

198 lines
7.6 KiB
C#
Raw Normal View History

2024-03-10 06:19:20 +08:00
#if UNITY_EDITOR
using DCFApixels.DragonECS.Unity.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace DCFApixels.DragonECS.Unity.Editors
{
[CustomEditor(typeof(PipelineProcessMonitor))]
internal class PipelineProcessesMonitorEditor : Editor
{
2024-03-10 08:10:58 +08:00
private static Type SYSTEM_INTERFACE_TYPE = typeof(IEcsProcess);
2024-03-10 06:19:20 +08:00
private bool _isInit = false;
2024-03-10 08:10:58 +08:00
private List<ProcessData> _processList = new List<ProcessData>();
2024-03-10 06:19:20 +08:00
private Dictionary<Type, int> _processeIndexes = new Dictionary<Type, int>();
2024-03-10 08:10:58 +08:00
private SystemData[] _systemsList;
2024-03-10 06:19:20 +08:00
private PipelineProcessMonitor Target => (PipelineProcessMonitor)target;
private bool IsShowInterfaces
{
get { return DebugMonitorPrefs.instance.IsShowInterfaces; }
set { DebugMonitorPrefs.instance.IsShowInterfaces = value; }
}
private bool IsShowHidden
{
get { return DebugMonitorPrefs.instance.IsShowHidden; }
set { DebugMonitorPrefs.instance.IsShowHidden = value; }
}
private void Init()
{
if (_isInit)
{
return;
}
2024-03-10 08:10:58 +08:00
_processList.Clear();
2024-03-10 06:19:20 +08:00
_processeIndexes.Clear();
2024-03-10 08:10:58 +08:00
IEnumerable<IEcsProcess> fileretSystems = Target.Pipeline.AllSystems;
2024-03-10 06:19:20 +08:00
if (IsShowHidden)
2024-03-10 08:10:58 +08:00
{ fileretSystems = fileretSystems.Where(o => o is SystemsLayerMarkerSystem == false); }
2024-03-10 06:19:20 +08:00
else
2024-03-10 08:10:58 +08:00
{ fileretSystems = fileretSystems.Where(o => o.GetMeta().IsHidden == false); }
_systemsList = fileretSystems.Select(o => new SystemData(o.GetMeta(), o)).ToArray();
2024-03-10 06:19:20 +08:00
2024-03-10 08:10:58 +08:00
for (int i = 0; i < _systemsList.Length; i++)
2024-03-10 06:19:20 +08:00
{
2024-03-10 08:10:58 +08:00
var system = _systemsList[i];
foreach (var interfaceType in system.meta.Type.GetInterfaces())
2024-03-10 06:19:20 +08:00
{
TypeMeta meta = interfaceType.ToMeta();
2024-03-10 08:10:58 +08:00
if (SYSTEM_INTERFACE_TYPE.IsAssignableFrom(interfaceType) && SYSTEM_INTERFACE_TYPE != interfaceType && (IsShowHidden || meta.IsHidden == false))
2024-03-10 06:19:20 +08:00
{
ProcessData data;
2024-03-10 08:10:58 +08:00
if (_processeIndexes.TryGetValue(interfaceType, out int index) == false)
2024-03-10 06:19:20 +08:00
{
2024-03-10 08:10:58 +08:00
index = _processList.Count;
2024-03-10 06:19:20 +08:00
_processeIndexes.Add(interfaceType, index);
data = new ProcessData();
2024-03-10 08:10:58 +08:00
data.interfaceMeta = meta;
data.systemsBitMask = new BitMask(_systemsList.Length);
_processList.Add(data);
2024-03-10 06:19:20 +08:00
}
2024-03-10 08:10:58 +08:00
data = _processList[index];
2024-03-10 06:19:20 +08:00
data.systemsBitMask[i] = true;
}
}
}
_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();
IsShowHidden = EditorGUILayout.Toggle("Show Hidden", IsShowHidden);
if (EditorGUI.EndChangeCheck())
{
_isInit = false;
}
Init();
GUILayout.Label("", GUILayout.ExpandWidth(true), GUILayout.Height(400f));
2024-03-10 08:10:58 +08:00
Rect rect = GUILayoutUtility.GetLastRect();
2024-03-10 06:19:20 +08:00
rect.height = 400f;
2024-03-10 08:10:58 +08:00
Rect rectView = new Rect(0f, 0f, _nameCellSize.x + _cellsize.x * _processList.Count, _nameCellSize.y + _cellsize.y * _systemsList.Length);
EditorGUI.DrawRect(rect, new Color(0, 0, 0, 0.6f));
2024-03-10 06:19:20 +08:00
_position = GUI.BeginScrollView(rect, _position, rectView, true, true);
2024-03-10 08:10:58 +08:00
//var blackStyle = UnityEditorUtility.GetStyle(Color.black, 0.04f);
//var whiteStyle = UnityEditorUtility.GetStyle(Color.white, 0.04f);
2024-03-10 06:19:20 +08:00
Vector2 pivod = _nameCellSize;
2024-03-10 08:10:58 +08:00
rect = default;
2024-03-10 06:19:20 +08:00
rect.y = _nameCellSize.y;
rect.width = _nameCellSize.x;
rect.height = _cellsize.x;
rect.y -= _cellsize.y;
2024-03-10 08:10:58 +08:00
//processes line
for (int i = 0; i < _processList.Count; i++)
2024-03-10 06:19:20 +08:00
{
2024-03-10 08:10:58 +08:00
TypeMeta meta = _processList[i].interfaceMeta;
Rect lineRect = rect;
2024-03-10 06:19:20 +08:00
lineRect.y = 0f;
lineRect.x = _nameCellSize.x + _cellsize.x * i;
lineRect.width = _cellsize.x;
lineRect.height = rectView.height;
2024-03-10 08:10:58 +08:00
lineRect = RectUtility.AddPadding(lineRect, 1, 0);
//GUI.Label(lineRect, "", i % 2 == 1 ? whiteStyle : blackStyle);
Color color = meta.Color.ToUnityColor();
color = NormalizeGridColor(i, color);
EditorGUI.DrawRect(lineRect, color);
2024-03-10 06:19:20 +08:00
GUIUtility.RotateAroundPivot(90, pivod);
2024-03-10 08:10:58 +08:00
GUI.Label(rect, UnityEditorUtility.GetLabel(meta.Name), EditorStyles.miniBoldLabel);
2024-03-10 06:19:20 +08:00
GUIUtility.RotateAroundPivot(-90, pivod);
pivod.x += _cellsize.x;
rect.x += _cellsize.x;
}
2024-03-10 08:10:58 +08:00
rect = default;
2024-03-10 06:19:20 +08:00
rect.y = _nameCellSize.y;
rect.width = _nameCellSize.x;
rect.height = _cellsize.x;
2024-03-10 08:10:58 +08:00
//systems line
for (int i = 0; i < _systemsList.Length; i++)
2024-03-10 06:19:20 +08:00
{
2024-03-10 08:10:58 +08:00
TypeMeta meta = _systemsList[i].meta;
2024-03-10 06:19:20 +08:00
string name = meta.Name;
2024-03-10 08:10:58 +08:00
Rect lineRect = rect;
2024-03-10 06:19:20 +08:00
lineRect.width = rectView.width;
2024-03-10 08:10:58 +08:00
lineRect = RectUtility.AddPadding(lineRect, 0, 1);
//GUI.Label(lineRect, "", i % 2 == 1 ? whiteStyle : blackStyle);
Color color = meta.Color.ToUnityColor();
color = NormalizeGridColor(i, color);
EditorGUI.DrawRect(lineRect, color);
2024-03-10 06:19:20 +08:00
2024-03-10 08:10:58 +08:00
GUI.Label(rect, UnityEditorUtility.GetLabel(name, i + " " + name), EditorStyles.miniBoldLabel);
2024-03-10 06:19:20 +08:00
rect.y += _cellsize.y;
}
2024-03-10 08:10:58 +08:00
//matrix
for (int x = 0; x < _processList.Count; x++)
2024-03-10 06:19:20 +08:00
{
2024-03-10 08:10:58 +08:00
var process = _processList[x];
for (int y = 0; y < _systemsList.Length; y++)
2024-03-10 06:19:20 +08:00
{
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 ? "^" : " ";
2024-03-10 08:10:58 +08:00
GUI.Label(rect, UnityEditorUtility.GetLabel(labeltext, $"{process.interfaceMeta.Name}-{_systemsList[x].meta.Name}"));
2024-03-10 06:19:20 +08:00
}
}
GUI.EndScrollView();
}
2024-03-10 08:10:58 +08:00
private static Color NormalizeGridColor(int index, Color color)
{
if (index % 2 == 1)
{
color = color / 1.4f;
color.a = 0.3f;
}
else
{
color.a = 0.5f;
}
return color;
}
private class SystemData
{
public TypeMeta meta;
public IEcsProcess system;
public SystemData(TypeMeta meta, IEcsProcess system)
{
this.meta = meta;
this.system = system;
}
}
2024-03-10 06:19:20 +08:00
private class ProcessData
{
2024-03-10 08:10:58 +08:00
public TypeMeta interfaceMeta;
2024-03-10 06:19:20 +08:00
public BitMask systemsBitMask;
}
}
}
#endif