Skip to content

Commit 19a60ef

Browse files
committed
chore: iterated upon the sorting order PR with enhanced UI and architecture
1 parent 6e96315 commit 19a60ef

14 files changed

+207
-83
lines changed

Assets/LDtkUnity/Editor/Builders/LDtkBuilderLevel.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
22
using UnityEngine;
33

44
namespace LDtkUnity.Editor
@@ -104,8 +104,7 @@ private void BuildLevelProcess()
104104
LDtkProfiler.EndSample();
105105

106106
LDtkProfiler.BeginSample("LDtkSortingOrder");
107-
var sortingOrders = _project.LayerSortingOrders.ToDictionary(x => x.Layer, x => x.Order);
108-
_sortingOrder = new LDtkSortingOrder(sortingOrders);
107+
CreateSortingOrder();
109108
LDtkProfiler.EndSample();
110109

111110
LDtkProfiler.BeginSample("BuildLayerInstances");
@@ -133,6 +132,24 @@ private void BuildLevelProcess()
133132
LDtkProfiler.EndSample();
134133
}
135134

135+
private void CreateSortingOrder()
136+
{
137+
var orders = _project.LayerCustomSortingOrders;
138+
139+
if (!_project.UseLayerCustomSortingOrders || orders.IsNullOrEmpty())
140+
{
141+
_sortingOrder = new LDtkSortingOrder();
142+
return;
143+
}
144+
145+
var sortingOrders = new Dictionary<string, int>(orders.Length);
146+
foreach (LDtkLayerCustomSortingOrder order in orders)
147+
{
148+
sortingOrders.Add(order._ldtkLayerName, order._ldtkLayerOrder);
149+
}
150+
_sortingOrder = new LDtkSortingOrder(sortingOrders);
151+
}
152+
136153
private void BuildFields()
137154
{
138155
bool addedFields = TryAddFields();
Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11
using System.Collections.Generic;
2-
using UnityEngine;
32

43
namespace LDtkUnity.Editor
54
{
5+
/// <summary>
6+
/// Increments for each occurrence of a tilemap or entity when building layers to ensure everything is layered correctly.
7+
/// This can be overridden to jump to custom-set sorting order numbers if needed.
8+
/// </summary>
69
internal sealed class LDtkSortingOrder
710
{
811
public int SortingOrderValue { get; private set; } = 0;
9-
private readonly Dictionary<string, int> _overrides;
10-
private int _autoValue = 0;
12+
private readonly Dictionary<string, int> _layerNameOverrides;
13+
private readonly HashSet<string> _layerNameOccurrences;
1114

12-
public LDtkSortingOrder(Dictionary<string, int> overrides = null)
15+
public LDtkSortingOrder(Dictionary<string, int> layerNameOverrides = null)
1316
{
14-
_overrides = overrides;
15-
}
17+
_layerNameOverrides = layerNameOverrides;
1618

19+
if (layerNameOverrides != null)
20+
{
21+
_layerNameOccurrences = new HashSet<string>(layerNameOverrides.Count);
22+
}
23+
}
24+
1725
public void Next(string layerIdentifier = null)
1826
{
19-
if (layerIdentifier != null && _overrides != null && _overrides.TryGetValue(layerIdentifier, out int order))
27+
//note: this supports repeated layer occurrences, even if that never happens.
28+
29+
//Jump to the custom order if we get the first occurrence of a layer
30+
if (layerIdentifier != null && _layerNameOccurrences != null && _layerNameOccurrences.Add(layerIdentifier))
2031
{
21-
SortingOrderValue = order;
22-
Debug.Log(order);
23-
return;
32+
if (_layerNameOverrides != null && _layerNameOverrides.TryGetValue(layerIdentifier, out int order))
33+
{
34+
SortingOrderValue = order;
35+
return;
36+
}
2437
}
2538

26-
SortingOrderValue = --_autoValue;
39+
SortingOrderValue--;
2740
}
2841
}
2942
}

