com.threerings.miso.util
Class MisoUtil

java.lang.Object
  extended by com.threerings.miso.util.MisoUtil
All Implemented Interfaces:
DirectionCodes

public class MisoUtil
extends Object
implements DirectionCodes

Miscellaneous isometric-display-related utility routines.


Field Summary
protected static int FULL_TILE_FACTOR
          Multiplication factor to embed tile coords in full coords.
 
Fields inherited from interface com.threerings.util.DirectionCodes
CARDINAL_DIRECTIONS, CCW, CW, DIRECTION_COUNT, DOWN, EAST, EASTNORTHEAST, EASTSOUTHEAST, FINE_DIRECTION_COUNT, HORIZONTAL, LEFT, NONE, NORTH, NORTHEAST, NORTHNORTHEAST, NORTHNORTHWEST, NORTHWEST, RIGHT, SOUTH, SOUTHEAST, SOUTHSOUTHEAST, SOUTHSOUTHWEST, SOUTHWEST, UP, VERTICAL, WEST, WESTNORTHWEST, WESTSOUTHWEST
 
Constructor Summary
MisoUtil()
           
 
Method Summary
static int coordsToKey(int x, int y)
          Turns x and y scene coordinates into an integer key.
static int fineToFull(MisoSceneMetrics metrics, int fine)
          Converts the given fine coordinate to a full coordinate (a tile coordinate plus a fine coordinate remainder).
static void fineToPixel(MisoSceneMetrics metrics, int x, int y, Point ppos)
          Convert the given fine coordinates to pixel coordinates within the containing tile.
static int fullToFine(int val)
          Returns the fine coordinate of the given full coordinate.
static Point fullToScreen(MisoSceneMetrics metrics, int x, int y, Point spos)
          Convert the given full coordinates to screen-based pixel coordinates.
static int fullToTile(int val)
          Returns the tile coordinate of the given full coordinate.
static int getDirection(MisoSceneMetrics metrics, int ax, int ay, int bx, int by)
          Given two points in screen pixel coordinates, return the compass direction that point B lies in from point A from an isometric perspective.
static Polygon getFootprintPolygon(MisoSceneMetrics metrics, int x, int y, int width, int height)
          Returns a polygon framing the specified scene footprint.
static int getIsoDirection(int ax, int ay, int bx, int by)
          Given two points in an isometric coordinate system (in which DirectionCodes.NORTH is in the direction of the negative x-axis and DirectionCodes.WEST in the direction of the negative y-axis), return the compass direction that point B lies in from point A.
static Polygon getMultiTilePolygon(MisoSceneMetrics metrics, Point sp1, Point sp2)
          Return a screen-coordinates polygon framing the two specified tile-coordinate points.
static int getProjectedIsoDirection(int ax, int ay, int bx, int by)
          Given two points in screen coordinates, return the isometrically projected compass direction that point B lies in from point A.
static Polygon getTilePolygon(MisoSceneMetrics metrics, int x, int y)
          Return a polygon framing the specified tile.
static void pixelToFine(MisoSceneMetrics metrics, int x, int y, Point fpos)
          Convert the given pixel coordinates, whose origin is at the top-left of a tile's containing rectangle, to fine coordinates within that tile.
static Point screenToFull(MisoSceneMetrics metrics, int sx, int sy, Point fpos)
          Convert the given screen-based pixel coordinates to full scene-based coordinates that include both the tile coordinates and the fine coordinates in each dimension.
static Point screenToTile(MisoSceneMetrics metrics, int sx, int sy, Point tpos)
          Convert the given screen-based pixel coordinates to their corresponding tile-based coordinates.
static Point tilePlusFineToFull(MisoSceneMetrics metrics, int tileX, int tileY, int fineX, int fineY, Point full)
          Adds the supplied fine coordinates to the supplied tile coordinates to compute full coordinates.
static int tileToFull(int tile)
          Returns the supplied tile coordinate as a full coordinate assuming a fine offset of 0.
