Java-Adventure-Game / Java-Based Adventure Game main / AdventureGameViews / SettingsView.java
SettingsView.java
Raw
package AdventureGameViews;

import javafx.animation.ScaleTransition;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * Class SettingsView.
 *
 * Includes functionality for the settings menu, including buttons and labels
 */
public class SettingsView {
    private AdventureGameView adventureGameView;
    public SettingsView(AdventureGameView adventureGameView) {
        this.adventureGameView = adventureGameView;

        final Stage stage = new Stage();

        // makes it so that we can't interact with the adventureGameView until closing the settings menu
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initOwner(adventureGameView.stage);

        // main vbox for the stage
        VBox dialogVbox = new VBox(20);
        dialogVbox.setPadding(new Insets(20, 20, 20, 20));
        // custom background
        dialogVbox.setStyle("-fx-background-image: url('Games/TinyGame/settingsMenuBackground.png');"
                + "-fx-background-size: cover;"); // optional: covers the entire VBox


        // labels/buttons to add to VBoxes
        Label settingsTitle = new Label(String.format("Settings Menu"));
        settingsTitle.setId("Settings");
        settingsTitle.setStyle("-fx-text-fill: #e8e6e3;");
        settingsTitle.setFont(new Font(28));

        Label pausedTitle = new Label(String.format("Paused"));
        pausedTitle.setId("Paused");
        pausedTitle.setStyle("-fx-text-fill: #e8e6e3;");
        pausedTitle.setFont(new Font(17));

        Label test = new Label(String.format("Mute Button"));
        test.setId("Mute Button");
        test.setStyle("-fx-text-fill: #e8e6e3;");
        test.setFont(new Font(18));

        // mute button code
        ToggleButton soundToggleButton = new ToggleButton();
        soundToggleButton.setStyle("-fx-background-color: #999999;");
        soundToggleButton.setId("SoundToggle");
        ImageView soundImage = new ImageView(new Image("Games/TinyGame/sound.png"));
        soundImage.setFitWidth(70);
        soundImage.setFitHeight(60);
        ImageView muteImage = new ImageView(new Image("Games/TinyGame/mute.png"));
        muteImage.setFitWidth(70);
        muteImage.setFitHeight(60);
        soundToggleButton.setGraphic(soundImage);

        ScaleTransition scaleTransition = new ScaleTransition(Duration.millis(200), soundToggleButton);
        scaleTransition.setToX(1.0);
        scaleTransition.setToY(1.0);
        scaleTransition.setAutoReverse(true);
        soundToggleButton.setOnAction(event -> {
            if (adventureGameView.mediaPlayer != null) {
                adventureGameView.mediaPlaying = !adventureGameView.mediaPlaying;
                if (adventureGameView.mediaPlaying) {
                    adventureGameView.mediaPlayer.play();
                    soundToggleButton.setGraphic(soundImage);
                } else {
                    adventureGameView.mediaPlayer.pause();
                    soundToggleButton.setGraphic(muteImage);
                }
                scaleTransition.play();
            }
        });

        Label tinyGame = new Label(String.format("TinyGame"));
        tinyGame.setId("TinyGame");
        tinyGame.setStyle("-fx-text-fill: #e8e6e3;");
        tinyGame.setFont(new Font(18));

        /* scrapped image displaying a collage of room images that could have been in the menu
        ImageView testImage = new ImageView(new Image("Games/TinyGame/collage.png"));
        testImage.setFitWidth(200);
        testImage.setFitHeight(200);
         */

        // left, middle, right VBoxes to add to dialogVbox and their positioning
        VBox middle = new VBox(10, settingsTitle, pausedTitle);
        middle.setAlignment(Pos.CENTER);
        VBox left = new VBox(10, test, soundToggleButton);
        left.setAlignment(Pos.CENTER);
        VBox.setMargin(left, new Insets(100, 100, 100, 100));
        VBox right = new VBox(10, tinyGame);
        right.setAlignment(Pos.BOTTOM_CENTER);
        VBox.setMargin(right, new Insets(100, 100, 100, 100));

        // add sub-children VBoxes to main VBox and set the size of the menu
        dialogVbox.getChildren().addAll(middle, left, right);
        Scene dialogScene = new Scene(dialogVbox, 450, 600);
        stage.setScene(dialogScene);
        // add a grey border with thickness 5
        BorderStroke borderStroke = new BorderStroke(
                Color.GREY, BorderStrokeStyle.SOLID,
                CornerRadii.EMPTY, new BorderWidths(5)
        );
        dialogVbox.setBorder(new Border(borderStroke));

        // finally, present the stage
        stage.show();
    }
}