diff --git a/src/java/com/threerings/media/tile/Tile.java b/src/java/com/threerings/media/tile/Tile.java index f611fe2dc..9a65ef6be 100644 --- a/src/java/com/threerings/media/tile/Tile.java +++ b/src/java/com/threerings/media/tile/Tile.java @@ -1,5 +1,5 @@ // -// $Id: Tile.java,v 1.2 2001/07/14 00:21:24 shaper Exp $ +// $Id: Tile.java,v 1.3 2001/07/16 00:45:07 shaper Exp $ package com.threerings.cocktail.miso.tile; @@ -24,6 +24,9 @@ public class Tile public static final int HALF_HEIGHT = HEIGHT / 2; public static final int HALF_WIDTH = WIDTH / 2; + public static final float EDGE_LENGTH = (float) + Math.sqrt((HALF_WIDTH * HALF_WIDTH) + (HALF_HEIGHT * HALF_HEIGHT)); + /** * Construct a new tile with the specified identifiers. Intended * only for use by the TileManager. Do not use this method. diff --git a/src/java/com/threerings/media/tile/TileSet.java b/src/java/com/threerings/media/tile/TileSet.java index 5c55a90e3..9eefddfd3 100644 --- a/src/java/com/threerings/media/tile/TileSet.java +++ b/src/java/com/threerings/media/tile/TileSet.java @@ -1,5 +1,5 @@ // -// $Id: TileSet.java,v 1.2 2001/07/14 00:21:24 shaper Exp $ +// $Id: TileSet.java,v 1.3 2001/07/16 00:45:07 shaper Exp $ package com.threerings.cocktail.miso.tile; @@ -55,11 +55,9 @@ public class TileSet // load the full tile image if we don't already have it if (_imgTiles == null) { Log.info("Getting full tileset image [file=" + _file + "]."); - if (_imgTiles == null) { - if ((_imgTiles = ImageManager.getImage(_file)) == null) { - Log.warning("Failed to retrieve full tileset image."); - return null; - } + if ((_imgTiles = ImageManager.getImage(_file)) == null) { + Log.warning("Failed to retrieve full tileset image."); + return null; } } diff --git a/src/java/com/threerings/media/util/MathUtil.java b/src/java/com/threerings/media/util/MathUtil.java new file mode 100644 index 000000000..970fe9d5f --- /dev/null +++ b/src/java/com/threerings/media/util/MathUtil.java @@ -0,0 +1,40 @@ +// +// $Id: MathUtil.java,v 1.1 2001/07/16 00:45:07 shaper Exp $ + +package com.threerings.cocktail.miso.util; + +import com.threerings.cocktail.miso.Log; + +import java.awt.Point; + +/** + * Provides miscellaneous useful utility routines for mathematical + * calculations. + */ +public class MathUtil +{ + /** + * Return the distance between the given points. + */ + public static float distance (int x0, int y0, int x1, int y1) + { + return (float)Math.sqrt(((x1 - x0) * (x1 - x0)) + + ((y1 - y0) * (y1 - y0))); + } + + /** + * Return a string representation of the given line. + */ + public static String lineToString (int x0, int y0, int x1, int y1) + { + return "(" + x0 + ", " + y0 + ") -> (" + x1 + ", " + y1 + ")"; + } + + /** + * Return a string representation of the given line. + */ + public static String lineToString (Point p1, Point p2) + { + return lineToString(p1.x, p1.y, p2.x, p2.y); + } +} diff --git a/src/java/com/threerings/miso/client/DisplayMisoSceneImpl.java b/src/java/com/threerings/miso/client/DisplayMisoSceneImpl.java index 2660f8334..af7d36b0a 100644 --- a/src/java/com/threerings/miso/client/DisplayMisoSceneImpl.java +++ b/src/java/com/threerings/miso/client/DisplayMisoSceneImpl.java @@ -1,5 +1,5 @@ // -// $Id: DisplayMisoSceneImpl.java,v 1.2 2001/07/14 00:21:23 shaper Exp $ +// $Id: DisplayMisoSceneImpl.java,v 1.3 2001/07/16 00:45:06 shaper Exp $ package com.threerings.cocktail.miso.scene; @@ -204,8 +204,8 @@ public class Scene protected static final short VERSION = 1; // scene width/height in tiles - protected static final int TILE_WIDTH = 20; - protected static final int TILE_HEIGHT = 40; + protected static final int TILE_WIDTH = 50; + protected static final int TILE_HEIGHT = 70; // layer identifiers and total number of layers protected static final int LAYER_BASE = 0; diff --git a/src/java/com/threerings/miso/client/IsoSceneView.java b/src/java/com/threerings/miso/client/IsoSceneView.java index 32fd849ce..70547ca5d 100644 --- a/src/java/com/threerings/miso/client/IsoSceneView.java +++ b/src/java/com/threerings/miso/client/IsoSceneView.java @@ -1,5 +1,5 @@ // -// $Id: IsoSceneView.java,v 1.2 2001/07/14 00:21:23 shaper Exp $ +// $Id: IsoSceneView.java,v 1.3 2001/07/16 00:45:06 shaper Exp $ package com.threerings.cocktail.miso.scene; @@ -7,6 +7,7 @@ import com.threerings.cocktail.miso.Log; import com.threerings.cocktail.miso.media.ImageManager; import com.threerings.cocktail.miso.tile.Tile; import com.threerings.cocktail.miso.tile.TileManager; +import com.threerings.cocktail.miso.util.MathUtil; import java.awt.*; import java.awt.image.*; @@ -25,16 +26,27 @@ public class IsoSceneView implements SceneView _viewY = 0; _bounds = new Rectangle(0, 0, DEF_BOUNDS_WIDTH, DEF_BOUNDS_HEIGHT); + + _htile = new Point(); + _htile.x = _htile.y = -1; + + _hcolor = Color.green; + _hstroke = new BasicStroke(3); + + _font = new Font("Arial", Font.PLAIN, 7); + + _lineX = new Point[2]; + _lineY = new Point[2]; + for (int ii = 0; ii < 2; ii++) { + _lineX[ii] = new Point(); + _lineY[ii] = new Point(); + } } public void paint (Graphics g) { Graphics2D g2 = (Graphics2D)g; -// Image img = ImageManager.getImage("/home/shaper/workspace/cocktail/rsrc/media/miso/tiles-base.png"); -// _scene.tiles[10][10][0].img = -// ImageManager.getImageCropped(img, 32, 0, 32, 16); - _offGraphics.setColor(Color.red); _offGraphics.fillRect(0, 0, _bounds.width, _bounds.height); @@ -78,10 +90,14 @@ public class IsoSceneView implements SceneView Tile tile = _scene.tiles[tx][ty][Scene.LAYER_BASE]; g2.drawImage(tile.img, screenX, screenY, null); - //Log.info("Drawing tile [tx=" + tx + ", ty=" + ty + - // ", sx=" + screenX + ", sy=" + screenY + - // ", img=" + tile.img + "]."); - + + //paintCoords(g2, tx, ty, screenX, screenY); + + // draw an outline around the highlighted tile + if (tx == _htile.x && ty == _htile.y) { + paintHighlightedTile(g2, screenX, screenY); + } + screenX += Tile.WIDTH; if ((tx += 1) > Scene.TILE_WIDTH - 1) tx = 0; @@ -96,8 +112,99 @@ public class IsoSceneView implements SceneView if ((my += 1) > Scene.TILE_HEIGHT - 1) my = 0; } } + + // draw the baseline x-axis line + g2.setColor(Color.red); + g2.drawLine(_lineX[0].x, _lineX[0].y, _lineX[1].x, _lineX[1].y); + + // draw line from last mouse pos to baseline + g2.setColor(Color.yellow); + g2.drawLine(_lineY[0].x, _lineY[0].y, _lineY[1].x, _lineY[1].y); + + // draw the most recent mouse cursor position + g2.setColor(Color.green); + g2.fillRect(_lineY[0].x, _lineY[0].y, 2, 2); + g2.setColor(Color.red); + g2.drawRect(_lineY[0].x - 1, _lineY[0].y - 1, 3, 3); } + protected void paintCoords (Graphics2D g2, int x, int y, int sx, int sy) + { + g2.setFont(_font); + g2.setColor(Color.white); + g2.drawString(""+x, sx+Tile.HALF_WIDTH-2, sy+Tile.HALF_HEIGHT-2); + g2.drawString(""+y, sx+Tile.HALF_WIDTH-2, sy+Tile.HEIGHT-2); + } + + protected void paintHighlightedTile (Graphics2D g2, int sx, int sy) + { + int x = sx; + int y = sy; + + Stroke ostroke = g2.getStroke(); + g2.setStroke(_hstroke); + g2.setColor(_hcolor); + + g2.drawLine(x, y + Tile.HALF_HEIGHT, + x + Tile.HALF_WIDTH, y); + g2.drawLine(x + Tile.HALF_WIDTH, y, + x + Tile.WIDTH, y + Tile.HALF_HEIGHT); + g2.drawLine(x + Tile.WIDTH, y + Tile.HALF_HEIGHT, + x + Tile.HALF_WIDTH, y + Tile.HEIGHT); + g2.drawLine(x + Tile.HALF_WIDTH, y + Tile.HEIGHT, + x, y + Tile.HALF_HEIGHT); + + g2.setStroke(ostroke); + } + + /** + * Highlight the tile at the specified pixel coordinates the next + * time the scene is re-rendered. + */ + public void setHighlightedTile (int x, int y) + { + float mX, mY; + int bX, bY; + + // calculate the x-axis line (from tile origin to end of visible axis) + mX = 0.5f; + _lineX[0].x = Tile.HALF_WIDTH; + bX = (int)-(mX * _lineX[0].x); + _lineX[0].y = 0; + _lineX[1].x = _bounds.width; + _lineX[1].y = (int)((mX * _lineX[1].x) + bX); + + // calculate line parallel to the y-axis (from mouse pos to x-axis) + _lineY[0].x = x; + _lineY[0].y = y; + mY = -0.5f; + bY = (int)(_lineY[0].y - (mY * _lineY[0].x)); + + // determine intersection of x- and y-axis lines + _lineY[1].x = (int)((bY - bX) / (mX - mY)); + _lineY[1].y = (int)((mY * _lineY[1].x) + bY); + + // determine distance of mouse pos along the x axis + int xdist = (int) + MathUtil.distance(_lineX[0].x, _lineX[0].y, + _lineY[1].x, _lineY[1].y); + _htile.x = (int)((xdist - Tile.EDGE_LENGTH) / Tile.EDGE_LENGTH); + + // determine distance of mouse pos along the y-axis + int ydist = (int) + MathUtil.distance(_lineY[0].x, _lineY[0].y, + _lineY[1].x, _lineY[1].y); + _htile.y = (int)(ydist / Tile.EDGE_LENGTH); + + //Log.info("[mX="+mX+", bX="+bX+", mY="+mY+", bY="+bY+"]"); + //Log.info("x-axis=" + MathUtil.lineToString(_lineX[0], _lineX[1])); + //Log.info("y-axis=" + MathUtil.lineToString(_lineY[0], _lineY[1])); + //Log.info("Highlighting tile [x="+_htile.x+", y="+_htile.y+"]."); + } + + // TODO: abstract into methods for tile -> screen coords, + // screen -> tile coords + public void setScene (Scene scene) { _scene = scene; @@ -115,17 +222,26 @@ public class IsoSceneView implements SceneView height + "]."); } - protected static final int OFF_X = -Tile.WIDTH; + protected static final int OFF_X = 0; protected static final int OFF_Y = 0; protected static final int DEF_BOUNDS_WIDTH = 600; protected static final int DEF_BOUNDS_HEIGHT = 600; + protected Point _lineX[], _lineY[]; + protected BufferedImage _offImg; protected Graphics2D _offGraphics; protected Rectangle _bounds; protected int _viewX, _viewY; + + protected Point _htile; + protected Color _hcolor; + protected Stroke _hstroke; + + protected Font _font; + protected Scene _scene; protected Component _target; protected TileManager _tmgr; diff --git a/src/java/com/threerings/miso/client/SceneView.java b/src/java/com/threerings/miso/client/SceneView.java index 326b41822..1a266d7fe 100644 --- a/src/java/com/threerings/miso/client/SceneView.java +++ b/src/java/com/threerings/miso/client/SceneView.java @@ -1,5 +1,5 @@ // -// $Id: SceneView.java,v 1.1 2001/07/12 22:38:03 shaper Exp $ +// $Id: SceneView.java,v 1.2 2001/07/16 00:45:06 shaper Exp $ package com.threerings.cocktail.miso.scene; @@ -18,6 +18,11 @@ public interface SceneView */ public void paint (Graphics g); + /** + * Set a tile to be highlighted when the scene is rendered. + */ + public void setHighlightedTile (int x, int y); + /** * Set the scene that we're rendering. */ diff --git a/src/java/com/threerings/miso/media/ImageManager.java b/src/java/com/threerings/miso/media/ImageManager.java index 5019e677f..fe1179fed 100644 --- a/src/java/com/threerings/miso/media/ImageManager.java +++ b/src/java/com/threerings/miso/media/ImageManager.java @@ -1,5 +1,5 @@ // -// $Id: ImageManager.java,v 1.2 2001/07/14 00:02:10 shaper Exp $ +// $Id: ImageManager.java,v 1.3 2001/07/16 00:45:06 shaper Exp $ package com.threerings.cocktail.miso.media; @@ -26,7 +26,7 @@ public class ImageManager return null; } - Image img = (Image) _imgs.get(fname); + Image img = (Image)_imgs.get(fname); if (img != null) { Log.info("Retrieved image from cache [fname=" + fname + "]."); return img; @@ -60,7 +60,7 @@ public class ImageManager int width, int height) { BufferedImage img = - new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = img.createGraphics(); g2.drawImage(fullImg, -x, -y, null);