DragonECS-Unity/src/Internal/Editor/EcsGUI.cs

206 lines
8.8 KiB
C#
Raw Normal View History

2024-03-03 19:59:39 +08:00
#if UNITY_EDITOR
using DCFApixels.DragonECS.Unity.Internal;
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace DCFApixels.DragonECS.Unity.Editors
{
2024-03-04 07:38:38 +08:00
internal static class EcsGUI
2024-03-03 19:59:39 +08:00
{
2024-03-04 07:38:38 +08:00
internal readonly static Color GrayColor = new Color32(100, 100, 100, 255);
internal readonly static Color GreenColor = new Color32(75, 255, 0, 255);
internal readonly static Color RedColor = new Color32(255, 0, 75, 255);
2024-03-03 19:59:39 +08:00
2024-03-07 21:22:48 +08:00
private static readonly Rect RemoveButtonRect = new Rect(0f, 0f, 17f, 19f);
private static readonly Rect TooltipIconRect = new Rect(0f, 0f, 21f, 15f);
2024-03-04 08:01:49 +08:00
//private static GUILayoutOption[] _defaultParams;
//private static bool _isInit = false;
//private static void Init()
2024-03-04 07:38:38 +08:00
//{
2024-03-04 08:01:49 +08:00
// if (_isInit)
2024-03-04 07:38:38 +08:00
// {
2024-03-04 08:01:49 +08:00
// return;
2024-03-04 07:38:38 +08:00
// }
2024-03-04 08:01:49 +08:00
// _defaultParams = new GUILayoutOption[] { GUILayout.ExpandWidth(true) };
// _isInit = true;
2024-03-04 07:38:38 +08:00
//}
2024-03-03 19:59:39 +08:00
public static class Layout
{
2024-03-04 08:01:49 +08:00
private static bool IsShowHidden
{
get { return DebugMonitorPrefs.instance.IsShowHidden; }
set { DebugMonitorPrefs.instance.IsShowHidden = value; }
}
2024-03-04 08:21:01 +08:00
private static bool IsShowRuntimeComponents
{
get { return DebugMonitorPrefs.instance.IsShowRuntimeComponents; }
set { DebugMonitorPrefs.instance.IsShowRuntimeComponents = value; }
}
2024-03-07 03:18:00 +08:00
public static void DrawRuntimeComponents(entlong entity, bool isWithFoldout = true)
2024-03-03 19:59:39 +08:00
{
if (entity.TryUnpack(out int entityID, out EcsWorld world))
{
2024-03-07 03:18:00 +08:00
DrawRuntimeComponents(entityID, world, isWithFoldout);
2024-03-03 19:59:39 +08:00
}
}
2024-03-07 03:18:00 +08:00
public static void DrawRuntimeComponents(int entityID, EcsWorld world, bool isWithFoldout = true)
2024-03-03 19:59:39 +08:00
{
2024-03-07 03:24:02 +08:00
var componentTypeIDs = world.GetComponentTypeIDsFor(entityID);
2024-03-04 08:01:49 +08:00
2024-03-07 03:18:00 +08:00
GUILayout.BeginVertical(UnityEditorUtility.GetStyle(Color.black, 0.2f));
2024-03-04 08:01:49 +08:00
2024-03-07 03:18:00 +08:00
if (isWithFoldout)
{
IsShowRuntimeComponents = EditorGUILayout.BeginFoldoutHeaderGroup(IsShowRuntimeComponents, "RUNTIME COMPONENTS", EditorStyles.foldout);
EditorGUILayout.EndFoldoutHeaderGroup();
}
if (isWithFoldout == false || IsShowRuntimeComponents)
2024-03-03 19:59:39 +08:00
{
2024-03-07 03:18:00 +08:00
GUILayout.Box("", UnityEditorUtility.GetStyle(GUI.color, 0.16f), GUILayout.ExpandWidth(true));
2024-03-05 00:46:47 +08:00
IsShowHidden = EditorGUI.Toggle(GUILayoutUtility.GetLastRect(), "Show Hidden", IsShowHidden);
2024-03-04 08:21:01 +08:00
foreach (var componentTypeID in componentTypeIDs)
2024-03-03 19:59:39 +08:00
{
2024-03-07 03:24:02 +08:00
var pool = world.GetPoolInstance(componentTypeID);
2024-03-04 08:21:01 +08:00
{
DrawRuntimeComponent(entityID, pool);
}
2024-03-03 19:59:39 +08:00
}
}
2024-03-04 07:38:38 +08:00
GUILayout.EndVertical();
2024-03-07 21:22:48 +08:00
if (GUILayout.Button("Add Component", GUILayout.Height(24f)))
{
GenericMenu genericMenu = RuntimeComponentsUtility.GetAddComponentGenericMenu(world);
RuntimeComponentsUtility.CurrentEntityID = entityID;
genericMenu.ShowAsContext();
}
2024-03-03 19:59:39 +08:00
}
private static readonly BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
2024-03-04 08:21:01 +08:00
private static void DrawRuntimeComponent(int entityID, IEcsPool pool)
2024-03-03 19:59:39 +08:00
{
2024-03-04 08:01:49 +08:00
var meta = pool.ComponentType.ToMeta();
if (meta.IsHidden == false || IsShowHidden)
2024-03-03 19:59:39 +08:00
{
2024-03-04 08:01:49 +08:00
object data = pool.GetRaw(entityID);
2024-03-05 00:46:47 +08:00
Color panelColor = meta.Color.ToUnityColor().Desaturate(EscEditorConsts.COMPONENT_DRAWER_DESATURATE);
2024-03-07 21:22:48 +08:00
float padding = EditorGUIUtility.standardVerticalSpacing;
Rect removeButtonRect = GUILayoutUtility.GetLastRect();
2024-03-07 03:18:00 +08:00
GUILayout.BeginVertical(UnityEditorUtility.GetStyle(panelColor, EscEditorConsts.COMPONENT_DRAWER_ALPHA));
2024-03-04 08:01:49 +08:00
EditorGUI.BeginChangeCheck();
2024-03-07 21:22:48 +08:00
bool isRemoveComponent = false;
removeButtonRect.yMin = removeButtonRect.yMax;
removeButtonRect.yMax += RemoveButtonRect.height;
removeButtonRect.xMin = removeButtonRect.xMax - RemoveButtonRect.width;
removeButtonRect.center += Vector2.up * padding * 2f;
if (GUI.Button(removeButtonRect, "x"))
{
isRemoveComponent = true;
}
2024-03-04 08:01:49 +08:00
Type componentType = pool.ComponentType;
ExpandMatrix expandMatrix = ExpandMatrix.Take(componentType);
2024-03-07 03:18:00 +08:00
bool changed = DrawRuntimeData(componentType, UnityEditorUtility.GetLabel(meta.Name), expandMatrix, data, out object resultData);
2024-03-07 21:22:48 +08:00
if (changed || isRemoveComponent)
2024-03-04 08:01:49 +08:00
{
2024-03-07 21:22:48 +08:00
if (isRemoveComponent)
{
pool.Del(entityID);
}
else
{
pool.SetRaw(entityID, resultData);
}
2024-03-04 08:01:49 +08:00
}
2024-03-03 19:59:39 +08:00
2024-03-04 08:01:49 +08:00
GUILayout.EndVertical();
}
2024-03-03 19:59:39 +08:00
}
2024-03-04 08:21:01 +08:00
private static bool DrawRuntimeData(Type fieldType, GUIContent label, ExpandMatrix expandMatrix, object data, out object outData)
2024-03-03 19:59:39 +08:00
{
Type type = data.GetType();
2024-03-05 03:35:38 +08:00
bool isUnityObject = typeof(UnityEngine.Object).IsAssignableFrom(fieldType);
2024-03-04 03:00:45 +08:00
ref bool isExpanded = ref expandMatrix.Down();
2024-03-04 07:38:38 +08:00
bool changed = false;
outData = data;
2024-03-03 22:46:26 +08:00
2024-03-05 03:35:38 +08:00
if (isUnityObject == false && (type.IsGenericType || !type.IsSerializable))
2024-03-03 19:59:39 +08:00
{
2024-03-05 00:46:47 +08:00
isExpanded = EditorGUILayout.BeginFoldoutHeaderGroup(isExpanded, label, EditorStyles.foldout);
EditorGUILayout.EndFoldoutHeaderGroup();
2024-03-04 03:00:45 +08:00
if (isExpanded)
2024-03-03 19:59:39 +08:00
{
2024-03-03 22:46:26 +08:00
EditorGUI.indentLevel++;
foreach (var field in type.GetFields(fieldFlags))
2024-03-03 19:59:39 +08:00
{
2024-03-07 03:18:00 +08:00
GUIContent subLabel = UnityEditorUtility.GetLabel(UnityEditorUtility.TransformFieldName(field.Name));
2024-03-04 08:21:01 +08:00
if (DrawRuntimeData(field.FieldType, subLabel, expandMatrix, field.GetValue(data), out object fieldData))
2024-03-03 22:46:26 +08:00
{
field.SetValue(data, fieldData);
2024-03-07 03:18:00 +08:00
outData = data;
2024-03-04 07:38:38 +08:00
changed = true;
2024-03-03 22:46:26 +08:00
}
2024-03-03 19:59:39 +08:00
}
2024-03-03 22:46:26 +08:00
EditorGUI.indentLevel--;
2024-03-03 19:59:39 +08:00
}
}
else
{
2024-03-05 03:35:38 +08:00
if (isUnityObject)
{
EditorGUI.BeginChangeCheck();
2024-03-07 03:18:00 +08:00
var uobj = (UnityEngine.Object)data;
bool isComponent = (typeof(UnityEngine.Component)).IsAssignableFrom(fieldType);
if (isComponent)
{
uobj = EditorGUILayout.ObjectField(label, uobj, typeof(UnityEngine.Object), true);
}
else
{
uobj = EditorGUILayout.ObjectField(label, uobj, fieldType, true);
}
if (isComponent && uobj is GameObject go)
{
uobj = go.GetComponent(fieldType);
}
2024-03-05 03:35:38 +08:00
if (EditorGUI.EndChangeCheck())
{
outData = uobj;
changed = true;
}
}
else
{
EditorGUI.BeginChangeCheck();
WrapperBase w = RefEditorWrapper.Take(data);
2024-03-03 19:59:39 +08:00
2024-03-05 03:35:38 +08:00
w.IsExpanded = isExpanded;
EditorGUILayout.PropertyField(w.Property, label, true);
isExpanded = w.IsExpanded;
2024-03-04 03:00:45 +08:00
2024-03-05 03:35:38 +08:00
if (EditorGUI.EndChangeCheck())
{
w.SO.ApplyModifiedProperties();
outData = w.Data;
changed = true;
}
2024-03-07 03:18:00 +08:00
w.Release();
2024-03-03 19:59:39 +08:00
}
}
2024-03-06 21:37:21 +08:00
2024-03-04 07:38:38 +08:00
expandMatrix.Up();
return changed;
2024-03-03 19:59:39 +08:00
}
}
}
}
#endif