aboutsummaryrefslogtreecommitdiff
path: root/src/gui/Driver.java
blob: 162d1feabfb91a5bcee025b20dd89722c5bf2b9f (plain) (blame)
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
package gui;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
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.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import main.LevelController;
import main.SettingsController;
import sound.AudioFilePlayer;

import javax.sound.sampled.AudioFileFormat;

// import javafx.scene.image.ImageView;
// import javafx.beans.property.Property;
// import javafx.util.Duration;
// import javafx.animation.KeyFrame;
// import javafx.animation.KeyValue;
// import javafx.animation.Timeline;

import devmenu.DebugMenu;
import gameplay.SongPlayer;


public class Driver extends Application 
{
    gameplay.SongPlayer placeHolder = new gameplay.SongPlayer();
    public static AudioFilePlayer mediaPlayer;
    
    public static Stage primaryStage;
    static Pane primaryPane = new Pane();

    public static SettingsController settingsController = new SettingsController();
    public static LevelController levelController = new LevelController();
    public static DebugMenu debug = new DebugMenu();

    /*
     * starts javafx
     */
    public static void main(String[] args) 
    {
        launch(args);
    }
    
    /*
     * sets up game windows and starts controllers
     * (automatically called by javafx on start)
     */
    @Override
    public void start(Stage newPrimaryStage)
    {   
        mediaPlayer = new AudioFilePlayer("src/assets/MenuMusicPlaceholder.wav");
        mediaPlayer.play();
        
        primaryStage = newPrimaryStage;

        Scene primaryScene = new Scene(primaryPane, 800, 600);
        primaryScene.getStylesheets().add("gui/style.css");

        primaryStage.setScene(primaryScene);
        primaryStage.setTitle("NPE Hero");

        fixMenuSync();
        setMenu(new MainMenu());
        setBackground("assets/forest.png");

        primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, event -> { //full screen stuff
            if (KeyCode.F11.equals(event.getCode())) {
                primaryStage.setFullScreen(!primaryStage.isFullScreen());
            }
        });
        primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
        primaryStage.setFullScreenExitHint("");
        primaryStage.show();
    }

    private void fixMenuSync() {
        primaryPane.getChildren().addAll(placeHolder);
        primaryPane.getChildren().removeAll(placeHolder);
    }

    /**
     * Replaces/adds a new pane to the primaryPane
     * @param pane  the new pane
     */
    public static void setMenu(Pane pane)
    {
        if (! primaryPane.getChildren().isEmpty())
        {
            primaryPane.getChildren().remove(0);
        }
        primaryPane.getChildren().add(pane);
        pane.prefWidthProperty().bind(primaryPane.widthProperty()); //makes pane fill the window
        pane.prefHeightProperty().bind(primaryPane.heightProperty());
        primaryPane.requestFocus(); //make the pane itself focused by the keyboard naviagtion so no button is highlighted by default
    }

    /**
     * @return the current pane in primaryPane
     */
    public static Pane getMenu(){
        return (Pane) primaryPane.getChildren().get(0);
    }

    /**
     * replaces the background image with a new one
     * @param url   the url of the image to set
     */
    public static void setBackground(String url) //replaces background with a new one
    {
        // Image image1;
        // Image image2;
        // ImageView imageView;
        // KeyFrame keyFrame1On = new KeyFrame(Duration.seconds(0), new KeyValue(imageView.imageProperty(), image1));
        // KeyFrame startFadeOut = new KeyFrame(Duration.seconds(0.2), new KeyValue(imageView.opacityProperty(), 1.0));
        // KeyFrame endFadeOut = new KeyFrame(Duration.seconds(0.5), new KeyValue(imageView.opacityProperty(), 0.0));
        // KeyFrame keyFrame2On = new KeyFrame(Duration.seconds(0.5), new KeyValue(imageView.imageProperty(), image2));
        // KeyFrame endFadeIn = new KeyFrame(Duration.seconds(0.8), new KeyValue(imageView.opacityProperty(), 1.0));
        // Timeline timelineOn = new Timeline(keyFrame1On, startFadeOut, endFadeOut, keyFrame2On, endFadeIn);

        primaryPane.setBackground(new Background(
            new BackgroundImage(
                    new Image(url),
                    BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT,
                    new BackgroundPosition(Side.LEFT, 0, true, Side.BOTTOM, 0, true),
                    new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, true, true, false, true)
            )));
    }

    /**
     * quits the application
     */
    public static void quit()
    {
        Platform.exit();
    }
}