Skip to content

Commit

Permalink
More compact items, safer refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
nomnomab committed Sep 12, 2022
1 parent 5aba23e commit 0e3a3c6
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 21 deletions.
38 changes: 27 additions & 11 deletions Editor/EcsLiteHierarchyWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ private static void Open() {
GetWindow<EcsLiteHierarchyWindow>("ecsLite Hierarchy").Show();
}

private static readonly Color32 _selectedColor = new Color32(44, 93, 135, 255);
private static readonly Color32 _colorInterval1 = new Color32(56, 56, 56, 255);
private static readonly Color32 _colorInterval2 = new Color32(88, 88, 88, 255);

private DropdownField _worldMenu;
private Toolbar _toolbar;
private ToolbarSearchField _searchField;
Expand Down Expand Up @@ -146,7 +150,7 @@ private void UpdateHierarchy() {
child.Q<Label>("name").text = mutation.entity.GetPrettyName();
break;
case WorldDebugView.ChangeType.Del:
child.Q<Label>("name").text = null;
child.Q<Label>("name").text = "-";
break;
default:
throw new ArgumentOutOfRangeException();
Expand All @@ -158,7 +162,7 @@ private VisualElement CreateEmptyRow(int id, WorldDebugView view) {
Button row = new Button();
VisualElement left = new VisualElement();
Label index = new Label(id.ToString());
Label name = new Label(null) {
Label name = new Label("-") {
name = "name"
};

Expand All @@ -167,23 +171,26 @@ private VisualElement CreateEmptyRow(int id, WorldDebugView view) {
row.Add(left);
row.Add(name);

int tmpId = id;
WorldDebugView.DebugEntity GetEntity() => view.GetEntities()[tmpId];
WorldDebugView.DebugEntity GetEntity(int id) => view.GetEntities()[id];
row.clicked += () =>
{
EcsLiteInspectorWindow.SetEntity(view, GetEntity());
int id = (int)row.userData;
EcsLiteInspectorWindow.SetEntity(view, GetEntity(id));
FilterEntities();
};

left.style.minWidth = new StyleLength(new Length(30, LengthUnit.Pixel));
left.style.backgroundColor = new StyleColor(new Color32(45, 45, 45, 255));

index.style.SetPadding(new StyleLength(new Length(2, LengthUnit.Pixel)));

row.name = "entity-row";
row.userData = id;

row.style.flexDirection = new StyleEnum<FlexDirection>(FlexDirection.Row);
row.style.flexWrap = new StyleEnum<Wrap>(Wrap.NoWrap);
row.style.SetPadding(0);
row.style.SetMargin(0);
row.style.SetBorderRadius(0);
row.style.SetBorderWidth(0);
row.style.backgroundImage = null;

name.style.flexWrap = new StyleEnum<Wrap>(Wrap.NoWrap);
Expand All @@ -201,6 +208,8 @@ private void FilterEntities() {
string filter = _searchField.value.ToLower();
int i = 0;

bool hasSelected = EcsLiteInspectorWindow.TryGetEntity(out int entityId);

foreach (VisualElement child in _hierarchy.Children()) {
Label name = child.ElementAt(1) as Label;
string text = name.text.ToLower();
Expand All @@ -213,11 +222,18 @@ private void FilterEntities() {
child.style.height = show ? StyleKeyword.Auto : 0;
child.style.display = new StyleEnum<DisplayStyle>(show ? DisplayStyle.Flex : DisplayStyle.None);

if (show) {
Color color = i % 2 == 0 ? new Color32(56, 56, 56, 255) : new Color32(88, 88, 88, 255);
child.style.backgroundColor = new StyleColor(color);
i++;
if (!show) {
continue;
}

int id = (int)child.userData;
Color color = hasSelected && id == entityId
? _selectedColor
: i % 2 == 0
? _colorInterval1
: _colorInterval2;
child.style.backgroundColor = new StyleColor(color);
i++;
}

_hierarchy.MarkDirtyRepaint();
Expand Down
8 changes: 6 additions & 2 deletions Editor/EcsLiteInspectorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public static void Open() {
private int _ticks;
private UQueryBuilder<ComponentGUI> _childQuery;

public static bool TryGetEntity(out int id) {
id = -1;
bool valid = _instance && _instance.IsEntityValid(out _, out id);
return valid;
}

public static void SetEntity(WorldDebugView view, WorldDebugView.DebugEntity entity, bool ignoreInstance = false) {
_view = view;
_entity = view != null ? entity.world.PackEntityWithWorld(entity.id) : null;
Expand Down Expand Up @@ -173,8 +179,6 @@ private bool IsEntityValid(out EcsWorld world, out int entity) {
return !(_entity == null || !_entity.Value.Unpack(out world, out entity) || entity == -1);
}

private Vector3 position;

public static void Update() {
if (_instance) {
_instance.CustomUpdate();
Expand Down
17 changes: 16 additions & 1 deletion Editor/UIElementsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace Nomnom.EcsLiteDebugger.Editor {
internal static class UIElementsExtensions {
Expand All @@ -22,5 +23,19 @@ internal static void SetBorderRadius(this IStyle style, StyleLength length) {
style.borderTopLeftRadius = length;
style.borderTopRightRadius = length;
}

internal static void SetBorderWidth(this IStyle style, StyleFloat length) {
style.borderBottomWidth = length;
style.borderTopWidth = length;
style.borderLeftWidth = length;
style.borderRightWidth = length;
}

internal static void SetBorderColor(this IStyle style, Color color) {
style.borderBottomColor = color;
style.borderTopColor = color;
style.borderLeftColor = color;
style.borderRightColor = color;
}
}
}
19 changes: 18 additions & 1 deletion Runtime/WorldDebugSystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using Leopotam.EcsLite;
using UnityEngine;

namespace Nomnom.EcsLiteDebugger {
public class WorldDebugSystem: IEcsPreInitSystem, IEcsRunSystem, IEcsDestroySystem, IEcsWorldEventListener {
Expand All @@ -9,12 +8,15 @@ public class WorldDebugSystem: IEcsPreInitSystem, IEcsRunSystem, IEcsDestroySyst
private List<(int, WorldDebugView.ChangeType)> _dirtyEntities;

public WorldDebugSystem(string name) {
#if UNITY_EDITOR
_name = name;
_dirtyEntities = new List<(int, WorldDebugView.ChangeType)>();
_view = new WorldDebugView();
#endif
}

public void PreInit(EcsSystems systems) {
#if UNITY_EDITOR
EcsWorld world = systems.GetWorld();
world.AddEventListener(this);

Expand All @@ -26,9 +28,11 @@ public void PreInit(EcsSystems systems) {
for (int i = 0; i < entityCount; i++) {
OnEntityCreated(entities[i]);
}
#endif
}

public void Run(EcsSystems systems) {
#if UNITY_EDITOR
if (_dirtyEntities.Count <= 0 || _view == null) {
return;
}
Expand All @@ -40,31 +44,43 @@ public void Run(EcsSystems systems) {
_view.IsDirty = true;

_dirtyEntities.Clear();
#endif
}

public void Destroy(EcsSystems systems) {
#if UNITY_EDITOR
_view?.Destroy();
#endif
}

public void OnEntityCreated(int entity) {
#if UNITY_EDITOR
_dirtyEntities.Add((entity, WorldDebugView.ChangeType.New));
#endif
}

public void OnEntityChanged(int entity) {
#if UNITY_EDITOR
_dirtyEntities.Add((entity, WorldDebugView.ChangeType.Modified));
#endif
}

public void OnEntityDestroyed(int entity) {
#if UNITY_EDITOR
_dirtyEntities.Add((entity, WorldDebugView.ChangeType.Del));
#endif
}

public void OnFilterCreated(EcsFilter filter) { }

public void OnWorldResized(int newSize) {
#if UNITY_EDITOR
_view.Repaint();
#endif
}

public void OnWorldDestroyed(EcsWorld world) {
#if UNITY_EDITOR
world.RemoveEventListener(this);

if (_view == null) {
Expand All @@ -75,6 +91,7 @@ public void OnWorldDestroyed(EcsWorld world) {
_view.Repaint();
_view.Destroy();
_view = null;
#endif
}
}
}
6 changes: 1 addition & 5 deletions Runtime/WorldDebugView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using Leopotam.EcsLite;
using UnityEngine;

namespace Nomnom.EcsLiteDebugger {
public class WorldDebugView {
Expand Down Expand Up @@ -50,10 +49,7 @@ public void UpdateEntity(int id, ChangeType changeType) {

public void CreateDummyList() {
for (int i = 0; i < _world.GetAllocatedEntitiesCount(); i++) {
_mutations.Add(new Mutation {
entity = _entities[i],
changeType = ChangeType.New
});
UpdateEntity(i, ChangeType.New);
}

IsDirty = true;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.nomnom.ecslite-debugger",
"version": "1.0.0",
"version": "1.0.1",
"displayName": "ecsLite Debugger",
"description": "Provides a hierarchy and inspector window to view world entities, and mutate components.",
"unity": "2021.3",
Expand Down

0 comments on commit 0e3a3c6

Please sign in to comment.