recrownedgtk/RecrownedGTK.Tools/TextureAtlasTools/TexturePackerCommand.cs

119 lines
6.2 KiB
C#
Raw Normal View History

2020-02-17 02:44:21 +00:00
using RecrownedGTK.Tools.CommandProcessor;
using RecrownedGTK.Tools.TextureAtlas;
using System;
using System.IO;
namespace RecrownedGTK.Tools.TextureAtlasTools
{
class TexturePackerCommand : EngineCommand
{
private const int DMP = 9;
private const int DSP = 6;
public TexturePackerCommand() : base("texturepacker")
{
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.";
commandArguments = new[] {
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\"."),
new EngineCommandArgument("-sp", "Starting power for one side of the texture. Default is " + DSP + "."),
new EngineCommandArgument("-mp", "Maximum power for one side of the texture. Default is " + DMP + "."),
new EngineCommandArgument("-dau", "Disables automatically upscaling the texture."),
};
}
public override void Run(IUserInput userInput, IUserOutput userOutput, string[] arguments)
2020-02-17 02:44:21 +00:00
{
TexturePacker texturePacker = null;
string path = null;
int mp = DMP;
int sp = DSP;
bool dau = false;
string output = null;
if (HasArgument("-interactive", arguments))
{
string input;
userOutput.WrappedOutput("Texture packer interactive mode triggered. Type \"q\" at any time to exit.");
userOutput.WrappedOutput("Please enter path of folder containing the textures to be packed.");
input = userInput.GetInput();
2020-02-17 02:44:21 +00:00
if (input == "q") return;
path = input.Replace("\"", "");
userOutput.WrappedOutput("Please enter output path of file name.");
input = userInput.GetInput();
2020-02-17 02:44:21 +00:00
if (input == "q") return;
output = input.Replace("\"", "");
do
{
userOutput.WrappedOutput("Please enter a valid starting power of two for the lengths of the resulting texture atlas. Leave blank for default of " + sp + ".");
input = userInput.GetInput();
2020-02-17 02:44:21 +00:00
if (input == "q") return;
if (input.Length == 0) break;
} while (!int.TryParse(input, out sp));
do
{
userOutput.WrappedOutput("Please enter a valid maximum power by two of the lengths of the resulting texture atlas. Leave blank for default of " + mp + ".");
input = userInput.GetInput();
2020-02-17 02:44:21 +00:00
if (input == "q") return;
if (input.Length == 0) break;
} while (!int.TryParse(input, out mp));
}
else
{
int indexOfInputArg = IndexOfArgument("-i", arguments);
if (indexOfInputArg + 1 >= arguments.Length) throw new ArgumentException("-i is not followed by input path.");
path = arguments[1 + IndexOfArgument("-i", arguments)];
if (HasArgument("-mp", arguments))
{
int.TryParse(arguments[IndexOfArgument("-mp", arguments) + 1], out mp);
}
if (HasArgument("-sp", arguments))
{
int.TryParse(arguments[IndexOfArgument("-sp", arguments) + 1], out sp);
}
int indexOfOutputArg = IndexOfArgument("-o", arguments);
if (indexOfOutputArg + 1 >= arguments.Length) throw new ArgumentException("-o is not followed by input path.");
output = arguments[IndexOfArgument("-o", arguments) + 1];
if (HasArgument("-dau", arguments)) dau = true;
}
texturePacker = new TexturePacker(userOutput, path, mp, sp);
userOutput.WrappedOutput("Calculated minimum texture size: " + texturePacker.TextureLength + "x" + texturePacker.TextureLength + " with a total of " + texturePacker.TexturesFound + " textures.");
2020-02-17 02:44:21 +00:00
try
{
texturePacker.Build(!dau);
}
catch (InvalidOperationException e)
{
throw new ArgumentException(e.Message);
}
for (int i = 0; i < arguments.Length; i++)
{
if (arguments[i] == "-9p")
{
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.)");
string[] nPatchArgs = arguments[i + 1].Split(',');
try
{
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.");
}
}
}
texturePacker.Save(Path.GetDirectoryName(output), Path.GetFileName(output));
userOutput.Output("Complete. Final texture size: " + texturePacker.TextureLength + "x" + texturePacker.TextureLength + ".");
2020-02-17 02:44:21 +00:00
}
}
}