Skip to content

Commit 437b8b9

Browse files
committed
Additional logic added to exporting functionality so sprites are grouped by name in logical "rows", each depicting one action with all its frames.
1 parent ef03843 commit 437b8b9

File tree

2 files changed

+57
-39
lines changed

2 files changed

+57
-39
lines changed

SpriteImageParser/Parser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static List<SpriteRegion> DetectSpritesInImage(
6363
/// sprite groups.</param>
6464
/// <returns>A matrix of lists of <see cref="SpriteRegion"/> representing each row of
6565
/// sprites.</returns>
66-
private static List<List<SpriteRegion>> GroupByRow(List<SpriteRegion> regions, int yTolerance)
66+
internal static List<List<SpriteRegion>> GroupByRow(List<SpriteRegion> regions, int yTolerance)
6767
{
6868
var rows = new List<List<SpriteRegion>>();
6969
foreach (var region in regions)

SpriteImageParser/SpriteRegionExporter.cs

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,34 @@ public class SpriteRegionExporter
1818
/// <param name="frameName">The name of the frame to use in the JSON output.</param>
1919
/// <param name="duration">Optional; the duration for each sprite frame, if applicable.</param>
2020
/// <returns>A JSON string representing the sprite regions provided.</returns>
21-
public static string SerializeToJson(List<SpriteRegion> regions, string frameName, float? duration)
21+
public static string SerializeToJson(List<SpriteRegion> regions,
22+
string frameName,
23+
float? duration,
24+
int yTolerance = 3)
2225
{
2326
var output = new List<Dictionary<string, object>>();
24-
int counter = 1;
25-
foreach (var region in regions)
27+
var grouped = Parser.GroupByRow(regions, yTolerance);
28+
for (int rowIndex = 0; rowIndex < grouped.Count; rowIndex++)
2629
{
27-
var regionData = new Dictionary<string, object>
30+
var row = grouped[rowIndex];
31+
for (int spriteIndex = 0; spriteIndex < row.Count; spriteIndex++)
2832
{
29-
{ "Name", "Sprite" + counter.ToString("D6") },
30-
{ frameName, new Dictionary<string, int>
31-
{
32-
{ "X", region.X },
33-
{ "Y", region.Y },
34-
{ "Width", region.Width },
35-
{ "Height", region.Height }
36-
}
37-
},
38-
{ "Duration", duration.HasValue ? duration.Value : 1 }
39-
};
40-
output.Add(regionData);
41-
counter++;
33+
var region = row[spriteIndex];
34+
var regionData = new Dictionary<string, object>
35+
{
36+
{ "Name", $"Row{rowIndex + 1:D6}Sprite{spriteIndex + 1:D6}" },
37+
{ frameName, new Dictionary<string, int>
38+
{
39+
{ "X", region.X },
40+
{ "Y", region.Y },
41+
{ "Width", region.Width },
42+
{ "Height", region.Height }
43+
}
44+
},
45+
{ "Duration", duration.HasValue ? duration.Value : 1 }
46+
};
47+
output.Add(regionData);
48+
}
4249
}
4350
return JsonSerializer.Serialize(output, new JsonSerializerOptions { WriteIndented = true });
4451
}
@@ -48,7 +55,8 @@ public static string SerializeToJson(List<SpriteRegion> regions, string frameNam
4855
/// </summary>
4956
/// <param name="regions">The list of sprite regions to serialize.</param>
5057
/// <returns>A JSON string representing the sprite regions provided.</returns>
51-
public static string SerializeToJson(List<SpriteRegion> regions) => SerializeToJson(regions, "Frame", 1);
58+
public static string SerializeToJson(List<SpriteRegion> regions)
59+
=> SerializeToJson(regions, "Frame", 1);
5260

5361
/// <summary>
5462
/// Serializes a list of sprite regions to an XML string.
@@ -57,30 +65,39 @@ public static string SerializeToJson(List<SpriteRegion> regions, string frameNam
5765
/// <param name="frameName">The name of the frame to use in the JSON output.</param>
5866
/// <param name="duration">Optional; the duration for each sprite frame, if applicable.</param>
5967
/// <returns>An XML string representing the sprite regions provided.</returns>
60-
public static string SerializeToXml(List<SpriteRegion> regions, string frameName, float? duration)
68+
public static string SerializeToXml(
69+
List<SpriteRegion> regions,
70+
string frameName,
71+
float? duration,
72+
int yTolerance = 3)
6173
{
6274
XmlDocument doc = new XmlDocument();
6375
XmlElement root = doc.CreateElement("Spritesheet");
6476
doc.AppendChild(root);
65-
int counter = 1;
66-
foreach (var region in regions)
77+
var grouped = Parser.GroupByRow(regions, yTolerance);
78+
for (int rowIndex = 0; rowIndex < grouped.Count; rowIndex++)
6779
{
68-
XmlElement regionElement = doc.CreateElement("SpriteRegion");
69-
regionElement.AppendChild(doc.CreateElement("Name")).InnerText =
70-
"Sprite" + counter.ToString("D6");
71-
regionElement.AppendChild(doc.CreateElement(frameName));
72-
regionElement.ChildNodes[regionElement.ChildNodes.Count - 1]
73-
.AppendChild(doc.CreateElement("X")).InnerText = region.X.ToString();
74-
regionElement.ChildNodes[regionElement.ChildNodes.Count - 1]
75-
.AppendChild(doc.CreateElement("Y")).InnerText = region.Y.ToString();
76-
regionElement.ChildNodes[regionElement.ChildNodes.Count - 1]
77-
.AppendChild(doc.CreateElement("Width")).InnerText = region.Width.ToString();
78-
regionElement.ChildNodes[regionElement.ChildNodes.Count - 1]
79-
.AppendChild(doc.CreateElement("Height")).InnerText = region.Height.ToString();
80-
regionElement.AppendChild(doc.CreateElement("Duration")).InnerText =
81-
duration.HasValue ? duration.Value.ToString() : "1";
82-
root.AppendChild(regionElement);
83-
counter++;
80+
var row = grouped[rowIndex];
81+
for (int spriteIndex = 0; spriteIndex < row.Count; spriteIndex++)
82+
{
83+
var region = row[spriteIndex];
84+
XmlElement regionElement = doc.CreateElement("SpriteRegion");
85+
regionElement.AppendChild(doc.CreateElement("Name")).InnerText =
86+
$"Row{rowIndex + 1:D6}Sprite{spriteIndex + 1:D6}";
87+
XmlElement frameElement = doc.CreateElement(frameName);
88+
frameElement.AppendChild(doc.CreateElement("X"))
89+
.InnerText = region.X.ToString();
90+
frameElement.AppendChild(doc.CreateElement("Y"))
91+
.InnerText = region.Y.ToString();
92+
frameElement.AppendChild(doc.CreateElement("Width"))
93+
.InnerText = region.Width.ToString();
94+
frameElement.AppendChild(doc.CreateElement("Height"))
95+
.InnerText = region.Height.ToString();
96+
regionElement.AppendChild(frameElement);
97+
regionElement.AppendChild(doc.CreateElement("Duration")).InnerText =
98+
duration.HasValue ? duration.Value.ToString() : "1";
99+
root.AppendChild(regionElement);
100+
}
84101
}
85102
return Beautify(doc);
86103
}
@@ -90,7 +107,8 @@ public static string SerializeToXml(List<SpriteRegion> regions, string frameName
90107
/// </summary>
91108
/// <param name="regions">The list of sprite regions to serialize.</param>
92109
/// <returns>An XML string representing the sprite regions provided.</returns>
93-
public static string SerializeToXml(List<SpriteRegion> regions) => SerializeToXml(regions, "Frame", 1);
110+
public static string SerializeToXml(List<SpriteRegion> regions)
111+
=> SerializeToXml(regions, "Frame", 1);
94112

95113
/// <summary>
96114
/// Beautifies an XML document by formatting it with indentation and new lines.

0 commit comments

Comments
 (0)