Add menu bar
This commit is contained in:
parent
b2b7369b93
commit
13d50a64b9
@ -2,6 +2,7 @@ package com.charego.freecellfx;
|
|||||||
|
|
||||||
import com.charego.freecellfx.model.Game;
|
import com.charego.freecellfx.model.Game;
|
||||||
import com.charego.freecellfx.view.GameCanvas;
|
import com.charego.freecellfx.view.GameCanvas;
|
||||||
|
import com.charego.freecellfx.view.GameMenuBar;
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
import javafx.scene.image.Image;
|
import javafx.scene.image.Image;
|
||||||
@ -16,14 +17,19 @@ public class FreeCellApplication extends Application {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage stage) throws Exception {
|
public void start(Stage stage) throws Exception {
|
||||||
AnchorPane root = new AnchorPane();
|
VBox root = new VBox();
|
||||||
root.setBackground(new Background(new BackgroundImage(new Image(
|
root.setBackground(new Background(new BackgroundImage(new Image(
|
||||||
"/deck/FELT.jpg"), BackgroundRepeat.NO_REPEAT,
|
"/deck/FELT.jpg"), BackgroundRepeat.NO_REPEAT,
|
||||||
BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,
|
BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,
|
||||||
BackgroundSize.DEFAULT)));
|
BackgroundSize.DEFAULT)));
|
||||||
|
|
||||||
GameCanvas canvas = new GameCanvas(new Game(), 731, 600);
|
Game game = new Game();
|
||||||
root.getChildren().add(canvas);
|
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);
|
Scene scene = new Scene(root);
|
||||||
|
|
||||||
stage.setTitle("FreeCell");
|
stage.setTitle("FreeCell");
|
||||||
|
@ -6,12 +6,11 @@ import com.charego.freecellfx.model.pile.Pile;
|
|||||||
import com.charego.freecellfx.view.pile.CascadingPileView;
|
import com.charego.freecellfx.view.pile.CascadingPileView;
|
||||||
import com.charego.freecellfx.view.pile.PileView;
|
import com.charego.freecellfx.view.pile.PileView;
|
||||||
import com.charego.freecellfx.view.pile.StackedPileView;
|
import com.charego.freecellfx.view.pile.StackedPileView;
|
||||||
|
import javafx.event.ActionEvent;
|
||||||
import javafx.event.EventHandler;
|
import javafx.event.EventHandler;
|
||||||
import javafx.scene.canvas.Canvas;
|
import javafx.scene.canvas.Canvas;
|
||||||
import javafx.scene.canvas.GraphicsContext;
|
import javafx.scene.canvas.GraphicsContext;
|
||||||
import javafx.scene.control.Alert;
|
import javafx.scene.control.Alert;
|
||||||
import javafx.scene.input.KeyCode;
|
|
||||||
import javafx.scene.input.KeyEvent;
|
|
||||||
import javafx.scene.input.MouseEvent;
|
import javafx.scene.input.MouseEvent;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -31,7 +30,6 @@ public class GameCanvas extends Canvas {
|
|||||||
this.game = game;
|
this.game = game;
|
||||||
this.gc = getGraphicsContext2D();
|
this.gc = getGraphicsContext2D();
|
||||||
this.pileViews = constructPileViews(game);
|
this.pileViews = constructPileViews(game);
|
||||||
setOnKeyReleased(new KeyReleaseHandler());
|
|
||||||
setOnMouseClicked(new MouseClickHandler());
|
setOnMouseClicked(new MouseClickHandler());
|
||||||
updateView();
|
updateView();
|
||||||
}
|
}
|
||||||
@ -101,27 +99,30 @@ public class GameCanvas extends Canvas {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class KeyReleaseHandler implements EventHandler<KeyEvent> {
|
public EventHandler<ActionEvent> getNewGameAction() {
|
||||||
@Override
|
return e -> {
|
||||||
public void handle(KeyEvent e) {
|
game.newGame();
|
||||||
if (e.isControlDown()) {
|
winMessageShown = false;
|
||||||
if (e.getCode() == KeyCode.Y) {
|
if (fromPile != null && fromPile.isSelected()) {
|
||||||
game.redo();
|
fromPile.toggleSelected();
|
||||||
} 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
fromPile = null;
|
||||||
updateView();
|
updateView();
|
||||||
}
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public EventHandler<ActionEvent> getUndoAction() {
|
||||||
|
return e -> {
|
||||||
|
game.undo();
|
||||||
|
updateView();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public EventHandler<ActionEvent> getRedoAction() {
|
||||||
|
return e -> {
|
||||||
|
game.redo();
|
||||||
|
updateView();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MouseClickHandler implements EventHandler<MouseEvent> {
|
private class MouseClickHandler implements EventHandler<MouseEvent> {
|
||||||
|
49
src/main/java/com/charego/freecellfx/view/GameMenuBar.java
Normal file
49
src/main/java/com/charego/freecellfx/view/GameMenuBar.java
Normal file
@ -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<ActionEvent> handler) {
|
||||||
|
newGame.setOnAction(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUndoAction(EventHandler<ActionEvent> handler) {
|
||||||
|
undoMove.setOnAction(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRedoAction(EventHandler<ActionEvent> handler) {
|
||||||
|
redoMove.setOnAction(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user