Permalink
Browse files

Move bench renderer into its own file

  • Loading branch information...
1 parent f99cf58 commit c06f2655fe67d92f70b1adbc1a0fd84f77ab45af @chellmuth committed Jan 9, 2014
Showing with 59 additions and 59 deletions.
  1. +39 −0 lib/bench.dart
  2. +17 −15 lib/view.dart
  3. +3 −44 web/hive.dart
View
@@ -0,0 +1,39 @@
+part of view;
+
+class Bench {
+ String player1, player2;
+ Bench(this.player1, this.player2);
+
+ void draw(CanvasRenderingContext2D context, CanvasElement canvas) {
+ context.save();
+
+ context.lineWidth = 2;
+ context.strokeStyle = '#4A4A4A';
+ context.fillStyle = 'rgba(236, 217, 176, .95)';
+ var widthRatio = .5;
+ var height = 260;
+
+ var left = canvas.width * (1 - widthRatio) / 2;
+ var top = canvas.height - height;
+ var width = canvas.width * widthRatio;
+ context.rect(left, top, width, height + 1);
+ context.fill();
+ context.stroke();
+
+ var fontSize = 45;
+ context.font = '${fontSize}px Futura';
+ context.fillStyle = '#000';
+
+ var verticalMargin = 10;
+ {
+ var metrics = context.measureText(player1);
+ var textWidth = metrics.width;
+ context.fillText(player1, left + width / 4 - textWidth / 2, top + fontSize + verticalMargin);
+ }
+ {
+ var metrics = context.measureText(player2);
+ var textWidth = metrics.width;
+ context.fillText(player2, left + width * 3 / 4 - textWidth / 2, top + fontSize + verticalMargin);
+ }
+ }
+}
View
@@ -6,18 +6,20 @@ import 'dart:math' show PI;
import 'assets.dart';
import 'gamemodel.dart';
+part 'bench.dart';
+
abstract class HexView {
static final num width = 80 * 2;
static final num height = 90 * 2;
-
+
static final num pointHeight = .25;
-
+
int get row;
int get col;
int get stackHeight => 1;
String get strokeColor;
String get fillColor;
-
+
num get xOffset {
var xOffset = col * width;
if (row % 2 == 1) { xOffset += .5 * width; }
@@ -30,7 +32,7 @@ abstract class HexView {
yOffset += 1;
return yOffset;
}
-
+
void draw(CanvasRenderingContext2D context) {
context.fillStyle = fillColor;
context.strokeStyle = strokeColor;
@@ -71,7 +73,7 @@ class TileView extends HexView {
int get stackHeight => tile.height;
String get fillColor => tile.piece.player == Player.WHITE ? '#595959' : '#FFFFF7';
String get strokeColor => '#333';
-
+
void draw(CanvasRenderingContext2D context) {
//super.draw(context);
@@ -80,7 +82,7 @@ class TileView extends HexView {
var yOffset = this.yOffset - (tile.height - 1) * yStackOffset;
ImageElement asset = AssetLibrary.imageForPiece(tile.piece);
- context.drawImageScaledFromSource(asset, 0, 0, asset.naturalWidth, asset.naturalHeight, xOffset - 6, yOffset - 4, asset.naturalWidth, asset.naturalHeight);
+ context.drawImageScaledFromSource(asset, 0, 0, asset.naturalWidth, asset.naturalHeight, xOffset - 6, yOffset - 4, asset.naturalWidth, asset.naturalHeight);
var boxSize = 20;
var dotContainerRect = new Rectangle(xOffset + .85 * HexView.width - boxSize / 2, yOffset + .3 * HexView.height - boxSize / 2, boxSize, boxSize);
@@ -113,13 +115,13 @@ class TileView extends HexView {
_renderDot(context, boundingRect, 1/4, 1/2);
_renderDot(context, boundingRect, 3/4, 1/2);
}
-
+
void _renderThreeDots(CanvasRenderingContext2D context, Rectangle boundingRect) {
_renderDot(context, boundingRect, 1/4, 2/3);
_renderDot(context, boundingRect, 3/4, 2/3);
_renderDot(context, boundingRect, 1/2, 1/3);
}
-
+
void _renderDot(CanvasRenderingContext2D context, Rectangle boundingRect, num xCenter, num yCenter) {
context.save();
@@ -135,21 +137,21 @@ class TileView extends HexView {
class CoveredPieceView {
Piece piece;
-
+
CoveredPieceView(this.piece);
-
+
String get letter {
- switch (piece.bug) {
+ switch (piece.bug) {
case Bug.ANT: return 'A';
case Bug.BEETLE: return 'B';
case Bug.GRASSHOPPER: return 'G';
case Bug.QUEEN: return 'Q';
case Bug.SPIDER: return 'S';
}
}
-
+
String get color {
- switch (piece.bug) {
+ switch (piece.bug) {
case Bug.ANT: return '#6DA9EE';
case Bug.BEETLE: return '#E56CFE';
case Bug.GRASSHOPPER: return '#9ADA54';
@@ -162,12 +164,12 @@ class CoveredPieceView {
void draw(CanvasRenderingContext2D context, Rectangle bounds) {
context.save();
context.fillStyle = color;
-
+
context.beginPath();
context.arc(bounds.left + bounds.width / 2, bounds.top + bounds.height / 2, bounds.width / 2, 0, PI * 2, true);
context.closePath();
context.fill();
-
+
var fontSize = 14;
context.font = '${fontSize}pt Futura';
context.fillStyle = piece.player == Player.WHITE ? '#FFFFF7' : '#595959';
View
@@ -12,8 +12,7 @@ import '../lib/gamemodel.dart';
import '../lib/rules.dart';
var camera = new Camera();
-var player1 = '';
-var player2 = '';
+Bench bench;
void main() {
@@ -114,8 +113,7 @@ void handleCanvasClick(MouseEvent event, GameState gamestate) {
void setupSGF(String sgf, GameState gamestate) {
ParsedGame parsedGame = SGF.parseSGF(sgf);
- player1 = parsedGame.player1;
- player2 = parsedGame.player2;
+ bench = new Bench(parsedGame.player1, parsedGame.player2);
gamestate.initialize(parsedGame.gameEvents);
gamestate.step(1);
render(gamestate);
@@ -208,44 +206,5 @@ void render(GameState gamestate, { List<Move> moves : null }) {
}
context.restore();
-
- _drawBench(context, gamestate);
+ bench.draw(context, canvas);
}
-
-void _drawBench(CanvasRenderingContext2D context, GameState gamestate) {
- CanvasElement canvas = querySelector("#hive-canvas-id");
-
- context.save();
-
- context.lineWidth = 2;
- context.strokeStyle = '#4A4A4A';
- context.fillStyle = 'rgba(236, 217, 176, .95)';
- // context.fillStyle = 'rgba(255, 255, 255, .3)';
- var widthRatio = .5;
- var height = 260;
-
- var left = canvas.width * (1 - widthRatio) / 2;
- var top = canvas.height - height;
- var width = canvas.width * widthRatio;
- context.rect(left, top, width, height + 1);
- context.fill();
- context.stroke();
-
- var fontSize = 45;
- context.font = '${fontSize}px Futura';
- context.fillStyle = '#000';
-
- var verticalMargin = 10;
- {
- var metrics = context.measureText(player1);
- var textWidth = metrics.width;
- context.fillText(player1, left + width / 4 - textWidth / 2, top + fontSize + verticalMargin);
- }
- {
- var metrics = context.measureText(player2);
- var textWidth = metrics.width;
- context.fillText(player2, left + width * 3 / 4 - textWidth / 2, top + fontSize + verticalMargin);
- }
-}
-
-

0 comments on commit c06f265

Please sign in to comment.