From 740c65a88735b3945e97848b91b9d925bf138413 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 14 Sep 2022 11:20:55 +0200 Subject: [PATCH] fixed new cpy instruction yielding incorrect pivots --- MLEM.Data/DataTextureAtlas.cs | 22 ++++++++++++---------- Tests/DataTextureAtlasTests.cs | 6 +++--- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/MLEM.Data/DataTextureAtlas.cs b/MLEM.Data/DataTextureAtlas.cs index deb12d8..a43a052 100644 --- a/MLEM.Data/DataTextureAtlas.cs +++ b/MLEM.Data/DataTextureAtlas.cs @@ -167,24 +167,26 @@ namespace MLEM.Data { // the location is the only mandatory instruction, which is why we check it here if (location == Rectangle.Empty || namesOffsets.Count <= 0) return; + foreach (var (name, addedOff) in namesOffsets) { + var loc = location; + var piv = pivot; + var off = offset + addedOff; - location.Offset(offset.ToPoint()); - if (pivot != Vector2.Zero) { - pivot += offset; - if (!pivotRelative) - pivot -= location.Location.ToVector2(); - } + loc.Offset(off); + if (piv != Vector2.Zero) { + piv += off; + if (!pivotRelative) + piv -= loc.Location.ToVector2(); + } - foreach (var (name, off) in namesOffsets) { - var region = new TextureRegion(texture, location.OffsetCopy(off.ToPoint())) { - PivotPixels = pivot + off, + var region = new TextureRegion(texture, loc) { + PivotPixels = piv, 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 2c0bd23..0b0f722 100644 --- a/Tests/DataTextureAtlasTests.cs +++ b/Tests/DataTextureAtlasTests.cs @@ -35,13 +35,13 @@ namespace Tests { Assert.AreEqual(negativePivot.Area, new Rectangle(0, 32, 16, 16)); Assert.AreEqual(negativePivot.PivotPixels, new Vector2(-32, 46 - 32)); - // copies + // copies (pivot pixels should be identical to LongTableUp because they're region-internal) var copy1 = atlas["Copy1"]; Assert.AreEqual(copy1.Area, new Rectangle(0 + 16, 32, 64, 48)); - Assert.AreEqual(copy1.PivotPixels, new Vector2(16 + 16, 48 - 32)); + Assert.AreEqual(copy1.PivotPixels, new Vector2(16, 48 - 32)); var copy2 = atlas["Copy2"]; Assert.AreEqual(copy2.Area, new Rectangle(0 + 32, 32 + 4, 64, 48)); - Assert.AreEqual(copy2.PivotPixels, new Vector2(16 + 32, 48 - 32 + 4)); + Assert.AreEqual(copy2.PivotPixels, new Vector2(16, 48 - 32)); // data var data = atlas["DataTest"];