<commit_before>/*
 * The MIT License (MIT)
 *
 * FXGL - JavaFX Game Library
 *
 * Copyright (c) 2015 AlmasB (<EMAIL>)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.almasb.fxgl;

import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Logger;

import javax.imageio.ImageIO;

import com.almasb.fxgl.asset.AssetManager;
import com.almasb.fxgl.asset.SaveLoadManager;
import com.almasb.fxgl.event.MenuEvent;
import com.almasb.fxgl.settings.SceneSettings;
import com.almasb.fxgl.settings.UserProfile;
import com.almasb.fxgl.settings.UserProfileSavable;
import com.almasb.fxgl.ui.FXGLDialogBox;
import com.almasb.fxgl.ui.FXGLScene;
import com.almasb.fxgl.ui.IntroScene;
import com.almasb.fxgl.ui.MenuFactory;
import com.almasb.fxgl.ui.UIFactory;
import com.almasb.fxgl.util.FXGLLogger;

import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyBooleanWrapper;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.Event;
import javafx.event.EventType;
import javafx.geometry.Dimension2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Dialog;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
 * Manages interactions between FXGL scenes and allows
 * creating scene-independent dialog boxes.
 *
 * @author <NAME> (AlmasB) (<EMAIL>)
 */
public final class SceneManager implements UserProfileSavable {

    private static final Logger log = FXGLLogger.getLogger("FXGL.SceneManager");

    /**
     * Game scene, this is where all in-game objects are shown.
     */
    private GameScene gameScene;

    /**
     * Intro scene, this is shown when the application started,
     * before menus and game.
     */
    private IntroScene introScene;

    /**
     * Main menu, this is the menu shown at the start of game
     */
    private FXGLScene mainMenuScene;

    /**
     * In-game menu, this is shown when menu key pressed during the game
     */
    private FXGLScene gameMenuScene;

    /**
     * Reference to current shown scene.
     */
    private FXGLScene currentScene;

    /**
     * Is menu enabled in settings
     */
    private boolean isMenuEnabled;

    /**
     * The key that triggers opening/closing game menu
     */
    private KeyCode menuKey = KeyCode.ESCAPE;

    /**
     * The dialog box used to communicate with the user.
     */
    private FXGLDialogBox dialogBox;

    /**
     * Game application instance.
     */
    private GameApplication app;

    /**
     * Underlying JavaFX scene. We only use 1 scene to avoid
     * problems in fullscreen mode. Switching between scenes
     * in FS mode will otherwise temporarily toggle FS.
     */
    private Scene scene;

    /**
     * Settings for all scenes to apply.
     */
    private SceneSettings sceneSettings;

    /**
     * Constructs scene manager.
     *
     * @param app   instance of game application
     * @param scene main scene
     */
    SceneManager(GameApplication app, Scene scene) {
        this.app = app;
        this.scene = scene;

        /*
         * Since FXGL scenes are not JavaFX nodes they don't get notified of events.
         * This is a desired behavior because we only have 1 JavaFX scene for all FXGL scenes.
         * So we copy the occurred event and reroute to whichever FXGL scene is current.
         */
        scene.addEventFilter(EventType.ROOT, event -> {
            Event copy = event.copyFor(null, null);
            currentScene.fireEvent(copy);
        });

        isMenuEnabled = app.getSettings().isMenuEnabled();
        menuOpen = new ReadOnlyBooleanWrapper(isMenuEnabled);

        sceneSettings = computeSceneSettings(app.getWidth(), app.getHeight());
        gameScene = new GameScene(sceneSettings);
        initDialogBox();
    }

    private void initDialogBox() {
        dialogBox = UIFactory.getDialogBox();
        dialogBox.setOnShown(e -> {
            if (!isMenuOpen())
                app.pause();

            app.getInputManager().clearAllInput();
        });
        dialogBox.setOnHidden(e -> {
            if (!isMenuOpen())
                app.resume();
        });
    }

    private List<SceneSettings.SceneDimension> sceneDimensions = new ArrayList<>();

