Skip to content

Commit

Permalink
Fix key handler
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoborges committed Sep 3, 2019
1 parent e334e80 commit 832ccfb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 49 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Javascript version: https://github.com/gabrielecirulli/2048

## Releases

[![Build Status](https://dev.azure.com/brunoborges-github/fx2048/_apis/build/status/brunoborges.fx2048?branchName=master)](https://dev.azure.com/brunoborges-github/fx2048/_build/latest?definitionId=1&branchName=master)
[![Build Status](https://dev.azure.com/brunocborges/fx2048/_apis/build/status/brunoborges.fx2048?branchName=master)](https://dev.azure.com/brunocborges/fx2048/_build/latest?definitionId=1&branchName=master)

You may find binaries available for download, for Windows, Mac and Linux, with Java bundled in (using Java 11 jlink custom images). The ZIP files come with a binary that will start the game with the bundled optimized/trimmed JVM with only the needed modules, making the binaries extremely small comparably with the normal JRE download size.

Expand All @@ -18,11 +18,18 @@ Check below the list of releases, and download the corresponding binary to your

## Building and running

You will need [OpenJDK 11](http://jdk.java.net/11/) (or newer) and [Gradle](https://gradle.org/) installed to build and run the project:
You will need [OpenJDK 11](http://jdk.java.net/11/) (or newer) installed to build and run the project:

```bash
gradlew build
gradlew run
./gradlew run
```

### Create a distribution to your operating system (Windows, Linux, or Mac OS)

Run

```bash
./gradle distro
```

## Feedback / Contributing / Comments
Expand Down
23 changes: 8 additions & 15 deletions src/main/java/game2048/Game2048.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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 All @@ -14,9 +13,9 @@
*/
public class Game2048 extends Application {

public static final String VERSION = "1.0.4";
public static final String VERSION = "1.1.0";

private GamePane root;
private GamePane gamePane;

private static Game2048 applicationInstance;

Expand All @@ -40,23 +39,23 @@ public synchronized static Game2048 getInstance() {

@Override
public void start(Stage primaryStage) {
root = new GamePane();
gamePane = new GamePane();

var scene = new Scene(root);
var scene = new Scene(gamePane);
scene.getStylesheets().add(getClass().getResource("game.css").toExternalForm());

setGameBounds(primaryStage, scene);
setEnhancedDeviceSettings(primaryStage, scene);
setQuitListener(primaryStage);

primaryStage.show();
root.requestFocus();
gamePane.requestFocus();
}

private void setQuitListener(Stage primaryStage) {
primaryStage.setOnCloseRequest(t -> {
t.consume();
root.getGameManager().quitGame();
gamePane.getGameManager().quitGame();
});
}

Expand All @@ -65,12 +64,6 @@ private void setEnhancedDeviceSettings(Stage primaryStage, Scene scene) {
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)) {
Expand All @@ -79,7 +72,7 @@ private void setEnhancedDeviceSettings(Stage primaryStage, Scene scene) {
}

private void setGameBounds(Stage primaryStage, Scene scene) {
var gameBounds = root.getGameManager().getLayoutBounds();
var gameBounds = gamePane.getGameManager().getLayoutBounds();
int MARGIN = GamePane.getMargin();
var visualBounds = Screen.getPrimary().getVisualBounds();
double factor = Math.min(visualBounds.getWidth() / (gameBounds.getWidth() + MARGIN),
Expand All @@ -94,7 +87,7 @@ private void setGameBounds(Stage primaryStage, Scene scene) {

@Override
public void stop() {
root.getGameManager().saveRecord();
gamePane.getGameManager().saveRecord();
}

/**
Expand Down
65 changes: 35 additions & 30 deletions src/main/java/game2048/GamePane.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.input.KeyCode;
Expand Down Expand Up @@ -50,24 +50,16 @@ public GamePane() {
widthProperty().addListener(resize);
heightProperty().addListener(resize);

addKeyHandlers(this);
addSwipeHandlers(this);
addKeyHandlers();
addSwipeHandlers();
setFocusTraversable(true);
this.setOnMouseClicked(e -> requestFocus());
setOnMouseClicked(e -> requestFocus());
}

private BooleanProperty cmdCtrlKeyPressed = new SimpleBooleanProperty(false);

private void addKeyHandlers(Node node) {
node.setOnKeyReleased(ke -> {
var keyCode = ke.getCode();

if (keyCode.equals(KeyCode.CONTROL)) {
cmdCtrlKeyPressed.set(false);
}
});

node.setOnKeyPressed(ke -> {
private void addKeyHandlers() {
setOnKeyPressed(ke -> {
var keyCode = ke.getCode();

if (keyCode.equals(KeyCode.CONTROL) || keyCode.equals(KeyCode.COMMAND)) {
Expand All @@ -90,44 +82,57 @@ private void addKeyHandlers(Node node) {
return;
}

if (cmdCtrlKeyPressed.get() == false && (keyCode.equals(KeyCode.Q) || keyCode.equals(KeyCode.ESCAPE))) {
if (cmdCtrlKeyPressed.get() == false && keyCode.equals(KeyCode.Q)) {
gameManager.quitGame();
return;
}

if (ke.getCode().equals(KeyCode.F)) {
var stage = ((Stage) getScene().getWindow());
stage.setFullScreen(!stage.isFullScreen());
return;
}

if (keyCode.isArrowKey()) {
var direction = Direction.valueFor(keyCode);
move(direction);
return;
}
});

setOnKeyReleased(ke -> {
var keyCode = ke.getCode();

if (keyCode.equals(KeyCode.CONTROL) || keyCode.equals(KeyCode.COMMAND)) {
cmdCtrlKeyPressed.set(false);
return;
}
});

}

private void addSwipeHandlers(Node node) {
node.setOnSwipeUp(e -> move(Direction.UP));
node.setOnSwipeRight(e -> move(Direction.RIGHT));
node.setOnSwipeLeft(e -> move(Direction.LEFT));
node.setOnSwipeDown(e -> move(Direction.DOWN));
private void addSwipeHandlers() {
setOnSwipeUp(e -> move(Direction.UP));
setOnSwipeRight(e -> move(Direction.RIGHT));
setOnSwipeLeft(e -> move(Direction.LEFT));
setOnSwipeDown(e -> move(Direction.DOWN));
}

private void move(Direction direction) {
gameManager.move(direction);
}

private HBox createToolBar() {
var toolbar = new HBox();
toolbar.setAlignment(Pos.CENTER);
toolbar.setPadding(new Insets(10.0));
var btItem1 = createButtonItem("mSave", "Save Session", t -> gameManager.saveSession());
var btItem2 =
createButtonItem("mRestore", "Restore Session", t -> gameManager.restoreSession());
var btItem2 = createButtonItem("mRestore", "Restore Session", t -> gameManager.restoreSession());
var btItem3 = createButtonItem("mPause", "Pause Game", t -> gameManager.pauseGame());
var btItem4 = createButtonItem("mReplay", "Try Again", t -> gameManager.tryAgain());
var btItem5 = createButtonItem("mInfo", "About the Game", t -> gameManager.aboutGame());
toolbar.getChildren().setAll(btItem1, btItem2, btItem3, btItem4, btItem5);
var btItem6 = createButtonItem("mQuit", "Quit Game", t -> {
gameManager.quitGame();
});
toolbar.getChildren().add(btItem6);
var btItem6 = createButtonItem("mQuit", "Quit Game", t -> gameManager.quitGame());

var toolbar = new HBox(btItem1, btItem2, btItem3, btItem4, btItem5, btItem6);
toolbar.setAlignment(Pos.CENTER);
toolbar.setPadding(new Insets(10.0));
return toolbar;
}

Expand Down

0 comments on commit 832ccfb

Please sign in to comment.