Skip to content

Commit

Permalink
Go Fullscreen if user press F
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoborges committed Sep 2, 2019
1 parent 08dd882 commit ec4c1a5
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions src/main/java/game2048/Game2048.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down

0 comments on commit ec4c1a5

Please sign in to comment.