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

Refactoring of code to simplify CardinalDirection

parent 9ba62fc5
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,7 @@ import view.GridTileCanvas;
public class RootLayoutController {
@FXML
public GridTileCanvas gridTilePane;
public GridTileCanvas GridTileCanvas;
private MainApp mainApp;
......
package model;
import view.PointType;
public enum CardinalDirection {
NORTH(PointType.NORTH_WEST_CORNER, PointType.NORTH_EAST_CORNER),
EAST(PointType.NORTH_EAST_CORNER, PointType.SOUTH_EAST_CORNER),
SOUTH(PointType.SOUTH_EAST_CORNER, PointType.SOUTH_WEST_CORNER),
WEST(PointType.NORTH_WEST_CORNER, PointType.SOUTH_WEST_CORNER);
private final PointType firstPoint;
private final PointType secondPoint;
CardinalDirection(PointType firstPoint, PointType secondPoint) {
this.firstPoint = firstPoint;
this.secondPoint = secondPoint;
}
public PointType getFirstPoint() {
return firstPoint;
}
public PointType getSecondPoint() {
return secondPoint;
}
NORTH,
EAST,
SOUTH,
WEST
}
......@@ -43,17 +43,19 @@ public class GridTileCanvas extends Canvas {
return (row + pointType.getYPosition()) * TILE_HEIGHT;
}
private void drawTriangle(int row, int column, CardinalDirection cardinalDirection, Color color){
double xFirstPoint = getXPosition(column, cardinalDirection.getFirstPoint());
double yFirstPoint = getYPosition(row, cardinalDirection.getFirstPoint());
double xSecondPoint = getXPosition(column, cardinalDirection.getSecondPoint());
double ySecondPoint = getYPosition(row, cardinalDirection.getSecondPoint());
double xCenter = getXPosition(column, PointType.CENTER);
double yCenter = getYPosition(row, PointType.CENTER);
private void drawTriangle(int row, int column, CardinalDirection side, Color color){
List<PointType> cornerTypes = PointType.getIncidentCornerTypes(side);
double[] xPoints = new double[3];
double[] yPoints = new double[3];
for(int index = 0; index < 2; index++){
xPoints[index] = getXPosition(column, cornerTypes.get(index));
yPoints[index] = getYPosition(row, cornerTypes.get(index));
}
xPoints[2] = getXPosition(column, PointType.CENTER);
yPoints[2] = getYPosition(row, PointType.CENTER);
GraphicsContext graphicsContext = getGraphicsContext2D();
graphicsContext.setFill(color);
graphicsContext.fillPolygon(new double[]{xFirstPoint, xSecondPoint, xCenter},
new double[]{yFirstPoint, ySecondPoint, yCenter}, 3);
graphicsContext.fillPolygon(xPoints, yPoints, 3);
}
}
package view;
import model.CardinalDirection;
import java.util.List;
public enum PointType {
NORTH_WEST_CORNER(0,0),
NORTH_EAST_CORNER(1,0),
......@@ -14,6 +18,15 @@ public enum PointType {
this.yPosition = yPosition;
}
static public List<PointType> getIncidentCornerTypes(CardinalDirection side){
return switch (side){
case NORTH -> List.of(PointType.NORTH_WEST_CORNER, PointType.NORTH_EAST_CORNER);
case EAST -> List.of(PointType.NORTH_EAST_CORNER, PointType.SOUTH_EAST_CORNER);
case SOUTH -> List.of(PointType.SOUTH_EAST_CORNER, PointType.SOUTH_WEST_CORNER);
case WEST -> List.of(PointType.NORTH_WEST_CORNER, PointType.SOUTH_WEST_CORNER);
};
}
public double getXPosition() {
return xPosition;
}
......
......@@ -12,7 +12,7 @@
fx:controller="controller.RootLayoutController">
<GridTileCanvas xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:id="gridTilePane"
fx:id="GridTileCanvas"
height="1000.0" width="1000.0">
</GridTileCanvas>
</BorderPane>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment