com.artromskiy.deltaecs.unity 0.0.53 → 0.0.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,14 @@
1
1
  using System;
2
2
  using System.Collections.Generic;
3
+
3
4
  using Delta.ECS;
4
- using UnityEditor;
5
- using UnityEditor.SceneManagement;
5
+
6
6
  using Unity.Hierarchy;
7
7
  using Unity.Hierarchy.Editor;
8
+
9
+ using UnityEditor;
10
+ using UnityEditor.SceneManagement;
11
+
8
12
  using UnityEngine;
9
13
  using UnityEngine.SceneManagement;
10
14
  using UnityEngine.UIElements;
@@ -14,10 +18,11 @@ namespace Delta.ECS.Unity.Editor
14
18
  [InitializeOnLoad]
15
19
  internal sealed class HierarchyNodeHandler : HierarchyNodeTypeHandler
16
20
  {
17
- private static readonly ComponentId[] NoRequiredComponents = Array.Empty<ComponentId>();
18
21
  private const double RuntimeHierarchyRefreshInterval = 0.5d;
22
+ private static readonly ComponentId[] NoRequiredComponents = Array.Empty<ComponentId>();
19
23
  private static double _nextRuntimeHierarchyRefresh;
20
24
  private static bool _wasPlaying;
25
+ private static readonly HashSet<HierarchyNodeHandler> Instances = new();
21
26
 
22
27
  private readonly Dictionary<EntityKey, EntityNode> _entityNodes = new();
23
28
  private readonly HashSet<EntityKey> _activeEntityKeys = new();
@@ -26,7 +31,6 @@ namespace Delta.ECS.Unity.Editor
26
31
  private readonly HashSet<HierarchyView> _boundViews = new();
27
32
  private readonly Dictionary<HierarchyView, BlockedDrag> _blockedDrags = new();
28
33
  private readonly Dictionary<SceneAuthoring, Action<SceneDataChange>> _sceneDataSubscriptions = new();
29
- private static readonly HashSet<HierarchyNodeHandler> Instances = new();
30
34
 
31
35
  static HierarchyNodeHandler()
32
36
  {
@@ -35,53 +39,6 @@ namespace Delta.ECS.Unity.Editor
35
39
  EditorApplication.update += UpdateRuntimeHierarchy;
36
40
  }
37
41
 
38
- private static void UpdateRuntimeHierarchy()
39
- {
40
- if (EditorApplication.isPlaying)
41
- {
42
- _wasPlaying = true;
43
- if (EditorApplication.timeSinceStartup < _nextRuntimeHierarchyRefresh)
44
- {
45
- return;
46
- }
47
-
48
- _nextRuntimeHierarchyRefresh = EditorApplication.timeSinceStartup + RuntimeHierarchyRefreshInterval;
49
- RefreshAll();
50
- return;
51
- }
52
-
53
- if (_wasPlaying && !EditorApplication.isPlayingOrWillChangePlaymode)
54
- {
55
- _wasPlaying = false;
56
- RefreshAll();
57
- }
58
- }
59
-
60
- private static void BindHierarchyView(HierarchyWindow window, HierarchyView view)
61
- {
62
- if (view == null)
63
- {
64
- return;
65
- }
66
-
67
- var handler =
68
- view.Source.GetOrCreateNodeTypeHandler<HierarchyNodeHandler>();
69
- handler.BindToView(view);
70
- }
71
-
72
- private static void BindExistingHierarchyViews()
73
- {
74
- var windows = Resources.FindObjectsOfTypeAll<HierarchyWindow>();
75
- for (var i = 0; i < windows.Length; i++)
76
- {
77
- var window = windows[i];
78
- if (window != null)
79
- {
80
- BindHierarchyView(window, window.View);
81
- }
82
- }
83
- }
84
-
85
42
  public override string GetNodeTypeName() => nameof(HierarchyNodeHandler);
86
43
 
87
44
  public override EntityId GetEntityIdFromNode(in HierarchyNode node) =>
@@ -178,6 +135,140 @@ namespace Delta.ECS.Unity.Editor
178
135
  item.Name.UnregisterValueChangedCallback(OnEntityNameChanged);
179
136
  }
180
137
 
