📄 tileset.java
字号:
/* * 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 <b.lindeijer@xs4all.nl> */package tiled.core;import java.awt.*;import java.awt.image.BufferedImage;import java.awt.image.FilteredImageSource;import java.io.File;import java.io.IOException;import java.util.Enumeration;import java.util.Iterator;import java.util.Properties;import java.util.Vector;import javax.imageio.ImageIO;import tiled.mapeditor.util.cutter.TileCutter;import tiled.mapeditor.util.cutter.BasicTileCutter;import tiled.mapeditor.util.TransparentImageFilter;import tiled.util.NumberedSet;/** * <p>TileSet handles operations on tiles as a set, or group. It has several * advanced internal functions aimed at reducing unnecessary data replication. * A 'tile' is represented internally as two distinct pieces of data. The * first and most important is a {@link Tile} object, and these are held in * a {@link Vector}.</p> * * <p>The other is the tile image.</p> * * @version $Id: TileSet.java 683 2006-06-25 14:17:37Z bjorn $ */public class TileSet{ private String base; private NumberedSet tiles, images; private int firstGid; private long tilebmpFileLastModified; private TileCutter tileCutter; private Rectangle tileDimensions; private int tileSpacing; private String externalSource; private File tilebmpFile; private String name; private Color transparentColor; private Properties defaultTileProperties; private Image tileSetImage; /** * Default constructor */ public TileSet() { tiles = new NumberedSet(); images = new NumberedSet(); tileDimensions = new Rectangle(); defaultTileProperties = new Properties(); } /** * Creates a tileset from a tileset image file. * * @param imgFilename * @param cutter * @throws IOException * @see TileSet#importTileBitmap(BufferedImage, TileCutter) */ public void importTileBitmap(String imgFilename, TileCutter cutter) throws IOException { setTilesetImageFilename(imgFilename); Image image = ImageIO.read(new File(imgFilename)); if (image == null) { throw new IOException("Failed to load " + tilebmpFile); } Toolkit tk = Toolkit.getDefaultToolkit(); if (transparentColor != null) { int rgb = transparentColor.getRGB(); image = tk.createImage( new FilteredImageSource(image.getSource(), new TransparentImageFilter(rgb))); } BufferedImage buffered = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); buffered.getGraphics().drawImage(image, 0, 0, null); importTileBitmap(buffered, cutter); } /** * Creates a tileset from a buffered image. Tiles are cut by the passed * cutter. * * @param tilebmp the image to be used, must not be null * @param cutter the tile cutter, must not be null */ private void importTileBitmap(BufferedImage tilebmp, TileCutter cutter) { assert tilebmp != null; assert cutter != null; tileCutter = cutter; tileSetImage = tilebmp; tileDimensions = new Rectangle(cutter.getTileDimensions()); if (cutter instanceof BasicTileCutter) { tileSpacing = ((BasicTileCutter) cutter).getTileSpacing(); } cutter.setImage(tilebmp); Image tile = cutter.getNextTile(); while (tile != null) { Tile newTile = new Tile(); newTile.setImage(addImage(tile)); addNewTile(newTile); tile = cutter.getNextTile(); } } public void checkUpdate() { if (tilebmpFile != null && tilebmpFile.lastModified() > tilebmpFileLastModified) { } } /** * Sets the URI path of the external source of this tile set. By setting * this, the set is implied to be external in all other operations. * * @param source a URI of the tileset image file */ public void setSource(String source) { externalSource = source; } /** * Sets the base directory for the tileset * * @param base a String containing the native format directory */ public void setBaseDir(String base) { this.base = base; } /** * Sets the filename of the tileset image. Doesn't change the tileset in * any other way. * * @param name */ public void setTilesetImageFilename(String name) { if (name != null) { tilebmpFile = new File(name); tilebmpFileLastModified = tilebmpFile.lastModified(); } else { tilebmpFile = null; } } /** * Sets the first global id used by this tileset. * * @param f first global id */ public void setFirstGid(int f) { firstGid = f; } /** * Sets the name of this tileset. * * @param name the new name for this tileset */ public void setName(String name) { this.name = name; } /** * Sets the transparent color in the tileset image. * * @param color */ public void setTransparentColor(Color color) { transparentColor = color; } /** * Adds the tile to the set, setting the id of the tile only if the current * value of id is -1. * * @param t the tile to add * @return int The <b>local</b> id of the tile */ public int addTile(Tile t) { if (t.getId() < 0) { t.setId(tiles.getMaxId()); } if (tileDimensions.width < t.getWidth()) { tileDimensions.width = t.getWidth(); } if (tileDimensions.height < t.getHeight()) { tileDimensions.height = t.getHeight(); } // Add any default properties // TODO: use parent properties instead? t.getProperties().putAll(defaultTileProperties); tiles.put(t.getId(), t); t.setTileSet(this); return t.getId(); } /** * This method takes a new Tile object as argument, and in addition to * the functionality of <code>addTile()</code>, sets the id of the tile * to -1. * * @see TileSet#addTile(Tile) * @param t the new tile to add. */ public void addNewTile(Tile t) { t.setId(-1); addTile(t); } /** * Removes a tile from this tileset. Does not invalidate other tile * indices. Removal is simply setting the reference at the specified * index to <b>null</b> * * @param i the index to remove */ public void removeTile(int i) { tiles.remove(i); } /** * Returns the amount of tiles in this tileset. * * @return the amount of tiles in this tileset */ public int size() { return tiles.size(); } /** * Returns the maximum tile id. * * @return the maximum tile id, or -1 when there are no tiles */ public int getMaxTileId() { return tiles.getMaxId(); } /** * Returns an iterator over the tiles in this tileset. * * @return an iterator over the tiles in this tileset. */ public Iterator iterator() { return tiles.iterator(); } /** * Generates a vector that removes the gaps that can * occur if a tile is removed from the middle of a set * of tiles. (Maps tiles contiguously) * * @return a {@link Vector} mapping ordered set location to the next non-null tile
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -