From b012c65990fc46c219ff267035707627d444321a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 2 Aug 2022 23:56:18 +0200 Subject: [PATCH] Allow data texture atlas pivots and offsets to be negative --- CHANGELOG.md | 3 +++ MLEM.Data/DataTextureAtlas.cs | 2 +- Tests/Content/Texture.atlas | 4 ++++ Tests/DataTextureAtlasTests.cs | 7 ++++++- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a267aa..43f519f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,9 @@ Improvements - Close other dropdowns when opening a dropdown ### MLEM.Data +Improvements +- Allow data texture atlas pivots and offsets to be negative + Fixes - Fixed data texture atlases not allowing most characters in their region names diff --git a/MLEM.Data/DataTextureAtlas.cs b/MLEM.Data/DataTextureAtlas.cs index 18cbd95..5e1b9d2 100644 --- a/MLEM.Data/DataTextureAtlas.cs +++ b/MLEM.Data/DataTextureAtlas.cs @@ -86,7 +86,7 @@ namespace MLEM.Data { var atlas = new DataTextureAtlas(texture); // parse each texture region: " loc [piv ] [off ]" - foreach (Match match in Regex.Matches(text, @"(.+)\s+loc\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s*(?:piv\s+([0-9.]+)\s+([0-9.]+))?\s*(?:off\s+([0-9.]+)\s+([0-9.]+))?")) { + foreach (Match match in Regex.Matches(text, @"(.+)\s+loc\s+([0-9+]+)\s+([0-9+]+)\s+([0-9+]+)\s+([0-9+]+)\s*(?:piv\s+([0-9.+-]+)\s+([0-9.+-]+))?\s*(?:off\s+([0-9.+-]+)\s+([0-9.+-]+))?")) { // offset var off = !match.Groups[8].Success ? Vector2.Zero : new Vector2( float.Parse(match.Groups[8].Value, CultureInfo.InvariantCulture), diff --git a/Tests/Content/Texture.atlas b/Tests/Content/Texture.atlas index 831462e..fd90b79 100644 --- a/Tests/Content/Texture.atlas +++ b/Tests/Content/Texture.atlas @@ -9,6 +9,10 @@ piv 80 16 Plant loc 96 0 16 32 +TestRegionNegativePivot +loc 0 32 +16 16 +piv -32 +46 + LongTableUp loc 0 32 64 48 piv 16 48 diff --git a/Tests/DataTextureAtlasTests.cs b/Tests/DataTextureAtlasTests.cs index beccba6..551e9ae 100644 --- a/Tests/DataTextureAtlasTests.cs +++ b/Tests/DataTextureAtlasTests.cs @@ -13,7 +13,7 @@ namespace Tests { using var game = TestGame.Create(); using var texture = new Texture2D(game.GraphicsDevice, 1, 1); var atlas = DataTextureAtlas.LoadAtlasData(new TextureRegion(texture), game.RawContent, "Texture.atlas"); - Assert.AreEqual(atlas.Regions.Count(), 7); + Assert.AreEqual(atlas.Regions.Count(), 8); // no added offset var table = atlas["LongTableUp"]; @@ -24,6 +24,11 @@ namespace Tests { var table2 = atlas["LongTableLeft"]; Assert.AreEqual(table2.Area, new Rectangle(64, 32, 64, 48)); Assert.AreEqual(table2.PivotPixels, new Vector2(112 - 64, 48 - 32)); + + // negative pivot + var negativePivot = atlas["TestRegionNegativePivot"]; + Assert.AreEqual(negativePivot.Area, new Rectangle(0, 32, 16, 16)); + Assert.AreEqual(negativePivot.PivotPixels, new Vector2(-32, 46 - 32)); } }