1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-27 23:09:10 +02:00

allow data texture atlases to have an underlying texture region

This commit is contained in:
Ell 2020-12-19 22:57:17 +01:00
parent 9895be2250
commit 47cc589042

View file

@ -19,7 +19,7 @@ namespace MLEM.Data {
/// <summary> /// <summary>
/// The texture to use for this atlas /// The texture to use for this atlas
/// </summary> /// </summary>
public readonly Texture2D Texture; public readonly TextureRegion Texture;
/// <summary> /// <summary>
/// Returns the texture region with the given name, or null if it does not exist. /// Returns the texture region with the given name, or null if it does not exist.
/// </summary> /// </summary>
@ -37,21 +37,23 @@ namespace MLEM.Data {
private readonly Dictionary<string, TextureRegion> regions = new Dictionary<string, TextureRegion>(); private readonly Dictionary<string, TextureRegion> regions = new Dictionary<string, TextureRegion>();
/// <summary> private DataTextureAtlas(TextureRegion texture) {
/// Creates a new data texture atlas with the given texture and region amount.
/// </summary>
/// <param name="texture">The texture to use for this atlas</param>
public DataTextureAtlas(Texture2D texture) {
this.Texture = texture; this.Texture = texture;
} }
internal static DataTextureAtlas Load(ContentManager content, string texturePath, string infoPath, bool pivotRelative) { /// <summary>
var info = new FileInfo(Path.Combine(content.RootDirectory, infoPath ?? $"{texturePath}.atlas")); /// Loads a <see cref="DataTextureAtlas"/> from the given loaded texture and texture data file.
/// </summary>
/// <param name="texture">The texture to use for this data texture atlas</param>
/// <param name="content">The content manager to use for loading</param>
/// <param name="infoPath">The path, including extension, to the atlas info file</param>
/// <param name="pivotRelative">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.</param>
/// <returns>A new data texture atlas with the given settings</returns>
public static DataTextureAtlas LoadAtlasData(TextureRegion texture, ContentManager content, string infoPath, bool pivotRelative = false) {
var info = new FileInfo(Path.Combine(content.RootDirectory, infoPath));
string text; string text;
using (var reader = info.OpenText()) using (var reader = info.OpenText())
text = reader.ReadToEnd(); text = reader.ReadToEnd();
var texture = content.Load<Texture2D>(texturePath);
var atlas = new DataTextureAtlas(texture); var atlas = new DataTextureAtlas(texture);
// parse each texture region: "<name> loc <u> <v> <w> <h> piv <px> <py>" // parse each texture region: "<name> loc <u> <v> <w> <h> piv <px> <py>"
@ -59,7 +61,7 @@ namespace MLEM.Data {
foreach (Match match in Regex.Matches(text, regex)) { foreach (Match match in Regex.Matches(text, regex)) {
var name = match.Groups[1].Value.Trim(); var name = match.Groups[1].Value.Trim();
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) + texture.U, int.Parse(match.Groups[3].Value) + texture.V,
int.Parse(match.Groups[4].Value), int.Parse(match.Groups[5].Value)); int.Parse(match.Groups[4].Value), int.Parse(match.Groups[5].Value));
var piv = Vector2.Zero; var piv = Vector2.Zero;
if (match.Groups[6].Success) { if (match.Groups[6].Success) {
@ -92,7 +94,8 @@ namespace MLEM.Data {
/// <param name="pivotRelative">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.</param> /// <param name="pivotRelative">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.</param>
/// <returns>A new data texture atlas with the given settings</returns> /// <returns>A new data texture atlas with the given settings</returns>
public static DataTextureAtlas LoadTextureAtlas(this ContentManager content, string texturePath, string infoPath = null, bool pivotRelative = false) { public static DataTextureAtlas LoadTextureAtlas(this ContentManager content, string texturePath, string infoPath = null, bool pivotRelative = false) {
return DataTextureAtlas.Load(content, texturePath, infoPath, pivotRelative); var texture = new TextureRegion(content.Load<Texture2D>(texturePath));
return DataTextureAtlas.LoadAtlasData(texture, content, infoPath ?? $"{texturePath}.atlas", pivotRelative);
} }
} }