recrownedathenaeum/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs

115 lines
6.0 KiB
C#
Raw Normal View History

using RecrownedAthenaeum.Tools.CommandProcessor;
2018-12-08 23:06:11 +00:00
using RecrownedAthenaeum.Tools.TextureAtlas;
using System;
using System.Collections.Generic;
2018-12-08 23:06:11 +00:00
using System.IO;
using System.Text;
namespace RecrownedAthenaeum.Tools.TextureAtlasTools
{
class TexturePackerCommand : EngineCommand
{
2018-12-09 03:28:58 +00:00
2018-12-29 00:52:52 +00:00
public TexturePackerCommand() : base("texturepacker")
2018-12-09 03:28:58 +00:00
{
help = "Packs a given directory composed of png and jpg files into an atlas. Can also add 9patch properties. Images with the associated \".9p\" files will automatically be defined in the resulting .tatlas file, but can be overwritten by use of the \"-9p\" argument.";
arguments = new EngineCommandArgument[7] {
new EngineCommandArgument("-interactive", "runs in interactive mode. Ninepatches must still be defined with arguments or previously defined with associated \".9p\" files. Other arguments will be ignored."),
new EngineCommandArgument("-i", "for input directory containing the textures.", true),
new EngineCommandArgument("-o", "Path for output files. Points to non-existent file. Will create texture and definitions file with name.", true),
new EngineCommandArgument("-9p", "Can be used multiple times for defining a 9patch. This parameter requires a name, left patch, right patch, top patch, and bottom patch in the format name,a,b,c,d."),
2018-12-29 00:52:52 +00:00
new EngineCommandArgument("-sp", "Starting power for one side of the texture. Default is 8."),
new EngineCommandArgument("-mp", "Maximum power for one side of the texture. Default is 8."),
new EngineCommandArgument("-dau", "Disables automatically upscaling the texture."),
};
2018-12-09 03:28:58 +00:00
}
public override void Run(string[] arguments)
{
2018-12-29 00:52:52 +00:00
TexturePacker texturePacker = null;
string path = null;
int mp = 9;
int sp = 6;
bool dau = false;
string output = null;
if (HasArgument("-interactive", arguments))
2018-12-08 23:06:11 +00:00
{
string input;
ConsoleUtilities.WriteWrappedLine("Texture packer interactive mode triggered. Type \"q\" at any time to exit.");
ConsoleUtilities.WriteWrappedLine("Please enter path of folder containing the textures to be packed.");
input = Console.ReadLine();
if (input == "q") return;
path = input;
ConsoleUtilities.WriteWrappedLine("Please enter output path of file name.");
input = Console.ReadLine();
if (input == "q") return;
output = input;
do
{
ConsoleUtilities.WriteWrappedLine("Please enter a valid starting power of two of the lengths of the resulting texture atlas. Leave blank for default of " + sp + ".");
input = Console.ReadLine();
if (input == "q") return;
if (input.Length == 0) break;
} while (!int.TryParse(input, out sp));
2018-12-29 00:52:52 +00:00
do
{
ConsoleUtilities.WriteWrappedLine("Please enter a valid maximum power by two of the lengths of the resulting texture atlas. Leave blank for default of " + mp + ".");
input = Console.ReadLine();
if (input == "q") return;
if (input.Length == 0) break;
} while (!int.TryParse(input, out mp));
}
else
{
int indexOfInputArg = IndexOfArgumentIn("-i", arguments);
if (indexOfInputArg + 1 >= arguments.Length) throw new ArgumentException("-i is not followed by input path.");
path = arguments[1 + IndexOfArgumentIn("-i", arguments)];
int.TryParse(arguments[IndexOfArgumentIn("-mp", arguments) + 1], out mp);
int.TryParse(arguments[IndexOfArgumentIn("-sp", arguments) + 1], out sp);
int indexOfOutputArg = IndexOfArgumentIn("-o", arguments);
if (indexOfOutputArg + 1 >= arguments.Length) throw new ArgumentException("-o is not followed by input path.");
output = arguments[IndexOfArgumentIn("-o", arguments) + 1];
if (HasArgument("-dau", arguments)) dau = true;
}
2018-12-29 00:52:52 +00:00
texturePacker = new TexturePacker(path, mp, sp);
ConsoleUtilities.WriteWrappedLine("Calculated minimum texture size: " + texturePacker.TextureLength + "x" + texturePacker.TextureLength + " with a total of " + texturePacker.TexturesFound + " textures.");
2018-12-29 00:52:52 +00:00
try
2018-12-08 23:06:11 +00:00
{
2018-12-29 00:52:52 +00:00
texturePacker.Build(!dau);
}
catch (InvalidOperationException e)
{
throw new ArgumentException(e.Message);
}
2018-12-29 00:52:52 +00:00
for (int i = 0; i < arguments.Length; i++)
{
if (arguments[i] == "-9p")
2018-12-08 23:06:11 +00:00
{
2019-01-08 20:58:57 +00:00
if (i + 1 >= arguments.Length) throw new ArgumentException("-9p is not followed by proper specifiers for a 9Patch (format: \"-9p textureName,a,b,c,d\" where a, b, c, and d are integers definining the border regions for the 9patch.)");
2018-12-29 00:52:52 +00:00
string[] nPatchArgs = arguments[i + 1].Split(',');
try
2018-12-08 23:06:11 +00:00
{
2018-12-29 00:52:52 +00:00
texturePacker.SetNinePatch(nPatchArgs[0], int.Parse(nPatchArgs[1]), int.Parse(nPatchArgs[2]), int.Parse(nPatchArgs[3]), int.Parse(nPatchArgs[4]));
}
catch (FormatException)
{
throw new ArgumentException("-9p argument parameters must be in the format \"-9p textureName,a,b,c,d\" where a, b, c, and d are integers definining the border regions for the 9patch.");
2018-12-08 23:06:11 +00:00
}
}
}
2018-12-29 00:52:52 +00:00
texturePacker.Save(Path.GetDirectoryName(output), Path.GetFileName(output));
Console.WriteLine("Complete. Final texture size: " + texturePacker.TextureLength + "x" + texturePacker.TextureLength + ".");
}
}
}