Skip to content

Commit d415590

Browse files
committed
perf: Refactor that removes the LDtkEntityDrawerComponent, and simplifies the data delivery for all of the drawn scene window handles.
1 parent 42d5cb1 commit d415590

31 files changed

+285
-679
lines changed

Assets/LDtkUnity/Editor/Builders/LDtkBuilderEntity.cs

Lines changed: 2 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using UnityEditor;
2-
using UnityEngine;
1+
using UnityEngine;
32

43
namespace LDtkUnity.Editor
54
{
@@ -67,8 +66,7 @@ private void BuildEntityInstance()
6766

6867
private void PopulateEntityComponent()
6968
{
70-
Vector2 size = ((Vector2)_entity.UnityPxSize / Project.PixelsPerUnit);
71-
69+
Vector2 size = (Vector2)_entity.UnityPxSize / Project.PixelsPerUnit;
7270
_entityComponent.OnImport(Importer.DefinitionObjects, _entity, LayerComponent, _fieldsComponent, _iidComponent, size);
7371
}
7472

@@ -96,10 +94,6 @@ private void AddFieldData()
9694
_fieldsComponent = fieldsFactory.FieldsComponent;
9795
LDtkProfiler.EndSample();
9896

99-
LDtkProfiler.BeginSample("AddHandleDrawers");
100-
AddHandleDrawers(_entityObj, _entity, Layer.GridSize);
101-
LDtkProfiler.EndSample();
102-
10397
LDtkProfiler.BeginSample("InterfaceEvents");
10498
InterfaceEvents();
10599
LDtkProfiler.EndSample();
@@ -144,115 +138,6 @@ private void PositionEntity()
144138

145139
_entityObj.transform.localPosition = localPos;
146140
}
147-
148-
/// <summary>
149-
/// Only doing this for importer performance. an early return to not build the rest
150-
/// </summary>
151-
private static bool DrawerEligibility(FieldInstance field)
152-
{
153-
EditorDisplayMode? mode = field.Definition.EditorDisplayMode;
154-
155-
switch (mode)
156-
{
157-
case EditorDisplayMode.Hidden: //do not show
158-
return false;
159-
160-
case EditorDisplayMode.ValueOnly: //all but point/point array
161-
return !field.IsPoint;
162-
163-
case EditorDisplayMode.NameAndValue: //all
164-
return true;
165-
166-
case EditorDisplayMode.EntityTile: //enum/enum array, tile/tile array
167-
return field.IsEnum || field.IsTile;
168-
169-
case EditorDisplayMode.RadiusGrid: //int, float
170-
case EditorDisplayMode.RadiusPx: //int, float
171-
return field.IsInt || field.IsFloat;
172-
173-
case EditorDisplayMode.PointStar: //point, point array
174-
case EditorDisplayMode.Points: //point, point array
175-
return field.IsPoint;
176-
177-
case EditorDisplayMode.PointPath: //point array only
178-
case EditorDisplayMode.PointPathLoop: //point array only
179-
return field.IsPoint && field.Definition.IsArray;
180-
181-
case EditorDisplayMode.ArrayCountNoLabel: //any arrays
182-
case EditorDisplayMode.ArrayCountWithLabel: //any arrays
183-
return field.Definition.IsArray;
184-
185-
case EditorDisplayMode.RefLinkBetweenCenters: //entity ref, entity ref array
186-
case EditorDisplayMode.RefLinkBetweenPivots: //entity ref, entity ref array
187-
return field.IsEntityRef;
188-
189-
default:
190-
LDtkDebug.LogError("No Drawer eligibility found!");
191-
return false;
192-
}
193-
}
194-
195-
private void AddHandleDrawers(GameObject gameObject, EntityInstance entityInstance, int gridSize)
196-
{
197-
LDtkEntityDrawerComponent drawerComponent = gameObject.gameObject.AddComponent<LDtkEntityDrawerComponent>();
198-
EntityDefinition entityDef = entityInstance.Definition;
199-
200-
string entityPath = GetEntityImageAndRect(entityInstance, Project.assetPath, out Rect entityIconRect);
201-
Vector2 size = (Vector2)entityInstance.UnityPxSize / Project.PixelsPerUnit;
202-
203-
Color smartColor = entityInstance.UnitySmartColor;
204-
205-
//entity handle data
206-
LDtkEntityDrawerData entityDrawerData = new LDtkEntityDrawerData(drawerComponent.transform, entityDef, entityPath, entityIconRect, size, smartColor);
207-
drawerComponent.AddEntityDrawer(entityDrawerData);
208-
209-
foreach (FieldInstance fieldInstance in entityInstance.FieldInstances)
210-
{
211-
if (!DrawerEligibility(fieldInstance))
212-
{
213-
continue;
214-
}
215-
216-
EditorDisplayMode displayMode = fieldInstance.Definition.EditorDisplayMode;
217-
Vector2 pivotOffset = LDtkCoordConverter.EntityPivotOffset(entityDef.UnityPivot, size);
218-
Vector3 middleCenter = gameObject.transform.position + (Vector3)pivotOffset;
219-
220-
LDtkFieldDrawerData data = new LDtkFieldDrawerData(_fieldsComponent, smartColor, displayMode, fieldInstance.Identifier, gridSize, Project.PixelsPerUnit, middleCenter);
221-
drawerComponent.AddReference(data);
222-
}
223-
}
224-
225-
//this would be used instead in the entity drawer for getting the texture that way
226-
private string GetEntityImageAndRect(EntityInstance entityInstance, string assetPath, out Rect rect)
227-
{
228-
rect = new Rect();
229-
230-
TilesetRectangle tile = entityInstance.Tile;
231-
if (tile == null)
232-
{
233-
return null;
234-
}
235-
236-
//todo while awaiting this fix, safely null check it https://github.com/deepnight/ldtk/issues/1107
237-
TilesetDefinition tileset = tile.Tileset;
238-
if (tileset == null)
239-
{
240-
return null;
241-
}
242-
243-
LDtkRelativeGetterTilesetTexture textureGetter = new LDtkRelativeGetterTilesetTexture();
244-
Texture2D tex = textureGetter.GetRelativeAsset(tileset, assetPath);
245-
if (tex == null)
246-
{
247-
return null;
248-
}
249-
250-
Rect src = tile.UnityRect;
251-
rect = LDtkCoordConverter.ImageSlice(src, tex.height);
252-
253-
string texPath = AssetDatabase.GetAssetPath(tex);
254-
return texPath;
255-
}
256141

