/* * Tiled Map Editor, (c) 2004-2006 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Adam Turk <aturk@biggeruniverse.com> * Bjorn Lindeijer <bjorn@lindeijer.nl> */ package tiled.mapeditor.util.cutter; import java.awt.Dimension; import java.awt.Image; import java.awt.image.BufferedImage; /** * Cuts tiles from a tileset image according to a regular rectangular pattern. * Supports a variable spacing between tiles and a margin around them. */ public class BasicTileCutter implements TileCutter { private int nextX, nextY; private BufferedImage image; private final int tileWidth; private final int tileHeight; private final int tileSpacing; private final int tileMargin; public BasicTileCutter(int tileWidth, int tileHeight, int tileSpacing, int tileMargin) { this.tileWidth = tileWidth; this.tileHeight = tileHeight; this.tileSpacing = tileSpacing; this.tileMargin = tileMargin; reset(); } public String getName() { return "Basic"; } public void setImage(BufferedImage image) { this.image = image; } public Image getNextTile() { if (nextY + tileHeight + tileMargin <= image.getHeight()) { BufferedImage tile = image.getSubimage(nextX, nextY, tileWidth, tileHeight); nextX += tileWidth + tileSpacing; if (nextX + tileWidth + tileMargin > image.getWidth()) { nextX = tileMargin; nextY += tileHeight + tileSpacing; } return tile; } return null; } public void reset() { nextX = tileMargin; nextY = tileMargin; } public Dimension getTileDimensions() { return new Dimension(tileWidth, tileHeight); } /** * Returns the spacing between tile images. * @return the spacing between tile images. */ public int getTileSpacing() { return tileSpacing; } /** * Returns the margin around the tile images. * @return the margin around the tile images. */ public int getTileMargin() { return tileMargin; } /** * Returns the number of tiles per row in the tileset image. * @return the number of tiles per row in the tileset image. */ public int getTilesPerRow() { return (image.getWidth() - 2 * tileMargin + tileSpacing) / (tileWidth + tileSpacing); } }