1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 04:53:29 +01:00

Allow specifying multiple names for a DataTextureAtlas region

This commit is contained in:
Ell 2022-07-18 20:41:19 +02:00
parent 08e28cb95b
commit ba1058748e
7 changed files with 21 additions and 19 deletions

View file

@ -80,6 +80,7 @@ Improvements
- Allow enumerating all region names of a DataTextureAtlas - Allow enumerating all region names of a DataTextureAtlas
- Cache RuntimeTexturePacker texture data while packing to improve performance - Cache RuntimeTexturePacker texture data while packing to improve performance
- Greatly improved RuntimeTexturePacker performance - Greatly improved RuntimeTexturePacker performance
- Allow specifying multiple names for a DataTextureAtlas region
Fixes Fixes
- Fixed SoundEffectReader incorrectly claiming it could read ogg and mp3 files - Fixed SoundEffectReader incorrectly claiming it could read ogg and mp3 files

2
FNA

@ -1 +1 @@
Subproject commit 62cbf1c3180c31de0265b267a7ed080efd20150c Subproject commit 700a6f096ad359cc12634eeb5608ee9c8d29798c

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
@ -5,7 +6,6 @@ using System.Text.RegularExpressions;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Textures; using MLEM.Textures;
namespace MLEM.Data { namespace MLEM.Data {
@ -84,9 +84,8 @@ namespace MLEM.Data {
} }
var atlas = new DataTextureAtlas(texture); var atlas = new DataTextureAtlas(texture);
// parse each texture region: "<name> loc <u> <v> <w> <h> [piv <px> <py>] [off <ox> <oy>]" // parse each texture region: "<names> loc <u> <v> <w> <h> [piv <px> <py>] [off <ox> <oy>]"
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.]+))?")) { 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 // offset
var off = !match.Groups[8].Success ? Vector2.Zero : new Vector2( var off = !match.Groups[8].Success ? Vector2.Zero : new Vector2(
float.Parse(match.Groups[8].Value, CultureInfo.InvariantCulture), 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[6].Value, CultureInfo.InvariantCulture) - (pivotRelative ? 0 : loc.X),
float.Parse(match.Groups[7].Value, CultureInfo.InvariantCulture) - (pivotRelative ? 0 : loc.Y)); float.Parse(match.Groups[7].Value, CultureInfo.InvariantCulture) - (pivotRelative ? 0 : loc.Y));
var region = new TextureRegion(texture, loc) { foreach (var name in Regex.Split(match.Groups[1].Value, @"\W")) {
PivotPixels = piv, var trimmed = name.Trim();
Name = name if (trimmed.Length <= 0)
}; continue;
atlas.regions.Add(name, region); var region = new TextureRegion(texture, loc) {
PivotPixels = piv,
Name = trimmed
};
atlas.regions.Add(trimmed, region);
}
} }
return atlas; return atlas;

View file

@ -13,6 +13,6 @@ LongTableUp
loc 0 32 64 48 loc 0 32 64 48
piv 16 48 piv 16 48
LongTableRight LongTableRight LongTableDown LongTableLeft
loc 64 32 64 48 loc 64 32 64 48
piv 112 48 piv 112 48

View file

@ -169,11 +169,8 @@ namespace Sandbox {
var region2 = new TextureRegion(round); var region2 = new TextureRegion(round);
var atlas = this.Content.LoadTextureAtlas("Textures/Furniture"); 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); 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<Vector2>(key));
}
this.OnDraw += (g, time) => { this.OnDraw += (g, time) => {
this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp); this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp);

View file

@ -13,7 +13,7 @@ LongTableUp
loc 0 32 64 48 loc 0 32 64 48
piv 16 48 piv 16 48
LongTableRight LongTableRight LongTableDown LongTableLeft
loc 32 30 64 48 loc 32 30 64 48
piv 80 46 piv 80 46
off 32 2 off 32 2

View file

@ -13,7 +13,7 @@ namespace Tests {
using var game = TestGame.Create(); using var game = TestGame.Create();
using var texture = new Texture2D(game.GraphicsDevice, 1, 1); using var texture = new Texture2D(game.GraphicsDevice, 1, 1);
var atlas = DataTextureAtlas.LoadAtlasData(new TextureRegion(texture), game.RawContent, "Texture.atlas"); 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 // no added offset
var table = atlas["LongTableUp"]; var table = atlas["LongTableUp"];
@ -21,7 +21,7 @@ namespace Tests {
Assert.AreEqual(table.PivotPixels, new Vector2(16, 48 - 32)); Assert.AreEqual(table.PivotPixels, new Vector2(16, 48 - 32));
// added offset // added offset
var table2 = atlas["LongTableRight"]; var table2 = atlas["LongTableLeft"];
Assert.AreEqual(table2.Area, new Rectangle(64, 32, 64, 48)); Assert.AreEqual(table2.Area, new Rectangle(64, 32, 64, 48));
Assert.AreEqual(table2.PivotPixels, new Vector2(112 - 64, 48 - 32)); Assert.AreEqual(table2.PivotPixels, new Vector2(112 - 64, 48 - 32));
} }