diff --git a/OpenRa.Game/TileSheetBuilder.cs b/OpenRa.Game/TileSheetBuilder.cs index 0c3c98b554..caf5893ac9 100644 --- a/OpenRa.Game/TileSheetBuilder.cs +++ b/OpenRa.Game/TileSheetBuilder.cs @@ -6,25 +6,48 @@ using System.Drawing; namespace OpenRa.Game { // T is probably going to be BluntDirectX.Direct3D.Texture - - public delegate T Provider(); public class TileSheetBuilder where T : class { - readonly Size pageSize; + readonly SizeF pageSize; readonly Provider pageProvider; - public TileSheetBuilder(Size pageSize, Provider pageProvider) + public TileSheetBuilder(SizeF pageSize, Provider pageProvider) { this.pageSize = pageSize; this.pageProvider = pageProvider; } - public SheetRectangle AddImage(Size imageSize) + T current = null; + float x = 0, y = 0, rowHeight = 0; + + public SheetRectangle AddImage(SizeF imageSize) { - throw new NotImplementedException(); + if (imageSize.Width > pageSize.Width || imageSize.Height > pageSize.Height) + return null; + + if (current == null) + current = pageProvider(); + + if (imageSize.Height > rowHeight || rowHeight == 0 || imageSize.Width + x > pageSize.Width) + { + y += rowHeight; + rowHeight = imageSize.Height; + x = 0; + } + + if (y + imageSize.Height > pageSize.Height) + { + current = pageProvider(); + x = y = rowHeight = 0; + } + + SheetRectangle rect = new SheetRectangle(current, new PointF(x, y), imageSize); + x += imageSize.Width; + + return rect; } }