Adjust margins and the size of the stones

This commit is contained in:
Savanni D'Gerinel 2023-04-06 23:26:46 -04:00
parent 26db170c79
commit 4590bc6688
1 changed files with 42 additions and 27 deletions

View File

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