Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring code #130

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/java/org/asdfjkl/jerryfx/gui/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ btnFullAnalysis, new Separator(),
EditMenuController editMenuController = new EditMenuController(gameModel);

gameModel.addListener(modeMenuController);
modeMenuController.activateEnterMovesMode();
engineController.activateEnterMovesMode(gameModel);

itmSaveCurrentGameAs.setOnAction(e -> {
gameMenuController.handleSaveCurrentGame();
Expand Down Expand Up @@ -442,7 +442,7 @@ btnFullAnalysis, new Separator(),
itmEnterMoves.setOnAction(actionEvent -> {
tglEngineOnOff.setSelected(false);
tglEngineOnOff.setText("Off");
modeMenuController.activateEnterMovesMode();
engineController.activateEnterMovesMode(gameModel);
});

itmFullGameAnalysis.setOnAction(actionEvent -> {
Expand All @@ -457,7 +457,7 @@ btnFullAnalysis, new Separator(),
} else {
itmEnterMoves.setSelected(true);
tglEngineOnOff.setText("Off");
modeMenuController.activateEnterMovesMode();
engineController.activateEnterMovesMode(gameModel);
}
});

Expand Down Expand Up @@ -768,7 +768,7 @@ btnFullAnalysis, new Separator(),
// enter moves mode
tglEngineOnOff.setSelected(false);
tglEngineOnOff.setText("Off");
modeMenuController.activateEnterMovesMode();
engineController.activateEnterMovesMode(gameModel);
}
if(event.getCode() == KeyCode.F11) {
stage.setFullScreen(true);
Expand Down Expand Up @@ -969,7 +969,7 @@ private void handleNewGame() {
modeMenuController.activatePlayBlackMode();
}
} else {
modeMenuController.activateEnterMovesMode();
engineController.activateEnterMovesMode(gameModel);
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/org/asdfjkl/jerryfx/gui/CanvasProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.asdfjkl.jerryfx.gui;

import javafx.scene.canvas.Canvas;

public abstract class CanvasProperties extends Canvas {
@Override
public boolean isResizable() {
return true;
}

@Override
public double maxHeight(double width) {
return Double.POSITIVE_INFINITY;
}

@Override
public double maxWidth(double height) {
return Double.POSITIVE_INFINITY;
}

@Override
public double minWidth(double height) {
return 50D;
}

@Override
public double minHeight(double width) {
return 50D;
}

public void resize(double width, double height) {
this.setWidth(width);
this.setHeight(height);

updateCanvas();
}

public void updateCanvas() {}
}
117 changes: 43 additions & 74 deletions src/main/java/org/asdfjkl/jerryfx/gui/Chessboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import static org.asdfjkl.jerryfx.lib.CONSTANTS.*;

public class Chessboard extends Canvas implements StateChangeListener {
public class Chessboard extends CanvasProperties implements StateChangeListener {

BoardStyle boardStyle;
final double outputScaleX;
Expand Down Expand Up @@ -63,7 +63,7 @@ public Chessboard(GameModel gameModel) {

boardStyle = new BoardStyle();
this.gameModel = gameModel;
pieceImageProvider = new PieceImageProvider();
pieceImageProvider = PieceImageProvider.getInstance();
outputScaleX = Screen.getPrimary().getOutputScaleX();
grabbedPiece.setPiece(-1);
moveSource = new Point(-1,-1);
Expand All @@ -89,49 +89,18 @@ public Chessboard(GameModel gameModel) {
}


@Override
public boolean isResizable() {
return true;
}

@Override
public double maxHeight(double width) {
return Double.POSITIVE_INFINITY;
}

@Override
public double maxWidth(double height) {
return Double.POSITIVE_INFINITY;
}

@Override
public double minWidth(double height) {
return 50D;
}

@Override
public double minHeight(double width) {
return 50D;
}

@Override
public void resize(double width, double height) {
this.setWidth(width);
this.setHeight(height);

updateCanvas();
}

public void updateCanvas() {

GraphicsContext gc = this.getGraphicsContext2D();
GraphicsContext graphicsContext = this.getGraphicsContext2D();

// fill background
gc.beginPath();
graphicsContext.beginPath();
//gc.setFill(Color.rgb(152, 152, 152));
gc.setFill(boardStyle.getDarkSquareColor());
gc.rect(0, 0, this.getWidth(), this.getHeight());
gc.fill();
graphicsContext.setFill(boardStyle.getDarkSquareColor());
graphicsContext.rect(0, 0, this.getWidth(), this.getHeight());
graphicsContext.fill();

// size of real board incl. corner
double height = this.getHeight();
Expand All @@ -154,10 +123,10 @@ public void updateCanvas() {
innerYOffset = (outerMargin + borderMargin);

// paint board inc. margin with letters & numbers
gc.beginPath();
gc.setFill(boardStyle.getBorderColor());
gc.rect(xOffset, outerMargin, (squareSize*8)+(borderMargin*2), (squareSize*8)+(borderMargin*2));
gc.fill();
graphicsContext.beginPath();
graphicsContext.setFill(boardStyle.getBorderColor());
graphicsContext.rect(xOffset, outerMargin, (squareSize*8)+(borderMargin*2), (squareSize*8)+(borderMargin*2));
graphicsContext.fill();

// get the from and to field of the last move
// to highlight those squares
Expand Down Expand Up @@ -193,10 +162,10 @@ public void updateCanvas() {
}
int y = (innerYOffset) + ((7-j)*squareSize);

gc.beginPath();
gc.setFill(fieldColor);
gc.rect(x,y,squareSize,squareSize);
gc.fill();
graphicsContext.beginPath();
graphicsContext.setFill(fieldColor);
graphicsContext.rect(x,y,squareSize,squareSize);
graphicsContext.fill();

if(lastMoveFrom != null && lastMoveTo != null) {
boolean markField = false;
Expand All @@ -213,10 +182,10 @@ public void updateCanvas() {
}
}
if(markField) {
gc.beginPath();
gc.setFill(lastMoveColor);
gc.rect(x, y, squareSize, squareSize);
gc.fill();
graphicsContext.beginPath();
graphicsContext.setFill(lastMoveColor);
graphicsContext.rect(x, y, squareSize, squareSize);
graphicsContext.fill();
}
}
}
Expand All @@ -225,41 +194,41 @@ public void updateCanvas() {
// paint colored fields
for(ColoredField coloredField : gameModel.getGame().getCurrentNode().getColoredFields()) {

int i = coloredField.x;
int j = coloredField.y;
int xColoredField = coloredField.x;
int yColoredField = coloredField.y;

int x = (innerXOffset) + (i*squareSize);
int y = (innerYOffset) + ((7-j)*squareSize);
int x = (innerXOffset) + (xColoredField*squareSize);
int y = (innerYOffset) + ((7-yColoredField)*squareSize);
if(flipBoard) {
x = innerXOffset+((7-i)*squareSize);
y = (innerYOffset) + (j*squareSize);
x = innerXOffset+((7-xColoredField)*squareSize);
y = (innerYOffset) + (yColoredField*squareSize);
}

gc.beginPath();
gc.setFill(coloredFieldColor);
gc.rect(x,y,squareSize,squareSize);
gc.fill();
graphicsContext.beginPath();
graphicsContext.setFill(coloredFieldColor);
graphicsContext.rect(x,y,squareSize,squareSize);
graphicsContext.fill();
}

// draw the board coordinates
gc.setFill(boardStyle.getCoordinateColor());
graphicsContext.setFill(boardStyle.getCoordinateColor());
for(int i=0;i<8;i++) {
if(flipBoard){
char ch = (char) (65 + (7 - i));
String idx = Character.toString(ch);
String num = Integer.toString(i + 1);
gc.beginPath();
gc.fillText(idx, innerXOffset + (i * squareSize) + (squareSize / 2) - 4,
graphicsContext.beginPath();
graphicsContext.fillText(idx, innerXOffset + (i * squareSize) + (squareSize / 2) - 4,
innerYOffset + (8 * squareSize) + (borderMargin * 0.8));
gc.fillText(num, xOffset + 5, innerYOffset + (i * squareSize) + (squareSize / 2) + 4);
graphicsContext.fillText(num, xOffset + 5, innerYOffset + (i * squareSize) + (squareSize / 2) + 4);
} else{
char ch = (char) (65 + i);
String idx = Character.toString(ch);
String num = Integer.toString(8 - i);
gc.beginPath();
gc.fillText(idx, innerXOffset + (i * squareSize) + (squareSize / 2) - 4,
graphicsContext.beginPath();
graphicsContext.fillText(idx, innerXOffset + (i * squareSize) + (squareSize / 2) - 4,
innerYOffset + (8 * squareSize) + (borderMargin * 0.8));
gc.fillText(num, xOffset + 5, innerYOffset + (i * squareSize) + (squareSize / 2) + 4);
graphicsContext.fillText(num, xOffset + 5, innerYOffset + (i * squareSize) + (squareSize / 2) + 4);
}
}

Expand Down Expand Up @@ -287,13 +256,13 @@ public void updateCanvas() {
if (!(drawGrabbedPiece && i == moveSource.x && j == moveSource.y)) {
Image pieceImage = pieceImageProvider.getImage(piece, (int) (squareSize * this.outputScaleX),
boardStyle.getPieceStyle());
gc.drawImage(pieceImage, x, y, squareSize, squareSize);
graphicsContext.drawImage(pieceImage, x, y, squareSize, squareSize);
}
} else {
if (!(drawGrabbedPiece && i == moveSource.x && (7-j) == moveSource.y)) {
Image pieceImage = pieceImageProvider.getImage(piece, (int) (squareSize * this.outputScaleX),
boardStyle.getPieceStyle());
gc.drawImage(pieceImage, x, y, squareSize, squareSize);
graphicsContext.drawImage(pieceImage, x, y, squareSize, squareSize);
}
}
}
Expand All @@ -316,18 +285,18 @@ public void updateCanvas() {
y_side_to_move = innerYOffset - 11;
}
}
gc.beginPath();
gc.setFill(boardStyle.getLightSquareColor());
gc.rect(x_side_to_move, y_side_to_move, 4,4);
gc.fill();
graphicsContext.beginPath();
graphicsContext.setFill(boardStyle.getLightSquareColor());
graphicsContext.rect(x_side_to_move, y_side_to_move, 4,4);
graphicsContext.fill();
}

// draw grabbed piece
if(drawGrabbedPiece) {
int offset = squareSize / 2;
Image pieceImage = pieceImageProvider.getImage(grabbedPiece.getPiece(),
(int) (squareSize * this.outputScaleX), boardStyle.getPieceStyle());
gc.drawImage(pieceImage, grabbedPiece.getCurrentXLocation() - offset,
graphicsContext.drawImage(pieceImage, grabbedPiece.getCurrentXLocation() - offset,
grabbedPiece.getCurrentYLocation() - offset,squareSize, squareSize);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/asdfjkl/jerryfx/gui/DialogPromotion.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class DialogPromotion {

public static int show(boolean playerColor, int pieceStyle, int colorTheme) {

PieceImageProvider provider = new PieceImageProvider();
PieceImageProvider provider = PieceImageProvider.getInstance();

choice = CONSTANTS.EMPTY;

Expand Down
43 changes: 40 additions & 3 deletions src/main/java/org/asdfjkl/jerryfx/gui/EngineController.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public class EngineController {

EngineThread engineThread;
final BlockingQueue<String> cmdQueue = new LinkedBlockingQueue<String>();;
ModeMenuController mmc = null;
ModeMenuController modeMenuController = null;

public EngineController(ModeMenuController modeMenuController) {

mmc = modeMenuController;
this.modeMenuController = modeMenuController;

}

Expand All @@ -53,7 +53,7 @@ public void changed(final ObservableValue<? extends String> observable,
@Override
public void run() {
String value = count.getAndSet(null);
mmc.handleEngineInfo(value);
modeMenuController.handleEngineInfo(value);
}
});
}
Expand Down Expand Up @@ -122,5 +122,42 @@ public void uciGoMoveTime(int milliseconds) {
}
}

void restartGame(Boolean setUciLimitStrength, GameModel gameModel) {
if(gameModel.activeEngine.supportsUciLimitStrength()) {
gameModel.activeEngine.setUciLimitStrength(setUciLimitStrength);
}
sendCommand("stop");
sendCommand("quit");
String cmdEngine = gameModel.activeEngine.getPath();
sendCommand("start "+cmdEngine);
sendCommand("uci");
sendCommand("ucinewgame");
}

public void activateEnterMovesMode(GameModel gameModel) {
sendCommand("stop");
sendCommand("quit");
gameModel.setMode(GameModel.MODE_ENTER_MOVES);
gameModel.triggerStateChange();
}

void handleStateChangeAnalysis(GameModel gameModel) {

String fen = gameModel.getGame().getCurrentNode().getBoard().fen();
sendCommand("stop");
sendCommand("position fen "+fen);
sendCommand("go infinite");

}

void resetEngine(GameModel gameModel) {
restartGame(false, gameModel);
for(EngineOption enOpt : gameModel.activeEngine.options) {
if(enOpt.isNotDefault()) {
sendCommand(enOpt.toUciCommand());
}
}
}


}
Loading