Skip to content
Snippets Groups Projects
MatrixPane.java 2.72 KiB
Newer Older
  • Learn to ignore specific revisions
  • Guyslain's avatar
    Guyslain committed
    import controller.Controller;
    import datastruct.Coordinate;
    
    import javafx.scene.input.MouseDragEvent;
    
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.GridPane;
    
    Guyslain's avatar
    Guyslain committed
    import javafx.scene.paint.Color;
    
    import javafx.scene.shape.Rectangle;
    
    
    /**
     * Created by Arnaud Labourel on 22/11/2018.
     */
    
    Guyslain's avatar
    Guyslain committed
    public class MatrixPane extends GridPane {
    
        private static final double CELL_SIZE = 14;
    
    
    Guyslain's avatar
    Guyslain committed
        public Controller getController() {
            return controller;
        }
    
        private Controller controller;
    
        public void initialize(Controller controller) {
            this.controller = controller;
            for (Coordinate coordinate : controller.coordinates()) {
                addCellRectangle(coordinate);
    
    Guyslain's avatar
    Guyslain committed
        private void addCellRectangle(Coordinate coord) {
    
            Rectangle rectangleCell = new Rectangle(CELL_SIZE, CELL_SIZE);
    
    Guyslain's avatar
    Guyslain committed
            addStatePropertyListener(rectangleCell, coord);
            updateFill(rectangleCell, coord);
            addEventHandler(rectangleCell, coord);
            add(rectangleCell, coord);
    
    Guyslain's avatar
    Guyslain committed
        private void add(Rectangle rectangleCell, Coordinate coord) {
            this.add(rectangleCell, coord.x(), coord.y());
        }
    
        private void addStatePropertyListener(Rectangle cellRectangle, Coordinate coord) {
            controller.getSimulation().setChangeListener(
                    coord,
                    () -> updateFill(cellRectangle, coord)
            );
    
    Guyslain's avatar
    Guyslain committed
        private void updateFill(Rectangle cellRectangle, Coordinate coord) {
            Color color = this.controller.getSimulation().getColor(coord);
            cellRectangle.setFill(color);
    
    Guyslain's avatar
    Guyslain committed
        private void addEventHandler(Rectangle cellRectangle, Coordinate coord) {
    
            cellRectangle.addEventHandler(
                    MouseEvent.MOUSE_PRESSED,
    
    Guyslain's avatar
    Guyslain committed
                    event -> mouseListener.onMousePressed(event, coord)
    
            );
            cellRectangle.addEventHandler(
                    MouseEvent.DRAG_DETECTED,
    
    Guyslain's avatar
    Guyslain committed
                    event -> this.startFullDrag()
    
                    MouseEvent.MOUSE_CLICKED,
                    event -> mouseListener.onMouseClicked(event, coord)
            );
            cellRectangle.addEventHandler(
                    MouseDragEvent.MOUSE_RELEASED,
    
    Guyslain's avatar
    Guyslain committed
                    event -> mouseListener.onMouseReleased(event, coord)
    
            );
            cellRectangle.addEventHandler(
                    MouseDragEvent.MOUSE_DRAG_ENTERED,
    
    Guyslain's avatar
    Guyslain committed
                    event -> mouseListener.onMouseEntered(event, coord)
    
            );
        }
    
        private MouseListener mouseListener = new WaitingMouseListener(this);
    
        void setMouseListener(MouseListener mouseListener) {
            this.mouseListener = mouseListener;
        }
    
        void resetWaitingListener() {
    
            setMouseListener(new WaitingMouseListener(this));