Assets/LDtkUnity/Editor/CustomEditor/Importer/LDtkProjectImporterEditor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal sealed class LDtkProjectImporterEditor : LDtkImporterEditor
1717
private LDtkSectionIntGrids _sectionIntGrids;
1818
private LDtkSectionEntities _sectionEntities;
1919
private LDtkSectionEnums _sectionEnums;
20+
private LDtkSectionLayerCustomSortingOrder _sectionCustomSortingOrder;
2021
private bool _shouldApply = true;
2122

2223

@@ -41,13 +42,15 @@ public override void OnEnable()
4142
_sectionIntGrids = new LDtkSectionIntGrids(this, serializedObject);
4243
_sectionEntities = new LDtkSectionEntities(this, serializedObject);
4344
_sectionEnums = new LDtkSectionEnums(this, serializedObject);
45+
_sectionCustomSortingOrder = new LDtkSectionLayerCustomSortingOrder(this, serializedObject);
4446

4547
_sectionDrawers = new[]
4648
{
4749
(ILDtkSectionDrawer)_sectionMain,
4850
_sectionIntGrids,
4951
_sectionEntities,
5052
_sectionEnums,
53+
_sectionCustomSortingOrder,
5154
SectionDependencies
5255
};
5356

@@ -175,6 +178,10 @@ private void ShowGUI()
175178
_sectionEnums.Draw(defs.Enums);
176179
LDtkProfiler.EndSample();
177180

181+
LDtkProfiler.BeginSample("CustomSortingOrderSection");
182+
_sectionCustomSortingOrder.Draw(defs.Layers);
183+
LDtkProfiler.EndSample();
184+
178185
LDtkProfiler.BeginSample("DependenciesSection");
179186
SectionDependencies.Draw();
180187
LDtkProfiler.EndSample();

Assets/LDtkUnity/Editor/CustomEditor/SectionDrawers/LDtkSectionDataDrawer.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ private void AddMissingData(string[] newAssetKeys)
157157
SerializedProperty[] elements = ArrayProp.GetArrayElements();
158158
string[] elementKeys = elements.Select(GetKeyForElement).ToArray();
159159

