-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenu.java
More file actions
276 lines (227 loc) · 6.55 KB
/
Menu.java
File metadata and controls
276 lines (227 loc) · 6.55 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.FadeTransition;
import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
/**
*
* Last Edited 24/05/2017 12:35 : Antony.J
*
*/
public class Menu extends Application {
public static void main(String[] args) {
launch(args);
}
public static Stage MenuStage;
private static final Font FONT = Font.font("", FontWeight.BOLD, 18);
private VBox menuBox;
// navigator of the menu
private int currentItem = 0;
public void start(Stage stage) throws Exception {
Scene scene = new Scene(createContent());
MenuStage = stage;
scene.setOnKeyPressed(event -> {
int temp;
if (event.getCode() == KeyCode.UP) {
if (currentItem > 0) {
temp = currentItem;
getMenuItem(currentItem - 1).setPrevActive(false);
getMenuItem(currentItem).setActive(false);
getMenuItem(--currentItem).setActive(true);
getMenuItem(temp).setPrevActive(true);
// depends on how many choices there are on menu
if (temp <= 2)
getMenuItem(++temp).setPrevActive(false);
}
}
if (event.getCode() == KeyCode.DOWN) {
if (currentItem < menuBox.getChildren().size() - 1) {
temp = currentItem;
getMenuItem(currentItem + 1).setPrevActive(false);
getMenuItem(currentItem).setActive(false);
getMenuItem(++currentItem).setActive(true);
getMenuItem(temp).setPrevActive(true);
if (temp >= 1)
getMenuItem(--temp).setPrevActive(false);
}
}
if (event.getCode() == KeyCode.ENTER) {
switch (getMenuItem(currentItem).getMenuName()) {
case "PLAY":
GUI gui = new GUI();
gui.setG("PLAY");
try {
gui.start(stage);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case "SANDBOX MODE":
GUI guiU = new GUI();
guiU.setG("USER");
try {
guiU.start(stage);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case "OPTIONS":
break;
case "EXIT":
System.exit(0);
}
}
});
stage.setScene(scene);
stage.initStyle(StageStyle.TRANSPARENT);
stage.show();
}
// main layout of the menu with UI
Parent createContent() {
StackPane root = new StackPane();
root.setPrefSize(500, 500);
MenuItem exit = new MenuItem("EXIT");
MenuItem play = new MenuItem("PLAY");
MenuItem sandbox = new MenuItem("SANDBOX MODE");
MenuItem options = new MenuItem("OPTIONS");
// Button exit = new Button("EXIT");
// Button play = new Button("PLAY");
// Button sandbox = new Button("SANDBOX");
// Button options = new Button("OPTIONS");
menuBox = new VBox(10, play, sandbox, options, exit);
StackPane.setAlignment(menuBox, Pos.CENTER);
menuBox.setTranslateX(360);
menuBox.setTranslateY(300);
Text about = new Text("COMP2911 \nMenu Prototype");
about.setTranslateX(50);
about.setTranslateY(500);
about.setFill(Color.WHITE);
about.setFont(FONT);
about.setOpacity(0.2);
menuBox.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");
getMenuItem(0).setActive(true);
root.getChildren().addAll(menuBox, about);
return menuBox;
}
private MenuItem getMenuItem(int index) {
if (index < 0)
return null;
return (MenuItem) menuBox.getChildren().get(index);
}
private static class MenuItem extends HBox {
private Box bbLeft = new Box(40, 40, "PLAYER");
private Box boxLeft = new Box(40, 40, "BOX");
private Text text;
private String name;
public MenuItem(String name) {
// constructor for distance between sprite and text
super(15);
Button button = new Button(name);
button.setId(name);
button.setOnAction(myHandler);
this.name = name;
setAlignment(Pos.CENTER_LEFT);
text = new Text(name);
text.setFont(FONT);
getChildren().addAll(bbLeft, boxLeft, button);
setPrevActive(false);
setActive(false);
}
public String getMenuName() {
return name;
}
public void setPrevActive(boolean b) {
bbLeft.setVisible(b);
}
public void setActive(boolean b) {
boxLeft.managedProperty().bind(boxLeft.visibleProperty());
boxLeft.setVisible(b);
text.setFill(b ? Color.WHITE : Color.BISQUE);
}
private static class Box extends Parent {
Rectangle r;
public Box(int x, int y, String type) {
r = new Rectangle(x, y);
r.setStroke(Color.WHITE);
if (type.equals("BOX")) {
r.setFill(Color.BROWN);
} else if (type.equals("PLAYER"))
r.setFill(Color.BLUE);
getChildren().add(r);
}
}
final EventHandler<ActionEvent> myHandler = new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent event) {
Button x = (Button) event.getSource();
switch (x.getId()) {
case "PLAY":
GUI gui = new GUI();
gui.setG("PLAY");
try {
MenuStage.setWidth(500);
MenuStage.setHeight(500);
gui.start(MenuStage);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case "SANDBOX MODE":
GUI guiU = new GUI();
guiU.setG("USER");
try {
guiU.start(MenuStage);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case "OPTIONS":
break;
case "EXIT":
System.exit(0);
}
}
};
}
}