diff --git a/RecrownedAthenaeum.ConsoleTools/RecrownedAthenaeum.Tools.csproj b/RecrownedAthenaeum.ConsoleTools/RecrownedAthenaeum.Tools.csproj index e72828c..8b581b6 100644 --- a/RecrownedAthenaeum.ConsoleTools/RecrownedAthenaeum.Tools.csproj +++ b/RecrownedAthenaeum.ConsoleTools/RecrownedAthenaeum.Tools.csproj @@ -11,6 +11,7 @@ + diff --git a/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePacker.cs b/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePacker.cs index 025ad3b..353c3a4 100644 --- a/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePacker.cs +++ b/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePacker.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; -using System.Drawing; using System.IO; -using System.Text; -using System.Drawing.Drawing2D; -using System.Drawing.Imaging; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; +using RecrownedAthenaeum.Pipeline.TextureAtlas; +using RecrownedAthenaeum.Pipeline.NinePatch; +using SixLabors.ImageSharp.Processing; +using SixLabors.Primitives; +using System.Linq; namespace RecrownedAthenaeum.Tools.TextureAtlas { @@ -19,11 +20,8 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas } int powLimit; - - Rectangle theoreticalSpace; Node masterNode; - ImageHandler[] imageHandlers; - Queue imageHandlerQueue; + Dictionary imageHandlers; int textureLength; /// @@ -36,7 +34,7 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas { this.powLimit = powLimit; string[] paths = Directory.GetFiles(rootDirectoryPath); - int currentPoT = startingPower; + textureLength = startingPower; List imageHandlers = new List(); for (int pathID = 0; pathID < paths.Length; pathID++) { @@ -48,9 +46,11 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas } } imageHandlers.Sort(); - this.imageHandlers = imageHandlers.ToArray(); - imageHandlerQueue = new Queue(imageHandlers); - textureLength = currentPoT; + this.imageHandlers = new Dictionary(); + foreach (ImageHandler imageHandler in imageHandlers) + { + this.imageHandlers.Add(imageHandler.Name, imageHandler); + } } /// @@ -62,7 +62,7 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas masterNode = new Node(); masterNode.region.Width = textureLength; masterNode.region.Height = textureLength; - + Queue imageHandlerQueue = new Queue(imageHandlers.Values); ImageHandler imageHandler; while (imageHandlerQueue.TryDequeue(out imageHandler)) { @@ -85,13 +85,50 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas /// /// Renders the build into a PNG file and generates the respective meant for serialization and later to be loaded. /// - public void Save() + /// + public void Save(string output) { + GraphicsOptions gOptions = new GraphicsOptions(); + TextureAtlasData.TextureAtlasRegion[] regions = new TextureAtlasData.TextureAtlasRegion[imageHandlers.Count]; + + using (Image atlasTexture = new Image(textureLength, textureLength)) + { + ImageHandler[] imageHandlers = this.imageHandlers.Values.ToArray(); + + for (int i = 0; i < imageHandlers.Length; i++) + { + regions[i] = new TextureAtlasData.TextureAtlasRegion(); + ImageHandler imageH = imageHandlers[i]; + regions[i].SetBounds(imageH.x, imageH.y, imageH.Width, imageH.Height); + regions[i].ninePatchData = imageH.ninePatchData; + atlasTexture.Mutate(img => img.DrawImage(gOptions, imageH.image, new Point(imageH.x, imageH.y))); + } + using (FileStream stream = new FileStream(output, FileMode.Create)) + { + atlasTexture.SaveAsPng(stream); + } + } + + + } + + public void SetNinePatch(string fileName, int a, int b, int c, int d) + { + ImageHandler imageHandler = imageHandlers[fileName]; + NinePatchData ninePatchData = new NinePatchData(fileName, a, b, c, d); + imageHandler.ninePatchData = ninePatchData; + } + + public void RemoveNinePatch(string fileName) + { + imageHandlers[fileName].ninePatchData = null; } public void Dispose() { + ImageHandler[] imageHandlers = this.imageHandlers.Values.ToArray(); + this.imageHandlers.Clear(); foreach (ImageHandler imageHandler in imageHandlers) { imageHandler.Dispose(); @@ -182,6 +219,7 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas public int Width { get { return image.Width; } } public int Height { get { return image.Height; } } public int x, y; + public NinePatchData ninePatchData; internal ImageHandler(String path) {