From 4918a7276070408d15c5f2546d304681aabb224a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 14 Sep 2022 11:04:51 +0200 Subject: [PATCH] fixed some issues with the new data texture atlas parser --- MLEM.Data/DataTextureAtlas.cs | 48 +++++++++++++++++++++------------- Tests/DataTextureAtlasTests.cs | 7 ++++- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/MLEM.Data/DataTextureAtlas.cs b/MLEM.Data/DataTextureAtlas.cs index fc801f3..deb12d8 100644 --- a/MLEM.Data/DataTextureAtlas.cs +++ b/MLEM.Data/DataTextureAtlas.cs @@ -143,24 +143,8 @@ namespace MLEM.Data { i += 2; break; default: - // if we have a location for the previous regions, they're valid so we add them - if (location != Rectangle.Empty && namesOffsets.Count > 0) { - location.Offset(offset.ToPoint()); - pivot += offset; - if (!pivotRelative) - pivot -= location.Location.ToVector2(); - - foreach (var (name, off) in namesOffsets) { - var region = new TextureRegion(texture, location.OffsetCopy(off.ToPoint())) { - PivotPixels = pivot + off, - Name = name - }; - foreach (var kv in customData) - region.SetData(kv.Key, kv.Value); - atlas.regions.Add(name, region); - } - namesOffsets.Clear(); - } + // if we have data for the previous regions, they're valid so we add them + AddCurrentRegions(); // we're starting a new region (or adding another name for a new region), so clear old data namesOffsets.Add((word.Trim(), Vector2.Zero)); @@ -175,7 +159,35 @@ namespace MLEM.Data { } } + // add the last region that was started on + AddCurrentRegions(); return atlas; + + void AddCurrentRegions() { + // the location is the only mandatory instruction, which is why we check it here + if (location == Rectangle.Empty || namesOffsets.Count <= 0) + return; + + location.Offset(offset.ToPoint()); + if (pivot != Vector2.Zero) { + pivot += offset; + if (!pivotRelative) + pivot -= location.Location.ToVector2(); + } + + foreach (var (name, off) in namesOffsets) { + var region = new TextureRegion(texture, location.OffsetCopy(off.ToPoint())) { + PivotPixels = pivot + off, + Name = name + }; + foreach (var kv in customData) + region.SetData(kv.Key, kv.Value); + atlas.regions.Add(name, region); + } + + // we only clear names offsets if the location was valid, otherwise we ignore multiple names for a region + namesOffsets.Clear(); + } } } diff --git a/Tests/DataTextureAtlasTests.cs b/Tests/DataTextureAtlasTests.cs index 35ed699..2c0bd23 100644 --- a/Tests/DataTextureAtlasTests.cs +++ b/Tests/DataTextureAtlasTests.cs @@ -15,13 +15,18 @@ namespace Tests { var atlas = DataTextureAtlas.LoadAtlasData(new TextureRegion(texture), game.RawContent, "Texture.atlas"); Assert.AreEqual(11, atlas.Regions.Count()); + // no pivot + var plant = atlas["Plant"]; + Assert.AreEqual(plant.Area, new Rectangle(96, 0, 16, 32)); + Assert.AreEqual(plant.PivotPixels, Vector2.Zero); + // no added offset var table = atlas["LongTableUp"]; Assert.AreEqual(table.Area, new Rectangle(0, 32, 64, 48)); Assert.AreEqual(table.PivotPixels, new Vector2(16, 48 - 32)); // added offset - var table2 = atlas["LongTableLeft"]; + var table2 = atlas["LongTableDown"]; Assert.AreEqual(table2.Area, new Rectangle(64, 32, 64, 48)); Assert.AreEqual(table2.PivotPixels, new Vector2(112 - 64, 48 - 32));