1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tools.texturepacker; 18 19 import com.badlogic.gdx.tools.texturepacker.TexturePacker.Packer; 20 import com.badlogic.gdx.tools.texturepacker.TexturePacker.Page; 21 import com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect; 22 import com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings; 23 import com.badlogic.gdx.utils.Array; 24 25 import java.util.Collections; 26 27 /** @author Nathan Sweet */ 28 public class GridPacker implements Packer { 29 private Settings settings; 30 GridPacker(Settings settings)31 public GridPacker (Settings settings) { 32 this.settings = settings; 33 } 34 pack(Array<Rect> inputRects)35 public Array<Page> pack (Array<Rect> inputRects) { 36 if (!settings.silent) System.out.print("Packing"); 37 38 int cellWidth = 0, cellHeight = 0; 39 for (int i = 0, nn = inputRects.size; i < nn; i++) { 40 Rect rect = inputRects.get(i); 41 cellWidth = Math.max(cellWidth, rect.width); 42 cellHeight = Math.max(cellHeight, rect.height); 43 } 44 cellWidth += settings.paddingX; 45 cellHeight += settings.paddingY; 46 47 inputRects.reverse(); 48 49 Array<Page> pages = new Array(); 50 while (inputRects.size > 0) { 51 Page result = packPage(inputRects, cellWidth, cellHeight); 52 pages.add(result); 53 } 54 return pages; 55 } 56 packPage(Array<Rect> inputRects, int cellWidth, int cellHeight)57 private Page packPage (Array<Rect> inputRects, int cellWidth, int cellHeight) { 58 Page page = new Page(); 59 page.outputRects = new Array(); 60 61 int maxWidth = settings.maxWidth, maxHeight = settings.maxHeight; 62 if (settings.edgePadding) { 63 maxWidth -= settings.paddingX; 64 maxHeight -= settings.paddingY; 65 } 66 int x = 0, y = 0; 67 for (int i = inputRects.size - 1; i >= 0; i--) { 68 if (x + cellWidth > maxWidth) { 69 y += cellHeight; 70 if (y > maxHeight - cellHeight) break; 71 x = 0; 72 } 73 Rect rect = inputRects.removeIndex(i); 74 rect.x = x; 75 rect.y = y; 76 rect.width += settings.paddingX; 77 rect.height += settings.paddingY; 78 page.outputRects.add(rect); 79 x += cellWidth; 80 page.width = Math.max(page.width, x); 81 page.height = Math.max(page.height, y + cellHeight); 82 } 83 84 // Flip so rows start at top. 85 for (int i = page.outputRects.size - 1; i >= 0; i--) { 86 Rect rect = page.outputRects.get(i); 87 rect.y = page.height - rect.y - rect.height; 88 } 89 return page; 90 } 91 } 92