using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Textures;
namespace MLEM.Data {
///
/// This class represents an atlas of objects which are loaded from a special texture atlas file.
/// To load a data texture atlas, you can use .
/// To see the structure of a Data Texture Atlas, you can check out the sandbox project: .
/// Additionally, if you are using Aseprite, there is a script to automatically populate it:
///
public class DataTextureAtlas {
///
/// The texture to use for this atlas
///
public readonly TextureRegion Texture;
///
/// Returns the texture region with the given name, or null if it does not exist.
///
/// The region's name
public TextureRegion this[string name] {
get {
this.regions.TryGetValue(name, out var ret);
return ret;
}
}
///
/// Returns an enumerable of all of the s in this atlas.
///
public IEnumerable Regions => this.regions.Values;
private readonly Dictionary regions = new Dictionary();
private DataTextureAtlas(TextureRegion texture) {
this.Texture = texture;
}
///
/// Loads a from the given loaded texture and texture data file.
///
/// The texture to use for this data texture atlas
/// The content manager to use for loading
/// The path, including extension, to the atlas info file
/// If this value is true, then the pivot points passed in the info file will be relative to the coordinates of the texture region, not relative to the entire texture's origin.
/// A new data texture atlas with the given settings
public static DataTextureAtlas LoadAtlasData(TextureRegion texture, ContentManager content, string infoPath, bool pivotRelative = false) {
var info = new FileInfo(Path.Combine(content.RootDirectory, infoPath));
string text;
using (var reader = info.OpenText())
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.]+))*")) {
var name = match.Groups[1].Value.Trim();
// 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));
// pivot
var piv = Vector2.Zero;
if (match.Groups[6].Success) {
piv = new Vector2(
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
};
// 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);
}
return atlas;
}
}
///
/// A set of extension methods for dealing with .
///
public static class DataTextureAtlasExtensions {
///
/// Loads a from the given texture and texture data file.
///
/// The content manager to use for loading
/// The path to the texture file
/// The path, including extension, to the atlas info file, or null if ".atlas" should be used
/// If this value is true, then the pivot points passed in the info file will be relative to the coordinates of the texture region, not relative to the entire texture's origin.
/// A new data texture atlas with the given settings
public static DataTextureAtlas LoadTextureAtlas(this ContentManager content, string texturePath, string infoPath = null, bool pivotRelative = false) {
var texture = new TextureRegion(content.Load(texturePath));
return DataTextureAtlas.LoadAtlasData(texture, content, infoPath ?? $"{texturePath}.atlas", pivotRelative);
}
}
}