    public List<SceneSettings.SceneDimension> getSceneDimensions() {
        return new ArrayList<>(sceneDimensions);
    }<commit_msg>missing doc<commit_after>/*
 * The MIT License (MIT)
 *
 * FXGL - JavaFX Game Library
 *
 * Copyright (c) 2015 AlmasB (<EMAIL>)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.almasb.fxgl;

import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Logger;

import javax.imageio.ImageIO;

import com.almasb.fxgl.asset.AssetManager;
import com.almasb.fxgl.asset.SaveLoadManager;
import com.almasb.fxgl.event.MenuEvent;
import com.almasb.fxgl.settings.SceneSettings;
import com.almasb.fxgl.settings.UserProfile;
import com.almasb.fxgl.settings.UserProfileSavable;
import com.almasb.fxgl.ui.FXGLDialogBox;
import com.almasb.fxgl.ui.FXGLScene;
import com.almasb.fxgl.ui.IntroScene;
import com.almasb.fxgl.ui.MenuFactory;
import com.almasb.fxgl.ui.UIFactory;
import com.almasb.fxgl.util.FXGLLogger;

import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyBooleanWrapper;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.Event;
import javafx.event.EventType;
import javafx.geometry.Dimension2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Dialog;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
 * Manages interactions between FXGL scenes and allows
 * creating scene-independent dialog boxes.
 *
 * @author <NAME> (AlmasB) (<EMAIL>)
 */
public final class SceneManager implements UserProfileSavable {

    private static final Logger log = FXGLLogger.getLogger("FXGL.SceneManager");

    /**
     * Game scene, this is where all in-game objects are shown.
     */
    private GameScene gameScene;

    /**
     * Intro scene, this is shown when the application started,
     * before menus and game.
     */
    private IntroScene introScene;

    /**
     * Main menu, this is the menu shown at the start of game
     */
    private FXGLScene mainMenuScene;

    /**
     * In-game menu, this is shown when menu key pressed during the game
     */
    private FXGLScene gameMenuScene;

    /**
     * Reference to current shown scene.
     */
    private FXGLScene currentScene;

    /**
     * Is menu enabled in settings
     */
    private boolean isMenuEnabled;

    /**
     * The key that triggers opening/closing game menu
     */
    private KeyCode menuKey = KeyCode.ESCAPE;

    /**
     * The dialog box used to communicate with the user.
     */
    private FXGLDialogBox dialogBox;

    /**
     * Game application instance.
     */
    private GameApplication app;

    /**
     * Underlying JavaFX scene. We only use 1 scene to avoid
     * problems in fullscreen mode. Switching between scenes
     * in FS mode will otherwise temporarily toggle FS.
     */
    private Scene scene;

    /**
     * Settings for all scenes to apply.
     */
    private SceneSettings sceneSettings;

    /**
     * Constructs scene manager.
     *
     * @param app   instance of game application
     * @param scene main scene
     */
    SceneManager(GameApplication app, Scene scene) {
        this.app = app;
        this.scene = scene;

        /*
         * Since FXGL scenes are not JavaFX nodes they don't get notified of events.
         * This is a desired behavior because we only have 1 JavaFX scene for all FXGL scenes.
         * So we copy the occurred event and reroute to whichever FXGL scene is current.
         */
        scene.addEventFilter(EventType.ROOT, event -> {
            Event copy = event.copyFor(null, null);
            currentScene.fireEvent(copy);
        });

        isMenuEnabled = app.getSettings().isMenuEnabled();
        menuOpen = new ReadOnlyBooleanWrapper(isMenuEnabled);

        sceneSettings = computeSceneSettings(app.getWidth(), app.getHeight());
        gameScene = new GameScene(sceneSettings);
        initDialogBox();
    }

    private void initDialogBox() {
        dialogBox = UIFactory.getDialogBox();
        dialogBox.setOnShown(e -> {
            if (!isMenuOpen())
                app.pause();

            app.getInputManager().clearAllInput();
        });
        dialogBox.setOnHidden(e -> {
            if (!isMenuOpen())
                app.resume();
        });
    }

    private List<SceneSettings.SceneDimension> sceneDimensions = new ArrayList<>();

    /**
     *
     * @return a list of supported scene dimensions with 360, 480, 720 and 1080 heights
     */
    public List<SceneSettings.SceneDimension> getSceneDimensions() {
        return new ArrayList<>(sceneDimensions);
    }
{'fraction_non_alphanumeric': 0.08841385207353569, 'fraction_numerical': 0.0024796921761436512, 'mean_word_length': 3.0681739130434784, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2972364', 'n_tokens_mistral': 3361, 'n_tokens_neox': 3219, 'n_words': 1079}