From 26a4850d2a49b49eee3a2aa14414b923b677e3a9 Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Sat, 29 Dec 2018 00:30:09 -0600 Subject: [PATCH] Added basic ninepatch file generator command. --- .../NinePatchTools/NinePatchCommand.cs | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 RecrownedAthenaeum.ConsoleTools/NinePatchTools/NinePatchCommand.cs diff --git a/RecrownedAthenaeum.ConsoleTools/NinePatchTools/NinePatchCommand.cs b/RecrownedAthenaeum.ConsoleTools/NinePatchTools/NinePatchCommand.cs new file mode 100644 index 0000000..10fb171 --- /dev/null +++ b/RecrownedAthenaeum.ConsoleTools/NinePatchTools/NinePatchCommand.cs @@ -0,0 +1,50 @@ +using Newtonsoft.Json; +using RecrownedAthenaeum.Pipeline.NinePatch; +using RecrownedAthenaeum.Tools.CommandProcessor; +using System; +using System.IO; + +namespace RecrownedAthenaeum.Tools.NinePatchTools +{ + public class NinePatchCommand : EngineCommand + { + public NinePatchCommand() : base("9p", "ninepatch", "9patch") + { + help = "Generates a 9 patch file for a given image."; + + arguments = new EngineCommandArgument[6]; + + arguments[0] = new EngineCommandArgument("-i", "defines the path to the texture to be used for the nine patch."); + arguments[1] = new EngineCommandArgument("-o", "defines path of output file."); + arguments[2] = new EngineCommandArgument("-l", "left patch."); + arguments[3] = new EngineCommandArgument("-r", "right patch."); + arguments[4] = new EngineCommandArgument("-u", "up patch."); + arguments[5] = new EngineCommandArgument("-d", "down patch."); + } + + public override void Run(string[] arguments = null) + { + if (arguments == null) throw new ArgumentException("Missing arguments. Type \"help 9p\" for more information."); + int leftBound = 0, rightBound = 0, topBound = 0, bottomBound = 0; + string imagePath, outPath; + if (IndexOfArgumentIn("-i", arguments) + 1 >= arguments.Length) throw new ArgumentException("Missing -i argument path."); + imagePath = arguments[IndexOfArgumentIn("-i", arguments) + 1]; + + if (IndexOfArgumentIn("-o", arguments) + 1 >= arguments.Length) throw new ArgumentException("Missing -o argument path."); + outPath = arguments[IndexOfArgumentIn("-o", arguments) + 1]; + + if (IndexOfArgumentIn("-l", arguments) + 1 >= arguments.Length && !int.TryParse(arguments[IndexOfArgumentIn("-l", arguments) + 1], out leftBound)) throw new ArgumentException("Missing -l argument bound."); + if (IndexOfArgumentIn("-r", arguments) + 1 >= arguments.Length && !int.TryParse(arguments[IndexOfArgumentIn("-r", arguments) + 1], out rightBound)) throw new ArgumentException("Missing -r argument bound."); + if (IndexOfArgumentIn("-u", arguments) + 1 >= arguments.Length && !int.TryParse(arguments[IndexOfArgumentIn("-u", arguments) + 1], out topBound)) throw new ArgumentException("Missing -u argument bound."); + if (IndexOfArgumentIn("-d", arguments) + 1 >= arguments.Length && !int.TryParse(arguments[IndexOfArgumentIn("-d", arguments) + 1], out bottomBound)) throw new ArgumentException("Missing -d argument bound."); + + NinePatchData npData = new NinePatchData(Path.GetFileName(imagePath), leftBound, rightBound, bottomBound, topBound); + string serialized = JsonConvert.SerializeObject(npData, Formatting.Indented); + + using (StringWriter stringWriter = new StringWriter()) + { + stringWriter.WriteLine(serialized); + } + } + } +}