Skip to content
Snippets Groups Projects
Commit 96cce2e3 authored by LABOUREL Arnaud's avatar LABOUREL Arnaud
Browse files

Refactoring du draw

parent 5bc396d5
No related branches found
No related tags found
No related merge requests found
Pipeline #3286 passed
...@@ -13,7 +13,7 @@ public class GridCanvas extends Canvas { ...@@ -13,7 +13,7 @@ public class GridCanvas extends Canvas {
private final double tileHeight; private final double tileHeight;
private final Integer numberOfColumns; private final Integer numberOfColumns;
private final Integer numberOfRows; private final Integer numberOfRows;
private final Grid tileGrid; private final Grid grid;
private final GraphicsContext graphicsContext; private final GraphicsContext graphicsContext;
public GridCanvas(@NamedArg("tileWidth") Double tileWidth, public GridCanvas(@NamedArg("tileWidth") Double tileWidth,
...@@ -26,7 +26,7 @@ public class GridCanvas extends Canvas { ...@@ -26,7 +26,7 @@ public class GridCanvas extends Canvas {
this.numberOfRows = numberOfRows; this.numberOfRows = numberOfRows;
this.setWidth(tileWidth * numberOfColumns); this.setWidth(tileWidth * numberOfColumns);
this.setHeight(tileHeight * numberOfRows); this.setHeight(tileHeight * numberOfRows);
tileGrid = new ArrayGrid(numberOfRows, numberOfColumns); grid = new ArrayGrid(numberOfRows, numberOfColumns);
graphicsContext = getGraphicsContext2D(); graphicsContext = getGraphicsContext2D();
} }
...@@ -36,7 +36,7 @@ public class GridCanvas extends Canvas { ...@@ -36,7 +36,7 @@ public class GridCanvas extends Canvas {
} }
private void drawGrid(){ private void drawGrid(){
drawGridTile(tileGrid); draw(grid);
} }
private void clear(){ private void clear(){
...@@ -44,35 +44,76 @@ public class GridCanvas extends Canvas { ...@@ -44,35 +44,76 @@ public class GridCanvas extends Canvas {
} }
public void fillGrid(TileGenerator tileGenerator){ public void fillGrid(TileGenerator tileGenerator){
tileGrid.fill(tileGenerator); grid.fill(tileGenerator);
} }
public void drawGridTile(Grid tileGrid){ public void draw(Grid tileGrid){
drawSquares(tileGrid);
strokeSquares(tileGrid);
}
private void drawSquares(Grid tileGrid) {
for(int row = 0; row < numberOfRows; row++) for(int row = 0; row < numberOfRows; row++)
for(int column = 0; column < numberOfColumns; column++){ for(int column = 0; column < numberOfColumns; column++){
Tile tile = tileGrid.getSquare(row, column).getTile(); Square square = tileGrid.getSquare(row, column);
drawTile(tile, column * tileWidth, row * tileHeight); drawSquare(square, column * tileWidth, row * tileHeight);
}
}
private void strokeSquares(Grid tileGrid) {
for(int row = 0; row < numberOfRows; row++)
for(int column = 0; column < numberOfColumns; column++){
Square square = tileGrid.getSquare(row, column);
strokeSquare(square, column * tileWidth, row * tileHeight);
}
}
private void strokeSquare(Square square, double x, double y) {
PointType[] cornerTypes = PointType.values();
double[] xPoints = new double[cornerTypes.length];
double[] yPoints = new double[cornerTypes.length];
for(int index = 0; index < cornerTypes.length; index++){
xPoints[index] = x + (cornerTypes[index].getXPosition() * tileWidth);
yPoints[index] = y + (cornerTypes[index].getYPosition() * tileHeight);
}
final int indexCenter = 4;
for(CardinalDirection side : CardinalDirection.values()) {
int indexCorner0 = side.ordinal();
int indexCorner1 = (indexCorner0+1)%4;
strokeSquareSide(square, xPoints[indexCorner0], yPoints[indexCorner0], xPoints[indexCorner1], yPoints[indexCorner1], side);
strokeInternalTriangleSide(square, xPoints[indexCorner0],yPoints[indexCorner0],xPoints[indexCenter],yPoints[indexCenter], side);
} }
} }
private void drawTile(Tile tile, double x, double y) { private void strokeSquareSide(Square square, double x0, double y0, double x1, double y1, CardinalDirection side) {
if(square.compatibleSides(List.of(square.getTile().side(side)), side).isEmpty())
graphicsContext.strokeLine(x0, y0, x1, y1);
}
private void strokeInternalTriangleSide(Square square, double x0, double y0, double x1, double y1, CardinalDirection side) {
Tile tile = square.getTile();
if(!tile.side(side).accept(tile.side(Rotation.THREE_QUARTER_TURN.rotatedDirection(side))))
graphicsContext.strokeLine(x0, y0, x1, y1);
}
private void drawSquare(Square square, double x, double y) {
for(CardinalDirection side: CardinalDirection.values()){ for(CardinalDirection side: CardinalDirection.values()){
drawSideTriangle(tile, x, y, side); drawSideTriangle(square, x, y, side);
} }
} }
private void drawSideTriangle(Tile tile, double x, double y, CardinalDirection side){ private void drawSideTriangle(Square square, double x, double y, CardinalDirection side){
Color color = square.getTile().side(side).color();
List<PointType> cornerTypes = PointType.trianglePointTypes(side); List<PointType> cornerTypes = PointType.trianglePointTypes(side);
double[] xPoints = new double[3]; int size = PointType.trianglePointTypes(side).size();
double[] yPoints = new double[3]; double[] xPoints = new double[size];
for(int index = 0; index < 3; index++){ double[] yPoints = new double[size];
for(int index = 0; index < size; index++){
xPoints[index] = x + (cornerTypes.get(index).getXPosition() * tileWidth); xPoints[index] = x + (cornerTypes.get(index).getXPosition() * tileWidth);
yPoints[index] = y + (cornerTypes.get(index).getYPosition() * tileHeight); yPoints[index] = y + (cornerTypes.get(index).getYPosition() * tileHeight);
} }
Color color = tile.side(side).color();
graphicsContext.setFill(color); graphicsContext.setFill(color);
graphicsContext.strokePolygon(xPoints, yPoints, 3); graphicsContext.fillPolygon(xPoints, yPoints, size);
graphicsContext.fillPolygon(xPoints, yPoints, 3);
} }
} }
...@@ -7,8 +7,8 @@ import java.util.List; ...@@ -7,8 +7,8 @@ import java.util.List;
public enum PointType { public enum PointType {
NORTH_WEST_CORNER(0,0), NORTH_WEST_CORNER(0,0),
NORTH_EAST_CORNER(1,0), NORTH_EAST_CORNER(1,0),
SOUTH_WEST_CORNER(0,1),
SOUTH_EAST_CORNER(1,1), SOUTH_EAST_CORNER(1,1),
SOUTH_WEST_CORNER(0,1),
CENTER(0.5,0.5); CENTER(0.5,0.5);
private final double xPosition; private final double xPosition;
private final double yPosition; private final double yPosition;
...@@ -23,7 +23,7 @@ public enum PointType { ...@@ -23,7 +23,7 @@ public enum PointType {
case NORTH -> List.of(PointType.NORTH_WEST_CORNER, PointType.NORTH_EAST_CORNER, PointType.CENTER); case NORTH -> List.of(PointType.NORTH_WEST_CORNER, PointType.NORTH_EAST_CORNER, PointType.CENTER);
case EAST -> List.of(PointType.NORTH_EAST_CORNER, PointType.SOUTH_EAST_CORNER, PointType.CENTER); case EAST -> List.of(PointType.NORTH_EAST_CORNER, PointType.SOUTH_EAST_CORNER, PointType.CENTER);
case SOUTH -> List.of(PointType.SOUTH_EAST_CORNER, PointType.SOUTH_WEST_CORNER, PointType.CENTER); case SOUTH -> List.of(PointType.SOUTH_EAST_CORNER, PointType.SOUTH_WEST_CORNER, PointType.CENTER);
case WEST -> List.of(PointType.NORTH_WEST_CORNER, PointType.SOUTH_WEST_CORNER, PointType.CENTER); case WEST -> List.of(PointType.SOUTH_WEST_CORNER, PointType.NORTH_WEST_CORNER, PointType.CENTER);
}; };
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment