1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-15 21:58:46 +02:00

fixed some issues with the new data texture atlas parser

This commit is contained in:
Ell 2022-09-14 11:04:51 +02:00
parent 914b0d9c2d
commit 4918a72760
2 changed files with 36 additions and 19 deletions

View file

@ -143,24 +143,8 @@ namespace MLEM.Data {
i += 2;
break;
default:
// if we have a location for the previous regions, they're valid so we add them
if (location != Rectangle.Empty && namesOffsets.Count > 0) {
location.Offset(offset.ToPoint());
pivot += offset;
if (!pivotRelative)
pivot -= location.Location.ToVector2();
foreach (var (name, off) in namesOffsets) {
var region = new TextureRegion(texture, location.OffsetCopy(off.ToPoint())) {
PivotPixels = pivot + off,
Name = name
};
foreach (var kv in customData)
region.SetData(kv.Key, kv.Value);
atlas.regions.Add(name, region);
}
namesOffsets.Clear();
}
// if we have data for the previous regions, they're valid so we add them
AddCurrentRegions();
// we're starting a new region (or adding another name for a new region), so clear old data
namesOffsets.Add((word.Trim(), Vector2.Zero));
@ -175,7 +159,35 @@ namespace MLEM.Data {
}
}
// add the last region that was started on
AddCurrentRegions();
return atlas;
void AddCurrentRegions() {
// the location is the only mandatory instruction, which is why we check it here
if (location == Rectangle.Empty || namesOffsets.Count <= 0)
return;
location.Offset(offset.ToPoint());
if (pivot != Vector2.Zero) {
pivot += offset;
if (!pivotRelative)
pivot -= location.Location.ToVector2();
}
foreach (var (name, off) in namesOffsets) {
var region = new TextureRegion(texture, location.OffsetCopy(off.ToPoint())) {
PivotPixels = pivot + off,
Name = name
};
foreach (var kv in customData)
region.SetData(kv.Key, kv.Value);
atlas.regions.Add(name, region);
}
// we only clear names offsets if the location was valid, otherwise we ignore multiple names for a region
namesOffsets.Clear();
}
}
}

View file

@ -15,13 +15,18 @@ namespace Tests {
var atlas = DataTextureAtlas.LoadAtlasData(new TextureRegion(texture), game.RawContent, "Texture.atlas");
Assert.AreEqual(11, atlas.Regions.Count());
// no pivot
var plant = atlas["Plant"];
Assert.AreEqual(plant.Area, new Rectangle(96, 0, 16, 32));
Assert.AreEqual(plant.PivotPixels, Vector2.Zero);
// 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["LongTableLeft"];
var table2 = atlas["LongTableDown"];
Assert.AreEqual(table2.Area, new Rectangle(64, 32, 64, 48));
Assert.AreEqual(table2.PivotPixels, new Vector2(112 - 64, 48 - 32));