Switched to dependency injection for console use.

Created interfaces for input and output for better structure and easier testing.
This commit is contained in:
2020-02-23 20:51:39 -05:00
parent 72a1ba903b
commit fa74bc9ae5
12 changed files with 119 additions and 105 deletions

View File

@@ -7,9 +7,8 @@ using SixLabors.Primitives;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using RecrownedGTK.Tools.CommandProcessor;
namespace RecrownedGTK.Tools.TextureAtlas
{
@@ -34,7 +33,7 @@ namespace RecrownedGTK.Tools.TextureAtlas
/// <param name="rootDirectoryPath">Path to textures.</param>
/// <param name="powLimit">Power of two limit for auto expanding texture. Default is 12 which is a 4096x4096 texture.</param>
/// <param name="startingPower">What power to start at and build up from. Default is 8 which is a 256x256 texture.</param>
internal TexturePacker(string rootDirectoryPath, int powLimit = 12, int startingPower = 8)
internal TexturePacker(IUserOutput userOutput, string rootDirectoryPath, int powLimit = 12, int startingPower = 8)
{
this.powLimit = powLimit;
string[] paths;
@@ -54,7 +53,7 @@ namespace RecrownedGTK.Tools.TextureAtlas
SupportedExtensions extension;
if (Enum.TryParse(Path.GetExtension(paths[pathID]).ToLower().Substring(1), out extension))
{
ConsoleUtilities.WriteWrappedLine("Reading texture data for: " + paths[pathID]);
userOutput.WrappedOutput("Reading texture data for: " + paths[pathID]);
ImageHandler image = new ImageHandler(paths[pathID]);
imageHandlers.Add(image);
minAreaRequired += image.Area;
@@ -66,7 +65,7 @@ namespace RecrownedGTK.Tools.TextureAtlas
else if (Path.GetExtension(paths[pathID]).ToLower() == ".9p")
{
if (ninePatchDictionary == null) ninePatchDictionary = new Dictionary<string, NinePatchInfo>();
ConsoleUtilities.WriteWrappedLine("Reading ninepatch data for: " + paths[pathID]);
userOutput.WrappedOutput("Reading ninepatch data for: " + paths[pathID]);
string serialized = File.ReadAllText(paths[pathID]);
NinePatchInfo npData = JsonConvert.DeserializeObject<NinePatchInfo>(serialized);
ninePatchDictionary.Add(npData.name, npData);

View File

@@ -23,7 +23,7 @@ namespace RecrownedGTK.Tools.TextureAtlasTools
};
}
public override void Run(string[] arguments)
public override void Run(IUserInput userInput, IUserOutput userOutput, string[] arguments)
{
TexturePacker texturePacker = null;
@@ -36,29 +36,29 @@ namespace RecrownedGTK.Tools.TextureAtlasTools
if (HasArgument("-interactive", arguments))
{
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();
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();
if (input == "q") return;
path = input.Replace("\"", "");
ConsoleUtilities.WriteWrappedLine("Please enter output path of file name.");
input = Console.ReadLine();
userOutput.WrappedOutput("Please enter output path of file name.");
input = userInput.GetInput();
if (input == "q") return;
output = input.Replace("\"", "");
do
{
ConsoleUtilities.WriteWrappedLine("Please enter a valid starting power of two for the lengths of the resulting texture atlas. Leave blank for default of " + sp + ".");
input = Console.ReadLine();
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();
if (input == "q") return;
if (input.Length == 0) break;
} while (!int.TryParse(input, out sp));
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();
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();
if (input == "q") return;
if (input.Length == 0) break;
} while (!int.TryParse(input, out mp));
@@ -83,8 +83,8 @@ namespace RecrownedGTK.Tools.TextureAtlasTools
if (HasArgument("-dau", arguments)) dau = true;
}
texturePacker = new TexturePacker(path, mp, sp);
ConsoleUtilities.WriteWrappedLine("Calculated minimum texture size: " + texturePacker.TextureLength + "x" + texturePacker.TextureLength + " with a total of " + texturePacker.TexturesFound + " textures.");
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.");
try
{
@@ -112,7 +112,7 @@ namespace RecrownedGTK.Tools.TextureAtlasTools
}
}
texturePacker.Save(Path.GetDirectoryName(output), Path.GetFileName(output));
Console.WriteLine("Complete. Final texture size: " + texturePacker.TextureLength + "x" + texturePacker.TextureLength + ".");
userOutput.Output("Complete. Final texture size: " + texturePacker.TextureLength + "x" + texturePacker.TextureLength + ".");
}
}
}