This commit is contained in:
Mikhail 2024-03-05 00:46:47 +08:00
parent 1176853cd5
commit 8bba0a01f8
7 changed files with 122 additions and 43 deletions

View File

@ -0,0 +1,20 @@
using UnityEngine;
namespace DCFApixels.DragonECS.Unity.Internal
{
internal static class ColorUtility
{
public static Color Desaturate(this Color self, float t)
{
float r = self.r;
float g = self.g;
float b = self.b;
//float gray = r * 0.299f + g * 0.587f + b * 0.114f;
float gray = r * 0.3333333f + g * 0.3333333f + b * 0.3333333f;
r = r + (gray - r) * (1 - t);
g = g + (gray - g) * (1 - t);
b = b + (gray - b) * (1 - t);
return new Color(r, g, b);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: daa1178cae0d21643b233d22f2c3e867
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,4 +1,5 @@
#if UNITY_EDITOR #if UNITY_EDITOR
using Codice.Utils;
using DCFApixels.DragonECS.Unity.Internal; using DCFApixels.DragonECS.Unity.Internal;
using System; using System;
using System.Reflection; using System.Reflection;
@ -105,8 +106,8 @@ namespace DCFApixels.DragonECS.Unity.Editors
IsShowRuntimeComponents = EditorGUILayout.Foldout(IsShowRuntimeComponents, "RUNTIME COMPONENTS"); IsShowRuntimeComponents = EditorGUILayout.Foldout(IsShowRuntimeComponents, "RUNTIME COMPONENTS");
if (IsShowRuntimeComponents) if (IsShowRuntimeComponents)
{ {
//TODO галочкаслишком чернаяя, невидно GUILayout.Box("", EcsEditor.GetStyle(GUI.color, 0.16f), GUILayout.ExpandWidth(true));
IsShowHidden = EditorGUILayout.Toggle("Show Hidden", IsShowHiddens); IsShowHidden = EditorGUI.Toggle(GUILayoutUtility.GetLastRect(), "Show Hidden", IsShowHidden);
foreach (var componentTypeID in componentTypeIDs) foreach (var componentTypeID in componentTypeIDs)
{ {
var pool = world.GetPool(componentTypeID); var pool = world.GetPool(componentTypeID);
@ -124,8 +125,8 @@ namespace DCFApixels.DragonECS.Unity.Editors
if (meta.IsHidden == false || IsShowHidden) if (meta.IsHidden == false || IsShowHidden)
{ {
object data = pool.GetRaw(entityID); object data = pool.GetRaw(entityID);
Color panelColor = meta.Color.ToUnityColor(); Color panelColor = meta.Color.ToUnityColor().Desaturate(EscEditorConsts.COMPONENT_DRAWER_DESATURATE);
GUILayout.BeginVertical(EcsEditor.GetStyle(panelColor, 0.22f)); GUILayout.BeginVertical(EcsEditor.GetStyle(panelColor, EscEditorConsts.COMPONENT_DRAWER_ALPHA));
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
Type componentType = pool.ComponentType; Type componentType = pool.ComponentType;
@ -151,7 +152,8 @@ namespace DCFApixels.DragonECS.Unity.Editors
if (uobj == null && (type.IsGenericType || !type.IsSerializable)) if (uobj == null && (type.IsGenericType || !type.IsSerializable))
{ {
isExpanded = EditorGUILayout.Foldout(isExpanded, label); isExpanded = EditorGUILayout.BeginFoldoutHeaderGroup(isExpanded, label, EditorStyles.foldout);
EditorGUILayout.EndFoldoutHeaderGroup();
if (isExpanded) if (isExpanded)
{ {

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DCFApixels.DragonECS.Unity.Editors
{
internal static class EscEditorConsts
{
public const float COMPONENT_DRAWER_ALPHA = 0.26f;
public const float COMPONENT_DRAWER_DESATURATE = 0.86f;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b0bed74c1b3b07d44b9ea1ea917809e4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -29,6 +29,14 @@ namespace DCFApixels.DragonECS.Unity.Internal
return (l, r); return (l, r);
} }
public static (Rect, Rect) VerticalSliceTop(Rect rect, float height)
{
Rect t = rect;
Rect b = rect;
t.yMax = t.yMin + height;
b.yMin += height;
return (t, b);
}
public static (Rect, Rect) VerticalSliceBottom(Rect rect, float height) public static (Rect, Rect) VerticalSliceBottom(Rect rect, float height)
{ {
Rect t = rect; Rect t = rect;
@ -37,5 +45,19 @@ namespace DCFApixels.DragonECS.Unity.Internal
b.yMin = b.yMax - height; b.yMin = b.yMax - height;
return (t, b); return (t, b);
} }
public static Rect AddPadding(Rect rect, float verticalHorizontal)
{
return AddPadding(rect, verticalHorizontal, verticalHorizontal);
}
public static Rect AddPadding(Rect rect, float vertical, float horizontal)
{
rect.xMax -= horizontal;
rect.xMin += horizontal;
rect.yMax -= vertical;
rect.yMin += vertical;
return rect;
}
} }
} }

View File

@ -10,8 +10,8 @@ namespace DCFApixels.DragonECS.Unity.Editors
public abstract class EntityTemplateEditorBase : Editor public abstract class EntityTemplateEditorBase : Editor
{ {
private static readonly Rect RemoveButtonRect = new Rect(0f, 0f, 15f, 15f); private static readonly Rect RemoveButtonRect = new Rect(0f, 0f, 17f, 19f);
private static readonly Rect TooltipIconRect = new Rect(0f, 0f, 15f, 15f); private static readonly Rect TooltipIconRect = new Rect(0f, 0f, 21f, 15f);
private GUIStyle removeButtonStyle; private GUIStyle removeButtonStyle;
private GenericMenu genericMenu; private GenericMenu genericMenu;
@ -96,14 +96,15 @@ namespace DCFApixels.DragonECS.Unity.Editors
Init(); Init();
SerializedProperty componentsProp = serializedObject.FindProperty(target.ComponentsPropertyName); SerializedProperty componentsProp = serializedObject.FindProperty(target.ComponentsPropertyName);
if (componentsProp == null) if (componentsProp == null)
{
return; return;
}
DrawTop(target); DrawTop(target);
GUILayout.BeginVertical(EcsEditor.GetStyle(Color.black, 0.2f)); GUILayout.BeginVertical(EcsEditor.GetStyle(Color.black, 0.2f));
for (int i = 0; i < componentsProp.arraySize; i++) for (int i = 0; i < componentsProp.arraySize; i++)
{ {
DrawComponentData(componentsProp.GetArrayElementAtIndex(i), i); DrawComponentData(componentsProp.GetArrayElementAtIndex(i), i);
GUILayout.Space(EditorGUIUtility.standardVerticalSpacing * 2);
} }
GUILayout.EndVertical(); GUILayout.EndVertical();
DrawFooter(target); DrawFooter(target);
@ -140,72 +141,70 @@ namespace DCFApixels.DragonECS.Unity.Editors
return; return;
} }
Type initializerType;
Type componentType; Type componentType;
SerializedProperty componentProperty = componentRefProp; SerializedProperty componentProperty = componentRefProp;
ComponentTemplateBase customInitializer = componentProperty.managedReferenceValue as ComponentTemplateBase; ComponentTemplateBase customInitializer = componentProperty.managedReferenceValue as ComponentTemplateBase;
if (customInitializer != null) if (customInitializer != null)
{ {
componentProperty = componentRefProp.FindPropertyRelative("component"); componentProperty = componentRefProp.FindPropertyRelative("component");
initializerType = customInitializer.Type;
componentType = customInitializer.GetType().GetField("component", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FieldType; componentType = customInitializer.GetType().GetField("component", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FieldType;
} }
else else
{ {
initializerType = componentProperty.managedReferenceValue.GetType(); componentType = componentProperty.managedReferenceValue.GetType(); ;
componentType = initializerType;
} }
string name = template.Name; string name = template.Name;
string description = template.Description; string description = template.Description;
Color panelColor = template.Color; Color panelColor = template.Color.Desaturate(EscEditorConsts.COMPONENT_DRAWER_DESATURATE);
GUILayout.BeginHorizontal();
GUILayout.BeginVertical(EcsEditor.GetStyle(panelColor, 0.22f));
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
GUIContent label = new GUIContent(name); GUIContent label = new GUIContent(name);
if (componentType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Length <= 0) bool isEmpty = componentType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Length <= 0;
float width = EditorGUIUtility.currentViewWidth;
float height = isEmpty ? EditorGUIUtility.singleLineHeight : EditorGUI.GetPropertyHeight(componentProperty, label, true);
float padding = EditorGUIUtility.standardVerticalSpacing;
Rect propertyFullRect = GUILayoutUtility.GetRect(width, height + padding * 3f);
propertyFullRect = RectUtility.AddPadding(propertyFullRect, padding / 2f);
Color alphaPanelColor = panelColor;
alphaPanelColor.a = EscEditorConsts.COMPONENT_DRAWER_ALPHA;
var (propertyRect, controlRect) = RectUtility.HorizontalSliceRight(propertyFullRect, RemoveButtonRect.width);
var (removeButtonRect, _) = RectUtility.VerticalSliceTop(controlRect, RemoveButtonRect.height);
#region Draw Component Block
EditorGUI.DrawRect(propertyFullRect, alphaPanelColor);
propertyRect = RectUtility.AddPadding(propertyRect, padding);
if (isEmpty)
{ {
GUILayout.Label(label); GUI.Label(propertyRect, label);
} }
else else
{ {
EditorGUILayout.PropertyField(componentProperty, label, true); EditorGUI.PropertyField(propertyRect, componentProperty, label, true);
} }
if (EditorGUI.EndChangeCheck()) if (GUI.Button(removeButtonRect, "x"))
{
componentProperty.serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(componentProperty.serializedObject.targetObject);
}
Rect lastrect = GUILayoutUtility.GetLastRect();
Rect removeButtonRect = RemoveButtonRect;
removeButtonRect.center = new Vector2(lastrect.xMax + removeButtonRect.width, lastrect.yMin + removeButtonRect.height / 2f);
GUILayout.EndVertical();
//Rect lineRect = GUILayoutUtility.GetLastRect();
//lineRect.y = lineRect.yMax;
//lineRect.height = 3f;
//Color rectColor = panelColor;
//rectColor.a = 0.34f;
//EditorGUI.DrawRect(lineRect, rectColor);
GUILayout.Label("", GUILayout.Width(removeButtonRect.width));
if (GUI.Button(removeButtonRect, "x", removeButtonStyle))
{ {
OnRemoveComponentAt(index); OnRemoveComponentAt(index);
} }
#endregion
if (!string.IsNullOrEmpty(description)) if (!string.IsNullOrEmpty(description))
{ {
Rect tooltipIconRect = TooltipIconRect; Rect tooltipIconRect = TooltipIconRect;
tooltipIconRect.center = new Vector2(lastrect.xMax - removeButtonRect.width / 2f, lastrect.yMin + removeButtonRect.height / 2f); tooltipIconRect.center = new Vector2(propertyRect.xMax - removeButtonRect.width / 2f, propertyRect.yMin + removeButtonRect.height / 2f);
GUIContent descriptionLabel = new GUIContent(EcsUnityConsts.INFO_MARK, description); GUIContent descriptionLabel = new GUIContent("( i )", description);
GUI.Label(tooltipIconRect, descriptionLabel, EditorStyles.boldLabel); GUI.Label(tooltipIconRect, descriptionLabel, EditorStyles.boldLabel);
} }
GUILayout.EndHorizontal();
if (EditorGUI.EndChangeCheck())
{
componentProperty.serializedObject.ApplyModifiedProperties();
componentProperty.serializedObject.SetIsDifferentCacheDirty();
}
} }
private void DrawDamagedComponent(SerializedProperty componentRefProp, int index) private void DrawDamagedComponent(SerializedProperty componentRefProp, int index)