Skip to content

Commit

Permalink
Grid.createCellShape() is now only for the current size
Browse files Browse the repository at this point in the history
Hex grids already ignored the `size` parameter and for square grids it was only ever called with the current size
anyways. Isometrics grids used it, but they can have their own private overload for that.
  • Loading branch information
kwvanderlinde committed Jun 11, 2024
1 parent c9e51ca commit 874b32a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
15 changes: 10 additions & 5 deletions src/main/java/net/rptools/maptool/model/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Grid(Grid grid) {
}

protected Object readResolve() {
cellShape = createCellShape(getSize());
cellShape = createCellShape();
return this;
}

Expand Down Expand Up @@ -274,7 +274,12 @@ public BufferedImage getCellHighlight() {
return null;
}

protected abstract Area createCellShape(int size);
/**
* Build the shape of a cell for the current grid size.
*
* @return The cell shape.
*/
protected abstract Area createCellShape();

/**
* @param offsetX The grid's x offset component
Expand Down Expand Up @@ -339,7 +344,7 @@ public int getSize() {
*/
public void setSize(int size) {
this.size = constrainSize(size);
cellShape = createCellShape(size);
cellShape = createCellShape();
fireGridChanged();
}

Expand Down Expand Up @@ -838,7 +843,7 @@ protected AffineTransform getGridOffset(Token token) {
* @return the {@link Area} conforming to the current grid layout for the given radius
*/
protected Area createGridArea(int gridRadius) {
final Area cellArea = new Area(createCellShape(getSize()));
final Area cellArea = new Area(getCellShape());
final Set<Point> points = generateRadius(gridRadius);
Area gridArea = new Area();

Expand Down Expand Up @@ -945,7 +950,7 @@ public static Grid fromDto(GridDto dto) {
grid.offsetX = dto.getOffsetX();
grid.offsetY = dto.getOffsetY();
grid.size = dto.getSize();
grid.cellShape = grid.createCellShape(grid.size);
grid.cellShape = grid.createCellShape();

return grid;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/rptools/maptool/model/GridlessGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public CellPoint convert(ZonePoint zp) {
}

@Override
protected Area createCellShape(int size) {
protected Area createCellShape() {
// Doesn't do this
return null;
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/net/rptools/maptool/model/HexGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ public Point2D.Double getCellCenter(CellPoint cell) {
}

@Override
protected Area createCellShape(int size) {
// don't use size. it has already been used to set the minorRadius
// and will only introduce a rounding error.
protected Area createCellShape() {
var hex = new GeneralPath();
hex.moveTo(0, minorRadius);
hex.lineTo(edgeProjection, 0);
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/net/rptools/maptool/model/IsometricGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,13 @@ public GridCapabilities getCapabilities() {
}

@Override
protected Area createCellShape(int size) {
int x[] = {size, size * 2, size, 0};
int y[] = {0, size / 2, size, size / 2};
protected Area createCellShape() {
return createCellShape(getSize());
}

private Area createCellShape(int size) {
int[] x = {size, size * 2, size, 0};
int[] y = {0, size / 2, size, size / 2};
return new Area(new Polygon(x, y, 4));
}

Expand Down Expand Up @@ -400,7 +404,7 @@ public Area getShapedArea(
footprint.x = -footprint.width / 2;
footprint.y = -footprint.height / 2;
// convert the cell footprint to an area
Area cellShape = getZone().getGrid().createCellShape(footprint.height);
Area cellShape = createCellShape(footprint.height);
// convert the area to isometric view
AffineTransform mtx = new AffineTransform();
mtx.translate(-footprint.width / 2, -footprint.height / 2);
Expand Down Expand Up @@ -432,7 +436,7 @@ public Area getTokenCellArea(Area bounds) {
footprint.x = -footprint.width / 2;
footprint.y = -footprint.height / 2;
// convert the cell footprint to an area
Area cellShape = getZone().getGrid().createCellShape(footprint.height);
Area cellShape = createCellShape(footprint.height);
// convert the area to isometric view
AffineTransform mtx = new AffineTransform();
mtx.translate(bounds.getBounds().getX(), bounds.getBounds().getY());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/rptools/maptool/model/SquareGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ public BufferedImage getCellHighlight() {
}

@Override
protected Area createCellShape(int size) {
protected Area createCellShape() {
var size = getSize();
return new Area(new Rectangle(0, 0, size, size));
}

Expand Down

0 comments on commit 874b32a

Please sign in to comment.