Skip to content
Snippets Groups Projects
Commit 669a2a62 authored by Guyslain's avatar Guyslain
Browse files

Refactoring

parent 0fa0c6d4
No related branches found
No related tags found
No related merge requests found
package view; package view;
import controller.Controller;
import datastruct.Coordinate;
import javafx.scene.input.MouseDragEvent; import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
import model.Cell;
import model.CellState;
import model.Grid;
/** /**
* Created by Arnaud Labourel on 22/11/2018. * Created by Arnaud Labourel on 22/11/2018.
...@@ -14,62 +14,69 @@ import model.Grid; ...@@ -14,62 +14,69 @@ import model.Grid;
public class MatrixPane extends GridPane { public class MatrixPane extends GridPane {
private static final double CELL_SIZE = 14; private static final double CELL_SIZE = 14;
public void initialize(Grid grid) { public Controller getController() {
for (int rowIndex = 0; rowIndex < grid.getNumberOfRows(); rowIndex++) { return controller;
for (int columnIndex = 0; columnIndex < grid.getNumberOfColumns(); columnIndex++) {
addCellRectangle(grid.getCell(rowIndex,columnIndex), rowIndex, columnIndex);
} }
private Controller controller;
public void initialize(Controller controller) {
this.controller = controller;
for (Coordinate coordinate : controller.coordinates()) {
addCellRectangle(coordinate);
} }
} }
private void addCellRectangle(Cell cell, int rowIndex, int columnIndex) { private void addCellRectangle(Coordinate coord) {
Rectangle rectangleCell = new Rectangle(CELL_SIZE, CELL_SIZE); Rectangle rectangleCell = new Rectangle(CELL_SIZE, CELL_SIZE);
addStatePropertyListener(cell, rectangleCell); addStatePropertyListener(rectangleCell, coord);
updateFill(rectangleCell, cell.getState()); updateFill(rectangleCell, coord);
addEventHandler(cell, rectangleCell); addEventHandler(rectangleCell, coord);
add(rectangleCell, columnIndex, rowIndex); add(rectangleCell, coord);
}
private void add(Rectangle rectangleCell, Coordinate coord) {
this.add(rectangleCell, coord.x(), coord.y());
} }
private void addStatePropertyListener(Cell cell, Rectangle cellRectangle) { private void addStatePropertyListener(Rectangle cellRectangle, Coordinate coord) {
cell.getStateProperty().addListener((observable, oldValue, newValue) -> controller.getSimulation().setChangeListener(
updateFill(cellRectangle, newValue)); coord,
() -> updateFill(cellRectangle, coord)
);
} }
private void updateFill(Rectangle cellRectangle, CellState newCellState) { private void updateFill(Rectangle cellRectangle, Coordinate coord) {
cellRectangle.setFill(newCellState.color); Color color = this.controller.getSimulation().getColor(coord);
cellRectangle.setFill(color);
} }
private void addEventHandler(Cell cell, Rectangle cellRectangle) { private void addEventHandler(Rectangle cellRectangle, Coordinate coord) {
cellRectangle.addEventHandler( cellRectangle.addEventHandler(
MouseEvent.MOUSE_PRESSED, MouseEvent.MOUSE_PRESSED,
event -> mouseListener.onMousePressed(event, cell) event -> mouseListener.onMousePressed(event, coord)
); );
cellRectangle.addEventHandler( cellRectangle.addEventHandler(
MouseEvent.DRAG_DETECTED, MouseEvent.DRAG_DETECTED,
event -> { event -> this.startFullDrag()
System.out.println("Full drag start");
this.startFullDrag();
}
); );
cellRectangle.addEventHandler( cellRectangle.addEventHandler(
MouseDragEvent.MOUSE_DRAG_RELEASED, MouseDragEvent.MOUSE_DRAG_RELEASED,
event -> mouseListener.onMouseReleased(event, cell) event -> mouseListener.onMouseReleased(event, coord)
); );
cellRectangle.addEventHandler( cellRectangle.addEventHandler(
MouseDragEvent.MOUSE_DRAG_ENTERED, MouseDragEvent.MOUSE_DRAG_ENTERED,
event -> mouseListener.onMouseEntered(event, cell) event -> mouseListener.onMouseEntered(event, coord)
); );
} }
private MouseListener mouseListener = new WaitingMouseListener(this); private MouseListener mouseListener = new WaitingMouseListener(this);
void setMouseListener(MouseListener mouseListener) { void setMouseListener(MouseListener mouseListener) {
System.out.println("Change listener");
this.mouseListener = mouseListener; this.mouseListener = mouseListener;
} }
void resetWaitingListener() { void resetWaitingListener() {
System.out.println("Reset listener");
this.mouseListener = new WaitingMouseListener(this); this.mouseListener = new WaitingMouseListener(this);
} }
} }
package view; package view;
import javafx.event.EventType; import datastruct.Coordinate;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import model.Cell;
interface MouseListener { interface MouseListener {
default void onMousePressed(MouseEvent event, Cell cell) {} default void onMousePressed(MouseEvent event, Coordinate coordinate) {}
default void onMouseReleased(MouseEvent event, Cell cell) {} default void onMouseReleased(MouseEvent event, Coordinate coordinate) {}
default void onMouseEntered(MouseEvent event, Cell cell) {}; default void onMouseEntered(MouseEvent event, Coordinate coordinate) {};
} }
package view; package view;
import datastruct.Coordinate;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import model.Cell;
import model.CellState;
class WaitingMouseListener implements MouseListener { class WaitingMouseListener implements MouseListener {
...@@ -14,9 +13,8 @@ class WaitingMouseListener implements MouseListener { ...@@ -14,9 +13,8 @@ class WaitingMouseListener implements MouseListener {
} }
@Override @Override
public void onMousePressed(MouseEvent event, Cell cell) { public void onMousePressed(MouseEvent event, Coordinate coord) {
cell.toggleState(); this.matrix.getController().getSimulation().next(coord);
CellState cellState = cell.getState(); matrix.setMouseListener(new FillingMouseListener(this.matrix, coord));
matrix.setMouseListener(new FillingMouseListener(this.matrix, cellState));
} }
} }
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.*?>
<?import javafx.scene.control.Label?> <?import javafx.scene.layout.*?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import view.MatrixPane?> <?import view.MatrixPane?>
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" <AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"
styleClass="background" stylesheets="@style.css" styleClass="background" stylesheets="@style.css"
......
...@@ -6,45 +6,45 @@ import org.junit.jupiter.api.Test; ...@@ -6,45 +6,45 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class GridTest { public class GridTest {
private Grid grid; private CellGrid grid;
@BeforeEach // @BeforeEach
public void initializeGrid() { // public void initializeGrid() {
grid = new Grid(6, 6); // grid = new CellGrid(6, 6);
} // }
//
@Test // @Test
public void testGetNeighbours() { // public void testGetNeighbours() {
assertThat(grid.getNeighbors(1, 1)).isNotNull(); // assertThat(grid.getNeighbors(1, 1)).isNotNull();
assertThat(grid.getNeighbors(1, 1)).hasSize(8); // assertThat(grid.getNeighbors(1, 1)).hasSize(8);
assertThat(grid.getNeighbors(1, 1)) // assertThat(grid.getNeighbors(1, 1))
.containsExactlyInAnyOrder(grid.getCell(0, 0), // .containsExactlyInAnyOrder(grid.getCell(0, 0),
grid.getCell(0, 1), // grid.getCell(0, 1),
grid.getCell(0, 2), // grid.getCell(0, 2),
grid.getCell(1, 0), // grid.getCell(1, 0),
grid.getCell(1, 2), // grid.getCell(1, 2),
grid.getCell(2, 0), // grid.getCell(2, 0),
grid.getCell(2, 1), // grid.getCell(2, 1),
grid.getCell(2, 2)); // grid.getCell(2, 2));
} // }
//
@Test // @Test
public void testCountAliveNeighbours() { // public void testCountAliveNeighbours() {
assertThat(grid.countAliveNeighbors(1, 1)).isEqualTo(0); // assertThat(grid.countAliveNeighbors(1, 1)).isEqualTo(0);
grid.getCell(2, 2).setState(CellState.ALIVE); // grid.getCell(2, 2).setState(CellState.ALIVE);
grid.getCell(0, 0).setState(CellState.ALIVE); // grid.getCell(0, 0).setState(CellState.ALIVE);
assertThat(grid.countAliveNeighbors(1, 1)).isEqualTo(2); // assertThat(grid.countAliveNeighbors(1, 1)).isEqualTo(2);
} // }
//
@Test // @Test
public void testCalculateNextState() { // public void testCalculateNextState() {
grid.getCell(1, 0).setState(CellState.ALIVE); // grid.getCell(1, 0).setState(CellState.ALIVE);
grid.getCell(1, 1).setState(CellState.ALIVE); // grid.getCell(1, 1).setState(CellState.ALIVE);
grid.getCell(1, 2).setState(CellState.ALIVE); // grid.getCell(1, 2).setState(CellState.ALIVE);
assertThat(grid.calculateNextState(0, 0).isAlive).isFalse(); // assertThat(grid.calculateNextState(0, 0).isAlive).isFalse();
assertThat(grid.calculateNextState(1, 0).isAlive).isFalse(); // assertThat(grid.calculateNextState(1, 0).isAlive).isFalse();
assertThat(grid.calculateNextState(1, 1).isAlive).isTrue(); // assertThat(grid.calculateNextState(1, 1).isAlive).isTrue();
assertThat(grid.calculateNextState(2, 1).isAlive).isTrue(); // assertThat(grid.calculateNextState(2, 1).isAlive).isTrue();
} // }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment