Skip to content

Commit

Permalink
Feat ScmlWriter: Can now fill in missing sprites with placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
skairunner committed Nov 16, 2019
1 parent a28fcea commit 1204bc9
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions kanimal/Writer/ScmlWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
Expand All @@ -16,6 +17,8 @@ public class ScmlWriter : Writer
protected XmlElement SpriterRoot;
protected XmlElement Entity;

public bool FillMissingSprites = true; // When true, if a file is referenced in frames but doesn't exist as a sprite, add it as a blank 1x1 sprite.

public ScmlWriter(Reader reader)
{
BuildData = reader.BuildData;
Expand Down Expand Up @@ -215,6 +218,35 @@ protected virtual void AddTimelineInfo(XmlElement parent, int animIndex)
object_def.SetAttribute("folder", "0");
var filename = element.FindFilename(AnimHashes);

if (!FilenameIndex.ContainsKey(filename))
{
if (FillMissingSprites)
{
// Must generate the missing sprite.
var sprite = new Sprite
{
Bitmap = new Bitmap(1, 1),
Name = filename
};
Sprites.Add(sprite);
// Also add it to the Spriter file
FilenameIndex[filename] = (BuildData.FrameCount + 1).ToString();
var fileNode = Scml.CreateElement("file");
fileNode.SetAttribute("id", FilenameIndex[filename]);
fileNode.SetAttribute("name", filename);
fileNode.SetAttribute("width", 1.ToString());
fileNode.SetAttribute("height", 1.ToString());
fileNode.SetAttribute("pivot_x", 0f.ToString("G9"));
fileNode.SetAttribute("pivot_y", 0f.ToString("G9"));
var folderNode = SpriterRoot.GetElementsByTagName("folder")[0];
folderNode.AppendChild(fileNode);
}
else
{
throw new ProjectParseException($"Animation \"{bank.Name}\" in \"{BuildData.Name}\" referenced a sprite \"{filename}\" that doesn't exist.");
}
}

object_def.SetAttribute("file", FilenameIndex[filename]);
object_def.SetAttribute("x", (trans.X * 0.5f).ToString("G5"));
object_def.SetAttribute("y", (-trans.Y * 0.5f).ToString("G5"));
Expand Down

0 comments on commit 1204bc9

Please sign in to comment.