160-
foreach (string newAssetKey in newAssetKeys)
160+
for (var i = 0; i < newAssetKeys.Length; i++)
161161
{
162+
string newAssetKey = newAssetKeys[i];
162163
if (elementKeys.Contains(newAssetKey))
163164
{
164165
//we previously already have it, don't add one
@@ -173,12 +174,17 @@ private void AddMissingData(string[] newAssetKeys)
173174
insertedKeyProp.stringValue = newAssetKey;
174175

175176
SerializedProperty insertedValueProp = GetValuePropForElement(insertedProp);
176-
insertedValueProp.objectReferenceValue = null;
177+
SetDefaultElementValue(insertedValueProp, i);
177178

178179
//Debug.Log($"Inserted new asset at {0} for key {newAssetKey}");
179180
}
180181
}
181-
182+
183+
protected virtual void SetDefaultElementValue(SerializedProperty insertedValueProp, int i)
184+
{
185+
insertedValueProp.objectReferenceValue = null;
186+
}
187+
182188
private void BubbleSortArray(string[] assetKeys)
183189
{
184190
if (ArrayProp.arraySize != assetKeys.Length)
@@ -251,11 +257,11 @@ private SerializedProperty GetKeyPropForArray(int arrayIndex)
251257
SerializedProperty element = ArrayProp.GetArrayElementAtIndex(arrayIndex);
252258
return GetKeyPropForElement(element);
253259
}
254-
private SerializedProperty GetKeyPropForElement(SerializedProperty element)
260+
protected virtual SerializedProperty GetKeyPropForElement(SerializedProperty element)
255261
{
256262
return element.FindPropertyRelative(LDtkAsset<Object>.PROPERTY_KEY);
257263
}
258-
private SerializedProperty GetValuePropForElement(SerializedProperty element)
264+
protected virtual SerializedProperty GetValuePropForElement(SerializedProperty element)
259265
{
260266
return element.FindPropertyRelative(LDtkAsset<Object>.PROPERTY_ASSET);
261267
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Collections.Generic;
2+
using UnityEditor;
3+
using UnityEngine;
4+
5+
namespace LDtkUnity.Editor
6+
{
7+
internal sealed class LDtkSectionLayerCustomSortingOrder : LDtkSectionDataDrawer<LayerDefinition>
8+
{
9+
private SerializedProperty _useProp;
10+
private static GUIContent _usePropLabel = new GUIContent("Use Custom Sorting Orders");
11+
12+
protected override string PropertyName => LDtkProjectImporter.LAYER_SORTING_ORDERS;
13+
protected override string GuiText => "Custom Sorting Orders";
14+
15+
protected override string GuiTooltip =>
16+
"Normally in the import process, the sorting order is decremented for all occurrences of LDtk layers, starting from 0.\n" +
17+
"However, this option can be toggled on to customize sorting orders for specific layers.";
18+
19+
protected override Texture GuiImage => LDtkIconUtility.LoadLayerIcon();
20+
protected override string ReferenceLink => LDtkHelpURL.SECTION_CUSTOM_SORTING_ORDER;
21+
22+
public LDtkSectionLayerCustomSortingOrder(LDtkImporterEditor editor, SerializedObject serializedObject) : base(editor, serializedObject)
23+
{
24+
}
25+
26+
public override void Init()
27+
{
28+
base.Init();
29+
_useProp = SerializedObject.FindProperty(LDtkProjectImporter.USE_LAYER_SORTING_ORDERS);
30+
}
31+
32+
protected override void GetDrawers(LayerDefinition[] defs, List<LDtkContentDrawer<LayerDefinition>> drawers)
33+
{
34+
for (var i = 0; i < defs.Length; i++)
35+
{
36+
LDtkDrawerLayerCustomSortingOrder drawer =
37+
new LDtkDrawerLayerCustomSortingOrder(defs[i], ArrayProp.GetArrayElementAtIndex(i));
38+
39+
drawers.Add(drawer);
40+
}
41+
}
42+
43+
protected override void DrawDropdownContent()
44+
{
45+
EditorGUILayout.PropertyField(_useProp, _usePropLabel);
46+
using (new EditorGUI.DisabledScope(!_useProp.boolValue))
47+
{
48+
base.DrawDropdownContent();
49+
}
50+
}
51+
52+
protected override SerializedProperty GetKeyPropForElement(SerializedProperty element)
53+
{
54+
return element.FindPropertyRelative(LDtkLayerCustomSortingOrder.NAME);
55+
}
56+
57+
protected override SerializedProperty GetValuePropForElement(SerializedProperty element)
58+
{
59+
return element.FindPropertyRelative(LDtkLayerCustomSortingOrder.ORDER);
60+
}
61+
62+
protected override void SetDefaultElementValue(SerializedProperty insertedValueProp, int i)
63+
{
64+
insertedValueProp.intValue = i * -100;
65+
}
66+
}
67+
}

Assets/LDtkUnity/Editor/CustomEditor/SectionDrawers/LDtkSectionLayerCustomSortingOrder.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/LDtkUnity/Editor/CustomEditor/SectionDrawers/LDtkSectionMain.cs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ internal sealed class LDtkSectionMain : LDtkSectionDrawer
5757
text = "Scale Entities",
5858
tooltip = "Apply a scale factor to entity prefab instances if they were resized in LDtk."
5959
};
60-
private static readonly GUIContent CustomSortingOrders = new GUIContent
61-
{
62-
text = "Custom Sorting Orders",
63-
tooltip = "Enable this to customize the sorting order for specific layers. If disabled, layers will automatically decrement starting from 0."
64-
};
6560

6661
protected override string GuiText => "Main";
6762
protected override string GuiTooltip => "This is the importer menu.\n" +
@@ -113,55 +108,9 @@ protected override void DrawDropdownContent()
113108
DrawField(UseParallax, LDtkProjectImporter.USE_PARALLAX);
114109
DrawField(ScaleEntities, LDtkProjectImporter.SCALE_ENTITIES);
115110

