Select Git revision
game_board_painter.dart
-
Benoît Harrault authoredBenoît Harrault authored
game_board_painter.dart 3.98 KiB
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:jeweled/models/game_cell.dart';
import 'package:jeweled/models/game.dart';
import 'package:jeweled/models/cell_location.dart';
import 'package:jeweled/utils/color_theme.dart';
class GameBoardPainter extends CustomPainter {
const GameBoardPainter(this.currentGame);
final Game currentGame;
static const drawTextValue = false;
@override
void paint(Canvas canvas, Size size) {
final int sizeHorizontal = currentGame.settings.boardSize;
final int sizeVertical = currentGame.settings.boardSize;
final List cells = currentGame.board.cells;
final double cellSize = size.width / max(sizeHorizontal, sizeVertical);
// background
for (var row = 0; row < sizeVertical; row++) {
final double y = cellSize * row;
for (var col = 0; col < sizeHorizontal; col++) {
final double x = cellSize * col;
final GameCell cell = cells[row][col];
final int colorCode = ColorTheme.getColorCode(cell.value);
final cellPaintBackground = Paint();
cellPaintBackground.color = Color(colorCode);
cellPaintBackground.style = PaintingStyle.fill;
final Rect cellBackground =
Rect.fromPoints(Offset(x - 1, y - 1), Offset(x + cellSize + 2, y + cellSize + 2));
canvas.drawRect(cellBackground, cellPaintBackground);
// draw value on cell
if (drawTextValue) {
final textPainter = TextPainter(
text: TextSpan(
text: cell.value.toString(),
style: const TextStyle(
color: Colors.black,
fontSize: 15,
),
),
textDirection: TextDirection.ltr,
)..layout(
minWidth: 0,
maxWidth: size.width,
);
textPainter.paint(canvas, Offset(x + 4, y + 2));
}
}
}
// borders
const double borderSize = 4;
final cellPaintBorder = Paint();
cellPaintBorder.color = ColorTheme.getBorderColor();
cellPaintBorder.strokeWidth = borderSize;
cellPaintBorder.strokeCap = StrokeCap.round;
for (var row = 0; row < sizeVertical; row++) {