2023-06-15 03:47:12 +00:00
|
|
|
use crate::{perftrace, CoreApi};
|
2023-03-31 02:25:11 +00:00
|
|
|
use gio::resources_lookup_data;
|
2023-03-24 14:14:01 +00:00
|
|
|
use glib::Object;
|
2023-03-31 02:25:11 +00:00
|
|
|
use gtk::{
|
|
|
|
gdk_pixbuf::{InterpType, Pixbuf},
|
|
|
|
prelude::*,
|
|
|
|
subclass::prelude::*,
|
|
|
|
};
|
|
|
|
use image::io::Reader as ImageReader;
|
2023-04-05 13:13:50 +00:00
|
|
|
use kifu_core::{
|
|
|
|
ui::{BoardElement, IntersectionElement},
|
|
|
|
Color,
|
|
|
|
};
|
2023-03-31 02:25:11 +00:00
|
|
|
use std::{cell::RefCell, io::Cursor, rc::Rc};
|
2023-03-25 13:00:56 +00:00
|
|
|
|
|
|
|
const WIDTH: i32 = 800;
|
|
|
|
const HEIGHT: i32 = 800;
|
2023-04-07 03:26:46 +00:00
|
|
|
const MARGIN: i32 = 20;
|
2023-03-24 14:14:01 +00:00
|
|
|
|
2023-03-31 01:01:48 +00:00
|
|
|
#[derive(Clone, Default, PartialEq)]
|
|
|
|
struct Addr {
|
|
|
|
row: u8,
|
|
|
|
column: u8,
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:13:50 +00:00
|
|
|
pub struct BoardPrivate {
|
2023-03-25 13:00:56 +00:00
|
|
|
drawing_area: gtk::DrawingArea,
|
|
|
|
|
2023-03-31 01:01:48 +00:00
|
|
|
current_player: Rc<RefCell<Color>>,
|
2023-04-05 13:13:50 +00:00
|
|
|
board: Rc<RefCell<BoardElement>>,
|
2023-04-07 02:29:15 +00:00
|
|
|
cursor_location: Rc<RefCell<Option<Addr>>>,
|
2023-04-07 01:52:39 +00:00
|
|
|
|
|
|
|
api: Rc<RefCell<Option<CoreApi>>>,
|
2023-03-25 13:00:56 +00:00
|
|
|
}
|
2023-03-24 14:14:01 +00:00
|
|
|
|
|
|
|
#[glib::object_subclass]
|
2023-04-05 13:13:50 +00:00
|
|
|
impl ObjectSubclass for BoardPrivate {
|
|
|
|
const NAME: &'static str = "Board";
|
|
|
|
type Type = Board;
|
2023-03-25 13:00:56 +00:00
|
|
|
type ParentType = gtk::Grid;
|
2023-03-31 01:01:48 +00:00
|
|
|
|
2023-04-05 13:13:50 +00:00
|
|
|
fn new() -> BoardPrivate {
|
|
|
|
BoardPrivate {
|
2023-03-31 01:01:48 +00:00
|
|
|
drawing_area: Default::default(),
|
|
|
|
current_player: Rc::new(RefCell::new(Color::Black)),
|
2023-04-05 13:13:50 +00:00
|
|
|
board: Default::default(),
|
2023-03-31 01:01:48 +00:00
|
|
|
cursor_location: Default::default(),
|
2023-04-07 01:52:39 +00:00
|
|
|
api: Default::default(),
|
2023-03-31 01:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-24 14:14:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:13:50 +00:00
|
|
|
impl ObjectImpl for BoardPrivate {
|
2023-03-25 13:00:56 +00:00
|
|
|
fn constructed(&self) {
|
|
|
|
self.drawing_area.set_width_request(WIDTH);
|
|
|
|
self.drawing_area.set_height_request(HEIGHT);
|
|
|
|
|
2023-04-05 13:13:50 +00:00
|
|
|
let board = self.board.clone();
|
2023-03-31 01:01:48 +00:00
|
|
|
let cursor_location = self.cursor_location.clone();
|
|
|
|
let current_player = self.current_player.clone();
|
2023-03-31 02:25:11 +00:00
|
|
|
|
|
|
|
let wood_texture = resources_lookup_data(
|
|
|
|
"/com/luminescent-dreams/kifu-gtk/wood_texture.jpg",
|
|
|
|
gio::ResourceLookupFlags::NONE,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let background = ImageReader::new(Cursor::new(wood_texture))
|
|
|
|
.with_guessed_format()
|
|
|
|
.unwrap()
|
|
|
|
.decode();
|
|
|
|
let background = background.map(|background| {
|
|
|
|
Pixbuf::from_bytes(
|
|
|
|
&glib::Bytes::from(background.as_bytes()),
|
|
|
|
gtk::gdk_pixbuf::Colorspace::Rgb,
|
|
|
|
false,
|
|
|
|
8,
|
|
|
|
background.width() as i32,
|
|
|
|
background.height() as i32,
|
|
|
|
background.to_rgb8().sample_layout().height_stride as i32,
|
|
|
|
)
|
|
|
|
.scale_simple(WIDTH, HEIGHT, InterpType::Nearest)
|
|
|
|
});
|
|
|
|
|
2023-03-25 13:00:56 +00:00
|
|
|
self.drawing_area
|
|
|
|
.set_draw_func(move |_, context, width, height| {
|
2023-06-15 03:47:12 +00:00
|
|
|
perftrace("render drawing area", || {
|
|
|
|
let render_start = std::time::Instant::now();
|
|
|
|
let board = board.borrow();
|
|
|
|
|
|
|
|
match background {
|
|
|
|
Ok(Some(ref background)) => {
|
2023-10-05 16:19:57 +00:00
|
|
|
context.set_source_pixbuf(background, 0., 0.);
|
2023-06-15 03:47:12 +00:00
|
|
|
context.paint().expect("paint should succeed");
|
|
|
|
}
|
|
|
|
Ok(None) | Err(_) => 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) / ((board.size.width - 1) as f64);
|
|
|
|
let vspace_between = ((height - 40) as f64) / ((board.size.height - 1) as f64);
|
|
|
|
|
|
|
|
let pen = Pen {
|
|
|
|
x_offset: MARGIN as f64,
|
|
|
|
y_offset: MARGIN as f64,
|
|
|
|
hspace_between,
|
|
|
|
vspace_between,
|
|
|
|
};
|
|
|
|
|
|
|
|
(0..board.size.width).for_each(|col| {
|
|
|
|
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(
|
|
|
|
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();
|
|
|
|
});
|
2023-04-07 03:26:46 +00:00
|
|
|
|
2023-06-15 03:47:12 +00:00
|
|
|
context.set_source_rgb(0.1, 0.1, 0.0);
|
|
|
|
vec![3, 9, 15].into_iter().for_each(|col| {
|
|
|
|
vec![3, 9, 15].into_iter().for_each(|row| {
|
|
|
|
pen.star_point(context, col, row);
|
|
|
|
});
|
2023-03-25 13:00:56 +00:00
|
|
|
});
|
2023-06-15 03:47:12 +00:00
|
|
|
|
|
|
|
(0..19).for_each(|col| {
|
|
|
|
(0..19).for_each(|row| {
|
2023-10-05 16:19:57 +00:00
|
|
|
if let IntersectionElement::Filled(stone) = board.stone(row, col) {
|
|
|
|
pen.stone(context, row, col, stone.color, stone.liberties);
|
2023-06-15 03:47:12 +00:00
|
|
|
};
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let cursor = cursor_location.borrow();
|
|
|
|
match *cursor {
|
|
|
|
None => {}
|
2023-10-05 16:19:57 +00:00
|
|
|
Some(ref cursor) => {
|
|
|
|
if let IntersectionElement::Empty(_) =
|
|
|
|
board.stone(cursor.row, cursor.column)
|
|
|
|
{
|
|
|
|
pen.ghost_stone(
|
|
|
|
context,
|
|
|
|
cursor.row,
|
|
|
|
cursor.column,
|
|
|
|
*current_player.borrow(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-06-15 03:47:12 +00:00
|
|
|
}
|
|
|
|
let render_end = std::time::Instant::now();
|
|
|
|
println!("board rendering time: {:?}", render_end - render_start);
|
|
|
|
})
|
2023-03-25 13:00:56 +00:00
|
|
|
});
|
2023-03-31 01:01:48 +00:00
|
|
|
|
|
|
|
let motion_controller = gtk::EventControllerMotion::new();
|
|
|
|
{
|
2023-04-05 13:13:50 +00:00
|
|
|
let board = self.board.clone();
|
2023-03-31 01:01:48 +00:00
|
|
|
let cursor = self.cursor_location.clone();
|
|
|
|
let drawing_area = self.drawing_area.clone();
|
|
|
|
motion_controller.connect_motion(move |_, x, y| {
|
2023-04-05 13:13:50 +00:00
|
|
|
let board = board.borrow();
|
2023-03-31 01:01:48 +00:00
|
|
|
let mut cursor = cursor.borrow_mut();
|
2023-04-05 13:13:50 +00:00
|
|
|
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);
|
2023-03-31 01:01:48 +00:00
|
|
|
|
2023-04-07 03:26:46 +00:00
|
|
|
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() - MARGIN as f64) / hspace_between).round() as u8,
|
|
|
|
row: ((y.round() - MARGIN as f64) / vspace_between).round() as u8,
|
|
|
|
})
|
|
|
|
};
|
2023-04-07 02:29:15 +00:00
|
|
|
|
|
|
|
if *cursor != addr.clone() {
|
2023-03-31 01:01:48 +00:00
|
|
|
*cursor = addr;
|
|
|
|
drawing_area.queue_draw();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:48:33 +00:00
|
|
|
let gesture = gtk::GestureClick::new();
|
|
|
|
{
|
|
|
|
let board = self.board.clone();
|
|
|
|
let cursor = self.cursor_location.clone();
|
2023-04-07 01:52:39 +00:00
|
|
|
let api = self.api.clone();
|
2023-04-05 13:48:33 +00:00
|
|
|
gesture.connect_released(move |_, _, _, _| {
|
|
|
|
let board = board.borrow();
|
|
|
|
let cursor = cursor.borrow();
|
2023-04-07 02:29:15 +00:00
|
|
|
match *cursor {
|
|
|
|
None => {}
|
2023-10-05 16:19:57 +00:00
|
|
|
Some(ref cursor) => {
|
|
|
|
if let IntersectionElement::Empty(request) =
|
|
|
|
board.stone(cursor.row, cursor.column)
|
|
|
|
{
|
2023-04-07 02:29:15 +00:00
|
|
|
println!("need to send request: {:?}", request);
|
|
|
|
api.borrow()
|
|
|
|
.as_ref()
|
|
|
|
.expect("API must exist")
|
|
|
|
.dispatch(request);
|
|
|
|
}
|
2023-10-05 16:19:57 +00:00
|
|
|
}
|
2023-04-05 13:48:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-31 01:01:48 +00:00
|
|
|
self.drawing_area.add_controller(motion_controller);
|
2023-04-05 13:48:33 +00:00
|
|
|
self.drawing_area.add_controller(gesture);
|
2023-03-25 13:00:56 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-05 13:13:50 +00:00
|
|
|
impl WidgetImpl for BoardPrivate {}
|
|
|
|
impl GridImpl for BoardPrivate {}
|
2023-03-24 14:14:01 +00:00
|
|
|
|
|
|
|
glib::wrapper! {
|
2023-04-05 13:13:50 +00:00
|
|
|
pub struct Board(ObjectSubclass<BoardPrivate>) @extends gtk::Grid, gtk::Widget;
|
2023-03-24 14:14:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:13:50 +00:00
|
|
|
impl Board {
|
2023-04-07 01:52:39 +00:00
|
|
|
pub fn new(api: CoreApi) -> Self {
|
2023-03-24 14:14:01 +00:00
|
|
|
let s: Self = Object::builder().build();
|
2023-04-07 01:52:39 +00:00
|
|
|
|
|
|
|
*s.imp().api.borrow_mut() = Some(api);
|
2023-03-25 13:00:56 +00:00
|
|
|
s.attach(&s.imp().drawing_area, 1, 1, 1, 1);
|
2023-04-07 01:52:39 +00:00
|
|
|
|
2023-03-24 14:14:01 +00:00
|
|
|
s
|
|
|
|
}
|
2023-03-25 13:00:56 +00:00
|
|
|
|
2023-04-05 13:13:50 +00:00
|
|
|
pub fn set_board(&self, board: BoardElement) {
|
|
|
|
*self.imp().board.borrow_mut() = board;
|
2023-03-25 13:00:56 +00:00
|
|
|
self.imp().drawing_area.queue_draw();
|
|
|
|
}
|
2023-03-31 01:01:48 +00:00
|
|
|
|
|
|
|
pub fn set_current_player(&self, color: Color) {
|
|
|
|
*self.imp().current_player.borrow_mut() = color;
|
|
|
|
}
|
2023-03-25 13:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-04-07 03:26:46 +00:00
|
|
|
5.,
|
2023-03-25 13:00:56 +00:00
|
|
|
0.,
|
|
|
|
2. * std::f64::consts::PI,
|
|
|
|
);
|
|
|
|
let _ = context.fill();
|
|
|
|
}
|
|
|
|
|
2023-04-27 22:56:59 +00:00
|
|
|
fn stone(
|
|
|
|
&self,
|
|
|
|
context: &cairo::Context,
|
|
|
|
row: u8,
|
|
|
|
col: u8,
|
|
|
|
color: Color,
|
|
|
|
liberties: Option<u8>,
|
|
|
|
) {
|
2023-03-25 13:00:56 +00:00
|
|
|
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),
|
|
|
|
};
|
2023-04-07 03:26:46 +00:00
|
|
|
self.draw_stone(context, row, col);
|
2023-04-27 22:56:59 +00:00
|
|
|
|
|
|
|
if let Some(liberties) = liberties {
|
|
|
|
let stone_location = self.stone_location(row, col);
|
|
|
|
context.set_source_rgb(1., 0., 1.);
|
|
|
|
context.set_font_size(32.);
|
|
|
|
context.move_to(stone_location.0 - 10., stone_location.1 + 10.);
|
|
|
|
let _ = context.show_text(&format!("{}", liberties));
|
|
|
|
}
|
2023-03-25 13:00:56 +00:00
|
|
|
}
|
2023-03-31 01:01:48 +00:00
|
|
|
|
|
|
|
fn ghost_stone(&self, context: &cairo::Context, row: u8, col: u8, color: Color) {
|
|
|
|
match color {
|
|
|
|
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),
|
|
|
|
};
|
2023-04-07 03:26:46 +00:00
|
|
|
self.draw_stone(context, row, col);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_stone(&self, context: &cairo::Context, row: u8, col: u8) {
|
|
|
|
let radius = self.hspace_between / 2. - 2.;
|
2023-04-27 22:56:59 +00:00
|
|
|
let (x_loc, y_loc) = self.stone_location(row, col);
|
|
|
|
context.arc(x_loc, y_loc, radius, 0.0, 2.0 * std::f64::consts::PI);
|
|
|
|
let _ = context.fill();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stone_location(&self, row: u8, col: u8) -> (f64, f64) {
|
|
|
|
(
|
2023-04-07 03:26:46 +00:00
|
|
|
self.x_offset + (col as f64) * self.hspace_between,
|
|
|
|
self.y_offset + (row as f64) * self.vspace_between,
|
2023-04-27 22:56:59 +00:00
|
|
|
)
|
2023-03-31 01:01:48 +00:00
|
|
|
}
|
2023-03-24 14:14:01 +00:00
|
|
|
}
|