116-
DrawLayerSortingOrders();
117-
118111
Editor.DrawDependenciesProperty();
119112
}
120113

121-
private void DrawLayerSortingOrders()
122-
{
123-
SerializedProperty customProp = DrawField(CustomSortingOrders, LDtkProjectImporter.CUSTOM_SORTING_ORDERS);
124-
125-
if (!customProp.boolValue)
126-
{
127-
return;
128-
}
129-
130-
SerializedProperty sortingOrderArray = SerializedObject.FindProperty(LDtkProjectImporter.LAYER_SORTING_ORDERS);
131-
132-
// Sync array with data
133-
LayerDefinition[] layers = _data.Defs.Layers;
134-
if (sortingOrderArray.arraySize != layers.Length)
135-
{
136-
sortingOrderArray.arraySize = layers.Length;
137-
}
138-
139-
for (int i = 0; i < layers.Length; i++)
140-
{
141-
SerializedProperty element = sortingOrderArray.GetArrayElementAtIndex(i);
142-
SerializedProperty layerNameProp = element.FindPropertyRelative("Layer");
143-
SerializedProperty orderProp = element.FindPropertyRelative("Order");
144-
145-
string layerName = layers[i].Identifier;
146-
147-
// If name is different or empty (newly created), set it and default order
148-
if (layerNameProp.stringValue != layerName)
149-
{
150-
layerNameProp.stringValue = layerName;
151-
// Default auto generated value: -1, -2, etc. (starts at 0 and decrements)
152-
orderProp.intValue = -(i + 1);
153-
}
154-
155-
EditorGUILayout.BeginHorizontal();
156-
using (new EditorGUI.DisabledScope(true))
157-
{
158-
EditorGUILayout.TextField(layerNameProp.stringValue);
159-
}
160-
EditorGUILayout.PropertyField(orderProp, GUIContent.none);
161-
EditorGUILayout.EndHorizontal();
162-
}
163-
}
164-
165114
private void PixelsPerUnitField()
166115
{
167116
GUIContent content = new GUIContent(PixelsPerUnit)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace LDtkUnity.Editor
5+
{
6+
internal sealed class LDtkDrawerLayerCustomSortingOrder : LDtkContentDrawer<LayerDefinition>
7+
{
8+
private SerializedProperty _propMain;
9+
private SerializedProperty _propLayerName;
10+
private SerializedProperty _propOrderValue;
11+
12+
private GUIContent _content;
13+
14+
public LDtkDrawerLayerCustomSortingOrder(LayerDefinition data, SerializedProperty property) : base(data)
15+
{
16+
_propMain = property;
17+
_propLayerName = _propMain.FindPropertyRelative(LDtkLayerCustomSortingOrder.NAME);
18+
_propOrderValue = _propMain.FindPropertyRelative(LDtkLayerCustomSortingOrder.ORDER);
19+
20+
_content = new GUIContent(_propLayerName.stringValue, LDtkIconUtility.GetIconForLayerDefinition(data));
21+
}
22+
23+
public override void Draw()
24+
{
25+
_propLayerName.stringValue = _data.Identifier;
26+
_propOrderValue.intValue = EditorGUILayout.IntField(_content, _propOrderValue.intValue);
27+
}
28+
}
29+
}

Assets/LDtkUnity/Editor/CustomEditor/SectionElementDrawers/LDtkDrawerLayerCustomSortingOrder.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace LDtkUnity.Editor
4+
{
5+
[Serializable]
6+
public struct LDtkLayerCustomSortingOrder
7+
{
8+
public const string NAME = nameof(_ldtkLayerName);
9+
public const string ORDER = nameof(_ldtkLayerOrder);
10+
11+
public string _ldtkLayerName;
12+
public int _ldtkLayerOrder;
13+
}
14+
}

0 commit comments

Comments
 (0)