138
+ protected override void OnUnbindView(HierarchyView view)
139
+ {
140
+ UnbindDragGuard(view);
141
+ view.FlagsChanged -= OnHierarchyFlagsChanged;
142
+ view.PopulateContextMenu -= PopulateContextMenu;
143
+ _boundViews.Remove(view);
144
+ HierarchySelection.Clear(view);
145
+ }
146
+
147
+ internal static void RefreshAll()
148
+ {
149
+ var changed = false;
150
+ foreach (var instance in Instances)
151
+ {
152
+ changed |= instance.Reconcile();
153
+ }
154
+
155
+ if (changed)
156
+ {
157
+ EditorApplication.RepaintHierarchyWindow();
158
+ }
159
+ }
160
+
161
+ internal static SceneAuthoring FindAuthoringForScene(Scene scene)
162
+ => SceneAuthoring.FindForScene(scene);
163
+
164
+ internal static void SelectEntity(HierarchyView view, SceneAuthoring authoring, string stableId)
165
+ {
166
+ var key = new EntityKey(authoring.GetEntityId(), stableId);
167
+ foreach (var instance in Instances)
168
+ {
169
+ if (instance._entityNodes.ContainsKey(key))
170
+ {
171
+ if (view != null)
172
+ {
173
+ if (instance._boundViews.Contains(view))
174
+ {
175
+ TrySelectEntity(instance, view, key);
176
+ }
177
+ }
178
+ else
179
+ {
180
+ foreach (var boundView in instance._boundViews)
181
+ {
182
+ if (TrySelectEntity(instance, boundView, key))
183
+ {
184
+ break;
185
+ }
186
+ }
187
+ }
188
+
189
+ return;
190
+ }
191
+ }
192
+ }
193
+
194
+ private static void UpdateRuntimeHierarchy()
195
+ {
196
+ if (EditorApplication.isPlaying)
197
+ {
198
+ _wasPlaying = true;
199
+ if (EditorApplication.timeSinceStartup < _nextRuntimeHierarchyRefresh)
200
+ {
201
+ return;
202
+ }
203
+
204
+ _nextRuntimeHierarchyRefresh = EditorApplication.timeSinceStartup + RuntimeHierarchyRefreshInterval;
205
+ RefreshAll();
206
+ return;
207
+ }
208
+
209
+ if (_wasPlaying && !EditorApplication.isPlayingOrWillChangePlaymode)
210
+ {
211
+ _wasPlaying = false;
212
+ RefreshAll();
213
+ }
214
+ }
215
+
216
+ private static void BindHierarchyView(HierarchyWindow window, HierarchyView view)
217
+ {
218
+ if (view == null)
219
+ {
220
+ return;
221
+ }
222
+
223
+ var handler =
224
+ view.Source.GetOrCreateNodeTypeHandler<HierarchyNodeHandler>();
225
+ handler.BindToView(view);
226
+ }
227
+
228
+ private static void BindExistingHierarchyViews()
229
+ {
230
+ var windows = Resources.FindObjectsOfTypeAll<HierarchyWindow>();
231
+ for (var i = 0; i < windows.Length; i++)
232
+ {
233
+ var window = windows[i];
234
+ if (window != null)
235
+ {
236
+ BindHierarchyView(window, window.View);
237
+ }
238
+ }
239
+ }
240
+
241
+ private static bool IsHierarchyScene(Scene scene) =>
242
+ scene.IsValid() && scene.isLoaded && !EditorSceneManager.IsPreviewScene(scene);
243
+
244
+ private static bool TrySelectEntity(
245
+ HierarchyNodeHandler handler,
246
+ HierarchyView view,
247
+ EntityKey key)
248
+ {
249
+ if (view == null || !handler._boundViews.Contains(view)
250
+ || !handler._entityNodes.TryGetValue(key, out var entityNode)
251
+ || !handler.Hierarchy.Exists(entityNode.Node))
252
+ {
253
+ return false;
254
+ }
255
+
256
+ if (view.UpdateNeeded)
257
+ {
258
+ view.Update();
259
+ }
260
+
261
+ var viewModel = view.ViewModel;
262
+ if (!viewModel.Contains(in entityNode.Node))
263
+ {
264
+ return false;
265
+ }
266
+
267
+ var node = entityNode.Node;
268
+ view.SetSelection(in node);
269
+ return true;
270
+ }
271
+
181
272
  private void BindToView(HierarchyView view) => OnBindView(view);
