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> <ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0005" /> <PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0005" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0005" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,12 +1,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.IO; using System.IO;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using SixLabors.ImageSharp; using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats; 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 namespace RecrownedAthenaeum.Tools.TextureAtlas
{ {
@ -19,11 +20,8 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas
} }
int powLimit; int powLimit;
Rectangle theoreticalSpace;
Node masterNode; Node masterNode;
ImageHandler[] imageHandlers; Dictionary<string, ImageHandler> imageHandlers;
Queue<ImageHandler> imageHandlerQueue;
int textureLength; int textureLength;
/// <summary> /// <summary>
@ -36,7 +34,7 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas
{ {
this.powLimit = powLimit; this.powLimit = powLimit;
string[] paths = Directory.GetFiles(rootDirectoryPath); string[] paths = Directory.GetFiles(rootDirectoryPath);
int currentPoT = startingPower; textureLength = startingPower;
List<ImageHandler> imageHandlers = new List<ImageHandler>(); List<ImageHandler> imageHandlers = new List<ImageHandler>();
for (int pathID = 0; pathID < paths.Length; pathID++) for (int pathID = 0; pathID < paths.Length; pathID++)
{ {
@ -48,9 +46,11 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas
} }
} }
imageHandlers.Sort(); imageHandlers.Sort();
this.imageHandlers = imageHandlers.ToArray(); this.imageHandlers = new Dictionary<string, ImageHandler>();
imageHandlerQueue = new Queue<ImageHandler>(imageHandlers); foreach (ImageHandler imageHandler in imageHandlers)
textureLength = currentPoT; {
this.imageHandlers.Add(imageHandler.Name, imageHandler);
}
} }
/// <summary> /// <summary>
@ -62,7 +62,7 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas
masterNode = new Node(); masterNode = new Node();
masterNode.region.Width = textureLength; masterNode.region.Width = textureLength;
masterNode.region.Height = textureLength; masterNode.region.Height = textureLength;
Queue<ImageHandler> imageHandlerQueue = new Queue<ImageHandler>(imageHandlers.Values);
ImageHandler imageHandler; ImageHandler imageHandler;
while (imageHandlerQueue.TryDequeue(out imageHandler)) while (imageHandlerQueue.TryDequeue(out imageHandler))
{ {
@ -85,13 +85,50 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas
/// <summary> /// <summary>
/// Renders the build into a PNG file and generates the respective <see cref="TextureAtlasData"/> meant for serialization and later to be loaded. /// Renders the build into a PNG file and generates the respective <see cref="TextureAtlasData"/> meant for serialization and later to be loaded.
/// </summary> /// </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() public void Dispose()
{ {
ImageHandler[] imageHandlers = this.imageHandlers.Values.ToArray();
this.imageHandlers.Clear();
foreach (ImageHandler imageHandler in imageHandlers) foreach (ImageHandler imageHandler in imageHandlers)
{ {
imageHandler.Dispose(); imageHandler.Dispose();
@ -182,6 +219,7 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas
public int Width { get { return image.Width; } } public int Width { get { return image.Width; } }
public int Height { get { return image.Height; } } public int Height { get { return image.Height; } }
public int x, y; public int x, y;
public NinePatchData ninePatchData;
internal ImageHandler(String path) internal ImageHandler(String path)
{ {