From 8bba0a01f818ab0b638139b340885ca7902ff7a4 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Tue, 5 Mar 2024 00:46:47 +0800 Subject: [PATCH] update --- src/Editor/ColorUtility.cs | 20 +++++ src/Editor/ColorUtility.cs.meta | 11 +++ src/Editor/EditorUtility.cs | 12 +-- src/Editor/EscEditorConsts.cs | 14 ++++ src/Editor/EscEditorConsts.cs.meta | 11 +++ src/Editor/RectUtility.cs | 22 ++++++ .../Editor/EntityTemplateEditor.cs | 75 +++++++++---------- 7 files changed, 122 insertions(+), 43 deletions(-) create mode 100644 src/Editor/ColorUtility.cs create mode 100644 src/Editor/ColorUtility.cs.meta create mode 100644 src/Editor/EscEditorConsts.cs create mode 100644 src/Editor/EscEditorConsts.cs.meta diff --git a/src/Editor/ColorUtility.cs b/src/Editor/ColorUtility.cs new file mode 100644 index 0000000..ca9f91d --- /dev/null +++ b/src/Editor/ColorUtility.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/src/Editor/ColorUtility.cs.meta b/src/Editor/ColorUtility.cs.meta new file mode 100644 index 0000000..22a57a0 --- /dev/null +++ b/src/Editor/ColorUtility.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: daa1178cae0d21643b233d22f2c3e867 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Editor/EditorUtility.cs b/src/Editor/EditorUtility.cs index f17bc5b..36c126b 100644 --- a/src/Editor/EditorUtility.cs +++ b/src/Editor/EditorUtility.cs @@ -1,4 +1,5 @@ #if UNITY_EDITOR +using Codice.Utils; using DCFApixels.DragonECS.Unity.Internal; using System; using System.Reflection; @@ -105,8 +106,8 @@ namespace DCFApixels.DragonECS.Unity.Editors IsShowRuntimeComponents = EditorGUILayout.Foldout(IsShowRuntimeComponents, "RUNTIME COMPONENTS"); if (IsShowRuntimeComponents) { - //TODO галочкаслишком чернаяя, невидно - IsShowHidden = EditorGUILayout.Toggle("Show Hidden", IsShowHiddens); + GUILayout.Box("", EcsEditor.GetStyle(GUI.color, 0.16f), GUILayout.ExpandWidth(true)); + IsShowHidden = EditorGUI.Toggle(GUILayoutUtility.GetLastRect(), "Show Hidden", IsShowHidden); foreach (var componentTypeID in componentTypeIDs) { var pool = world.GetPool(componentTypeID); @@ -124,8 +125,8 @@ namespace DCFApixels.DragonECS.Unity.Editors if (meta.IsHidden == false || IsShowHidden) { object data = pool.GetRaw(entityID); - Color panelColor = meta.Color.ToUnityColor(); - GUILayout.BeginVertical(EcsEditor.GetStyle(panelColor, 0.22f)); + Color panelColor = meta.Color.ToUnityColor().Desaturate(EscEditorConsts.COMPONENT_DRAWER_DESATURATE); + GUILayout.BeginVertical(EcsEditor.GetStyle(panelColor, EscEditorConsts.COMPONENT_DRAWER_ALPHA)); EditorGUI.BeginChangeCheck(); Type componentType = pool.ComponentType; @@ -151,7 +152,8 @@ namespace DCFApixels.DragonECS.Unity.Editors if (uobj == null && (type.IsGenericType || !type.IsSerializable)) { - isExpanded = EditorGUILayout.Foldout(isExpanded, label); + isExpanded = EditorGUILayout.BeginFoldoutHeaderGroup(isExpanded, label, EditorStyles.foldout); + EditorGUILayout.EndFoldoutHeaderGroup(); if (isExpanded) { diff --git a/src/Editor/EscEditorConsts.cs b/src/Editor/EscEditorConsts.cs new file mode 100644 index 0000000..e6ff535 --- /dev/null +++ b/src/Editor/EscEditorConsts.cs @@ -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; + } +} diff --git a/src/Editor/EscEditorConsts.cs.meta b/src/Editor/EscEditorConsts.cs.meta new file mode 100644 index 0000000..e704e9c --- /dev/null +++ b/src/Editor/EscEditorConsts.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b0bed74c1b3b07d44b9ea1ea917809e4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Editor/RectUtility.cs b/src/Editor/RectUtility.cs index 97c99af..76d7825 100644 --- a/src/Editor/RectUtility.cs +++ b/src/Editor/RectUtility.cs @@ -29,6 +29,14 @@ namespace DCFApixels.DragonECS.Unity.Internal 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) { Rect t = rect; @@ -37,5 +45,19 @@ namespace DCFApixels.DragonECS.Unity.Internal b.yMin = b.yMax - height; 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; + } } } diff --git a/src/EntityTemplate/Editor/EntityTemplateEditor.cs b/src/EntityTemplate/Editor/EntityTemplateEditor.cs index 5b98b33..1eda107 100644 --- a/src/EntityTemplate/Editor/EntityTemplateEditor.cs +++ b/src/EntityTemplate/Editor/EntityTemplateEditor.cs @@ -10,8 +10,8 @@ namespace DCFApixels.DragonECS.Unity.Editors public abstract class EntityTemplateEditorBase : Editor { - private static readonly Rect RemoveButtonRect = new Rect(0f, 0f, 15f, 15f); - private static readonly Rect TooltipIconRect = 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, 21f, 15f); private GUIStyle removeButtonStyle; private GenericMenu genericMenu; @@ -96,14 +96,15 @@ namespace DCFApixels.DragonECS.Unity.Editors Init(); SerializedProperty componentsProp = serializedObject.FindProperty(target.ComponentsPropertyName); if (componentsProp == null) + { return; + } DrawTop(target); GUILayout.BeginVertical(EcsEditor.GetStyle(Color.black, 0.2f)); for (int i = 0; i < componentsProp.arraySize; i++) { DrawComponentData(componentsProp.GetArrayElementAtIndex(i), i); - GUILayout.Space(EditorGUIUtility.standardVerticalSpacing * 2); } GUILayout.EndVertical(); DrawFooter(target); @@ -140,72 +141,70 @@ namespace DCFApixels.DragonECS.Unity.Editors return; } - Type initializerType; Type componentType; SerializedProperty componentProperty = componentRefProp; ComponentTemplateBase customInitializer = componentProperty.managedReferenceValue as ComponentTemplateBase; if (customInitializer != null) { componentProperty = componentRefProp.FindPropertyRelative("component"); - initializerType = customInitializer.Type; componentType = customInitializer.GetType().GetField("component", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FieldType; } else { - initializerType = componentProperty.managedReferenceValue.GetType(); - componentType = initializerType; + componentType = componentProperty.managedReferenceValue.GetType(); ; } string name = template.Name; string description = template.Description; - Color panelColor = template.Color; - - GUILayout.BeginHorizontal(); - - GUILayout.BeginVertical(EcsEditor.GetStyle(panelColor, 0.22f)); + Color panelColor = template.Color.Desaturate(EscEditorConsts.COMPONENT_DRAWER_DESATURATE); EditorGUI.BeginChangeCheck(); + 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 { - EditorGUILayout.PropertyField(componentProperty, label, true); + EditorGUI.PropertyField(propertyRect, componentProperty, label, true); } - if (EditorGUI.EndChangeCheck()) - { - 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)) + if (GUI.Button(removeButtonRect, "x")) { OnRemoveComponentAt(index); } + #endregion if (!string.IsNullOrEmpty(description)) { Rect tooltipIconRect = TooltipIconRect; - tooltipIconRect.center = new Vector2(lastrect.xMax - removeButtonRect.width / 2f, lastrect.yMin + removeButtonRect.height / 2f); - GUIContent descriptionLabel = new GUIContent(EcsUnityConsts.INFO_MARK, description); + tooltipIconRect.center = new Vector2(propertyRect.xMax - removeButtonRect.width / 2f, propertyRect.yMin + removeButtonRect.height / 2f); + GUIContent descriptionLabel = new GUIContent("( i )", description); GUI.Label(tooltipIconRect, descriptionLabel, EditorStyles.boldLabel); } - GUILayout.EndHorizontal(); + + if (EditorGUI.EndChangeCheck()) + { + componentProperty.serializedObject.ApplyModifiedProperties(); + componentProperty.serializedObject.SetIsDifferentCacheDirty(); + } } private void DrawDamagedComponent(SerializedProperty componentRefProp, int index)