static Point tileToScreen(MisoSceneMetrics metrics, int x, int y, Point spos)
          Convert the given tile-based coordinates to their corresponding screen-based pixel coordinates.
static int toFull(int tile, int fine)
          Composes the supplied tile coordinate and fine coordinate offset into a full coordinate.
static int toIsoDirection(int dir)
          Converts a non-isometric orientation (where north points toward the top of the screen) to an isometric orientation where north points toward the upper-left corner of the screen.
static int xCoordFromKey(int key)
          Gets the x coordinate from an integer hash key.
static int yCoordFromKey(int key)
          Gets the y coordinate from an integer hash key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FULL_TILE_FACTOR

protected static final int FULL_TILE_FACTOR
Multiplication factor to embed tile coords in full coords.

See Also:
Constant Field Values
Constructor Detail

MisoUtil

public MisoUtil()
Method Detail

getDirection

public static int getDirection(MisoSceneMetrics metrics,
                               int ax,
                               int ay,
                               int bx,
                               int by)
Given two points in screen pixel coordinates, return the compass direction that point B lies in from point A from an isometric perspective.

Parameters:
ax - the x-position of point A.
ay - the y-position of point A.
bx - the x-position of point B.
by - the y-position of point B.
Returns:
the direction specified as one of the Sprite class's direction constants.

getIsoDirection

public static int getIsoDirection(int ax,
                                  int ay,
                                  int bx,
                                  int by)
Given two points in an isometric coordinate system (in which DirectionCodes.NORTH is in the direction of the negative x-axis and DirectionCodes.WEST in the direction of the negative y-axis), return the compass direction that point B lies in from point A. This method is used to determine direction for both tile coordinates and fine coordinates within a tile, since the coordinate systems are the same.

Parameters:
ax - the x-position of point A.
ay - the y-position of point A.
bx - the x-position of point B.
by - the y-position of point B.
Returns:
the direction specified as one of the Sprite class's direction constants, or DirectionCodes.NONE if point B is equivalent to point A.

getProjectedIsoDirection

public static int getProjectedIsoDirection(int ax,
                                           int ay,
                                           int bx,
                                           int by)
Given two points in screen coordinates, return the isometrically projected compass direction that point B lies in from point A.

Parameters:
ax - the x-position of point A.
ay - the y-position of point A.
bx - the x-position of point B.
by - the y-position of point B.
Returns:
the direction specified as one of the Sprite class's direction constants, or DirectionCodes.NONE if point B is equivalent to point A.

toIsoDirection

public static int toIsoDirection(int dir)
Converts a non-isometric orientation (where north points toward the top of the screen) to an isometric orientation where north points toward the upper-left corner of the screen.


fullToTile

public static int fullToTile(int val)
Returns the tile coordinate of the given full coordinate.


fullToFine

public static int fullToFine(int val)
Returns the fine coordinate of the given full coordinate.


screenToTile

public static Point screenToTile(MisoSceneMetrics metrics,
                                 int sx,
                                 int sy,
                                 Point tpos)
Convert the given screen-based pixel coordinates to their corresponding tile-based coordinates. Converted coordinates are placed in the given point object.

Parameters:
sx - the screen x-position pixel coordinate.
sy - the screen y-position pixel coordinate.
tpos - the point object to place coordinates in.
Returns:
the point instance supplied via the tpos parameter.

tileToScreen

public static Point tileToScreen(MisoSceneMetrics metrics,
                                 int x,
                                 int y,
                                 Point spos)
Convert the given tile-based coordinates to their corresponding screen-based pixel coordinates. The screen coordinate for a tile is the upper-left coordinate of the rectangle that bounds the tile polygon. Converted coordinates are placed in the given point object.

Parameters:
x - the tile x-position coordinate.
y - the tile y-position coordinate.
spos - the point object to place coordinates in.
Returns:
the point instance supplied via the spos parameter.

fineToPixel