182
273
 
183
274
  private void OnEntityNameChanged(ChangeEvent<string> evt)
@@ -210,15 +301,6 @@ namespace Delta.ECS.Unity.Editor
210
301
  }
211
302
  }
212
303
 
213
- protected override void OnUnbindView(HierarchyView view)
214
- {
215
- UnbindDragGuard(view);
216
- view.FlagsChanged -= OnHierarchyFlagsChanged;
217
- view.PopulateContextMenu -= PopulateContextMenu;
218
- _boundViews.Remove(view);
219
- HierarchySelection.Clear(view);
220
- }
221
-
222
304
  private void BindDragGuard(HierarchyView view)
223
305
  {
224
306
  view.RegisterCallback<PointerDownEvent>(OnHierarchyPointerDown, TrickleDown.TrickleDown);
@@ -322,40 +404,6 @@ namespace Delta.ECS.Unity.Editor
322
404
  }
323
405
  }
324
406
 
325
- private struct BlockedDrag
326
- {
327
- public readonly int PointerId;
328
- public readonly Vector2 StartPosition;
329
- public bool ThresholdReached;
330
-
331
- public BlockedDrag(int pointerId, Vector2 startPosition)
332
- {
333
- PointerId = pointerId;
334
- StartPosition = startPosition;
335
- ThresholdReached = false;
336
- }
337
- }
338
-
339
- internal static void RefreshAll()
340
- {
341
- var changed = false;
342
- foreach (var instance in Instances)
343
- {
344
- changed |= instance.Reconcile();
345
- }
346
-
347
- if (changed)
348
- {
349
- EditorApplication.RepaintHierarchyWindow();
350
- }
351
- }
352
-
353
- internal static SceneAuthoring FindAuthoringForScene(Scene scene)
354
- => SceneAuthoring.FindForScene(scene);
355
-
356
- private static bool IsHierarchyScene(Scene scene) =>
357
- scene.IsValid() && scene.isLoaded && !EditorSceneManager.IsPreviewScene(scene);
358
-
359
407
  private bool Reconcile()
360
408
  {
361
409
  if (!Hierarchy.IsCreated)
@@ -715,64 +763,6 @@ namespace Delta.ECS.Unity.Editor
715
763
  return true;
716
764
  }
717
765
 
718
- internal static void SelectEntity(HierarchyView view, SceneAuthoring authoring, string stableId)
719
- {
720
- var key = new EntityKey(authoring.GetEntityId(), stableId);
721
- foreach (var instance in Instances)
722
- {
723
- if (instance._entityNodes.ContainsKey(key))
724
- {
725
- if (view != null)
726
- {
727
- if (instance._boundViews.Contains(view))
728
- {
729
- TrySelectEntity(instance, view, key);
730
- }
731
- }
732
- else
733
- {
734
- foreach (var boundView in instance._boundViews)
735
- {
736
- if (TrySelectEntity(instance, boundView, key))
737
- {
738
- break;
739
- }
740
- }
741
- }
742
-
743
- return;
744
- }
745
- }
746
- }
747
-
748
- private static bool TrySelectEntity(
749
- HierarchyNodeHandler handler,
750
- HierarchyView view,
751
- EntityKey key)
752
- {
753
- if (view == null || !handler._boundViews.Contains(view)
754
- || !handler._entityNodes.TryGetValue(key, out var entityNode)
755
- || !handler.Hierarchy.Exists(entityNode.Node))
756
- {
757
- return false;
758
- }
759
-
760
- if (view.UpdateNeeded)
761
- {
762
- view.Update();
763
- }
764
-
765
- var viewModel = view.ViewModel;
766
- if (!viewModel.Contains(in entityNode.Node))
767
- {
768
- return false;
769
- }
770
-
771
- var node = entityNode.Node;
772
- view.SetSelection(in node);
773
- return true;
774
- }
775
-
776
766
  private void PopulateContextMenu(HierarchyView view, HierarchyViewItem item, DropdownMenu menu)
777
767
  {
778
768
  if (EditorApplication.isPlayingOrWillChangePlaymode)
@@ -864,6 +854,20 @@ namespace Delta.ECS.Unity.Editor
864
854
  }
865
855
  }
866
856
 
