1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

added the ability to specify a coordinate offset in data texture atlases

This commit is contained in:
Ell 2021-07-12 03:14:05 +02:00
parent ee2b0b82fe
commit 27fc5a74d9
4 changed files with 28 additions and 12 deletions

View file

@ -24,6 +24,10 @@ Additions
Fixes Fixes
- Fixed a crash if a paragraph has a link formatting code, but no font - 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 ## 5.0.0
### MLEM ### MLEM
Additions Additions

View file

@ -56,13 +56,20 @@ namespace MLEM.Data {
text = reader.ReadToEnd(); text = reader.ReadToEnd();
var atlas = new DataTextureAtlas(texture); var atlas = new DataTextureAtlas(texture);
// parse each texture region: "<name> loc <u> <v> <w> <h> piv <px> <py>" followed by extra data in the form "key <x> <y>" // parse each texture region: "<name> 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+(\w+)\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(); 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 // location
var loc = new Rectangle( var loc = new Rectangle(
int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value), int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value),
int.Parse(match.Groups[4].Value), int.Parse(match.Groups[5].Value)); int.Parse(match.Groups[4].Value), int.Parse(match.Groups[5].Value));
loc.Offset(off);
// pivot // pivot
var piv = Vector2.Zero; var piv = Vector2.Zero;
if (match.Groups[6].Success) { 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[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));
} }
piv += off;
var region = new TextureRegion(texture, loc) { var region = new TextureRegion(texture, loc) {
PivotPixels = piv, PivotPixels = piv,
Name = name 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); atlas.regions.Add(name, region);
} }

View file

@ -1,14 +1,19 @@
SimpleDeskUp SimpleDeskUp
loc 0 0 48 32 loc 0 0 48 32
piv 16 16 piv 16 16
SimpleDeskRight SimpleDeskRight
loc 48 0 48 32 loc 48 0 48 32
piv 80 16 piv 80 16
Plant Plant
loc 96 0 16 32 loc 96 0 16 32
LongTableUp LongTableUp
loc 0 32 64 48 loc 0 32 64 48
piv 16 48 piv 16 48
LongTableRight LongTableRight
loc 64 32 64 48 loc 32 30 64 48
piv 112 48 piv 80 46
off 32 2

View file

@ -15,9 +15,15 @@ namespace Tests {
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(), 5);
// no added offset
var table = atlas["LongTableUp"]; var table = atlas["LongTableUp"];
Assert.AreEqual(table.Area, new Rectangle(0, 32, 64, 48)); Assert.AreEqual(table.Area, new Rectangle(0, 32, 64, 48));
Assert.AreEqual(table.PivotPixels, new Vector2(16, 48 - 32)); 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));
} }
} }