Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

There is a warning when jumping between MainMenu and GameMenu, and it is not possible to jump from the GameMenu menu to the MainMenu menu. #1373

Open
huayunliufeng opened this issue May 22, 2024 · 3 comments

Comments

@huayunliufeng
Copy link

My partial code is as follows:
`
@OverRide
protected void initSettings(GameSettings settings) {
settings.setMainMenuEnabled(true);
settings.setGameMenuEnabled(true);
settings.setSceneFactory(new SceneFactory() {
@NotNull
@OverRide
public FXGLMenu newGameMenu() {
return new CustomGameMenu();
}
@NotNull
@OverRide
public FXGLMenu newMainMenu() {
return new CustomMainMenu();
}

    });
}

`

CustomMainMenu:

`
public CustomMainMenu() {
super(MenuType.MAIN_MENU);
}

@Override
public void onCreate() {
    getContentRoot().getChildren().setAll(getMainMenu());
}

private VBox getMainMenu() {
    if (mainMenu == null) {
        Button gameListBtn = new Button("game list");
        Button exitBtn = new Button("exit game");

        mainMenu = getMenuContainer(gameListBtn, exitBtn);
        gameListBtn.setOnAction(MainMenuController.gameListHandler(contentRoot, mainMenu, getGameList()));
        exitBtn.setOnAction(MainMenuController.exitHandler());
    }
    return mainMenu;
}

private VBox getGameList() {
    if (gameList == null) {
        Button game1Btn = new Button("game 1");

        Button game2Btn = new Button("game 2");

        Button game3Btn = new Button("game 3");

        Button goBackBtn = new Button("go back");

        gameList = getMenuContainer(game1Btn, game2Btn, game3Btn, goBackBtn);
        gameList.getStyleClass().addAll("game-list-container");

        game1Btn.setOnAction(MainMenuController.game1Handler(contentRoot, gameList));

        goBackBtn.setOnAction(MainMenuController.goBackHandler(contentRoot, getMainMenu(), gameList));
    }
    return gameList;
}

private VBox getMenuContainer(Node... children) {
    VBox vBox = new VBox(children);
    vBox.setLayoutX(MAIN_MENU_LAYOUT_X);
    vBox.setLayoutY(MAIN_MENU_LAYOUT_Y);
    return vBox;
}

`

CustomGameMenu:

`
public CustomGameMenu() {
super(MenuType.GAME_MENU);
}

@Override
public void onCreate() {
    getContentRoot().getChildren().setAll(getGameMenu());
}

private VBox getGameMenu() {
    if (gameMenu == null) {
        Button newGameBtn = new Button("new game");

        Button continueGameBtn = new Button("continue");

        Button goBackBtn = new Button("go back");

        gameMenu = getMenuContainer(newGameBtn, continueGameBtn, goBackBtn);

        newGameBtn.setOnAction(GameMenuController.newGameHandler());
        continueGameBtn.setOnAction(GameMenuController.continueGameHandler());
        goBackBtn.setOnAction(GameMenuController.goBackHandler());
    }
    return gameMenu;
}

private VBox getMenuContainer(Node... children) {
    VBox vBox = new VBox(children);
    vBox.setLayoutX(GAME_MENU_LAYOUT_X);
    vBox.setLayoutY(GAME_MENU_LAYOUT_Y);
    return vBox;
}

GameMenuController:
private static EventHandler newGameHandler;
private static EventHandler goBackHandler;

public static EventHandler<ActionEvent> newGameHandler() {
    if (newGameHandler == null) {
        newGameHandler = _ -> FXGL.getGameController().startNewGame();
    }
    return newGameHandler;
}

// TODO: Returning to the main menu from this method will result in a warning and will not be successful.
public static EventHandler goBackHandler() {
if (goBackHandler == null) {
goBackHandler = new EventHandler() {
@OverRide
public void handle(ActionEvent event) {
FXGL.getGameController().gotoMainMenu();
}
};
}
return goBackHandler;
}
`

MainMenuController:

`
private static EventHandler gameListHandler;
private static EventHandler exitHandler;

private static EventHandler<ActionEvent> game1Handler;

private static EventHandler<ActionEvent> goBackHandler;

public static EventHandler<ActionEvent> goBackHandler(Pane contentRoot, VBox mainMenu, VBox gameList) {
    if (goBackHandler == null) {
        goBackHandler = _ -> contentRoot.getChildren().setAll(mainMenu);
    }
    return goBackHandler;
}

public static EventHandler<ActionEvent> game1Handler(Pane contentRoot, VBox gameList) {
    if (game1Handler == null) {
        game1Handler = _ -> {
            FXGL.getGameController().gotoGameMenu();
        };
    }
    return game1Handler;
}

public static EventHandler<ActionEvent> gameListHandler(Pane contentRoot, VBox mainMenu, VBox gameList) {
    if (gameListHandler == null) {
        gameListHandler = event -> {
            contentRoot.getChildren().setAll(gameList);
        };
    }
    return gameListHandler;
}

public static EventHandler<ActionEvent> exitHandler() {
    if (exitHandler == null) {
        exitHandler = event -> FXGL.getDialogService().showConfirmationBox(
                "?", b -> {
                    if (b) {
                        FXGL.getGameController().exit();
                    }
                }
        );
    }
    return exitHandler;
}

`

@AlmasB
Copy link
Owner

AlmasB commented May 24, 2024

Thanks, this behaviour is by design, however, it was the old design and it was never reconsidered. Leave this with me, I'll have a think about the use cases and see if I can update the design and the implementation.

In the meantime, you may wish to add a switch to game scene before you move between menus.

@huayunliufeng
Copy link
Author

Thank you, I have resolved the issue through other means.

@AlmasB
Copy link
Owner

AlmasB commented May 27, 2024

Great that you resolved it, I'm just going to reopen this, so I don't forget to revisit the menu transitions design

@AlmasB AlmasB reopened this May 27, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants