From 6ad3668114e46d3135b7016273ab5b830c339b13 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 13 May 2024 19:20:07 +0800 Subject: [PATCH] update entity bar display --- .../Editor/EcsEntityConnectEditor.cs | 2 +- src/Debug/Editor/EntlongDrawer.cs | 3 +- .../Monitors/Editor/EntityMonitorEditor.cs | 2 +- src/Internal/Editor/EcsGUI.cs | 172 ++++++++++++------ src/Internal/Editor/UnityEditorUtility.cs | 13 +- 5 files changed, 134 insertions(+), 58 deletions(-) diff --git a/src/Connectors/Editor/EcsEntityConnectEditor.cs b/src/Connectors/Editor/EcsEntityConnectEditor.cs index 61c3345..2cb3568 100644 --- a/src/Connectors/Editor/EcsEntityConnectEditor.cs +++ b/src/Connectors/Editor/EcsEntityConnectEditor.cs @@ -42,7 +42,7 @@ namespace DCFApixels.DragonECS.Unity.Editors { bool isConnected = Target.Entity.TryUnpackForUnityEditor(out int id, out short gen, out short worldID, out EcsWorld world); EcsGUI.EntityStatus status = IsMultipleTargets ? EcsGUI.EntityStatus.Undefined : isConnected ? EcsGUI.EntityStatus.Alive : EcsGUI.EntityStatus.NotAlive; - EcsGUI.Layout.EntityBar(status, id, gen, worldID); + EcsGUI.Layout.EntityBarForAlive(status, id, gen, worldID); } private void DrawTemplates() diff --git a/src/Debug/Editor/EntlongDrawer.cs b/src/Debug/Editor/EntlongDrawer.cs index efa49e8..be8fff2 100644 --- a/src/Debug/Editor/EntlongDrawer.cs +++ b/src/Debug/Editor/EntlongDrawer.cs @@ -18,7 +18,8 @@ namespace DCFApixels.DragonECS.Unity.Editors var (labelRect, barRect) = RectUtility.HorizontalSliceLeft(position, EditorGUIUtility.labelWidth * 0.65f); EditorGUI.LabelField(labelRect, label); - EcsGUI.EntityBar(barRect, EcsGUI.EntityStatus.Alive, slotInfo.id, slotInfo.gen, slotInfo.world); + bool isAlive = EcsWorld.GetWorld(slotInfo.world).IsAlive(slotInfo.id, slotInfo.gen); + EcsGUI.EntityBar(barRect, false, isAlive ? EcsGUI.EntityStatus.Alive : EcsGUI.EntityStatus.NotAlive, slotInfo.id, slotInfo.gen, slotInfo.world); } } } diff --git a/src/Debug/Monitors/Editor/EntityMonitorEditor.cs b/src/Debug/Monitors/Editor/EntityMonitorEditor.cs index 8db5274..c7a9195 100644 --- a/src/Debug/Monitors/Editor/EntityMonitorEditor.cs +++ b/src/Debug/Monitors/Editor/EntityMonitorEditor.cs @@ -12,7 +12,7 @@ namespace DCFApixels.DragonECS.Unity.Editors public override void OnInspectorGUI() { bool isAlive = Target.Entity.TryUnpackForUnityEditor(out int id, out short gen, out short worldID, out EcsWorld world); - EcsGUI.Layout.EntityBar(isAlive ? EcsGUI.EntityStatus.Alive : EcsGUI.EntityStatus.NotAlive, id, gen, worldID); + EcsGUI.Layout.EntityBarForAlive(isAlive ? EcsGUI.EntityStatus.Alive : EcsGUI.EntityStatus.NotAlive, id, gen, worldID); EcsGUI.Layout.DrawRuntimeComponents(Target.Entity, false); } } diff --git a/src/Internal/Editor/EcsGUI.cs b/src/Internal/Editor/EcsGUI.cs index 5fb0cc6..c7e7ef4 100644 --- a/src/Internal/Editor/EcsGUI.cs +++ b/src/Internal/Editor/EcsGUI.cs @@ -9,33 +9,47 @@ namespace DCFApixels.DragonECS.Unity.Editors { internal static class EcsGUI { + #region Scores + public struct LabelWidthScore : IDisposable + { + private readonly float _value; + public LabelWidthScore(float value) + { + _value = EditorGUIUtility.labelWidth; + EditorGUIUtility.labelWidth = value; + } + public void Dispose() + { + EditorGUIUtility.labelWidth = _value; + } + } public struct ColorScope : IDisposable { - private readonly Color _oldColor; - public ColorScope(Color color) + private readonly Color _value; + public ColorScope(Color value) { - _oldColor = GUI.color; - GUI.color = color; + _value = GUI.color; + GUI.color = value; } public void Dispose() { - GUI.color = _oldColor; + GUI.color = _value; } } - public struct ContentColorScope : IDisposable { - private readonly Color _oldColor; - public ContentColorScope(Color color) + private readonly Color _value; + public ContentColorScope(Color value) { - _oldColor = GUI.contentColor; - GUI.contentColor = color; + _value = GUI.contentColor; + GUI.contentColor = value; } public void Dispose() { - GUI.contentColor = _oldColor; + GUI.contentColor = _value; } } + #endregion internal readonly static Color GrayColor = new Color32(100, 100, 100, 255); internal readonly static Color GreenColor = new Color32(75, 255, 0, 255); @@ -149,51 +163,82 @@ namespace DCFApixels.DragonECS.Unity.Editors { return IconButton(position, Icons.Instance.CloseIcon, 0f, "Delete Entity"); } - public static void EntityBar(Rect position, EntityStatus status, int id, short gen, short world) + + + public static void EntityBarForAlive(Rect position, EntityStatus status, int id, short gen, short world) { - var (entityInfoRect, statusRect) = RectUtility.VerticalSliceBottom(position, 3f); - - Color w = Color.gray; - w.a = 0.6f; - Color b = Color.black; - b.a = 0.55f; - EditorGUI.DrawRect(entityInfoRect, w); - - var (idRect, genWorldRect) = RectUtility.HorizontalSliceLerp(entityInfoRect, 0.4f); - var (genRect, worldRect) = RectUtility.HorizontalSliceLerp(genWorldRect, 0.5f); - - idRect = RectUtility.AddPadding(idRect, 2, 1, 0, 0); - genRect = RectUtility.AddPadding(genRect, 1, 1, 0, 0); - worldRect = RectUtility.AddPadding(worldRect, 1, 2, 0, 0); - EditorGUI.DrawRect(idRect, b); - EditorGUI.DrawRect(genRect, b); - EditorGUI.DrawRect(worldRect, b); - - - GUIStyle style = new GUIStyle(EditorStyles.numberField); - style.alignment = TextAnchor.MiddleCenter; - style.font = EditorStyles.boldFont; - if (status == EntityStatus.Alive) + EntityBar(position, status != EntityStatus.Alive, status, id, gen, world); + } + public static void EntityBar(Rect position, int id, short gen, short world) + { + EntityBar_Internal(position, false, id, gen, world); + } + public static void EntityBar(Rect position) + { + EntityBar_Internal(position, true); + } + public static void EntityBar(Rect position, bool isPlaceholder, EntityStatus status, int id = 0, short gen = 0, short world = 0) + { + using (new LabelWidthScore(0f)) { - Color statusColor = EcsGUI.GreenColor; - statusColor.a = 0.6f; - EditorGUI.DrawRect(statusRect, statusColor); + var (entityInfoRect, statusRect) = RectUtility.VerticalSliceBottom(position, 3f); - EditorGUI.IntField(idRect, id, style); - EditorGUI.IntField(genRect, gen, style); - EditorGUI.IntField(worldRect, world, style); - } - else - { - Color statusColor = status == EntityStatus.Undefined ? new Color32(200, 200, 200, 255) : EcsGUI.RedColor; - statusColor.a = 0.6f; - EditorGUI.DrawRect(statusRect, statusColor); - - using (new EditorGUI.DisabledScope(true)) + Color statusColor; + switch (status) { - GUI.Label(idRect, "Entity ID", style); - GUI.Label(genRect, "Generation", style); - GUI.Label(worldRect, "World ID", style); + case EntityStatus.NotAlive: + statusColor = EcsGUI.RedColor; + break; + case EntityStatus.Alive: + statusColor = EcsGUI.GreenColor; + break; + default: + statusColor = new Color32(200, 200, 200, 255); + break; + } + + statusColor.a = 0.6f; + EditorGUI.DrawRect(statusRect, statusColor); + + EntityBar_Internal(entityInfoRect, isPlaceholder, id, gen, world); + } + } + private static void EntityBar_Internal(Rect position, bool isPlaceHolder, int id = 0, short gen = 0, short world = 0) + { + using (new LabelWidthScore(0f)) + { + Color w = Color.gray; + w.a = 0.6f; + Color b = Color.black; + b.a = 0.55f; + EditorGUI.DrawRect(position, w); + + var (idRect, genWorldRect) = RectUtility.HorizontalSliceLerp(position, 0.4f); + var (genRect, worldRect) = RectUtility.HorizontalSliceLerp(genWorldRect, 0.5f); + + idRect = RectUtility.AddPadding(idRect, 2, 1, 0, 0); + genRect = RectUtility.AddPadding(genRect, 1, 1, 0, 0); + worldRect = RectUtility.AddPadding(worldRect, 1, 2, 0, 0); + EditorGUI.DrawRect(idRect, b); + EditorGUI.DrawRect(genRect, b); + EditorGUI.DrawRect(worldRect, b); + + GUIStyle style = UnityEditorUtility.GetInputFieldCenterAnhor(); + + if (isPlaceHolder) + { + using (new EditorGUI.DisabledScope(true)) + { + GUI.Label(idRect, "Entity ID", style); + GUI.Label(genRect, "Generation", style); + GUI.Label(worldRect, "World ID", style); + } + } + else + { + EditorGUI.IntField(idRect, id, style); + EditorGUI.IntField(genRect, gen, style); + EditorGUI.IntField(worldRect, world, style); } } } @@ -235,12 +280,31 @@ namespace DCFApixels.DragonECS.Unity.Editors EditorGUILayout.IntField("Leaked Entites", leakedEntitesCount, EditorStyles.boldLabel); } } - public static void EntityBar(EntityStatus status, int id, short gen, short world) + public static void EntityBarForAlive(EntityStatus status, int id, short gen, short world) { float width = EditorGUIUtility.currentViewWidth; float height = EntityBarHeight; - EcsGUI.EntityBar(GUILayoutUtility.GetRect(width, height), status, id, gen, world); + EcsGUI.EntityBarForAlive(GUILayoutUtility.GetRect(width, height), status, id, gen, world); } + public static void EntityBar(EntityStatus status, bool isPlaceholder, int id, short gen, short world) + { + float width = EditorGUIUtility.currentViewWidth; + float height = EntityBarHeight; + EcsGUI.EntityBar(GUILayoutUtility.GetRect(width, height), isPlaceholder, status, id, gen, world); + } + public static void EntityBar(int id, short gen, short world) + { + float width = EditorGUIUtility.currentViewWidth; + float height = EntityBarHeight; + EcsGUI.EntityBar(GUILayoutUtility.GetRect(width, height), id, gen, world); + } + public static void EntityBar() + { + float width = EditorGUIUtility.currentViewWidth; + float height = EntityBarHeight; + EcsGUI.EntityBar(GUILayoutUtility.GetRect(width, height)); + } + public static bool AddComponentButtons() { return EcsGUI.AddComponentButtons(GUILayoutUtility.GetRect(EditorGUIUtility.currentViewWidth, 36f)); diff --git a/src/Internal/Editor/UnityEditorUtility.cs b/src/Internal/Editor/UnityEditorUtility.cs index da3175a..57ebb3d 100644 --- a/src/Internal/Editor/UnityEditorUtility.cs +++ b/src/Internal/Editor/UnityEditorUtility.cs @@ -112,9 +112,20 @@ namespace DCFApixels.DragonECS.Unity.Editors private static SparseArray colorBoxeStyles = new SparseArray(); private static GUIContent _singletonIconContent = null; private static GUIContent _singletonContent = null; - + private static GUIStyle _inputFieldCenterAnhor = null; #region Label + public static GUIStyle GetInputFieldCenterAnhor() + { + if (_inputFieldCenterAnhor == null) + { + GUIStyle style = new GUIStyle(EditorStyles.numberField); + style.alignment = TextAnchor.MiddleCenter; + style.font = EditorStyles.boldFont; + _inputFieldCenterAnhor = style; + } + return _inputFieldCenterAnhor; + } public static GUIContent GetLabelTemp() { if (_singletonContent == null)