Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
Refactored out board drawing code
Browse files Browse the repository at this point in the history
  • Loading branch information
vapour101 committed Sep 15, 2017
1 parent 26e2534 commit 4a2f6f4
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 71 deletions.
58 changes: 58 additions & 0 deletions src/main/java/ui/drawer/BoardDrawer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2017
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package ui.drawer;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import util.CoordProjector;
import util.DimensionHelper;
import util.DrawCoords;

public abstract class BoardDrawer {

private Canvas canvas;

BoardDrawer(Canvas canvas) {
this.canvas = canvas;
}

public void draw() {
drawBackground();
drawLines();
}

abstract void drawBackground();

abstract void drawLines();

GraphicsContext getGraphicsContext() {
return canvas.getGraphicsContext2D();
}

double getBoardLength() {
return DimensionHelper.getBoardLength(canvas);
}

DrawCoords getTopLeftCorner() {
return DimensionHelper.getTopLeftCorner(canvas);
}

CoordProjector getProjector() {
return DimensionHelper.getProjector(canvas);
}
}
63 changes: 11 additions & 52 deletions src/main/java/ui/drawer/GameDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@

import java.util.Collection;

import static util.Coords.getCoords;
import static util.DimensionHelper.getBoardLength;
import static util.DimensionHelper.getTopLeftCorner;
import static util.HandicapHelper.getHandicapStones;
import static util.Move.play;
import static util.StoneColour.BLACK;

