Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hexagon geometry #4831

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions src/main/java/net/rptools/maptool/model/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import net.rptools.maptool.events.MapToolEventBus;
import net.rptools.maptool.model.TokenFootprint.OffsetTranslator;
import net.rptools.maptool.model.zones.GridChanged;
import net.rptools.maptool.server.Mapper;
import net.rptools.maptool.server.proto.GridDto;
import net.rptools.maptool.util.GraphicsUtil;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -67,12 +66,13 @@ public abstract class Grid implements Cloneable {
private static final Dimension NO_DIM = new Dimension();
private static final DirectionCalculator calculator = new DirectionCalculator();
private static Map<Integer, Area> gridShapeCache = new ConcurrentHashMap<>();
protected Map<KeyStroke, Action> movementKeys = null;

protected transient Map<KeyStroke, Action> movementKeys = null;
private transient Zone zone;
private transient Area cellShape;
private int offsetX = 0;
private int offsetY = 0;
private int size;
private Zone zone;
private Area cellShape;

public Grid() {
setSize(AppPreferences.getDefaultGridSize());
Expand All @@ -83,11 +83,16 @@ public Grid(Grid grid) {
setOffset(grid.offsetX, grid.offsetY);
}

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

protected synchronized Map<Integer, Area> getGridShapeCache() {
return gridShapeCache;
}

protected synchronized void setGridShapeCache(int gridRadius, Area newGridArea) {
private synchronized void setGridShapeCache(int gridRadius, Area newGridArea) {
final AffineTransform at = new AffineTransform();
final double gridScale = (double) MAX_GRID_SIZE / getSize();
at.scale(gridScale, gridScale);
Expand Down Expand Up @@ -265,15 +270,16 @@ public Area getCellShape() {
return cellShape;
}

protected void setCellShape(Area cellShape) {
this.cellShape = cellShape;
}

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 @@ -338,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 @@ -371,6 +377,7 @@ public void setSize(int size) {
double footprintWidth = token.getFootprint(this).getBounds(this).getWidth() / 2;

// Test for gridless maps
var cellShape = getCellShape();
if (cellShape == null) {
double tokenBoundsWidth = token.getBounds(getZone()).getWidth() / 2;
visionRange += (footprintWidth > tokenBoundsWidth) ? tokenBoundsWidth : tokenBoundsWidth;
Expand Down Expand Up @@ -516,7 +523,7 @@ protected Area createHex(double x, double y, double radius, double rotation) {
}

private void fireGridChanged() {
gridShapeCache.clear();
getGridShapeCache().clear();
new MapToolEventBus().getMainEventBus().post(new GridChanged(this.zone));
}

Expand Down Expand Up @@ -836,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 All @@ -846,8 +853,6 @@ protected Area createGridArea(int gridRadius) {
gridArea.add(cellArea.createTransformedArea(at));
}

setGridShapeCache(gridRadius, gridArea);

return gridArea;
}

Expand Down Expand Up @@ -921,7 +926,8 @@ protected Area getGridAreaFromCache(int gridRadius) {
// Or if the flag is enabled, recreate cache
if (DeveloperOptions.Toggle.IgnoreGridShapeCache.isEnabled()
|| !getGridShapeCache().containsKey(gridRadius)) {
createGridArea(gridRadius);
var newArea = createGridArea(gridRadius);
setGridShapeCache(gridRadius, newArea);
}

double rescale = getSize() / (double) MAX_GRID_SIZE;
Expand All @@ -944,11 +950,8 @@ public static Grid fromDto(GridDto dto) {
grid.offsetX = dto.getOffsetX();
grid.offsetY = dto.getOffsetY();
grid.size = dto.getSize();
if (dto.hasCellShape()) {
grid.cellShape = Mapper.map(dto.getCellShape());
} else {
grid.cellShape = null;
}
grid.cellShape = grid.createCellShape();

return grid;
}

Expand All @@ -960,9 +963,6 @@ public GridDto toDto() {
dto.setOffsetX(offsetX);
dto.setOffsetY(offsetY);
dto.setSize(size);
if (cellShape != null) {
dto.setCellShape(Mapper.map(cellShape));
}
return dto.build();
}

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
Loading
Loading