Skip to content

Commit 832ccfb

Browse files
committed
Fix key handler
1 parent e334e80 commit 832ccfb

File tree

3 files changed

+54
-49
lines changed

3 files changed

+54
-49
lines changed

README.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Javascript version: https://github.com/gabrielecirulli/2048
55

66
## Releases
77

8-
[![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)
8+
[![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)
99

1010
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.
1111

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

1919
## Building and running
2020

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

2323
```bash
24-
gradlew build
25-
gradlew run
24+
./gradlew run
25+
```
26+
27+
### Create a distribution to your operating system (Windows, Linux, or Mac OS)
28+
29+
Run
30+
31+
```bash
32+
./gradle distro
2633
```
2734

2835
## Feedback / Contributing / Comments

src/main/java/game2048/Game2048.java

+8-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import javafx.application.Platform;
66
import javafx.scene.Cursor;
77
import javafx.scene.Scene;
8-
import javafx.scene.input.KeyCode;
98
import javafx.stage.Screen;
109
import javafx.stage.Stage;
1110

@@ -14,9 +13,9 @@
1413
*/
1514
public class Game2048 extends Application {
1615

17-
public static final String VERSION = "1.0.4";
16+
public static final String VERSION = "1.1.0";
1817

19-
private GamePane root;
18+
private GamePane gamePane;
2019

2120
private static Game2048 applicationInstance;
2221

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

4140
@Override
4241
public void start(Stage primaryStage) {
43-
root = new GamePane();
42+
gamePane = new GamePane();
4443

45-
var scene = new Scene(root);
44+
var scene = new Scene(gamePane);
4645
scene.getStylesheets().add(getClass().getResource("game.css").toExternalForm());
4746

4847
setGameBounds(primaryStage, scene);
4948
setEnhancedDeviceSettings(primaryStage, scene);
5049
setQuitListener(primaryStage);
5150

5251
primaryStage.show();
53-
root.requestFocus();
52+
gamePane.requestFocus();
5453
}
5554

5655
private void setQuitListener(Stage primaryStage) {
5756
primaryStage.setOnCloseRequest(t -> {
5857
t.consume();
59-
root.getGameManager().quitGame();
58+
gamePane.getGameManager().quitGame();
6059
});
6160
}
6261

@@ -65,12 +64,6 @@ private void setEnhancedDeviceSettings(Stage primaryStage, Scene scene) {
6564
if (isARM) {
6665
primaryStage.setFullScreen(true);
6766
primaryStage.setFullScreenExitHint("");
68-
} else {
69-
root.setOnKeyPressed(ke -> {
70-
if (ke.getCode().equals(KeyCode.F)) {
71-
primaryStage.setFullScreen(true);
72-
}
73-
});
7467
}
7568

7669
if (Platform.isSupported(ConditionalFeature.INPUT_TOUCH)) {
@@ -79,7 +72,7 @@ private void setEnhancedDeviceSettings(Stage primaryStage, Scene scene) {
7972
}
8073

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

9588
@Override
9689
public void stop() {
97-
root.getGameManager().saveRecord();
90+
gamePane.getGameManager().saveRecord();
9891
}
9992

10093
/**

src/main/java/game2048/GamePane.java

+35-30
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import javafx.geometry.Bounds;
99
import javafx.geometry.Insets;
1010
import javafx.geometry.Pos;
11-
import javafx.scene.Node;
11+
import javafx.stage.Stage;
1212
import javafx.scene.control.Button;
1313
import javafx.scene.control.Tooltip;
1414
import javafx.scene.input.KeyCode;
@@ -50,24 +50,16 @@ public GamePane() {
5050
widthProperty().addListener(resize);
5151
heightProperty().addListener(resize);
5252

53-
addKeyHandlers(this);
54-
addSwipeHandlers(this);
53+
addKeyHandlers();
54+
addSwipeHandlers();
5555
setFocusTraversable(true);
56-
this.setOnMouseClicked(e -> requestFocus());
56+
setOnMouseClicked(e -> requestFocus());
5757
}
5858

5959
private BooleanProperty cmdCtrlKeyPressed = new SimpleBooleanProperty(false);
6060

61-
private void addKeyHandlers(Node node) {
62-
node.setOnKeyReleased(ke -> {
63-
var keyCode = ke.getCode();
64-
65-
if (keyCode.equals(KeyCode.CONTROL)) {
66-
cmdCtrlKeyPressed.set(false);
67-
}
68-
});
69-
70-
node.setOnKeyPressed(ke -> {
61+
private void addKeyHandlers() {
62+
setOnKeyPressed(ke -> {
7163
var keyCode = ke.getCode();
7264

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

93-
if (cmdCtrlKeyPressed.get() == false && (keyCode.equals(KeyCode.Q) || keyCode.equals(KeyCode.ESCAPE))) {
85+
if (cmdCtrlKeyPressed.get() == false && keyCode.equals(KeyCode.Q)) {
9486
gameManager.quitGame();
9587
return;
9688
}
9789

90+
if (ke.getCode().equals(KeyCode.F)) {
91+
var stage = ((Stage) getScene().getWindow());
92+
stage.setFullScreen(!stage.isFullScreen());
93+
return;
94+
}
95+
9896
if (keyCode.isArrowKey()) {
9997
var direction = Direction.valueFor(keyCode);
10098
move(direction);
99+
return;
101100
}
102101
});
102+
103+
setOnKeyReleased(ke -> {
104+
var keyCode = ke.getCode();
105+
106+
if (keyCode.equals(KeyCode.CONTROL) || keyCode.equals(KeyCode.COMMAND)) {
107+
cmdCtrlKeyPressed.set(false);
108+
return;
109+
}
110+
});
111+
103112
}
104113

105-
private void addSwipeHandlers(Node node) {
106-
node.setOnSwipeUp(e -> move(Direction.UP));
107-
node.setOnSwipeRight(e -> move(Direction.RIGHT));
108-
node.setOnSwipeLeft(e -> move(Direction.LEFT));
109-
node.setOnSwipeDown(e -> move(Direction.DOWN));
114+
private void addSwipeHandlers() {
115+
setOnSwipeUp(e -> move(Direction.UP));
116+
setOnSwipeRight(e -> move(Direction.RIGHT));
117+
setOnSwipeLeft(e -> move(Direction.LEFT));
118+
setOnSwipeDown(e -> move(Direction.DOWN));
110119
}
111120

112121
private void move(Direction direction) {
113122
gameManager.move(direction);
114123
}
115124

116125
private HBox createToolBar() {
117-
var toolbar = new HBox();
118-
toolbar.setAlignment(Pos.CENTER);
119-
toolbar.setPadding(new Insets(10.0));
120126
var btItem1 = createButtonItem("mSave", "Save Session", t -> gameManager.saveSession());
121-
var btItem2 =
122-
createButtonItem("mRestore", "Restore Session", t -> gameManager.restoreSession());
127+
var btItem2 = createButtonItem("mRestore", "Restore Session", t -> gameManager.restoreSession());
123128
var btItem3 = createButtonItem("mPause", "Pause Game", t -> gameManager.pauseGame());
124129
var btItem4 = createButtonItem("mReplay", "Try Again", t -> gameManager.tryAgain());
125130
var btItem5 = createButtonItem("mInfo", "About the Game", t -> gameManager.aboutGame());
126-
toolbar.getChildren().setAll(btItem1, btItem2, btItem3, btItem4, btItem5);
127-
var btItem6 = createButtonItem("mQuit", "Quit Game", t -> {
128-
gameManager.quitGame();
129-
});
130-
toolbar.getChildren().add(btItem6);
131+
var btItem6 = createButtonItem("mQuit", "Quit Game", t -> gameManager.quitGame());
132+
133+
var toolbar = new HBox(btItem1, btItem2, btItem3, btItem4, btItem5, btItem6);
134+
toolbar.setAlignment(Pos.CENTER);
135+
toolbar.setPadding(new Insets(10.0));
131136
return toolbar;
132137
}
133138

0 commit comments

Comments
 (0)