From 13d50a64b9d25036240caa711b01414267d206cc Mon Sep 17 00:00:00 2001 From: Charles Gould Date: Sat, 18 Apr 2015 20:39:42 -0400 Subject: [PATCH] Add menu bar --- .../freecellfx/FreeCellApplication.java | 12 +++-- .../charego/freecellfx/view/GameCanvas.java | 45 ++++++++--------- .../charego/freecellfx/view/GameMenuBar.java | 49 +++++++++++++++++++ 3 files changed, 81 insertions(+), 25 deletions(-) create mode 100644 src/main/java/com/charego/freecellfx/view/GameMenuBar.java diff --git a/src/main/java/com/charego/freecellfx/FreeCellApplication.java b/src/main/java/com/charego/freecellfx/FreeCellApplication.java index 06bbd3f..70eb86c 100644 --- a/src/main/java/com/charego/freecellfx/FreeCellApplication.java +++ b/src/main/java/com/charego/freecellfx/FreeCellApplication.java @@ -2,6 +2,7 @@ package com.charego.freecellfx; import com.charego.freecellfx.model.Game; import com.charego.freecellfx.view.GameCanvas; +import com.charego.freecellfx.view.GameMenuBar; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.image.Image; @@ -16,14 +17,19 @@ public class FreeCellApplication extends Application { @Override public void start(Stage stage) throws Exception { - AnchorPane root = new AnchorPane(); + VBox root = new VBox(); root.setBackground(new Background(new BackgroundImage(new Image( "/deck/FELT.jpg"), BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, BackgroundSize.DEFAULT))); - GameCanvas canvas = new GameCanvas(new Game(), 731, 600); - root.getChildren().add(canvas); + Game game = new Game(); + GameMenuBar menuBar = new GameMenuBar(); + GameCanvas canvas = new GameCanvas(game, 731, 600); + menuBar.setNewGameAction(canvas.getNewGameAction()); + menuBar.setUndoAction(canvas.getUndoAction()); + menuBar.setRedoAction(canvas.getRedoAction()); + root.getChildren().addAll(menuBar, canvas); Scene scene = new Scene(root); stage.setTitle("FreeCell"); diff --git a/src/main/java/com/charego/freecellfx/view/GameCanvas.java b/src/main/java/com/charego/freecellfx/view/GameCanvas.java index a060937..18bc150 100644 --- a/src/main/java/com/charego/freecellfx/view/GameCanvas.java +++ b/src/main/java/com/charego/freecellfx/view/GameCanvas.java @@ -6,12 +6,11 @@ import com.charego.freecellfx.model.pile.Pile; import com.charego.freecellfx.view.pile.CascadingPileView; import com.charego.freecellfx.view.pile.PileView; import com.charego.freecellfx.view.pile.StackedPileView; +import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.control.Alert; -import javafx.scene.input.KeyCode; -import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseEvent; import java.util.ArrayList; @@ -31,7 +30,6 @@ public class GameCanvas extends Canvas { this.game = game; this.gc = getGraphicsContext2D(); this.pileViews = constructPileViews(game); - setOnKeyReleased(new KeyReleaseHandler()); setOnMouseClicked(new MouseClickHandler()); updateView(); } @@ -101,27 +99,30 @@ public class GameCanvas extends Canvas { } } - private class KeyReleaseHandler implements EventHandler { - @Override - public void handle(KeyEvent e) { - if (e.isControlDown()) { - if (e.getCode() == KeyCode.Y) { - game.redo(); - } else if (e.getCode() == KeyCode.Z) { - game.undo(); - } - } else { - if (e.getCode() == KeyCode.F2) { - game.newGame(); - winMessageShown = false; - if (fromPile != null && fromPile.isSelected()) { - fromPile.toggleSelected(); - } - fromPile = null; - } + public EventHandler getNewGameAction() { + return e -> { + game.newGame(); + winMessageShown = false; + if (fromPile != null && fromPile.isSelected()) { + fromPile.toggleSelected(); } + fromPile = null; updateView(); - } + }; + } + + public EventHandler getUndoAction() { + return e -> { + game.undo(); + updateView(); + }; + } + + public EventHandler getRedoAction() { + return e -> { + game.redo(); + updateView(); + }; } private class MouseClickHandler implements EventHandler { diff --git a/src/main/java/com/charego/freecellfx/view/GameMenuBar.java b/src/main/java/com/charego/freecellfx/view/GameMenuBar.java new file mode 100644 index 0000000..e45cb0c --- /dev/null +++ b/src/main/java/com/charego/freecellfx/view/GameMenuBar.java @@ -0,0 +1,49 @@ +package com.charego.freecellfx.view; + +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.scene.control.Menu; +import javafx.scene.control.MenuBar; +import javafx.scene.control.MenuItem; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyCodeCombination; +import javafx.scene.input.KeyCombination; + +public class GameMenuBar extends MenuBar { + + private final MenuItem newGame; + private final MenuItem undoMove; + private final MenuItem redoMove; + private final MenuItem exitGame; + + public GameMenuBar() { + super(); + newGame = new MenuItem("New"); + undoMove = new MenuItem("Undo"); + redoMove = new MenuItem("Redo"); + exitGame = new MenuItem("Exit"); + setAccelerators(); + Menu gameMenu = new Menu("Game"); + gameMenu.getItems().addAll(newGame, undoMove, redoMove, exitGame); + super.getMenus().add(gameMenu); + } + + private void setAccelerators() { + newGame.setAccelerator(new KeyCodeCombination(KeyCode.F2)); + undoMove.setAccelerator(new KeyCodeCombination(KeyCode.Z, KeyCombination.CONTROL_DOWN)); + redoMove.setAccelerator(new KeyCodeCombination(KeyCode.Y, KeyCombination.CONTROL_DOWN)); + } + + public void setNewGameAction(EventHandler handler) { + newGame.setOnAction(handler); + } + + public void setUndoAction(EventHandler handler) { + undoMove.setOnAction(handler); + } + + public void setRedoAction(EventHandler handler) { + redoMove.setOnAction(handler); + } + +}