aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/controller/MainWindowController.java
blob: 0d59029037c424b8a5a625e84dbda2759c89f7be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package controller;

import adapter.IModelAdapter;
import adapter.ModelAdapter;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import model.Board;
import model.IModel;
import observer.IObserver;


/**
 * Created by loic on 22/09/16.
 */
public class MainWindowController implements IObserver {

    @FXML Canvas boardCanvas;

    private ModelAdapter adapter;
    private IModel model;

    private int squareSize=50;
    private int squarePadding=10;
    private int[] boardPosition={0,0};





    public void loadComponent(ModelAdapter adapter, IModel model, Scene scene){
        this.adapter=adapter;
        this.model=model;
        this.update();
        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if(event.getCode().toString().equals("UP")){
                    goUp(null);
                }
                else if(event.getCode().toString().equals("DOWN")){
                    goDown(null);
                }
                else if(event.getCode().toString().equals("LEFT")){
                    goLeft(null);
                }
                else if(event.getCode().toString().equals("RIGHT")){
                    goRight(null);
                }
            }
        });

    }

    @FXML protected void goUp(ActionEvent event) {
        this.adapter.goUp();
    }
    @FXML protected void goDown(ActionEvent event) {
        this.adapter.goDown();
    }
    @FXML protected void goLeft(ActionEvent event) {
        this.adapter.goLeft();
    }
    @FXML protected void goRight(ActionEvent event) {
        this.adapter.goRight();
    }

    @Override
    public void update() {
        this.draw();
    }




    private void draw(){

        GraphicsContext gc = boardCanvas.getGraphicsContext2D();
        gc.clearRect(0,0,500,500);
        int[][] board=this.model.getBoard();



        for(int i=0; i<board.length;i++) {
            for (int j = 0; j < board[i].length; j++) {
                gc.setFill(Color.GRAY);

                int x=this.boardPosition[0] + (j*this.squareSize);
                int y=this.boardPosition[1] + (i*this.squareSize);

                if(j>0){
                    x+=j*squarePadding;
                }
                if(i>0){
                    y+=i*squarePadding;
                }

                gc.fillRect(x,y, this.squareSize, this.squareSize);
                gc.setFill(Color.BLACK);

                int value=board[i][j];
                if(value<0){
                    value=0;
                }

                gc.fillText("" + value, x + (this.squareSize / 2), y + (this.squareSize / 2));

            }
        }
    }

}