-
Notifications
You must be signed in to change notification settings - Fork 3
/
MenuBox.java
155 lines (133 loc) · 4.02 KB
/
MenuBox.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
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
package com.danilchican.pacman;
import javafx.geometry.*;
import javafx.scene.layout.*;
import javafx.scene.image.*;
import javafx.scene.Parent;
import javafx.scene.effect.DropShadow;
import javafx.animation.TranslateTransition;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.util.Duration;
/**
* The class wich contains menu box of menu items
*
* @author Vlad Danilchik
*
*/
public class MenuBox extends StackPane {
private enum MenuStatus {
OPENED, CLOSED
}
private MenuStatus status = MenuStatus.OPENED;
/**
* MenuBox constructor
*
* @param items to display the MenuBox
* @see MenuBox#MenuBox(MenuItem...)
*/
@SuppressWarnings("static-access")
public MenuBox(MenuItem... items) {
DropShadow shadow = new DropShadow(7, 5, 0, Color.BLACK);
shadow.setSpread(0.8);
VBox vbox = new VBox();
vbox.setAlignment(Pos.TOP_CENTER);
vbox.setPadding(new Insets(280, 0, 0, 350));
VBox box = new VBox();
box.setAlignment(Pos.TOP_CENTER);
for (MenuItem item : items) {
box.setMargin(item, new Insets(1, 0, 2, 0));
box.getChildren().add(item);
}
vbox.getChildren().addAll(items);
setAlignment(Pos.TOP_CENTER);
getChildren().addAll(vbox);
}
/**
* Show the menu box
*
* @see MenuBox#show()
*/
public void show() {
this.status = MenuStatus.OPENED;
setVisible(true);
TranslateTransition trans = new TranslateTransition(Duration.seconds(0.5), this);
trans.setToY(0);
trans.play();
}
/**
* Hide the menu box
*
* @see MenuBox#hide()
*/
public void hide() {
this.status = MenuStatus.CLOSED;
TranslateTransition trans = new TranslateTransition(Duration.seconds(0.5), this);
trans.setToY(-600);
trans.setOnFinished(event -> setVisible(false));
trans.play();
}
/**
* Check opened menu
*
* @return boolean
* @see MenuBox#isOpened()
*/
public boolean isOpened() {
return (this.status == MenuStatus.OPENED);
}
/**
* Set menu options
*
* @see MenuBox#setMenuOptions()
*
*/
public static void setMenuOptions() {
Constants.main_scene.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ESCAPE) {
if (!(Constants.menu.isOpened()) && Constants.subMenuLevels.isOpened()) {
Constants.subMenuLevels.hide();
Constants.menu.show();
} else if (Constants.menu.isOpened() && !(Constants.subMenuLevels.isOpened())) {
Constants.menu.hide();
Constants.subMenuLevels.show();
}
}
});
}
/**
* Method wich draws menu
*
* @return Parent
* @see MenuBox#drawMenu()
*/
public static Parent drawMenu() {
Constants.mainRoot.setPrefSize(Constants.screenWidth, Constants.screenHeight);
Image im = new Image(MenuBox.class.getResourceAsStream("pacman.jpg"));
ImageView img = new ImageView(im);
img.setFitWidth(Constants.screenWidth);
img.setFitHeight(Constants.screenHeight);
Constants.mainRoot.getChildren().add(img);
Constants.menu = new MenuBox(new MenuItem("NEW GAME", MenuItem.Options.NEW_GAME),
new MenuItem("LEVELS", MenuItem.Options.LEVELS),
new MenuItem("PLAY SAVED", MenuItem.Options.PLAY_SAVED),
new MenuItem("BOOT", MenuItem.Options.BOOT), new MenuItem("SAVES", MenuItem.Options.SAVES),
new MenuItem("SORTING", MenuItem.Options.SORTING),
new MenuItem("SORTING LIST", MenuItem.Options.SORTING_LIST),
new MenuItem("QUIT", MenuItem.Options.QUIT));
Constants.mainRoot.getChildren().add(Constants.menu);
return Constants.mainRoot;
}
/**
* Method wich draw submenu levels
*
* @see MenuBox#drawSubMenuLevels()
*
*/
public static void drawSubMenuLevels() {
Constants.subMenuLevels = new MenuBox(new MenuItem("LEVEL 1", MenuItem.Options.LEVEL_1),
new MenuItem("LEVEL 2", MenuItem.Options.LEVEL_2),
new MenuItem("BACK", MenuItem.Options.BACK));
Constants.mainRoot.getChildren().add(Constants.subMenuLevels);
Constants.subMenuLevels.hide();
}
}