198 lines
6.9 KiB
Java
198 lines
6.9 KiB
Java
package nl.minkema.leendert;
|
|
|
|
import javafx.event.EventHandler;
|
|
import javafx.scene.Group;
|
|
import javafx.scene.Node;
|
|
import javafx.scene.Scene;
|
|
import javafx.scene.control.Alert;
|
|
import javafx.scene.control.ButtonType;
|
|
import javafx.scene.control.Tooltip;
|
|
import javafx.scene.image.Image;
|
|
import javafx.scene.image.ImageView;
|
|
import javafx.scene.input.MouseEvent;
|
|
import javafx.scene.layout.StackPane;
|
|
import javafx.scene.media.Media;
|
|
import javafx.scene.media.MediaPlayer;
|
|
import javafx.scene.paint.Color;
|
|
import javafx.scene.shape.Rectangle;
|
|
import javafx.scene.text.Text;
|
|
import javafx.scene.text.TextBoundsType;
|
|
|
|
import java.net.MalformedURLException;
|
|
import java.net.URISyntaxException;
|
|
import java.util.ArrayList;
|
|
import java.util.Random;
|
|
|
|
/**
|
|
* @author J0QUE
|
|
* @version 1.0
|
|
*/
|
|
public class Menu extends Group {
|
|
|
|
|
|
public static Image[] images;
|
|
public static String[] names;
|
|
private static Random random = new Random(System.currentTimeMillis());
|
|
private int hoeveel = -1;
|
|
|
|
private ArrayList<Integer> chosen = new ArrayList<>();
|
|
|
|
public Menu() throws URISyntaxException, MalformedURLException {
|
|
ArrayList<Node> nodes = new ArrayList<>();
|
|
Image bg = new Image(ModLoader.getFile("/images/background.png"));
|
|
ImageView view = new ImageView(bg);
|
|
|
|
view.setX(0);
|
|
view.setY(0);
|
|
view.setFitHeight(850);
|
|
view.setPreserveRatio(false);
|
|
view.setFitWidth(1250);
|
|
|
|
String loc = this.getClass().getResource("/music/menu.mp4").toString();
|
|
//loc = new File("menu.mp4").toURI().toURL().toString();
|
|
//loc = ModLoader.getFile("/music/menu.mp4");
|
|
Media music = new Media(loc);
|
|
MediaPlayer menuplayer = new MediaPlayer(music);
|
|
menuplayer.setCycleCount(Integer.MAX_VALUE);
|
|
menuplayer.setVolume(0.6f);
|
|
menuplayer.setAutoPlay(true);
|
|
menuplayer.play();
|
|
menuplayer.play();
|
|
|
|
nodes.add(view);
|
|
|
|
|
|
int imagewidth = 100;
|
|
images = new Image[]{
|
|
new Image("/svg/p1.svg", imagewidth, 0, true, false),
|
|
new Image("/svg/p2.svg", imagewidth, 0, true, false),
|
|
new Image("/svg/p3.svg", imagewidth, 0, true, false),
|
|
new Image("/svg/p4.svg", imagewidth, 0, true, false),
|
|
new Image("/svg/p5.svg", imagewidth, 0, true, false)};
|
|
|
|
names = new String[]{"Paulus", "Pinkeltje", "Petunia", "Kwebbel", "Stuart"};
|
|
int x = 0, y = 0;
|
|
for (int i1 = 0; i1 < images.length; i1++) {
|
|
Image i = images[i1];
|
|
ImageView iv = new ImageView(i);
|
|
iv.setX(750 + x);
|
|
iv.setY(400 + y);
|
|
Tooltip.install(iv, new Tooltip(names[i1]));
|
|
x += i.getRequestedWidth();
|
|
if (x > 200) {
|
|
y += 150;
|
|
x = -70;
|
|
}
|
|
final int ch = i1;
|
|
iv.setOnMouseClicked(e -> {
|
|
if (chosen.contains(ch)) {
|
|
new Alert(Alert.AlertType.ERROR, "Sorry, dit karakter is al door iemand anders gekozen...").show();
|
|
return;
|
|
}
|
|
if (hoeveel < 0) {
|
|
Alert alert = new Alert(Alert.AlertType.ERROR, "Je moet eerst kiezen hoe veel spelers je wilt!\nDruk links op de juiste hoeveelheid kaboutertjes!", ButtonType.OK);
|
|
alert.show();
|
|
} else {
|
|
Image image = new Image(i.impl_getUrl(), 0, 50, true, false);
|
|
Game.players.add(new Player(image, ch));
|
|
hoeveel--;
|
|
if (hoeveel == 0) {
|
|
menuplayer.stop();
|
|
Main.scene.setRoot(new Game());
|
|
} else {
|
|
String een = "Er moet nog 1 persoon kiezen!";
|
|
String meer = String.format("Er moeten nog %d mensen kiezen!", hoeveel);
|
|
Alert alert = new Alert(Alert.AlertType.NONE, hoeveel > 1 ? meer : een, ButtonType.OK);
|
|
alert.show();
|
|
chosen.add(ch);
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
nodes.add(iv);
|
|
}
|
|
|
|
int x2 = 30, y2 = 50;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
if (i == 0) continue;
|
|
int number = 1 + i;
|
|
int locy = y2 + 150 * i + 10;
|
|
for (int e = 0; e < number; e++) {
|
|
ImageView view1 = new ImageView(new Image("/svg/p1.svg", 75, 0, true, false));
|
|
view1.setX(x2 + 85 * e + 10 * e);
|
|
view1.setY(locy);
|
|
nodes.add(view1);
|
|
}
|
|
Rectangle clickable = new Rectangle(x2 - 10, locy + 125, 95 + i * 95, 150);
|
|
clickable.setFill(Color.TRANSPARENT);
|
|
clickable.setStroke(Color.BLACK);
|
|
clickable.setArcHeight(50);
|
|
clickable.setArcWidth(50);
|
|
final int s = i + 1;
|
|
clickable.setOnMouseClicked(e -> {
|
|
hoeveel = s;
|
|
new Alert(Alert.AlertType.INFORMATION, "Alle " + hoeveel + " spelers kunnen nu een karakter kiezen. \nAls iemand de verkeerde kiest kan je opnieuw \nop de knop drukken om opnieuw te beginnen.").show();
|
|
});
|
|
Tooltip tip = new Tooltip("Hier kan je op klikken");
|
|
Tooltip.install(clickable, tip);
|
|
nodes.add(clickable);
|
|
}
|
|
|
|
getChildren().addAll(nodes);
|
|
}
|
|
|
|
public static StackPane createButton(String string, double x, double y, EventHandler<MouseEvent> onClick) {
|
|
Text text = createText(string);
|
|
Rectangle rectangle = rectangulate(text);
|
|
StackPane pane = new StackPane(rectangle, text);
|
|
pane.setOnMouseClicked(onClick);
|
|
pane.setLayoutX(x);
|
|
pane.setLayoutY(y);
|
|
return pane;
|
|
}
|
|
|
|
private static Text createText(String string) {
|
|
Text text = new Text(string);
|
|
text.setBoundsType(TextBoundsType.VISUAL);
|
|
text.setStyle(
|
|
"-fx-font-family: \"Arial\";" +
|
|
//"-fx-font-style: normal;" +
|
|
"-fx-font-size: 24px;"
|
|
);
|
|
|
|
return text;
|
|
}
|
|
|
|
private static Rectangle rectangulate(Text text) {
|
|
Rectangle rectangle = new Rectangle();
|
|
rectangle.setFill(getRandomColor());
|
|
final double PADDING = 25;
|
|
rectangle.setWidth(getWidth(text) + PADDING);
|
|
rectangle.setHeight(getHeight(text) + PADDING);
|
|
rectangle.setArcHeight(20);
|
|
rectangle.setArcWidth(20);
|
|
return rectangle;
|
|
}
|
|
|
|
private static double getWidth(Text text) {
|
|
new Scene(new Group(text));
|
|
text.applyCss();
|
|
|
|
return text.getLayoutBounds().getWidth();
|
|
}
|
|
|
|
private static double getHeight(Text text) {
|
|
new Scene(new Group(text));
|
|
text.applyCss();
|
|
|
|
return text.getLayoutBounds().getHeight();
|
|
}
|
|
|
|
public static Color getRandomColor() {
|
|
|
|
return Color.hsb(random.nextInt(360), 0.8, 0.8);
|
|
}
|
|
}
|