-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.java
110 lines (93 loc) · 2.75 KB
/
Cell.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
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
class Cell extends StackPane {
private Label text;
private boolean selected;
int value;
TextField textField;
boolean correct;
Cell(){
super();
getStyleClass().add("game-grid-cell");
selected = false;
correct = true;
if(value == 0) {
this.text = new Label("");
}
else {
this.text = new Label(String.format("%d", value));
}
text.getStyleClass().add("cell-label-medium");
StackPane.setAlignment(text, Pos.CENTER);
textField = new TextField();
textField.setVisible(false);
textField.setMaxWidth(48);
textField.setPrefWidth(48);
textField.getStyleClass().add("text-field");
getChildren().addAll(text, textField);
}
void showHighlighting(){
setHighlighting(!correct);
}
void setHighlighting(boolean state){
getStyleClass().clear();
if(state)
getStyleClass().add("game-grid-incorrect-cell");
else
getStyleClass().add("game-grid-cell");
}
void setValue(int i){
value = i;
if(value == 0)
textField.setText("");
else
textField.setText(Integer.toString(value));
update();
}
void setSelected(Boolean state) {
this.selected = state;
update();
}
void update(){
// Swap visibility of text and textfield
text.setVisible(!selected);
textField.setVisible(selected);
// If cell is deselected
if(!selected){
String textFieldText = textField.getText();
try {
value = Integer.parseInt(textFieldText);
} catch (NumberFormatException nfm){
value = 0;
}
updateTextBoxes();
}
}
void updateTextBoxes(){
if(value <= 0) {
value = 0;
text.setText("");
textField.setText("");
}
else {
text.setText(Integer.toString(value));
textField.setText(Integer.toString(value));
}
}
void setFont(int size){
text.getStyleClass().clear();
switch(size){
case 1:
text.getStyleClass().add("cell-label-small");
break;
case 2:
text.getStyleClass().add("cell-label-medium");
break;
case 3:
text.getStyleClass().add("cell-label-large");
break;
}
}
}