Skip to content

Commit e94dc53

Browse files
authored
Group Layer Tree Matching and Linked Cel Texture Extraction (#28)
* Track previous layer groups by level to preserve actual group hierarchy * Add check for linked cels in AsepriteCel.ExtractCel extension
1 parent 793ff80 commit e94dc53

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

source/AsepriteDotNet/Aseprite/Types/AsepriteCel.Extensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public static Texture ExtractCel(this AsepriteCel cel, string? name = null)
4040
return tilemapCel.ExtractCel(name);
4141
}
4242

43+
if (cel is AsepriteLinkedCel linkedCel)
44+
{
45+
return linkedCel.Cel.ExtractCel(name);
46+
}
47+
4348
throw new InvalidOperationException("This cel does not contain pixel data");
4449
}
4550

source/AsepriteDotNet/IO/AsepriteFileLoader.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,10 @@ private static AsepriteFile LoadFile(string fileName, AsepriteBinaryReader reade
219219
// means of seeing why some data may not be what they expect it to be.
220220
List<string> warnings = new List<string>();
221221

222-
// Reference to the last group layer that was read so that subsequent child layers can be added to it.
223-
AsepriteGroupLayer? lastGroupLayer = null;
222+
// References to the most recent previous group layers read by child level.
223+
// Key: Child level of the group layer
224+
// Value: Group layer
225+
Dictionary<int, AsepriteGroupLayer> lastGroupsByChildLevel = new();
224226

225227
// Flag to determine if the palette has been read. This is used to flag that a user data chunk is for the
226228
// sprite due to changes in Aseprite 1.3
@@ -332,14 +334,14 @@ private static AsepriteFile LoadFile(string fileName, AsepriteBinaryReader reade
332334
throw new InvalidOperationException($"Unknown layer type: {properties.Type}");
333335
}
334336

335-
if (properties.Level != 0 && lastGroupLayer is not null)
337+
if (properties.Level != 0 && lastGroupsByChildLevel.TryGetValue(properties.Level - 1, out var group))
336338
{
337-
lastGroupLayer.AddChild(layer);
339+
group.AddChild(layer);
338340
}
339341

340342
if (layer is AsepriteGroupLayer groupLayer)
341343
{
342-
lastGroupLayer = groupLayer;
344+
lastGroupsByChildLevel[groupLayer.ChildLevel] = groupLayer;
343345
}
344346

345347
currentUserData = layer.UserData;

0 commit comments

Comments
 (0)