Refactored the tileset trimming code so that it can more easily be reused

to trim object tilesets.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1524 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-06-21 18:09:37 +00:00
parent ad9a54086e
commit c3c08a03b3
3 changed files with 96 additions and 44 deletions
@@ -1,11 +1,16 @@
//
// $Id: TrimmedTileSet.java,v 1.1 2002/05/06 18:08:32 mdb Exp $
// $Id: TrimmedTileSet.java,v 1.2 2002/06/21 18:09:34 mdb Exp $
package com.threerings.media.tile;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.IOException;
import java.io.OutputStream;
import com.threerings.media.tile.util.TileSetTrimmer;
/**
* Contains the necessary information to create a set of trimmed tiles
* from a base image and the associated trim metrics.
@@ -18,17 +23,6 @@ public class TrimmedTileSet extends TileSet
return _obounds.length;
}
/**
* Provides this tileset with access to the trimmed tile metrics. This
* generally only is called when generating a trimmed tileset from a
* regular tileset.
*/
public void setTileMetrics (Rectangle[] obounds, Rectangle[] tbounds)
{
_obounds = obounds;
_tbounds = tbounds;
}
// documentation inherited
protected Rectangle computeTileBounds (int tileIndex, Image tilesetImage)
{
@@ -43,6 +37,51 @@ public class TrimmedTileSet extends TileSet
tilesetImage, _obounds[tileIndex], _tbounds[tileIndex]);
}
/**
* Creates a trimmed tileset from the supplied source tileset. See
* {@link TileSetTrimmer#trimTileSet} for further information.
*/
public static TrimmedTileSet trimTileSet (
TileSet source, OutputStream destImage)
throws IOException
{
final TrimmedTileSet tset = new TrimmedTileSet();
int tcount = source.getTileCount();
// grab the dimensions of the original tiles
tset._obounds = new Rectangle[tcount];
for (int ii = 0; ii < tcount; ii++) {
try {
Tile tile = source.getTile(ii);
tset._obounds[ii] = new Rectangle();
tset._obounds[ii].width = tile.getWidth();
tset._obounds[ii].height = tile.getHeight();
} catch (NoSuchTileException nste) {
String errmsg = "Urk! TileSet is ill-behaved. " +
"Claimed to have " + tcount + " tiles, but choked when " +
"we asked for tile " + ii + " [tset=" + source + "].";
throw new RuntimeException(errmsg);
}
}
tset._tbounds = new Rectangle[tcount];
// create the trimmed tileset image
TileSetTrimmer.TrimMetricsReceiver tmr =
new TileSetTrimmer.TrimMetricsReceiver() {
public void trimmedTile (int tileIndex, int imageX, int imageY,
int trimX, int trimY,
int trimWidth, int trimHeight) {
tset._obounds[tileIndex].x = imageX;
tset._obounds[tileIndex].y = imageY;
tset._tbounds[tileIndex] =
new Rectangle(trimX, trimY, trimWidth, trimHeight);
}
};
TileSetTrimmer.trimTileSet(source, destImage, tmr);
return tset;
}
/** The width and height of the untrimmed tile, and the x and y offset
* of the trimmed image within our tileset image. */
protected Rectangle[] _obounds;