From a57b85fd3f844bda356fab4be6330ff9c0f586c6 Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Wed, 3 Jul 2024 12:47:32 +0200 Subject: [PATCH] TextureAtlasEditor - Clean up Tile initialization --- PCK-Studio/Forms/Editor/TextureAtlasEditor.cs | 44 ++++++++----------- PCK-Studio/Internal/Json/TileInfo.cs | 4 +- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/PCK-Studio/Forms/Editor/TextureAtlasEditor.cs b/PCK-Studio/Forms/Editor/TextureAtlasEditor.cs index d36f07f7..f9bdfe87 100644 --- a/PCK-Studio/Forms/Editor/TextureAtlasEditor.cs +++ b/PCK-Studio/Forms/Editor/TextureAtlasEditor.cs @@ -136,34 +136,26 @@ namespace PckStudio.Forms.Editor var images = atlas.Split(_tileAreaSize, _imageLayout); - var tiles = images.enumerate().Select( - p => new AtlasTile( - p.index, + var tiles = images.enumerate().Select(MakeTile); - GetAtlasArea( - p.index, - tileInfos.IndexInRange(p.index) - ? tileInfos[p.index].Width : 1, - tileInfos.IndexInRange(p.index) - ? tileInfos[p.index].Height : 1, - _rowCount, - _columnCount, - _tileAreaSize, - _imageLayout), + AtlasTile MakeTile((int index, Image value) p) + { + int i = p.index; + JsonTileInfo tileInfo = tileInfos.IndexInRange(i) ? tileInfos[i] : null; - tileInfos.IndexInRange(p.index) - ? tileInfos[p.index] : null, + Rectangle atlasArea = GetAtlasArea(i, tileInfo?.TileWidth ?? 1, tileInfo?.TileHeight ?? 1, _rowCount, _columnCount, _tileAreaSize, _imageLayout); // get texture for tiles that are not 1x1 tiles - tileInfos.IndexInRange(p.index) - ? atlas.GetArea( - new Rectangle( - GetSelectedPoint(p.index, _rowCount, _columnCount, _imageLayout).X * _tileAreaSize.Width, - GetSelectedPoint(p.index, _rowCount, _columnCount, _imageLayout).Y * _tileAreaSize.Height, - tileInfos[p.index].Width * _tileAreaSize.Width, - tileInfos[p.index].Height * _tileAreaSize.Height)) - : p.value) - ); + Point selectedPoint = GetSelectedPoint(i, _rowCount, _columnCount, _imageLayout); + + var textureLocation = new Point(selectedPoint.X * _tileAreaSize.Width, selectedPoint.Y * _tileAreaSize.Height); + var textureSize = new Size(tileInfos[i].TileWidth * _tileAreaSize.Width, tileInfos[i].TileHeight * _tileAreaSize.Height); + var textureArea = new Rectangle(textureLocation, textureSize); + + Image texture = tileInfos.IndexInRange(i) ? atlas.GetArea(textureArea) : p.value; + return new AtlasTile(i, atlasArea, tileInfo, texture); + } + _tiles = new List(tiles); SelectedIndex = 0; @@ -369,11 +361,11 @@ namespace PckStudio.Forms.Editor }; } - private static Rectangle GetAtlasArea(int index, int width, int height, int rowCount, int columnCount, Size size, ImageLayoutDirection imageLayout) + private static Rectangle GetAtlasArea(int index, int tileWidth, int tileHeight, int rowCount, int columnCount, Size size, ImageLayoutDirection imageLayout) { var p = GetSelectedPoint(index, rowCount, columnCount, imageLayout); var ap = new Point(p.X * size.Width, p.Y * size.Height); - return new Rectangle(ap, new Size(size.Width * width, size.Height * height)); + return new Rectangle(ap, new Size(size.Width * tileWidth, size.Height * tileHeight)); } private static Point GetSelectedPoint(int index, int rowCount, int columnCount, ImageLayoutDirection imageLayout) diff --git a/PCK-Studio/Internal/Json/TileInfo.cs b/PCK-Studio/Internal/Json/TileInfo.cs index 54e27d2f..5c8aa704 100644 --- a/PCK-Studio/Internal/Json/TileInfo.cs +++ b/PCK-Studio/Internal/Json/TileInfo.cs @@ -16,10 +16,10 @@ namespace PckStudio.Internal.Json public string InternalName { get; set; } [JsonProperty("width")] - public int Width { get; set; } = 1; + public int TileWidth { get; set; } = 1; [JsonProperty("height")] - public int Height { get; set; } = 1; + public int TileHeight { get; set; } = 1; [JsonProperty("hasColourEntry", DefaultValueHandling = DefaultValueHandling.Populate)] public bool HasColourEntry { get; set; }