857
+ private struct BlockedDrag
858
+ {
859
+ public readonly int PointerId;
860
+ public readonly Vector2 StartPosition;
861
+ public bool ThresholdReached;
862
+
863
+ public BlockedDrag(int pointerId, Vector2 startPosition)
864
+ {
865
+ PointerId = pointerId;
866
+ StartPosition = startPosition;
867
+ ThresholdReached = false;
868
+ }
869
+ }
870
+
867
871
  private readonly struct NodeReference
868
872
  {
869
873
  public readonly SceneAuthoring Authoring;
@@ -1,12 +1,20 @@
1
1
  using System;
2
+
2
3
  using Delta.ECS;
4
+
3
5
  using Unity.Hierarchy;
6
+
4
7
  using UnityEditor;
5
8
 
6
9
  namespace Delta.ECS.Unity.Editor
7
10
  {
8
11
  internal static class HierarchySelection
9
12
  {
13
+
14
+ private static HierarchyNodeHandler _handler;
15
+ private static HierarchyNode _node;
16
+ private static HierarchyView _view;
17
+ private static bool _selectionCheckScheduled;
10
18
  internal static EntityInspectorTarget Current { get; private set; }
11
19
  internal static SceneAuthoring Authoring => Current.Authoring;
12
20
  internal static string StableId => Current.StableId;
@@ -14,11 +22,6 @@ namespace Delta.ECS.Unity.Editor
14
22
  internal static Entity RuntimeEntity => Current.RuntimeEntity;
15
23
  internal static event Action Changed;
16
24
 
17
- private static HierarchyNodeHandler _handler;
18
- private static HierarchyNode _node;
19
- private static HierarchyView _view;
20
- private static bool _selectionCheckScheduled;
21
-
22
25
  internal static void SetFromHierarchy(
23
26
  HierarchyNodeHandler handler,
24
27
  HierarchyView view,
@@ -2,7 +2,9 @@ using System;
2
2
  using System.Collections.Generic;
3
3
  using System.Globalization;
4
4
  using System.Linq;
5
+
5
6
  using UnityEditor;
7
+
6
8
  using UnityEngine;
7
9
 
8
10
  namespace Delta.ECS.Unity.Editor
@@ -13,13 +15,6 @@ namespace Delta.ECS.Unity.Editor
13
15
  private const string LegacyOrderPreferencePrefix = "DeltaEcs.Inspector.ComponentOrder.";
14
16
  private const string LegacyCollapsePreferencePrefix = "DeltaEcs.Inspector.ComponentCollapsed.";
15
17
 
16
- [Serializable]
17
- private struct ComponentFoldout
18
- {
19
- public string Schema;
20
- public bool Expanded;
21
- }
22
-
23
18
  [SerializeField] private List<string> _componentOrder = new();
24
19
  [SerializeField] private List<ComponentFoldout> _componentFoldouts = new();
25
20
 
@@ -148,6 +143,14 @@ namespace Delta.ECS.Unity.Editor
148
143
  Save(true);
149
144
  }
150
145
 
146
+ private static string GetLegacyCollapseKey(ulong schema) =>
147
+ $"{LegacyCollapsePreferencePrefix}{Application.dataPath}:{FormatSchema(schema)}";
148
+
149
+ private static string FormatSchema(ulong schema) => schema.ToString("X16", CultureInfo.InvariantCulture);
150
+
151
+ private static bool TryParseSchema(string value, out ulong schema) =>
152
+ ulong.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out schema);
153
+
151
154
  private int FindFoldout(ulong schema)
152
155
  {
153
156
  string schemaText = FormatSchema(schema);
@@ -209,12 +212,11 @@ namespace Delta.ECS.Unity.Editor
209
212
  }
210
213
  }
211
214
 
212
- private static string GetLegacyCollapseKey(ulong schema) =>
213
- $"{LegacyCollapsePreferencePrefix}{Application.dataPath}:{FormatSchema(schema)}";
214
-
215
- private static string FormatSchema(ulong schema) => schema.ToString("X16", CultureInfo.InvariantCulture);
216
-
217
- private static bool TryParseSchema(string value, out ulong schema) =>
218
- ulong.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out schema);
215
+ [Serializable]
216
+ private struct ComponentFoldout
217
+ {
218
+ public string Schema;
219
+ public bool Expanded;
220
+ }
219
221
  }
220
222
  }