Skip to content

Commit

Permalink
Merge pull request #10 from daviscook477/interpolate-opt-in
Browse files Browse the repository at this point in the history
Interpolate opt in
  • Loading branch information
skairunner authored Jan 14, 2020
2 parents 770891a + 8c61137 commit 0e3b6c1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
7 changes: 7 additions & 0 deletions kanimal-cli/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ public abstract class ConversionOptions : ProgramOptions
{
[Option('S', "strict", Required = false, HelpText = "When writing to scml, enabling this flag ")]
public bool Strict { get; set; }

[Option('i', "interpolate", Required = false, HelpText = "Enable interpolating scml files on load.")]
public bool Interp { get; set; }

[Option('b', "debone", Required = false, HelpText = "Enable deboning scml files on load.")]
public bool Debone { get; set; }

}

[Verb("dump", HelpText = "Output a dump of the specified kanim.")]
Expand Down
8 changes: 6 additions & 2 deletions kanimal-cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ private static void Convert(string inputFormat, string outputFormat, List<string
var scml = files.Find(path => path.EndsWith(".scml"));
var scmlreader = new ScmlReader(scml)
{
AllowMissingSprites = !opt.Strict
AllowMissingSprites = !opt.Strict,
AllowInFramePivots = !opt.Strict,
InterpolateMissingFrames = opt.Interp,
Debone = opt.Debone
};
scmlreader.Read();
reader = scmlreader;
Expand Down Expand Up @@ -112,7 +115,8 @@ private static void Convert(string inputFormat, string outputFormat, List<string
case "scml":
var scmlWriter = new ScmlWriter(reader)
{
FillMissingSprites = !opt.Strict
FillMissingSprites = !opt.Strict,
AllowDuplicateSprites = !opt.Strict
};
scmlWriter.SaveToDir(Path.Join(opt.OutputPath));
break;
Expand Down
31 changes: 27 additions & 4 deletions kanimal/Reader/ScmlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ScmlReader : Reader
private Dictionary<Filename, Bitmap> inputSprites; // The keys in this dictionary are filenames, *with* the file extension, if it exists.

public bool AllowMissingSprites = true;
public bool AllowInFramePivots = true;
public bool InterpolateMissingFrames = true;
public bool Debone = true;

Expand Down Expand Up @@ -128,6 +129,17 @@ public override void Read()
Logger.Info("Reading image files.");
ReadProjectSprites();

// Only use the sprites that are included in the project
List<SpriteName> allSprites = inputSprites.Select(sprite => sprite.Key.ToSpriteName()).ToList();
inputSprites = inputSprites.Where(sprite => projectSprites.ContainsKey(sprite.Key.ToSpriteName())).ToDictionary(x => x.Key, x => x.Value);
List<SpriteName> usedSprites = inputSprites.Select(sprite => sprite.Key.ToSpriteName()).ToList();

List<string> unusedSprites = allSprites.FindAll(sprite => !usedSprites.Contains(sprite)).Select(sprite => sprite.ToFilename().ToString()).ToList();
if (unusedSprites.Count > 0)
{
Logger.Warn($"There were unused sprites in the SCML project folder: {unusedSprites.Join()}. Did you forget to included these in the SCML file? You must manually add in files that are part of a symbol_override if they aren't explicitly placed into any animations in the SCML. ");
}

// Also set the output list of sprites
Sprites = new List<Sprite>();
Sprites = inputSprites.Select(sprite => new Sprite {Bitmap = sprite.Value, Name = sprite.Key.ToSpriteName()}).ToList();
Expand Down Expand Up @@ -587,15 +599,26 @@ float Interpolate(string attrName, float defaultValue)
if (hasInconsistentIntervals)
{
var anims = inconsistentAnims.ToList().Join();
throw new ProjectParseException(
$"SCML format exception: The intervals in the anims {anims} were inconsistent. Aborting read.");
string error = $"SCML format exception: The intervals in the anims {anims} were inconsistent. Aborting read.";
if (!InterpolateMissingFrames)
{
error += " Try enabling keyframe interpolation with the \"-i\" flag and try again.";
}
throw new ProjectParseException(error);
}

if (hasPivotsSpecifiedInTimeline)
{
var anims = pivotAnims.ToList().Join();
throw new ProjectParseException(
$"SCML format exception: There were pivot points specified in timelines rather than only on the sprites in anims {anims}. Aborting read.");
if (AllowInFramePivots)
{
Logger.Warn($"Encountered pivot points specified in timelines in anims {anims}. These pivot point changes will not be respected. Strict-mode is off. Converting anyway.");
}
else
{
throw new ProjectParseException($"SCML format exception: There were pivot points specified in timelines rather than only on the sprites in anims {anims}. Aborting read.");
}

}

AnimData.AnimCount = animCount;
Expand Down
12 changes: 10 additions & 2 deletions kanimal/Writer/ScmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ScmlWriter : Writer
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 bool AllowDuplicateSprites = true; // When true if a sprite is defined multiple times, only utilize the first definition.

public ScmlWriter(Reader reader)
{
Expand Down Expand Up @@ -97,7 +98,14 @@ protected virtual void AddFolderInfo()
var key = row.GetSpriteName().ToFilename();
if (filenameindex.ContainsKey(key))
{
throw new Exception($"filenameindex already contains a key for the sprite {key.Value}!");
if (AllowDuplicateSprites)
{
Logger.Warn($"Duplicate sprite for {key.Value}. This may be indicative of a problem with the source file.");
}
else
{
throw new Exception($"filenameindex already contains a key for the sprite {key.Value}! Try again without strict mode if you believe this is in error.");
}
}

filenameindex[key] = fileIndex;
Expand All @@ -112,7 +120,7 @@ protected virtual void AddFolderInfo()

var fileNode = Scml.CreateElement("file");
fileNode.SetAttribute("id", fileIndex.ToString());
fileNode.SetAttribute("name", $"{row.Name}_{row.Index}");
fileNode.SetAttribute("name", $"{row.Name}_{row.Index}.png");
fileNode.SetAttribute("width", ((int) row.Width).ToString());
fileNode.SetAttribute("height", ((int) row.Height).ToString());
fileNode.SetAttribute("pivot_x", pivotX.ToStringInvariant());
Expand Down

0 comments on commit 0e3b6c1

Please sign in to comment.