From ba1058748ecf02a987cb8f4a6ddfa7532949d94e Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 18 Jul 2022 20:41:19 +0200 Subject: [PATCH] Allow specifying multiple names for a DataTextureAtlas region --- CHANGELOG.md | 1 + FNA | 2 +- MLEM.Data/DataTextureAtlas.cs | 20 ++++++++++++-------- Sandbox/Content/Textures/Furniture.atlas | 4 ++-- Sandbox/GameImpl.cs | 5 +---- Tests/Content/Texture.atlas | 4 ++-- Tests/DataTextureAtlasTests.cs | 4 ++-- 7 files changed, 21 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5dcb498..df1ba53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ Improvements - Allow enumerating all region names of a DataTextureAtlas - Cache RuntimeTexturePacker texture data while packing to improve performance - Greatly improved RuntimeTexturePacker performance +- Allow specifying multiple names for a DataTextureAtlas region Fixes - Fixed SoundEffectReader incorrectly claiming it could read ogg and mp3 files diff --git a/FNA b/FNA index 62cbf1c..700a6f0 160000 --- a/FNA +++ b/FNA @@ -1 +1 @@ -Subproject commit 62cbf1c3180c31de0265b267a7ed080efd20150c +Subproject commit 700a6f096ad359cc12634eeb5608ee9c8d29798c diff --git a/MLEM.Data/DataTextureAtlas.cs b/MLEM.Data/DataTextureAtlas.cs index 8373328..a25a1de 100644 --- a/MLEM.Data/DataTextureAtlas.cs +++ b/MLEM.Data/DataTextureAtlas.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -5,7 +6,6 @@ using System.Text.RegularExpressions; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; -using MLEM.Extensions; using MLEM.Textures; namespace MLEM.Data { @@ -84,9 +84,8 @@ namespace MLEM.Data { } var atlas = new DataTextureAtlas(texture); - // parse each texture region: " loc [piv ] [off ]" + // 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), @@ -103,11 +102,16 @@ 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)); - var region = new TextureRegion(texture, loc) { - PivotPixels = piv, - Name = name - }; - atlas.regions.Add(name, region); + foreach (var name in Regex.Split(match.Groups[1].Value, @"\W")) { + var trimmed = name.Trim(); + if (trimmed.Length <= 0) + continue; + var region = new TextureRegion(texture, loc) { + PivotPixels = piv, + Name = trimmed + }; + atlas.regions.Add(trimmed, region); + } } return atlas; diff --git a/Sandbox/Content/Textures/Furniture.atlas b/Sandbox/Content/Textures/Furniture.atlas index 25f8313..80b4a98 100644 --- a/Sandbox/Content/Textures/Furniture.atlas +++ b/Sandbox/Content/Textures/Furniture.atlas @@ -13,6 +13,6 @@ LongTableUp loc 0 32 64 48 piv 16 48 -LongTableRight +LongTableRight LongTableDown LongTableLeft loc 64 32 64 48 -piv 112 48 \ No newline at end of file +piv 112 48 diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index b356196..b5c74ec 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -169,11 +169,8 @@ namespace Sandbox { var region2 = new TextureRegion(round); var atlas = this.Content.LoadTextureAtlas("Textures/Furniture"); - foreach (var r in atlas.Regions) { + foreach (var r in atlas.Regions) Console.WriteLine(r.Name + ": " + r.U + " " + r.V + " " + r.Width + " " + r.Height + " " + r.PivotPixels); - foreach (var key in r.GetDataKeys()) - Console.WriteLine(key + " " + r.GetData(key)); - } this.OnDraw += (g, time) => { this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp); diff --git a/Tests/Content/Texture.atlas b/Tests/Content/Texture.atlas index 93847a5..831462e 100644 --- a/Tests/Content/Texture.atlas +++ b/Tests/Content/Texture.atlas @@ -13,7 +13,7 @@ LongTableUp loc 0 32 64 48 piv 16 48 -LongTableRight +LongTableRight LongTableDown LongTableLeft loc 32 30 64 48 piv 80 46 -off 32 2 \ No newline at end of file +off 32 2 diff --git a/Tests/DataTextureAtlasTests.cs b/Tests/DataTextureAtlasTests.cs index 586949e..beccba6 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(), 5); + Assert.AreEqual(atlas.Regions.Count(), 7); // no added offset var table = atlas["LongTableUp"]; @@ -21,7 +21,7 @@ namespace Tests { Assert.AreEqual(table.PivotPixels, new Vector2(16, 48 - 32)); // added offset - var table2 = atlas["LongTableRight"]; + var table2 = atlas["LongTableLeft"]; Assert.AreEqual(table2.Area, new Rectangle(64, 32, 64, 48)); Assert.AreEqual(table2.PivotPixels, new Vector2(112 - 64, 48 - 32)); }