Skip to content

Commit

Permalink
refactor(drawer): Generalize draw_position from draw_initial_position
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfarias committed Sep 1, 2024
1 parent d820141 commit e696611
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions src/drawer/board.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use image::{imageops, ImageBuffer, Rgba, RgbaImage};
use shakmaty::{self, Board, File, Move, Rank, Role, Square};
use shakmaty::{self, Chess, File, Move, Position, Rank, Role, Square};
use tiny_skia::{self, Pixmap, PixmapPaint, Transform};
use usvg::FitTo;

Expand Down Expand Up @@ -70,9 +70,12 @@ impl BoardDrawer {
pub fn light_square(&self) -> RgbaImage {
ImageBuffer::from_pixel(self.square_size(), self.square_size(), self.light)
}

pub fn draw_initial_position(&mut self, svgs: &SVGForest) -> Result<RgbaImage, DrawerError> {
log::debug!("Drawing initial board");
pub fn draw_position(
&mut self,
position: &Chess,
svgs: &SVGForest,
) -> Result<RgbaImage, DrawerError> {
log::debug!("Drawing position");
let mut counter = 1;
let mut column = ImageBuffer::from_fn(self.square_size(), self.size, |_, y| {
if y >= self.square_size() * counter {
Expand All @@ -91,19 +94,25 @@ impl BoardDrawer {
imageops::flip_vertical_in_place(&mut column)
}

let board = Board::default();
for (square, piece) in board.into_iter() {
log::debug!("Initializing {:?} in {:?}", piece, square);
self.draw_piece(
&square,
&piece.role,
piece.color,
false,
&mut board_img,
None,
svgs,
false,
)?;
for rank in Rank::ALL.into_iter().rev() {
for file in File::ALL {
let square = Square::from_coords(file, rank);
if let Some(piece) = position.board().piece_at(square) {
log::debug!("Drawing {:?} in {:?}", piece, square);
self.draw_piece(
&square,
&piece.role,
piece.color,
false,
&mut board_img,
None,
svgs,
false,
)?;
} else {
self.draw_square(&square, &mut board_img, svgs)?;
}
}
}

self.draw_ranks(2, 6, &mut board_img, svgs)?;
Expand All @@ -116,6 +125,14 @@ impl BoardDrawer {
Ok(board_img)
}

pub fn draw_initial_position(&mut self, svgs: &SVGForest) -> Result<RgbaImage, DrawerError> {
log::debug!("Drawing initial position");
let position = Chess::default();
let board_img = self.draw_position(&position, svgs)?;

Ok(board_img)
}

pub fn draw_ranks(
&mut self,
from: u32,
Expand Down

0 comments on commit e696611

Please sign in to comment.