/** * Returns a scaled instance of the tile image. Using a MediaTracker instance, this function waits * until the scaling operation is done. * * <p>Internally it caches the scaled image in order to optimize the common case, where the same * scale is requested as the last time. * * @param zoom the requested zoom level * @return Image */ public Image getScaledImage(double zoom) { if (zoom == 1.0) { return getImage(); } else if (zoom == myZoom && scaledImage != null) { return scaledImage; } else { Image img = getImage(); Rectangle bounds = img.getBounds(); if (img != null) { ImageData imageData = img.getImageData(); if (tileset != null && tileset.getTransparentColor() != null) { imageData.transparentPixel = imageData.palette.getPixel(tileset.getTransparentColor()); } scaledImage = new Image( Display.getDefault(), imageData.scaledTo( (int) Math.round(bounds.width * zoom), (int) Math.round(bounds.height * zoom))); // MediaTracker mediaTracker = new MediaTracker(new Canvas()); // mediaTracker.addImage(scaledImage, 0); // try { // mediaTracker.waitForID(0); // } // catch (InterruptedException ie) { // System.err.println(ie); // } // mediaTracker.removeImage(scaledImage); myZoom = zoom; return scaledImage; } } return null; }
/** * Get the tile set that matches the given global tile id, only to be used when loading a map. * * @param gid a global tile id * @return the tileset containing the tile with the given global tile id, or <code>null</code> * when no such tileset exists */ public TileSet findTileSetForTileGID(int gid) { TileSet has = null; for (TileSet tileset : tilesets) { if (tileset.getFirstGid() <= gid) { has = tileset; } } return has; }
/** * Sets the parent tileset for a tile. If the tile is already a member of a set, and this method * is called with a different set as argument, the tile image is transferred to the new set. * * @param set */ public void setTileSet(TileSet set) { if (tileset != null && tileset != set) { setImage(set.addImage(getImage())); } else { if (internalImage != null) { setImage(set.addImage(internalImage)); internalImage = null; } } tileset = set; }
/** * Returns the maximum tile height. This is the height of the highest tile in all tilesets or the * tile height used by this map if it's smaller. * * @return int The maximum tile height */ public int getTileHeightMax() { int maxHeight = tileHeight; for (TileSet tileset : tilesets) { int height = tileset.getTileHeight(); if (height > maxHeight) { maxHeight = height; } } return maxHeight; }
/** * Writes a reference to an external tileset into a XML document. In the case where the tileset is * not stored in an external file, writes the contents of the tileset instead. * * @param set the tileset to write a reference to * @param w the XML writer to write to * @param wp the working directory of the map * @throws java.io.IOException */ private void writeTilesetReference(TileSet set, XMLWriter w, String wp) throws IOException { String source = set.getSource(); if (source == null) { writeTileset(set, w, wp); } else { w.startElement("tileset"); w.writeAttribute("firstgid", getFirstGidForTileset(set)); w.writeAttribute("source", getRelativePath(wp, source)); if (set.getBaseDir() != null) { w.writeAttribute("basedir", set.getBaseDir()); } w.endElement(); } }
/** * Changes the image of the tile as long as it is not null. * * @param i the new image of the tile */ public void setImage(Image i) { if (tileset != null) { tileset.overlayImage(tileImageId, i); } else { internalImage = i; } }
/** * Returns the tile image for this Tile. * * @return Image */ public Image getImage() { if (tileset != null) { return tileset.getImageById(tileImageId); } else { return internalImage; } }
public int getHeight() { if (tileset != null) { Point d = tileset.getImageDimensions(tileImageId); return d.y; } else if (internalImage != null) { return internalImage.getBounds().height; } return 0; }
public int getWidth() { if (tileset != null) { Point d = tileset.getImageDimensions(tileImageId); return d.x; } else if (internalImage != null) { return internalImage.getBounds().width; } return 0; }
private void writeMap(Map map, XMLWriter w, String wp) throws IOException { w.writeDocType("map", null, "http://mapeditor.org/dtd/1.0/map.dtd"); w.startElement("map"); w.writeAttribute("version", "1.0"); Map.Orientation orientation = map.getOrientation(); w.writeAttribute("orientation", String.valueOf(orientation)); w.writeAttribute("width", map.getWidth()); w.writeAttribute("height", map.getHeight()); w.writeAttribute("tilewidth", map.getTileWidth()); w.writeAttribute("tileheight", map.getTileHeight()); switch (orientation) { case HEXAGONAL: w.writeAttribute("hexsidelength", map.getHexSideLength()); case STAGGERED: w.writeAttribute("staggeraxis", String.valueOf(map.getStaggerAxis())); w.writeAttribute("staggerindex", String.valueOf(map.getStaggerIndex())); } writeProperties(map.getProperties(), w); firstGidPerTileset = new HashMap<>(); int firstgid = 1; for (TileSet tileset : map.getTileSets()) { setFirstGidForTileset(tileset, firstgid); writeTilesetReference(tileset, w, wp); firstgid += tileset.getMaxTileId() + 1; } for (MapLayer layer : map) { writeMapLayer(layer, w, wp); } firstGidPerTileset = null; w.endElement(); }
/** * Removes a {@link TileSet} from the map, and removes any tiles in the set from the map layers. A * {@link MapChangedEvent} is fired when all processing is complete. * * @param tileset TileSet to remove * @throws LayerLockedException when the tileset is in use on a locked layer */ public void removeTileset(TileSet tileset) throws LayerLockedException { // Sanity check final int tilesetIndex = tilesets.indexOf(tileset); if (tilesetIndex == -1) return; // Go through the map and remove any instances of the tiles in the set Iterator<Object> tileIterator = tileset.iterator(); while (tileIterator.hasNext()) { Tile tile = (Tile) tileIterator.next(); Iterator<MapLayer> layerIterator = getLayers(); while (layerIterator.hasNext()) { MapLayer ml = (MapLayer) layerIterator.next(); if (ml instanceof TileLayer) { ((TileLayer) ml).removeTile(tile); } } } tilesets.remove(tileset); fireTilesetRemoved(tilesetIndex); }
/** * Adds a Tileset to this Map. If the set is already attached to this map, <code>addTileset</code> * simply returns. * * @param tileset a tileset to add */ public void addTileset(TileSet tileset) { if (tileset == null || tilesets.indexOf(tileset) > -1) { return; } Tile t = tileset.getTile(0); if (t != null) { int tw = t.getWidth(); int th = t.getHeight(); if (tw != tileWidth) { if (tileWidth == 0) { tileWidth = tw; tileHeight = th; } } } tilesets.add(tileset); fireTilesetAdded(tileset); }
private void writeTileset(TileSet set, XMLWriter w, String wp) throws IOException { String tileBitmapFile = set.getTilebmpFile(); String name = set.getName(); w.startElement("tileset"); w.writeAttribute("firstgid", getFirstGidForTileset(set)); if (name != null) { w.writeAttribute("name", name); } if (tileBitmapFile != null) { w.writeAttribute("tilewidth", set.getTileWidth()); w.writeAttribute("tileheight", set.getTileHeight()); final int tileSpacing = set.getTileSpacing(); final int tileMargin = set.getTileMargin(); if (tileSpacing != 0) { w.writeAttribute("spacing", tileSpacing); } if (tileMargin != 0) { w.writeAttribute("margin", tileMargin); } } if (set.getBaseDir() != null) { w.writeAttribute("basedir", set.getBaseDir()); } if (tileBitmapFile != null) { w.startElement("image"); w.writeAttribute("source", getRelativePath(wp, tileBitmapFile)); Color trans = set.getTransparentColor(); if (trans != null) { w.writeAttribute("trans", Integer.toHexString(trans.getRGB()).substring(2)); } w.endElement(); // Write tile properties when necessary. for (Tile tile : set) { // todo: move the null check back into the iterator? if (tile != null && !tile.getProperties().isEmpty()) { w.startElement("tile"); w.writeAttribute("id", tile.getId()); writeProperties(tile.getProperties(), w); w.endElement(); } } } else { // Check to see if there is a need to write tile elements boolean needWrite = false; // As long as one has properties, they all need to be written. // TODO: This shouldn't be necessary for (Tile tile : set) { if (!tile.getProperties().isEmpty() || tile.getSource() != null) { needWrite = true; break; } } if (needWrite) { w.writeAttribute("tilewidth", set.getTileWidth()); w.writeAttribute("tileheight", set.getTileHeight()); w.writeAttribute("tilecount", set.size()); w.writeAttribute("columns", set.getTilesPerRow()); for (Tile tile : set) { // todo: move this check back into the iterator? if (tile != null) { writeTile(tile, w, wp); } } } } w.endElement(); }
/** * Returns the global tile id by adding the tile id to the map-assigned. * * @return id */ public int getGid() { if (tileset != null) { return id + tileset.getFirstGid(); } return id; }