-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenControl.java
130 lines (130 loc) · 5.18 KB
/
ScreenControl.java
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
121
122
123
124
125
126
127
128
129
130
import javafx.scene.layout.Pane;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
/**
* This class helps for controlling what goes on on the field
* @author JT Herndon
* @version 1.0
*/
public class ScreenControl {
private Pane canvas;
private Production production;
private double sX;
private double sY;
private Rectangle selectRect;
private Buttons buttons;
//tmp variables for dragging stuff
/**
* Main constructor
* @param canvas the field Pane
* @param production the current Production
*/
public ScreenControl(Pane canvas, Buttons buttons) {
this.canvas = canvas;
this.production = buttons.getProduction();
this.buttons = buttons;
selectRect = new Rectangle();
selectRect.setVisible(false);
selectRect.setFill(Color.CYAN);
selectRect.setStroke(Color.BLUE);
selectRect.setOpacity(0.3);
canvas.getChildren().add(selectRect);
initControls();
}
/**
* Initiates the controls
*/
private void initControls() {
canvas.addEventHandler(MouseEvent.MOUSE_PRESSED,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
//System.out.println(e.getX() + " " + e.getY());
Mode mode = production.getMode();
switch(mode) {
case ADDDOT:
//System.out.println("Adding dot");
production.addDot(e.getY(), e.getX(), canvas);
break;
case MOVEDOT:
production.setCurrentDot(production
.selectClosestDot(e.getY(), e.getX()));
production.setMode(Mode.MOVEDOT);
break;
case BOXSELECT:
sX = e.getX();
sY = e.getY();
selectRect.setVisible(true);
selectRect.setWidth(0);
selectRect.setHeight(0);
break;
case SELECT:
production.getSelector().toggleSelect(production
.selectClosestDot(e.getY(), e.getX()));
break;
case MOVESELECTED:
sX = e.getX();
sY = e.getY();
production.getSelector().initMove();
break;
default:
break;
}
//System.out.println(production.getMode());
buttons.enableAll();
}
}
);
canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED,
new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
Mode mode = production.getMode();
switch(mode) {
case MOVEDOT:
production.getCurrentDot().change(production
.getSet(), e.getY(), e.getX());
production.getCurrentDot().move(production.getSet());
break;
case BOXSELECT:
selectRect.setX(e.getX() < sX ? e.getX() : sX);
selectRect.setY(e.getY() < sY ? e.getY() : sY);
selectRect.setHeight(e.getY() < sY ?
sY - e.getY() : e.getY() - sY);
selectRect.setWidth(e.getX() < sX ?
sX - e.getX() : e.getX() - sX);
production.getSelector().boxSelect(sX, e.getX(), sY,
e.getY());
break;
case MOVESELECTED:
production.getSelector().move(sX, sY, e.getX(),
e.getY());
break;
}
}
}
);
canvas.addEventHandler(MouseEvent.MOUSE_RELEASED,
new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
Mode mode = production.getMode();
switch(mode) {
case MOVEDOT:
production.getCurrentDot().change(production
.getSet(), e.getY(), e.getX());
production.getCurrentDot().move(production.getSet());
break;
case BOXSELECT:
selectRect.setVisible(false);
buttons.enableAll();
break;
default:
buttons.enableAll();
}
}
}
);
}
}