using System.Text.Json; using System.Text.Json.Serialization; namespace MrGameEng.Atlases; /// /// Serializable description of one packed atlas: its page image files and the source-relative /// region keys with their pixel rectangles. Stored as a JSON .atlas file next to the pages. /// public sealed class AtlasMetadata { private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; /// Format version, bumped on breaking metadata changes. public int Version { get; init; } = 1; /// Atlas name (group key with '/' replaced by '.'). public string Name { get; init; } = ""; /// Maximum page size the atlas was built with (staleness check input). public int PageSize { get; init; } /// Padding in pixels the atlas was built with (staleness check input). public int Padding { get; init; } /// Page image files (relative to the metadata file), in page-index order. public List Pages { get; init; } = []; /// Packed regions, sorted by key. public List Regions { get; init; } = []; /// Serializes this metadata to indented JSON. public string ToJson() => JsonSerializer.Serialize(this, JsonOptions); /// Parses metadata from JSON produced by . public static AtlasMetadata FromJson(string json) => JsonSerializer.Deserialize(json, JsonOptions) ?? throw new InvalidDataException("Atlas metadata JSON deserialized to null."); } /// One page image of an atlas. public sealed class AtlasPage { /// Image file name, relative to the metadata file. public string File { get; init; } = ""; /// Page width in pixels. public int Width { get; init; } /// Page height in pixels. public int Height { get; init; } } /// One packed source texture inside an atlas. public sealed class AtlasRegion { /// /// Region key: the source path relative to the build source root, forward slashes, /// without the file extension (e.g. Things/Pawn/Animal/Fox). /// public string Key { get; init; } = ""; /// Index of the page containing this region. public int Page { get; init; } /// X of the region in page pixels. public int X { get; init; } /// Y of the region in page pixels. public int Y { get; init; } /// Region width in pixels. [JsonPropertyName("w")] public int Width { get; init; } /// Region height in pixels. [JsonPropertyName("h")] public int Height { get; init; } }