Skip to content

Commit 0e3a3c6

Browse files
committed
More compact items, safer refresh
1 parent 5aba23e commit 0e3a3c6

File tree

6 files changed

+69
-21
lines changed

6 files changed

+69
-21
lines changed

Editor/EcsLiteHierarchyWindow.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ private static void Open() {
1414
GetWindow<EcsLiteHierarchyWindow>("ecsLite Hierarchy").Show();
1515
}
1616

17+
private static readonly Color32 _selectedColor = new Color32(44, 93, 135, 255);
18+
private static readonly Color32 _colorInterval1 = new Color32(56, 56, 56, 255);
19+
private static readonly Color32 _colorInterval2 = new Color32(88, 88, 88, 255);
20+
1721
private DropdownField _worldMenu;
1822
private Toolbar _toolbar;
1923
private ToolbarSearchField _searchField;
@@ -146,7 +150,7 @@ private void UpdateHierarchy() {
146150
child.Q<Label>("name").text = mutation.entity.GetPrettyName();
147151
break;
148152
case WorldDebugView.ChangeType.Del:
149-
child.Q<Label>("name").text = null;
153+
child.Q<Label>("name").text = "-";
150154
break;
151155
default:
152156
throw new ArgumentOutOfRangeException();
@@ -158,7 +162,7 @@ private VisualElement CreateEmptyRow(int id, WorldDebugView view) {
158162
Button row = new Button();
159163
VisualElement left = new VisualElement();
160164
Label index = new Label(id.ToString());
161-
Label name = new Label(null) {
165+
Label name = new Label("-") {
162166
name = "name"
163167
};
164168

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

170-
int tmpId = id;
171-
WorldDebugView.DebugEntity GetEntity() => view.GetEntities()[tmpId];
174+
WorldDebugView.DebugEntity GetEntity(int id) => view.GetEntities()[id];
172175
row.clicked += () =>
173176
{
174-
EcsLiteInspectorWindow.SetEntity(view, GetEntity());
177+
int id = (int)row.userData;
178+
EcsLiteInspectorWindow.SetEntity(view, GetEntity(id));
179+
FilterEntities();
175180
};
176181

177182
left.style.minWidth = new StyleLength(new Length(30, LengthUnit.Pixel));
178183
left.style.backgroundColor = new StyleColor(new Color32(45, 45, 45, 255));
179-
180-
index.style.SetPadding(new StyleLength(new Length(2, LengthUnit.Pixel)));
181184

185+
row.name = "entity-row";
186+
row.userData = id;
187+
182188
row.style.flexDirection = new StyleEnum<FlexDirection>(FlexDirection.Row);
183189
row.style.flexWrap = new StyleEnum<Wrap>(Wrap.NoWrap);
184190
row.style.SetPadding(0);
185191
row.style.SetMargin(0);
186192
row.style.SetBorderRadius(0);
193+
row.style.SetBorderWidth(0);
187194
row.style.backgroundImage = null;
188195

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

211+
bool hasSelected = EcsLiteInspectorWindow.TryGetEntity(out int entityId);
212+
204213
foreach (VisualElement child in _hierarchy.Children()) {
205214
Label name = child.ElementAt(1) as Label;
206215
string text = name.text.ToLower();
@@ -213,11 +222,18 @@ private void FilterEntities() {
213222
child.style.height = show ? StyleKeyword.Auto : 0;
214223
child.style.display = new StyleEnum<DisplayStyle>(show ? DisplayStyle.Flex : DisplayStyle.None);
215224

216-
if (show) {
217-
Color color = i % 2 == 0 ? new Color32(56, 56, 56, 255) : new Color32(88, 88, 88, 255);
218-
child.style.backgroundColor = new StyleColor(color);
219-
i++;
225+
if (!show) {
226+
continue;
220227
}
228+
229+
int id = (int)child.userData;
230+
Color color = hasSelected && id == entityId
231+
? _selectedColor
232+
: i % 2 == 0
233+
? _colorInterval1
234+
: _colorInterval2;
235+
child.style.backgroundColor = new StyleColor(color);
236+
i++;
221237
}
222238

223239
_hierarchy.MarkDirtyRepaint();

Editor/EcsLiteInspectorWindow.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public static void Open() {
2727
private int _ticks;
2828
private UQueryBuilder<ComponentGUI> _childQuery;
2929

30+
public static bool TryGetEntity(out int id) {
31+
id = -1;
32+
bool valid = _instance && _instance.IsEntityValid(out _, out id);
33+
return valid;
34+
}
35+
3036
public static void SetEntity(WorldDebugView view, WorldDebugView.DebugEntity entity, bool ignoreInstance = false) {
3137
_view = view;
3238
_entity = view != null ? entity.world.PackEntityWithWorld(entity.id) : null;
@@ -173,8 +179,6 @@ private bool IsEntityValid(out EcsWorld world, out int entity) {
173179
return !(_entity == null || !_entity.Value.Unpack(out world, out entity) || entity == -1);
174180
}
175181

176-
private Vector3 position;
177-
178182
public static void Update() {
179183
if (_instance) {
180184
_instance.CustomUpdate();

Editor/UIElementsExtensions.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using UnityEngine.UIElements;
1+
using UnityEngine;
2+
using UnityEngine.UIElements;
23

34
namespace Nomnom.EcsLiteDebugger.Editor {
45
internal static class UIElementsExtensions {
@@ -22,5 +23,19 @@ internal static void SetBorderRadius(this IStyle style, StyleLength length) {
2223
style.borderTopLeftRadius = length;
2324
style.borderTopRightRadius = length;
2425
}
26+
27+
internal static void SetBorderWidth(this IStyle style, StyleFloat length) {
28+
style.borderBottomWidth = length;
29+
style.borderTopWidth = length;
30+
style.borderLeftWidth = length;
31+
style.borderRightWidth = length;
32+
}
33+
34+
internal static void SetBorderColor(this IStyle style, Color color) {
35+
style.borderBottomColor = color;
36+
style.borderTopColor = color;
37+
style.borderLeftColor = color;
38+
style.borderRightColor = color;
39+
}
2540
}
2641
}

Runtime/WorldDebugSystem.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Generic;
22
using Leopotam.EcsLite;
3-
using UnityEngine;
43

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

1110
public WorldDebugSystem(string name) {
11+
#if UNITY_EDITOR
1212
_name = name;
1313
_dirtyEntities = new List<(int, WorldDebugView.ChangeType)>();
1414
_view = new WorldDebugView();
15+
#endif
1516
}
1617

1718
public void PreInit(EcsSystems systems) {
19+
#if UNITY_EDITOR
1820
EcsWorld world = systems.GetWorld();
1921
world.AddEventListener(this);
2022

@@ -26,9 +28,11 @@ public void PreInit(EcsSystems systems) {
2628
for (int i = 0; i < entityCount; i++) {
2729
OnEntityCreated(entities[i]);
2830
}
31+
#endif
2932
}
3033

3134
public void Run(EcsSystems systems) {
35+
#if UNITY_EDITOR
3236
if (_dirtyEntities.Count <= 0 || _view == null) {
3337
return;
3438
}
@@ -40,31 +44,43 @@ public void Run(EcsSystems systems) {
4044
_view.IsDirty = true;
4145

4246
_dirtyEntities.Clear();
47+
#endif
4348
}
4449

4550
public void Destroy(EcsSystems systems) {
51+
#if UNITY_EDITOR
4652
_view?.Destroy();
53+
#endif
4754
}
4855

4956
public void OnEntityCreated(int entity) {
57+
#if UNITY_EDITOR
5058
_dirtyEntities.Add((entity, WorldDebugView.ChangeType.New));
59+
#endif
5160
}
5261

5362
public void OnEntityChanged(int entity) {
63+
#if UNITY_EDITOR
5464
_dirtyEntities.Add((entity, WorldDebugView.ChangeType.Modified));
65+
#endif
5566
}
5667

5768
public void OnEntityDestroyed(int entity) {
69+
#if UNITY_EDITOR
5870
_dirtyEntities.Add((entity, WorldDebugView.ChangeType.Del));
71+
#endif
5972
}
6073

6174
public void OnFilterCreated(EcsFilter filter) { }
6275

6376
public void OnWorldResized(int newSize) {
77+
#if UNITY_EDITOR
6478
_view.Repaint();
79+
#endif
6580
}
6681

6782
public void OnWorldDestroyed(EcsWorld world) {
83+
#if UNITY_EDITOR
6884
world.RemoveEventListener(this);
6985

7086
if (_view == null) {
@@ -75,6 +91,7 @@ public void OnWorldDestroyed(EcsWorld world) {
7591
_view.Repaint();
7692
_view.Destroy();
7793
_view = null;
94+
#endif
7895
}
7996
}
8097
}

Runtime/WorldDebugView.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using Leopotam.EcsLite;
5-
using UnityEngine;
65

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

5150
public void CreateDummyList() {
5251
for (int i = 0; i < _world.GetAllocatedEntitiesCount(); i++) {
53-
_mutations.Add(new Mutation {
54-
entity = _entities[i],
55-
changeType = ChangeType.New
56-
});
52+
UpdateEntity(i, ChangeType.New);
5753
}
5854

5955
IsDirty = true;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.nomnom.ecslite-debugger",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"displayName": "ecsLite Debugger",
55
"description": "Provides a hierarchy and inspector window to view world entities, and mutate components.",
66
"unity": "2021.3",

0 commit comments

Comments
 (0)