texture packer save and ninepatch controls added.

This commit is contained in:
Harrison Deng 2018-12-07 21:48:02 -06:00
parent cf964cfe76
commit 901f900527
2 changed files with 53 additions and 14 deletions

View File

@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0005" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0005" />
</ItemGroup>
<ItemGroup>

View File

@ -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<ImageHandler> imageHandlerQueue;
Dictionary<string, ImageHandler> imageHandlers;
int textureLength;
/// <summary>
@ -36,7 +34,7 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas
{
this.powLimit = powLimit;
string[] paths = Directory.GetFiles(rootDirectoryPath);
int currentPoT = startingPower;
textureLength = startingPower;
List<ImageHandler> imageHandlers = new List<ImageHandler>();
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<ImageHandler>(imageHandlers);
textureLength = currentPoT;
this.imageHandlers = new Dictionary<string, ImageHandler>();
foreach (ImageHandler imageHandler in imageHandlers)
{
this.imageHandlers.Add(imageHandler.Name, imageHandler);
}
}
/// <summary>
@ -62,7 +62,7 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas
masterNode = new Node();
masterNode.region.Width = textureLength;
masterNode.region.Height = textureLength;
Queue<ImageHandler> imageHandlerQueue = new Queue<ImageHandler>(imageHandlers.Values);
ImageHandler imageHandler;
while (imageHandlerQueue.TryDequeue(out imageHandler))
{
@ -85,13 +85,50 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas
/// <summary>
/// Renders the build into a PNG file and generates the respective <see cref="TextureAtlasData"/> meant for serialization and later to be loaded.
/// </summary>
public void Save()
/// <param name="output"></param>
public void Save(string output)
{
GraphicsOptions gOptions = new GraphicsOptions();
TextureAtlasData.TextureAtlasRegion[] regions = new TextureAtlasData.TextureAtlasRegion[imageHandlers.Count];
using (Image<Rgba32> atlasTexture = new Image<Rgba32>(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)
{