From 27fc5a74d9ecd5a4c57a8ee72151d92284d2b4e8 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 12 Jul 2021 03:14:05 +0200 Subject: [PATCH] added the ability to specify a coordinate offset in data texture atlases --- CHANGELOG.md | 4 ++++ MLEM.Data/DataTextureAtlas.cs | 21 +++++++++++---------- Tests/Content/Texture.atlas | 9 +++++++-- Tests/DataTextureAtlasTests.cs | 6 ++++++ 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de3ef0e..9778719 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,10 @@ Additions Fixes - Fixed a crash if a paragraph has a link formatting code, but no font +### MLEM.Data +Additions +- Added the ability to specify a coordinate offset in data texture atlases + ## 5.0.0 ### MLEM Additions diff --git a/MLEM.Data/DataTextureAtlas.cs b/MLEM.Data/DataTextureAtlas.cs index 20fc086..d8c0aa4 100644 --- a/MLEM.Data/DataTextureAtlas.cs +++ b/MLEM.Data/DataTextureAtlas.cs @@ -56,13 +56,20 @@ namespace MLEM.Data { text = reader.ReadToEnd(); var atlas = new DataTextureAtlas(texture); - // parse each texture region: " loc piv " followed by extra data in the form "key " - foreach (Match match in Regex.Matches(text, @"(.+)\W+loc\W+([0-9]+)\W+([0-9]+)\W+([0-9]+)\W+([0-9]+)(?:\W+piv\W+([0-9.]+)\W+([0-9.]+))?(?:\W+(\w+)\W+([0-9.]+)\W+([0-9.]+))*")) { + // parse each texture region: " loc [piv ] [off ]" + foreach (Match match in Regex.Matches(text, @"(.+)\W+loc\W+([0-9]+)\W+([0-9]+)\W+([0-9]+)\W+([0-9]+)\W*(?:piv\W+([0-9.]+)\W+([0-9.]+))?\W*(?:off\W+([0-9.]+)\W+([0-9.]+))?")) { var name = match.Groups[1].Value.Trim(); + // offset + var off = !match.Groups[8].Success ? Vector2.Zero : new Vector2( + float.Parse(match.Groups[8].Value, CultureInfo.InvariantCulture), + float.Parse(match.Groups[9].Value, CultureInfo.InvariantCulture)); + // location var loc = new Rectangle( int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value), int.Parse(match.Groups[4].Value), int.Parse(match.Groups[5].Value)); + loc.Offset(off); + // pivot var piv = Vector2.Zero; if (match.Groups[6].Success) { @@ -70,18 +77,12 @@ namespace MLEM.Data { float.Parse(match.Groups[6].Value, CultureInfo.InvariantCulture) - (pivotRelative ? 0 : loc.X), float.Parse(match.Groups[7].Value, CultureInfo.InvariantCulture) - (pivotRelative ? 0 : loc.Y)); } + piv += off; + var region = new TextureRegion(texture, loc) { PivotPixels = piv, Name = name }; - // additional data - if (match.Groups[8].Success) { - for (var i = 0; i < match.Groups[8].Captures.Count; i++) { - region.SetData(match.Groups[8].Captures[i].Value, new Vector2( - float.Parse(match.Groups[9].Captures[i].Value, CultureInfo.InvariantCulture) - (pivotRelative ? 0 : loc.X), - float.Parse(match.Groups[10].Captures[i].Value, CultureInfo.InvariantCulture) - (pivotRelative ? 0 : loc.Y))); - } - } atlas.regions.Add(name, region); } diff --git a/Tests/Content/Texture.atlas b/Tests/Content/Texture.atlas index b7b5658..93847a5 100644 --- a/Tests/Content/Texture.atlas +++ b/Tests/Content/Texture.atlas @@ -1,14 +1,19 @@ SimpleDeskUp loc 0 0 48 32 piv 16 16 + SimpleDeskRight loc 48 0 48 32 piv 80 16 + Plant loc 96 0 16 32 + LongTableUp loc 0 32 64 48 piv 16 48 + LongTableRight -loc 64 32 64 48 -piv 112 48 \ No newline at end of file +loc 32 30 64 48 +piv 80 46 +off 32 2 \ No newline at end of file diff --git a/Tests/DataTextureAtlasTests.cs b/Tests/DataTextureAtlasTests.cs index 5dc6232..62de0b2 100644 --- a/Tests/DataTextureAtlasTests.cs +++ b/Tests/DataTextureAtlasTests.cs @@ -15,9 +15,15 @@ namespace Tests { var atlas = DataTextureAtlas.LoadAtlasData(new TextureRegion(texture), game.RawContent, "Texture.atlas"); Assert.AreEqual(atlas.Regions.Count(), 5); + // 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["LongTableRight"]; + Assert.AreEqual(table2.Area, new Rectangle(64, 32, 64, 48)); + Assert.AreEqual(table2.PivotPixels, new Vector2(112 - 64, 48 - 32)); } }