diff --git a/projects/atlanti/rsrc/media/tiles.png b/projects/atlanti/rsrc/media/tiles.png new file mode 100644 index 00000000..8e33d6b1 Binary files /dev/null and b/projects/atlanti/rsrc/media/tiles.png differ diff --git a/projects/atlanti/src/java/com/samskivert/atlanti/client/AtlantiBoard.java b/projects/atlanti/src/java/com/samskivert/atlanti/client/AtlantiBoard.java index 094eae9b..703ddfeb 100644 --- a/projects/atlanti/src/java/com/samskivert/atlanti/client/AtlantiBoard.java +++ b/projects/atlanti/src/java/com/samskivert/atlanti/client/AtlantiBoard.java @@ -1,5 +1,5 @@ // -// $Id: AtlantiBoard.java,v 1.14 2001/11/07 18:03:33 mdb Exp $ +// $Id: AtlantiBoard.java,v 1.15 2001/11/08 08:00:22 mdb Exp $ package com.threerings.venison; @@ -144,7 +144,7 @@ public class VenisonBoard // the necessary claim group adjustments can be made) tile.setPiecen(piecen, _tiles); // and repaint - repaint(); + repaintTile(tile); } else { Log.warning("Requested to place piecen for which we could " + @@ -166,7 +166,7 @@ public class VenisonBoard // clear the piecen out of the tile tile.clearPiecen(); // and repaint - repaint(); + repaintTile(tile); // and get on out return; } @@ -223,9 +223,6 @@ public class VenisonBoard if (_placingTile != null) { updatePlacingInfo(true); } - - // and repaint - repaint(); } /** @@ -246,10 +243,21 @@ public class VenisonBoard _ty = (getHeight() - TILE_HEIGHT * _height)/2; } + /** + * Causes the supplied tile to be repainted. + */ + public void repaintTile (VenisonTile tile) + { + int offx = _tx + (tile.x + _origX) * TILE_WIDTH; + int offy = _ty + (tile.y + _origY) * TILE_HEIGHT; + repaint(offx, offy, TILE_WIDTH, TILE_HEIGHT); + } + // documentation inherited public void paintComponent (Graphics g) { super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; // center the tile display if we are bigger than we need to be @@ -271,7 +279,7 @@ public class VenisonBoard g.setColor(Color.blue); int sx = (_placingTile.x + _origX) * TILE_WIDTH; int sy = (_placingTile.y + _origY) * TILE_HEIGHT; - g.drawRect(sx, sy, TILE_WIDTH, TILE_HEIGHT); + g.drawRect(sx, sy, TILE_WIDTH-1, TILE_HEIGHT-1); } // if we have a recently placed tile, draw that one as well @@ -283,7 +291,7 @@ public class VenisonBoard g.setColor(Color.white); int sx = (_placedTile.x + _origX) * TILE_WIDTH; int sy = (_placedTile.y + _origY) * TILE_HEIGHT; - g.drawRect(sx, sy, TILE_WIDTH, TILE_HEIGHT); + g.drawRect(sx, sy, TILE_WIDTH-1, TILE_HEIGHT-1); } // draw a white rectangle around the last placed @@ -291,7 +299,7 @@ public class VenisonBoard g.setColor(Color.white); int sx = (_lastPlacedTile.x + _origX) * TILE_WIDTH; int sy = (_lastPlacedTile.y + _origY) * TILE_HEIGHT; - g.drawRect(sx, sy, TILE_WIDTH, TILE_HEIGHT); + g.drawRect(sx, sy, TILE_WIDTH-1, TILE_HEIGHT-1); } // undo our translations @@ -306,10 +314,9 @@ public class VenisonBoard _mouseY = evt.getY() - _ty; if (_placingTile != null) { - // if we have a tile to be placed, update its coordinates - if (updatePlacingInfo(false)) { - repaint(); - } + // if we have a tile to be placed, update its coordinates (it + // will automatically be repainted) + updatePlacingInfo(false); } else if (_placedTile != null && _placingPiecen) { // if we have a recently placed tile, we're doing piecen @@ -349,7 +356,7 @@ public class VenisonBoard } if (changed) { - repaint(); + repaintTile(_placedTile); } } } @@ -367,7 +374,7 @@ public class VenisonBoard // clear out any placed piecen because we're placing nothing if (_placedTile != null && _placedTile.piecen != null) { _placedTile.piecen = null; - repaint(); + repaintTile(_placedTile); } // post the action Controller.postAction(this, PLACE_NOTHING); @@ -416,12 +423,12 @@ public class VenisonBoard /** * Updates the coordinates and orientation of the placing tile based - * on the last known coordinates of the mouse and returns true if the - * coordinates or orientation changed from their previous values. + * on the last known coordinates of the mouse and causes it to be + * repainted. */ - protected boolean updatePlacingInfo (boolean force) + protected void updatePlacingInfo (boolean force) { - boolean changed = false; + boolean updated = false; // convert mouse coordinates into tile coordinates and offset them // by the origin @@ -431,12 +438,18 @@ public class VenisonBoard // if these are different than the values currently in the placing // tile, update the tile coordinates if (_placingTile.x != x || _placingTile.y != y || force) { + // if we have a valid orientation presently, and we're moving, + // we need to clear out the old orientation + if (_validPlacement) { + repaintTile(_placingTile); + } + // update the coordinates of the tile _placingTile.x = x; _placingTile.y = y; - // we've changed the display, so make a note of it - changed = true; + // make a note that we moved + updated = true; // we also need to recompute the valid orientations for the // tile in this new position @@ -460,14 +473,19 @@ public class VenisonBoard if (_validOrients[candOrient]) { if (_placingTile.orientation != candOrient) { _placingTile.orientation = candOrient; - changed = true; + // make a note that we moved + updated = true; } _validPlacement = true; break; } } - return changed; + // if we now have a valid orientation and something was changed, + // we want to repaint at the new tile location + if (_validPlacement && updated) { + repaintTile(_placingTile); + } } /** diff --git a/projects/atlanti/src/java/com/samskivert/atlanti/client/AtlantiPanel.java b/projects/atlanti/src/java/com/samskivert/atlanti/client/AtlantiPanel.java index 4bbe12d1..c9ae7658 100644 --- a/projects/atlanti/src/java/com/samskivert/atlanti/client/AtlantiPanel.java +++ b/projects/atlanti/src/java/com/samskivert/atlanti/client/AtlantiPanel.java @@ -6,13 +6,20 @@ package com.threerings.venison; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; +import java.awt.Frame; + import javax.swing.*; +import javax.swing.event.AncestorEvent; +import javax.swing.event.AncestorListener; import com.samskivert.swing.Controller; import com.samskivert.swing.ControllerProvider; import com.samskivert.swing.HGroupLayout; import com.samskivert.swing.VGroupLayout; +import com.threerings.resource.ResourceManager; +import com.threerings.media.ImageManager; + import com.threerings.crowd.data.PlaceObject; import com.threerings.crowd.client.PlaceView; @@ -93,6 +100,21 @@ public class VenisonPanel // we'll need this later to provide it _controller = controller; + + // we can't create our image manager until we have access to our + // containing frame + addAncestorListener(new AncestorListener() { + public void ancestorAdded (AncestorEvent event) { + // create our image manager + JRootPane rpane = getRootPane(); + VenisonTile.setImageManager( + new ImageManager(_rmgr, (Frame)rpane.getParent())); + } + public void ancestorMoved (AncestorEvent event) { + } + public void ancestorRemoved (AncestorEvent event) { + } + }); } // documentation inherited @@ -116,4 +138,14 @@ public class VenisonPanel /** A reference to our controller that we need to implement the {@link * ControllerProvider} interface. */ protected VenisonController _controller; + + // this stuff is all a bit of a hack right now. by all rights, the + // venison app should set up the resource manager, because it knows + // about that sort of stuff, and make it available via the venison + // context and we may have some better place for the image manager to + // live and the tile set would be fetched through the tile + // manager. but it's late and i want to get this working, so fooey. + + /** Our resource manager. */ + protected static ResourceManager _rmgr = new ResourceManager("rsrc"); } diff --git a/projects/atlanti/src/java/com/samskivert/atlanti/data/AtlantiTile.java b/projects/atlanti/src/java/com/samskivert/atlanti/data/AtlantiTile.java index dc81dedb..80559307 100644 --- a/projects/atlanti/src/java/com/samskivert/atlanti/data/AtlantiTile.java +++ b/projects/atlanti/src/java/com/samskivert/atlanti/data/AtlantiTile.java @@ -1,11 +1,13 @@ // -// $Id: AtlantiTile.java,v 1.10 2001/10/18 20:56:45 mdb Exp $ +// $Id: AtlantiTile.java,v 1.11 2001/11/08 08:00:22 mdb Exp $ package com.threerings.venison; import java.awt.Color; +import java.awt.Image; import java.awt.Graphics2D; import java.awt.Polygon; +import java.awt.geom.AffineTransform; import java.io.IOException; import java.io.DataInputStream; @@ -15,6 +17,11 @@ import java.util.List; import com.samskivert.util.IntTuple; import com.samskivert.util.StringUtil; +import com.threerings.media.ImageManager; +import com.threerings.media.tile.NoSuchTileException; +import com.threerings.media.tile.Tile; +import com.threerings.media.tile.UniformTileSet; + import com.threerings.presents.dobj.DSet; /** @@ -258,6 +265,11 @@ public class VenisonTile { int tidx = type-1; + // obtain our tile image + if (_tileImage == null) { + _tileImage = getTileImage(type); + } + // compute our screen coordinates int sx = (x + xoff) * TILE_WIDTH; int sy = (y + yoff) * TILE_HEIGHT; @@ -265,10 +277,23 @@ public class VenisonTile // translate to our screen coordinates g.translate(sx, sy); + // render our tile image + if (_tileImage != null) { + if (orientation > 0) { + g.drawImage(_tileImage, _xforms[orientation], null); + } else { + g.drawImage(_tileImage, 0, 0, null); + } + + } else { + Log.warning("No tile image!? [type=" + type + + ", img=" + _tileImage + "]."); + } + // render our features and piecen for (int i = 0; i < features.length; i++) { // paint the feature - features[i].paint(g, orientation, claims[i]); +// features[i].paint(g, orientation, claims[i]); // if we have a piecen on this tile, render it as well if (piecen != null && piecen.featureIndex == i) { @@ -277,9 +302,9 @@ public class VenisonTile } } - // draw a rectangular outline - g.setColor(Color.black); - g.drawRect(0, 0, TILE_WIDTH, TILE_HEIGHT); +// // draw a rectangular outline +// g.setColor(Color.black); +// g.drawRect(0, 0, TILE_WIDTH-1, TILE_HEIGHT-1); // if we have a shield, draw a square in the lower right if (hasShield) { @@ -393,4 +418,70 @@ public class VenisonTile ", claims=" + StringUtil.toString(claims) + ", piecen=" + piecen + "]"; } + + /** + * Someone needs to configure this so that we can display tiles on + * screen. + */ + public static void setImageManager (ImageManager imgr) + { + _imgr = imgr; + } + + /** + * Fetches the image for the tile of the specified type. + */ + protected static Image getTileImage (int type) + { + // load up the tile set if we haven't already + if (_tset == null) { + _tset = new UniformTileSet( + _imgr, TILES_IMG_PATH, TILE_TYPES, TILE_WIDTH, TILE_HEIGHT); + } + + // fetch the tile + try { + Tile tile = _tset.getTile(type-1); + if (tile != null) { + return tile.img; + } + + } catch (NoSuchTileException nste) { + // fall through + } + + Log.warning("Unable to load tile image [type=" + type + "]."); + return null; + } + + /** The tile image that we use to render this tile. */ + protected Image _tileImage; + + /** Our image manager. */ + protected static ImageManager _imgr; + + /** Our tileset. */ + protected static UniformTileSet _tset; + + /** The path to our tileset image. */ + protected static final String TILES_IMG_PATH = "media/tiles.png"; + + /** Three affine transforms for rendering an image in three rotated + * orientations. */ + protected static AffineTransform[] _xforms; + + static { + // the first element will be left blank + _xforms = new AffineTransform[4]; + + // create our three orientation transforms + AffineTransform xform = new AffineTransform(); + for (int orient = 1; orient < 4; orient++) { + // rotate the xform into the next orientation + xform.translate(TILE_WIDTH, 0); + xform.rotate(Math.PI/2); + // and save it + _xforms[orient] = (AffineTransform)xform.clone(); + } + } } diff --git a/projects/atlanti/src/java/com/samskivert/atlanti/util/TileUtil.java b/projects/atlanti/src/java/com/samskivert/atlanti/util/TileUtil.java index 54a4d483..5dbf1608 100644 --- a/projects/atlanti/src/java/com/samskivert/atlanti/util/TileUtil.java +++ b/projects/atlanti/src/java/com/samskivert/atlanti/util/TileUtil.java @@ -1,10 +1,8 @@ // -// $Id: TileUtil.java,v 1.14 2001/11/07 10:39:00 mdb Exp $ +// $Id: TileUtil.java,v 1.15 2001/11/08 08:00:22 mdb Exp $ package com.threerings.venison; -import java.awt.Polygon; - import java.util.ArrayList; import java.util.Collections; import java.util.Iterator;