public static void fineToPixel(MisoSceneMetrics metrics,
                               int x,
                               int y,
                               Point ppos)
Convert the given fine coordinates to pixel coordinates within the containing tile. Converted coordinates are placed in the given point object.

Parameters:
x - the x-position fine coordinate.
y - the y-position fine coordinate.
ppos - the point object to place coordinates in.

pixelToFine

public static void pixelToFine(MisoSceneMetrics metrics,
                               int x,
                               int y,
                               Point fpos)
Convert the given pixel coordinates, whose origin is at the top-left of a tile's containing rectangle, to fine coordinates within that tile. Converted coordinates are placed in the given point object.

Parameters:
x - the x-position pixel coordinate.
y - the y-position pixel coordinate.
fpos - the point object to place coordinates in.

screenToFull

public static Point screenToFull(MisoSceneMetrics metrics,
                                 int sx,
                                 int sy,
                                 Point fpos)
Convert the given screen-based pixel coordinates to full scene-based coordinates that include both the tile coordinates and the fine coordinates in each dimension. Converted coordinates are placed in the given point object.

Parameters:
sx - the screen x-position pixel coordinate.
sy - the screen y-position pixel coordinate.
fpos - the point object to place coordinates in.
Returns:
the point passed in to receive the coordinates.

fullToScreen

public static Point fullToScreen(MisoSceneMetrics metrics,
                                 int x,
                                 int y,
                                 Point spos)
Convert the given full coordinates to screen-based pixel coordinates. Converted coordinates are placed in the given point object.

Parameters:
x - the x-position full coordinate.
y - the y-position full coordinate.
spos - the point object to place coordinates in.
Returns:
the point passed in to receive the coordinates.

fineToFull

public static int fineToFull(MisoSceneMetrics metrics,
                             int fine)
Converts the given fine coordinate to a full coordinate (a tile coordinate plus a fine coordinate remainder). The fine coordinate is assumed to be relative to tile (0, 0).


tileToFull

public static int tileToFull(int tile)
Returns the supplied tile coordinate as a full coordinate assuming a fine offset of 0.


toFull

public static int toFull(int tile,
                         int fine)
Composes the supplied tile coordinate and fine coordinate offset into a full coordinate.


getTilePolygon

public static Polygon getTilePolygon(MisoSceneMetrics metrics,
                                     int x,
                                     int y)
Return a polygon framing the specified tile.

Parameters:
x - the tile x-position coordinate.
y - the tile y-position coordinate.

getMultiTilePolygon

public static Polygon getMultiTilePolygon(MisoSceneMetrics metrics,
                                          Point sp1,
                                          Point sp2)
Return a screen-coordinates polygon framing the two specified tile-coordinate points.


getFootprintPolygon

public static Polygon getFootprintPolygon(MisoSceneMetrics metrics,
                                          int x,
                                          int y,
                                          int width,
                                          int height)
Returns a polygon framing the specified scene footprint.

Parameters:
x - the x tile coordinate of the "upper-left" of the footprint.
y - the y tile coordinate of the "upper-left" of the footprint.
width - the width in tiles of the footprint.
height - the height in tiles of the footprint.

tilePlusFineToFull

public static Point tilePlusFineToFull(MisoSceneMetrics metrics,
                                       int tileX,
                                       int tileY,
                                       int fineX,
                                       int fineY,
                                       Point full)
Adds the supplied fine coordinates to the supplied tile coordinates to compute full coordinates.

Returns:
the point object supplied as full.

coordsToKey

public static final int coordsToKey(int x,
                                    int y)
Turns x and y scene coordinates into an integer key.

Returns:
the hash key, given x and y.

xCoordFromKey

public static final int xCoordFromKey(int key)
Gets the x coordinate from an integer hash key.

Returns:
the x coordinate.

yCoordFromKey

public static final int yCoordFromKey(int key)
Gets the y coordinate from an integer hash key.

Returns:
the y coordinate from the hash key.