From ec4c1a5601cda7e4e27f1241c0304e7ccbc75744 Mon Sep 17 00:00:00 2001 From: Bruno Borges Date: Sun, 1 Sep 2019 18:19:35 -0700 Subject: [PATCH] Go Fullscreen if user press F --- src/main/java/game2048/Game2048.java | 46 ++++++++++++++++++---------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/main/java/game2048/Game2048.java b/src/main/java/game2048/Game2048.java index 2de1175..6c5e083 100644 --- a/src/main/java/game2048/Game2048.java +++ b/src/main/java/game2048/Game2048.java @@ -5,6 +5,7 @@ import javafx.application.Platform; import javafx.scene.Cursor; import javafx.scene.Scene; +import javafx.scene.input.KeyCode; import javafx.stage.Screen; import javafx.stage.Stage; @@ -42,17 +43,42 @@ public void start(Stage primaryStage) { root = new GamePane(); var scene = new Scene(root); - scene.getStylesheets().add(Game2048.class.getResource("game.css").toExternalForm()); + scene.getStylesheets().add(getClass().getResource("game.css").toExternalForm()); - if (isARMDevice()) { + setGameBounds(primaryStage, scene); + setEnhancedDeviceSettings(primaryStage, scene); + setQuitListener(primaryStage); + + primaryStage.show(); + root.requestFocus(); + } + + private void setQuitListener(Stage primaryStage) { + primaryStage.setOnCloseRequest(t -> { + t.consume(); + root.getGameManager().quitGame(); + }); + } + + private void setEnhancedDeviceSettings(Stage primaryStage, Scene scene) { + var isARM = System.getProperty("os.arch").toUpperCase().contains("ARM"); + if (isARM) { primaryStage.setFullScreen(true); primaryStage.setFullScreenExitHint(""); + } else { + root.setOnKeyPressed(ke -> { + if (ke.getCode().equals(KeyCode.F)) { + primaryStage.setFullScreen(true); + } + }); } if (Platform.isSupported(ConditionalFeature.INPUT_TOUCH)) { scene.setCursor(Cursor.NONE); } + } + private void setGameBounds(Stage primaryStage, Scene scene) { var gameBounds = root.getGameManager().getLayoutBounds(); int MARGIN = GamePane.getMargin(); var visualBounds = Screen.getPrimary().getVisualBounds(); @@ -62,20 +88,8 @@ public void start(Stage primaryStage) { primaryStage.setScene(scene); primaryStage.setMinWidth(gameBounds.getWidth() / 2d); primaryStage.setMinHeight(gameBounds.getHeight() / 2d); - primaryStage.setWidth((gameBounds.getWidth() + MARGIN) * factor); - primaryStage.setHeight((gameBounds.getHeight() + MARGIN) * factor); - - primaryStage.setOnCloseRequest(t -> { - t.consume(); - root.getGameManager().quitGame(); - }); - primaryStage.show(); - - root.requestFocus(); - } - - private boolean isARMDevice() { - return System.getProperty("os.arch").toUpperCase().contains("ARM"); + primaryStage.setWidth(((gameBounds.getWidth() + MARGIN) * factor) / 1.5d); + primaryStage.setHeight(((gameBounds.getHeight() + MARGIN) * factor) / 1.5d); } @Override