aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/sowgro/npehero/Driver.java
blob: 411bf876fbdc6823d1f63795957b7180141fd11e (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
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
package net.sowgro.npehero;

import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.util.Duration;
import net.sowgro.npehero.main.ErrorDisplay;
import net.sowgro.npehero.levelapi.Levels;
import net.sowgro.npehero.main.*;
import net.sowgro.npehero.gui.MainMenu;

import java.io.IOException;
import java.net.URL;
import java.util.Stack;


public class Driver extends Application
{
    public static final Image MENU_BACKGROUND = new Image(Driver.class.getResource("mountains.png").toExternalForm());

    public static Stage primaryStage;
    public static ScrollPane primaryPane = new ScrollPane();
    static ImageView backgroundImage = new ImageView();
    static ImageView backgroundImage2 = new ImageView();

    static Page currentPage = null;

    /*
     * 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) {

        primaryStage = newPrimaryStage;

        primaryPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

        StackPane root = new StackPane(backgroundImage2, backgroundImage, primaryPane);
        Scene primaryScene = new Scene(root, 800,600);

//        Cant figure out how to center this
        backgroundImage.fitHeightProperty().bind(primaryScene.heightProperty());
        backgroundImage2.fitHeightProperty().bind(primaryScene.heightProperty());
        backgroundImage.setPreserveRatio(true);
        backgroundImage2.setPreserveRatio(true);

        primaryScene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

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

        primaryPane.getStyleClass().remove("scroll-pane");

        setMenuBackground();

        Sound.playSong(Sound.MENU_SONG);

        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();

        Stack<String> errors = new Stack<>();
        System.out.println("Loading settings...");
        try {
            Settings.read();
            System.out.println("Settings loaded");
        } catch (Exception e) {
            errors.push("Failed to load settings from file\n"+e);
        }
        System.out.println("Loading controls...");
        try {
            Control.readFromFile();
            System.out.println("Controls loaded");
        } catch (Exception e) {
            errors.push("Failed to load controls from file\n"+e);
        }
        System.out.println("Loading levels...");
        try {
            Levels.readData();
            System.out.println("Loaded " + Levels.list.size() + " levels (" + Levels.getValidList().size() + " valid)");
        } catch (IOException e) {
            errors.push("Failed to load levels\n");
        }
        Page last = new MainMenu();
        while (!errors.empty()) {
            last = new ErrorDisplay(errors.pop(), last);
        }
        Driver.setMenu(last);
    }

    public static void setMenu(Page p) {
        if (currentPage != null) {
            currentPage.onLeave();
        }
        currentPage = p;
        Pane pane = currentPage.getContent();
        pane.setOpacity(0.0);
        primaryPane.setContent(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 navigation so no button is highlighted by default
        currentPage.onView();
        FadeTransition ft = new FadeTransition(Duration.millis(100), currentPage.getContent());
        ft.setInterpolator(Interpolator.EASE_BOTH);
        ft.setFromValue(0.0);
        ft.setToValue(1.0);
        ft.play();
    }

    /**
     * Replaces the background image with a new one.
     * @param image The image to set.
     */
    public static void setBackground(Image image) //replaces background with a new one
    {
        // TODO center on screen
        if (image == backgroundImage.getImage()) {
            return;
        }
        backgroundImage2.setImage(image);

        FadeTransition ft = new FadeTransition(Duration.seconds(0.2), backgroundImage);
        ft.setInterpolator(Interpolator.EASE_BOTH);
        ft.setFromValue(1.0);
        ft.setToValue(0.0);

        ScaleTransition st2 = new ScaleTransition(Duration.seconds(0.2), backgroundImage);
        st2.setInterpolator(Interpolator.LINEAR);
        st2.setFromX(1);
        st2.setFromY(1);
        st2.setToX(1.05);
        st2.setToY(1.05);

        ScaleTransition st = new ScaleTransition(Duration.seconds(0.2), backgroundImage2);
        st.setInterpolator(Interpolator.LINEAR);
        st.setFromX(1.05);
        st.setFromY(1.05);
        st.setToX(1.0);
        st.setToY(1.0);

        ParallelTransition pt = new ParallelTransition(ft, st, st2);
        pt.setDelay(Duration.seconds(0.3));
        pt.play();
        st.setOnFinished(_ -> backgroundImage.setImage(image));
    }

    public static void setMenuBackground() {
        setBackground(MENU_BACKGROUND);
    }

    public static URL getResource(String fileName) {
        return Driver.class.getResource(fileName);
    }
}