2023-03-24 14:14:01 +00:00
|
|
|
use glib::Object;
|
|
|
|
use gtk::{prelude::*, subclass::prelude::*};
|
2023-03-25 13:00:56 +00:00
|
|
|
use kifu_core::{ui::GobanElement, Color};
|
|
|
|
use std::{cell::RefCell, rc::Rc};
|
|
|
|
|
|
|
|
const WIDTH: i32 = 800;
|
|
|
|
const HEIGHT: i32 = 800;
|
2023-03-24 14:14:01 +00:00
|
|
|
|
|
|
|
#[derive(Default)]
|
2023-03-25 13:00:56 +00:00
|
|
|
pub struct GobanPrivate {
|
|
|
|
drawing_area: gtk::DrawingArea,
|
|
|
|
|
|
|
|
goban: Rc<RefCell<GobanElement>>,
|
|
|
|
}
|
2023-03-24 14:14:01 +00:00
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
impl ObjectSubclass for GobanPrivate {
|
|
|
|
const NAME: &'static str = "Goban";
|
|
|
|
type Type = Goban;
|
2023-03-25 13:00:56 +00:00
|
|
|
type ParentType = gtk::Grid;
|
2023-03-24 14:14:01 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 13:00:56 +00:00
|
|
|
impl ObjectImpl for GobanPrivate {
|
|
|
|
fn constructed(&self) {
|
|
|
|
self.drawing_area.set_width_request(WIDTH);
|
|
|
|
self.drawing_area.set_height_request(HEIGHT);
|
|
|
|
|
|
|
|
let goban = self.goban.clone();
|
|
|
|
self.drawing_area
|
|
|
|
.set_draw_func(move |_, context, width, height| {
|
|
|
|
let goban = goban.borrow();
|
|
|
|
context.set_source_rgb(0.7, 0.7, 0.7);
|
|
|
|
let _ = context.paint();
|
|
|
|
|
|
|
|
context.set_source_rgb(0.1, 0.1, 0.1);
|
|
|
|
context.set_line_width(2.);
|
|
|
|
let hspace_between = ((width - 40) as f64) / ((goban.size.width - 1) as f64);
|
|
|
|
let vspace_between = ((height - 40) as f64) / ((goban.size.height - 1) as f64);
|
|
|
|
|
|
|
|
let pen = Pen {
|
|
|
|
x_offset: 20.,
|
|
|
|
y_offset: 20.,
|
|
|
|
hspace_between,
|
|
|
|
vspace_between,
|
|
|
|
};
|
|
|
|
|
|
|
|
(0..goban.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);
|
|
|
|
let _ = context.stroke();
|
|
|
|
});
|
|
|
|
(0..goban.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);
|
|
|
|
let _ = context.stroke();
|
|
|
|
});
|
|
|
|
|
|
|
|
context.set_source_rgb(0.1, 0.1, 0.0);
|
2023-03-25 13:24:36 +00:00
|
|
|
vec![3, 9, 15].into_iter().for_each(|col| {
|
|
|
|
vec![3, 9, 15].into_iter().for_each(|row| {
|
2023-03-25 13:00:56 +00:00
|
|
|
pen.star_point(context, col, row);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
(0..19).for_each(|col| {
|
|
|
|
(0..19).for_each(|row| {
|
|
|
|
match goban.stone(row, col) {
|
|
|
|
None => {}
|
|
|
|
Some(element) => {
|
|
|
|
pen.stone(&context, row, col, element.color);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 14:14:01 +00:00
|
|
|
impl WidgetImpl for GobanPrivate {}
|
2023-03-25 13:00:56 +00:00
|
|
|
impl GridImpl for GobanPrivate {}
|
2023-03-24 14:14:01 +00:00
|
|
|
|
|
|
|
glib::wrapper! {
|
2023-03-25 13:00:56 +00:00
|
|
|
pub struct Goban(ObjectSubclass<GobanPrivate>) @extends gtk::Grid, gtk::Widget;
|
2023-03-24 14:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Goban {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let s: Self = Object::builder().build();
|
2023-03-25 13:00:56 +00:00
|
|
|
s.attach(&s.imp().drawing_area, 1, 1, 1, 1);
|
2023-03-24 14:14:01 +00:00
|
|
|
s
|
|
|
|
}
|
2023-03-25 13:00:56 +00:00
|
|
|
|
|
|
|
pub fn set_board(&self, goban: GobanElement) {
|
|
|
|
*self.imp().goban.borrow_mut() = goban;
|
|
|
|
self.imp().drawing_area.queue_draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Pen {
|
|
|
|
x_offset: f64,
|
|
|
|
y_offset: f64,
|
|
|
|
hspace_between: f64,
|
|
|
|
vspace_between: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Pen {
|
|
|
|
fn star_point(&self, context: &cairo::Context, row: u8, col: u8) {
|
|
|
|
context.arc(
|
|
|
|
self.x_offset + (col as f64) * self.hspace_between,
|
|
|
|
self.y_offset + (row as f64) * self.vspace_between,
|
|
|
|
10.,
|
|
|
|
0.,
|
|
|
|
2. * std::f64::consts::PI,
|
|
|
|
);
|
|
|
|
let _ = context.fill();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stone(&self, context: &cairo::Context, row: u8, col: u8, color: Color) {
|
|
|
|
match color {
|
|
|
|
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();
|
|
|
|
}
|
2023-03-24 14:14:01 +00:00
|
|
|
}
|