Home | History | Annotate | Download | only in tiledmappacker
      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.tiledmappacker;
     18 
     19 import java.awt.image.BufferedImage;
     20 import java.io.IOException;
     21 
     22 import javax.imageio.ImageIO;
     23 
     24 import com.badlogic.gdx.files.FileHandle;
     25 import com.badlogic.gdx.maps.tiled.TiledMapTileSet;
     26 import com.badlogic.gdx.math.Vector2;
     27 import com.badlogic.gdx.utils.IntMap;
     28 
     29 /** Contains extra information that can only be calculated after a Tiled Map's tile set images are loaded.
     30  * @author David Fraska */
     31 public class TileSetLayout {
     32 
     33 	public final BufferedImage image;
     34 	private final IntMap<Vector2> imageTilePositions;
     35 	private int numRows;
     36 	private int numCols;
     37 	public final int numTiles;
     38 	public final int firstgid;
     39 
     40 	/** Constructs a Tile Set layout. The tile set image contained in the baseDir should be the original tile set images before
     41 	 * being processed by {@link TiledMapPacker} (the ones actually read by Tiled).
     42 	 * @param tileset the tile set to process
     43 	 * @param baseDir the directory in which the tile set image is stored */
     44 	protected TileSetLayout (int firstgid, TiledMapTileSet tileset, FileHandle baseDir) throws IOException {
     45 		int tileWidth = tileset.getProperties().get("tilewidth", Integer.class);
     46 		int tileHeight = tileset.getProperties().get("tileheight", Integer.class);
     47 		int margin = tileset.getProperties().get("margin", Integer.class);
     48 		int spacing = tileset.getProperties().get("spacing", Integer.class);
     49 
     50 		this.firstgid = firstgid;
     51 
     52 		image = ImageIO.read(baseDir.child(tileset.getProperties().get("imagesource", String.class)).read());
     53 
     54 		imageTilePositions = new IntMap<Vector2>();
     55 
     56 		// fill the tile regions
     57 		int x, y, tile = 0;
     58 		numRows = 0;
     59 		numCols = 0;
     60 
     61 		int stopWidth = image.getWidth() - tileWidth;
     62 		int stopHeight = image.getHeight() - tileHeight;
     63 
     64 		for (y = margin; y <= stopHeight; y += tileHeight + spacing) {
     65 			for (x = margin; x <= stopWidth; x += tileWidth + spacing) {
     66 				if (y == margin) numCols++;
     67 				imageTilePositions.put(tile, new Vector2(x, y));
     68 				tile++;
     69 			}
     70 			numRows++;
     71 		}
     72 
     73 		numTiles = numRows * numCols;
     74 	}
     75 
     76 	public int getNumRows () {
     77 		return numRows;
     78 	}
     79 
     80 	public int getNumCols () {
     81 		return numCols;
     82 	}
     83 
     84 	/** Returns the location of the tile in {@link TileSetLayout#image} */
     85 	public Vector2 getLocation (int tile) {
     86 		return imageTilePositions.get(tile - firstgid);
     87 	}
     88 }
     89