Place a stone onto the drawing area #38

Merged
savanni merged 7 commits from feature/place-stone into main 2023-04-07 12:14:04 +00:00
1 changed files with 42 additions and 27 deletions
Showing only changes of commit 4590bc6688 - Show all commits

View File

@ -15,6 +15,7 @@ use std::{cell::RefCell, io::Cursor, rc::Rc};
const WIDTH: i32 = 800;
const HEIGHT: i32 = 800;
const MARGIN: i32 = 20;
#[derive(Clone, Default, PartialEq)]
struct Addr {
@ -84,6 +85,7 @@ impl ObjectImpl for BoardPrivate {
.set_draw_func(move |_, context, width, height| {
let render_start = std::time::Instant::now();
let board = board.borrow();
match background {
Ok(Some(ref background)) => {
context.set_source_pixbuf(&background, 0., 0.);
@ -99,20 +101,32 @@ impl ObjectImpl for BoardPrivate {
let vspace_between = ((height - 40) as f64) / ((board.size.height - 1) as f64);
let pen = Pen {
x_offset: 20.,
y_offset: 20.,
x_offset: MARGIN as f64,
y_offset: MARGIN as f64,
hspace_between,
vspace_between,
};
(0..board.size.width).for_each(|col| {
context.move_to(20.0 + (col as f64) * hspace_between, 20.0);
context.line_to(20.0 + (col as f64) * hspace_between, (height as f64) - 20.0);
context.move_to(
(MARGIN as f64) + (col as f64) * hspace_between,
MARGIN as f64,
);
context.line_to(
(MARGIN as f64) + (col as f64) * hspace_between,
(height as f64) - (MARGIN as f64),
);
let _ = context.stroke();
});
(0..board.size.height).for_each(|row| {
context.move_to(20.0, 20.0 + (row as f64) * vspace_between);
context.line_to((width - 20) as f64, 20.0 + (row as f64) * vspace_between);
context.move_to(
MARGIN as f64,
(MARGIN as f64) + (row as f64) * vspace_between,
);
context.line_to(
(width - MARGIN) as f64,
(MARGIN as f64) + (row as f64) * vspace_between,
);
let _ = context.stroke();
});
@ -162,13 +176,16 @@ impl ObjectImpl for BoardPrivate {
let hspace_between = ((WIDTH - 40) as f64) / ((board.size.width - 1) as f64);
let vspace_between = ((HEIGHT - 40) as f64) / ((board.size.height - 1) as f64);
let addr =
if x.round() < 20. || x.round() > 780. || y.round() < 20. || y.round() > 780. {
let addr = if x.round() < MARGIN as f64
|| x.round() > (WIDTH - MARGIN) as f64
|| y.round() < MARGIN as f64
|| y.round() > (HEIGHT - MARGIN) as f64
{
None
} else {
Some(Addr {
column: ((x.round() - 20.) / hspace_between).round() as u8,
row: ((y.round() - 20.) / vspace_between).round() as u8,
column: ((x.round() - MARGIN as f64) / hspace_between).round() as u8,
row: ((y.round() - MARGIN as f64) / vspace_between).round() as u8,
})
};
@ -246,7 +263,7 @@ impl Pen {
context.arc(
self.x_offset + (col as f64) * self.hspace_between,
self.y_offset + (row as f64) * self.vspace_between,
10.,
5.,
0.,
2. * std::f64::consts::PI,
);
@ -258,14 +275,7 @@ impl Pen {
Color::White => context.set_source_rgb(0.9, 0.9, 0.9),
Color::Black => context.set_source_rgb(0.0, 0.0, 0.0),
};
context.arc(
20.0 + (col as f64) * self.hspace_between,
20.0 + (row as f64) * self.vspace_between,
25.0,
0.0,
2.0 * std::f64::consts::PI,
);
let _ = context.fill();
self.draw_stone(context, row, col);
}
fn ghost_stone(&self, context: &cairo::Context, row: u8, col: u8, color: Color) {
@ -273,10 +283,15 @@ impl Pen {
Color::White => context.set_source_rgba(0.9, 0.9, 0.9, 0.5),
Color::Black => context.set_source_rgba(0.0, 0.0, 0.0, 0.5),
};
self.draw_stone(context, row, col);
}
fn draw_stone(&self, context: &cairo::Context, row: u8, col: u8) {
let radius = self.hspace_between / 2. - 2.;
context.arc(
20.0 + (col as f64) * self.hspace_between,
20.0 + (row as f64) * self.vspace_between,
25.0,
self.x_offset + (col as f64) * self.hspace_between,
self.y_offset + (row as f64) * self.vspace_between,
radius,
0.0,
2.0 * std::f64::consts::PI,
);