public class GameDrawer {

private Canvas canvas;
private GameHandler game;
private StoneDrawer stoneDrawer;
private BoardDrawer boardDrawer;
private Move hoverStone;

public GameDrawer(Canvas canvas, GameHandler game) {
Expand All @@ -47,23 +43,22 @@ public GameDrawer(Canvas canvas, GameHandler game) {
hoverStone = null;

setStoneDrawer(new SimpleStoneDrawer(canvas));
setBoardDrawer(new SimpleBoardDrawer(canvas));

canvas.widthProperty().addListener(this::onCanvasResize);
canvas.heightProperty().addListener(this::onCanvasResize);
}

public void setBoardDrawer(BoardDrawer boardDrawer) {
this.boardDrawer = boardDrawer;
}

StoneDrawer getStoneDrawer() {
return stoneDrawer;
}

public void setStoneDrawer(StoneDrawer stoneDrawer) {
this.stoneDrawer = stoneDrawer;
stoneDrawer.setRadiusBuilder(this::getStoneRadius);
stoneDrawer.setProjectorBuilder(this::getProjector);
}

double getStoneRadius() {
return getBoardLength(canvas) / (19 + 1) / 2;
}

private void onCanvasResize(Observable observable) {
Expand All @@ -72,8 +67,7 @@ private void onCanvasResize(Observable observable) {

public void draw() {
drawBackground();
drawBoardTexture();
drawBoardLines();
boardDrawer.draw();
drawStones();
}

Expand Down Expand Up @@ -102,45 +96,6 @@ private GraphicsContext getGraphicsContext() {
return canvas.getGraphicsContext2D();
}

private void drawBoardTexture() {
DrawCoords topLeft = getTopLeftCorner(canvas);
GraphicsContext context = getGraphicsContext();
double length = getBoardLength(canvas);

context.setFill(Color.web("0xB78600"));
context.fillRect(topLeft.getX(), topLeft.getY(), length, length);
}

private void drawBoardLines() {
CoordProjector projector = getProjector();

GraphicsContext context = getGraphicsContext();

for (int i = 1; i < 20; i++) {
//Horizontal Lines
DrawCoords start = projector.fromBoardCoords(getCoords(1, i));
DrawCoords end = projector.fromBoardCoords(getCoords(19, i));

context.strokeLine(start.getX(), start.getY(), end.getX(), end.getY());

//Vertical Lines
start = projector.fromBoardCoords(getCoords(i, 1));
end = projector.fromBoardCoords(getCoords(i, 19));

context.strokeLine(start.getX(), start.getY(), end.getX(), end.getY());
}

StoneDrawer drawer = new SimpleStoneDrawer(canvas);
drawer.setProjectorBuilder(() -> projector);
drawer.setRadiusBuilder(() -> context.getLineWidth() * 4);

drawer.drawStones(getHandicapStones(9), BLACK);
}

private CoordProjector getProjector() {
return new CoordProjector(getBoardLength(canvas), getTopLeftCorner(canvas));
}

public void setHoverStone(DrawCoords position, StoneColour colour) {
CoordProjector projector = getProjector();

Expand All @@ -163,4 +118,8 @@ public void setHoverStone(DrawCoords position, StoneColour colour) {
hoverStone = move;
}
}

private CoordProjector getProjector() {
return DimensionHelper.getProjector(canvas);
}
}
74 changes: 74 additions & 0 deletions src/main/java/ui/drawer/SimpleBoardDrawer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2017
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package ui.drawer;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import util.CoordProjector;
import util.DimensionHelper;
import util.DrawCoords;

import static util.Coords.getCoords;
import static util.HandicapHelper.getHandicapStones;
import static util.StoneColour.BLACK;

public class SimpleBoardDrawer extends BoardDrawer {

SimpleBoardDrawer(Canvas canvas) {
super(canvas);
}

@Override
public void drawBackground() {
DrawCoords topLeft = getTopLeftCorner();
GraphicsContext context = getGraphicsContext();
double length = getBoardLength();

context.setFill(Color.web("0xB78600"));
context.fillRect(topLeft.getX(), topLeft.getY(), length, length);
}

@Override
public void drawLines() {
CoordProjector projector = getProjector();

GraphicsContext context = getGraphicsContext();

for (int i = 1; i < 20; i++) {
//Horizontal Lines
DrawCoords start = projector.fromBoardCoords(getCoords(1, i));
DrawCoords end = projector.fromBoardCoords(getCoords(19, i));

context.strokeLine(start.getX(), start.getY(), end.getX(), end.getY());

//Vertical Lines
start = projector.fromBoardCoords(getCoords(i, 1));
end = projector.fromBoardCoords(getCoords(i, 19));

context.strokeLine(start.getX(), start.getY(), end.getX(), end.getY());
}
Canvas canvas = context.getCanvas();

StoneDrawer drawer = new SimpleStoneDrawer(canvas);
double scale = DimensionHelper.getStoneRadius(canvas);
scale = (context.getLineWidth() * 4) / scale;

drawer.drawStones(getHandicapStones(9), BLACK, scale);
}
}
24 changes: 5 additions & 19 deletions src/main/java/ui/drawer/StoneDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,19 @@

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.util.Builder;
import util.CoordProjector;
import util.Coords;
import util.DrawCoords;
import util.StoneColour;
import util.*;

import java.util.Collection;


public abstract class StoneDrawer {

private Canvas canvas;
private Builder<CoordProjector> projectorBuilder;
private Builder<Double> radiusBuilder;

StoneDrawer(Canvas canvas) {
this.canvas = canvas;
}

public void setProjectorBuilder(Builder<CoordProjector> projectorBuilder) {
this.projectorBuilder = projectorBuilder;
}

public void setRadiusBuilder(Builder<Double> radiusBuilder) {
this.radiusBuilder = radiusBuilder;
}

public void drawGhostStone(DrawCoords position, StoneColour colour) {
GraphicsContext context = getGraphicsContext();

Expand Down Expand Up @@ -73,8 +59,8 @@ public void drawStones(Collection<Coords> stones, StoneColour colour) {
draw(getProjector().fromBoardCoords(stone), colour);
}

protected CoordProjector getProjector() {
return projectorBuilder.build();
private CoordProjector getProjector() {
return DimensionHelper.getProjector(canvas);
}

public void drawStones(Collection<Coords> stones, StoneColour colour, double scale) {
Expand All @@ -84,7 +70,7 @@ public void drawStones(Collection<Coords> stones, StoneColour colour, double sca

public abstract void draw(DrawCoords position, StoneColour colour, double scale);

protected double getRadius() {
return radiusBuilder.build();
double getRadius() {
return DimensionHelper.getStoneRadius(canvas);
}
}
8 changes: 8 additions & 0 deletions src/main/java/util/DimensionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

public class DimensionHelper {

public static CoordProjector getProjector(Canvas canvas) {
return new CoordProjector(getBoardLength(canvas), getTopLeftCorner(canvas));
}

public static DrawCoords getTopLeftCorner(Canvas canvas) {
double length = getBoardLength(canvas);
double canvasWidth = canvas.getWidth();
Expand All @@ -43,4 +47,8 @@ public static double getBoardLength(Canvas canvas) {

return Math.min(canvasHeight, canvasWidth);
}

public static double getStoneRadius(Canvas canvas) {
return getBoardLength(canvas) / (19 + 1) / 2;
}
}

0 comments on commit 4a2f6f4

Please sign in to comment.