Skip to content

Commit

Permalink
Quit the game if user triggers any of the Quit shortcuts after the Qu…
Browse files Browse the repository at this point in the history
…it overlay is being displayed.

Prevent Cmd+Q to trigger double actions.
  • Loading branch information
brunoborges committed Sep 2, 2019
1 parent a6ffa58 commit 08dd882
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
12 changes: 7 additions & 5 deletions src/main/java/game2048/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private void keepGoing() {
timer.play();
}

private void quit() {
private void exitGame() {
timerPause.stop();
Platform.exit();
}
Expand Down Expand Up @@ -383,11 +383,11 @@ private void initGameProperties() {
});

bQuit.getStyleClass().add("game-button");
bQuit.setOnTouchPressed(e -> quit());
bQuit.setOnMouseClicked(e -> quit());
bQuit.setOnTouchPressed(e -> exitGame());
bQuit.setOnMouseClicked(e -> exitGame());
bQuit.setOnKeyPressed(e -> {
if (e.getCode().equals(KeyCode.ENTER) || e.getCode().equals(KeyCode.SPACE)) {
quit();
exitGame();
}
});

Expand Down Expand Up @@ -623,7 +623,9 @@ public void aboutGame() {
}

public void quitGame() {
if (!gameQuitProperty.get()) {
if (gameQuitProperty.get()) {
exitGame();
} else {
gameQuitProperty.set(true);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/game2048/Game2048.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Game2048() {
applicationInstance = this;
}

public synchronized static Application getInstance() {
public synchronized static Game2048 getInstance() {
if (applicationInstance == null) {
while (applicationInstance == null) {
try {
Expand All @@ -33,6 +33,7 @@ public synchronized static Application getInstance() {
}
}
}

return applicationInstance;
}

Expand Down
32 changes: 28 additions & 4 deletions src/main/java/game2048/GamePane.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package game2048;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
Expand Down Expand Up @@ -48,31 +50,51 @@ public GamePane() {
widthProperty().addListener(resize);
heightProperty().addListener(resize);

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

private void addKeyHandler(Node node) {
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 -> {
var keyCode = ke.getCode();

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

if (keyCode.equals(KeyCode.S)) {
gameManager.saveSession();
return;
}

if (keyCode.equals(KeyCode.R)) {
gameManager.restoreSession();
return;
}

if (keyCode.equals(KeyCode.P)) {
gameManager.pauseGame();
return;
}
if (keyCode.equals(KeyCode.Q) || keyCode.equals(KeyCode.ESCAPE)) {

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

if (keyCode.isArrowKey()) {
var direction = Direction.valueFor(keyCode);
move(direction);
Expand Down Expand Up @@ -102,7 +124,9 @@ private HBox createToolBar() {
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());
var btItem6 = createButtonItem("mQuit", "Quit Game", t -> {
gameManager.quitGame();
});
toolbar.getChildren().add(btItem6);
return toolbar;
}
Expand Down

0 comments on commit 08dd882

Please sign in to comment.