257142
public PointParseData GetParsedPointData()
258143
{

Assets/LDtkUnity/Editor/CustomEditor/LDtkEntityDrawerComponentEditor.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

Assets/LDtkUnity/Editor/CustomEditor/LDtkEntityDrawerComponentEditor.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Assets/LDtkUnity/Editor/CustomEditor/SceneDrawer/ComponentDrawers/LDtkSceneDrawerEntity.cs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,69 @@
11
using System;
2-
using UnityEditor;
2+
using System.Linq;
33
using UnityEngine;
44

55
namespace LDtkUnity.Editor
66
{
7+
/// <summary>
8+
/// Draws entity stuff, no field stuff
9+
/// </summary>
710
internal static class LDtkSceneDrawerEntity
811
{
9-
public static ILDtkHandleDrawer DrawEntity(LDtkEntityDrawerData entity)
12+
public static void DrawEntityDrawer(LDtkComponentEntity entityComponent)
1013
{
1114
Vector2 offset = Vector2.zero;
1215

13-
if (entity.Transform == null)
16+
if (!entityComponent.Def)
1417
{
15-
return null;
18+
return;
1619
}
1720

18-
switch (entity.EntityMode)
21+
switch (entityComponent.Def.RenderMode)
1922
{
2023
case RenderMode.Cross:
2124
case RenderMode.Ellipse:
2225
case RenderMode.Rectangle:
2326
case RenderMode.Tile:
24-
LDtkEntityDrawerShapes.Data shapeData = new LDtkEntityDrawerShapes.Data()
25-
{
26-
EntityMode = entity.EntityMode,
27-
FillOpacity = entity.FillOpacity,
28-
LineOpacity = entity.LineOpacity,
29-
Hollow = entity.Hollow,
30-
Pivot = entity.Pivot,
31-
Size = entity.Size
32-
};
33-
LDtkEntityDrawerShapes entityDrawer = new LDtkEntityDrawerShapes(entity.Transform, shapeData);
27+
LDtkEntityDrawerShapes entityDrawer = new LDtkEntityDrawerShapes(entityComponent);
3428
entityDrawer.OnDrawHandles();
3529
break;
3630
default:
3731
throw new ArgumentOutOfRangeException();
3832
}
3933

40-
offset = TryDrawTile(entity, offset);
41-
TryDrawName(entity, offset);
34+
offset = TryDrawTile(entityComponent, offset);
35+
TryDrawName(entityComponent, offset);
4236

43-
return null;
37+
return;
4438
}
4539

46-
private static void TryDrawName(LDtkEntityDrawerData entity, Vector2 offset)
40+
private static void TryDrawName(LDtkComponentEntity entity, Vector2 offset)
4741
{
48-
if (entity.ShowName && LDtkPrefs.ShowEntityIdentifier)
42+
if (entity.Def.ShowName && LDtkPrefs.ShowEntityIdentifier)
4943
{
50-
HandleUtil.DrawText(entity.Identifier, entity.Transform.position, entity.SmartColor, offset);
44+
HandleUtil.DrawText(entity.Identifier, entity.transform.position, entity.SmartColor, offset);
5145
}
5246
}
5347

54-
private static Vector2 TryDrawTile(LDtkEntityDrawerData entity, Vector2 offset)
48+
private static Vector2 TryDrawTile(LDtkComponentEntity entityComponent, Vector2 offset)
5549
{
56-
if (!entity.DrawTile)
50+
bool entityDrawsTile = entityComponent.Def.RenderMode == RenderMode.Tile;
51+
bool fieldDrawsTile = entityComponent.Def.FieldDefs.Any(field => field.EditorDisplayMode == EditorDisplayMode.EntityTile);
52+
53+
bool drawsTile = entityDrawsTile || fieldDrawsTile;
54+
55+
if (!drawsTile)
5756
{
5857
return offset;
5958
}
6059

61-
Texture2D tex = AssetDatabase.LoadAssetAtPath<Texture2D>(entity.TexPath);
62-
if (tex == null)
60+
Sprite tile = entityComponent.Tile;
61+
if (!tile)
6362
{
6463
return offset;
6564
}
6665

67-
LDtkEntityDrawerIcon iconDrawer = new LDtkEntityDrawerIcon(entity.Transform, tex, entity.TexRect);
66+
LDtkEntityDrawerIcon iconDrawer = new LDtkEntityDrawerIcon(entityComponent.transform, tile.texture, tile.rect);
6867

6968
iconDrawer.PrecalculateValues();
7069
offset += iconDrawer.OffsetToNextUI;

Assets/LDtkUnity/Editor/CustomEditor/SceneDrawer/ComponentDrawers/LDtkSceneDrawerField.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

Assets/LDtkUnity/Editor/CustomEditor/SceneDrawer/ComponentDrawers/LDtkSceneDrawerField.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Assets/LDtkUnity/Editor/CustomEditor/SceneDrawer/HandleDrawers/LDtkEntityDrawerIcon.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace LDtkUnity.Editor
55
{
6-
internal sealed class LDtkEntityDrawerIcon : ILDtkHandleDrawer
6+
internal sealed class LDtkEntityDrawerIcon
77
{
88
private readonly Transform _transform;
99
private readonly Texture _tex;
@@ -94,5 +94,24 @@ public void OnDrawHandles()
9494

9595
Handles.EndGUI();
9696
}
97+
98+
//todo incorporate this eventually to simplify the code
99+
public static void DrawSprite(Rect position, Sprite sprite)
100+
{
101+
if (!sprite || !sprite.texture)
102+
{
103+
Debug.LogWarning("Sprite or its texture is null.");
104+
return;
105+
}
106+
107+
Rect texCoords = new Rect(
108+
sprite.textureRect.x / sprite.texture.width,
109+
sprite.textureRect.y / sprite.texture.height,
110+
sprite.textureRect.width / sprite.texture.width,
111+
sprite.textureRect.height / sprite.texture.height
112+
);
113+
114+
GUI.DrawTextureWithTexCoords(position, sprite.texture, texCoords);
115+
}
97116
}
98117
}

0 commit comments

Comments
 (0)