Set up the game review page along with #229

Merged
savanni merged 24 commits from otg/game-review into main 2024-03-31 23:37:51 +00:00
2 changed files with 84 additions and 20 deletions
Showing only changes of commit 556f91b70b - Show all commits

View File

@ -32,13 +32,23 @@ You should have received a copy of the GNU General Public License along with On
// Okay, that wasn't so bad. I'm a little confused that I don't have a code action for renaming a // Okay, that wasn't so bad. I'm a little confused that I don't have a code action for renaming a
// symbol, but I'll fix that some other time. Anyway, now let's focus on the goban. // symbol, but I'll fix that some other time. Anyway, now let's focus on the goban.
// Now, we know what kind of object we have for the current board representation. Let's make use of // Now, we know what kind of object we have for the current board representation. Let's make use of
// that. // that.
use crate::perftrace;
use gio::resources_lookup_data;
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{
use std::{rc::Rc, cell::RefCell}; gdk_pixbuf::{InterpType, Pixbuf},
prelude::*,
subclass::prelude::*,
};
use image::io::Reader as ImageReader;
use std::{cell::RefCell, io::Cursor, rc::Rc};
const WIDTH: i32 = 800;
const HEIGHT: i32 = 800;
const MARGIN: i32 = 20;
// Internal representation of the Goban drawing area. // Internal representation of the Goban drawing area.
#[derive(Default)] #[derive(Default)]
@ -46,6 +56,8 @@ pub struct GobanPrivate {
board_state: Rc<RefCell<otg_core::Goban>>, board_state: Rc<RefCell<otg_core::Goban>>,
} }
impl GobanPrivate {}
#[glib::object_subclass] #[glib::object_subclass]
impl ObjectSubclass for GobanPrivate { impl ObjectSubclass for GobanPrivate {
const NAME: &'static str = "Goban"; const NAME: &'static str = "Goban";
@ -57,28 +69,76 @@ impl ObjectImpl for GobanPrivate {}
impl WidgetImpl for GobanPrivate {} impl WidgetImpl for GobanPrivate {}
impl DrawingAreaImpl for GobanPrivate {} impl DrawingAreaImpl for GobanPrivate {}
/// This Goban, being in the `components` crate, is merely the rendering of a board. This is not // This Goban, being in the `components` crate, is merely the rendering of a board. This is not
/// the primary representation of the board. // the primary representation of the board.
/// //
/// In a game of Go, there are certain rules about what are valid moves and what are not. // In a game of Go, there are certain rules about what are valid moves and what are not.
/// Internally, I want to keep track of those, and doing so requires a few things. // Internally, I want to keep track of those, and doing so requires a few things.
/// //
/// - We can never repeat a game state (though I think maybe that is allowed in a few rulesets, but // - We can never repeat a game state (though I think maybe that is allowed in a few rulesets, but
/// I'm coding to the AGA ruleset) // I'm coding to the AGA ruleset)
/// - We can never play a suicidal move // - We can never play a suicidal move
/// //
/// Finally, updating the board state is non-GUI logic. So, sorry, might be dropping away from GUI // Finally, updating the board state is non-GUI logic. So, sorry, might be dropping away from GUI
/// code for a while to work on the backend representation, some of which already exists. // code for a while to work on the backend representation, some of which already exists.
glib::wrapper! { glib::wrapper! {
pub struct Goban(ObjectSubclass<GobanPrivate>) @extends gtk::Widget, gtk::DrawingArea; pub struct Goban(ObjectSubclass<GobanPrivate>) @extends gtk::DrawingArea, gtk::Widget;
} }
impl Goban { impl Goban {
pub fn new(board_state: otg_core::Goban) -> Self { pub fn new(board_state: otg_core::Goban) -> Self {
let s: Self = Object::new(); let s: Self = Object::builder().build();
*s.imp().board_state.borrow_mut() = board_state; *s.imp().board_state.borrow_mut() = board_state;
s.set_width_request(WIDTH);
s.set_height_request(HEIGHT);
s.set_draw_func({
let s = s.clone();
move |_, ctx, width, height| {
perftrace("render drawing area", || s.redraw(ctx, width, height));
}
});
s s
} }
fn redraw(&self, ctx: &cairo::Context, width: i32, height: i32) {
println!("{} x {}", width, height);
/*
let wood_texture = resources_lookup_data(
"/com/luminescent-dreams/otg-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)
});
match background {
Ok(Some(ref background)) => {
ctx.set_source_pixbuf(background, 0., 0.);
ctx.paint().expect("paint should never fail");
}
Ok(None) | Err(_) => ctx.set_source_rgb(0.7, 0.7, 0.7),
}
*/
ctx.set_source_rgb(0.7, 0.7, 0.7);
let _ = ctx.paint();
}
} }

View File

@ -38,15 +38,15 @@ impl Default for GameReviewPrivate {
impl ObjectSubclass for GameReviewPrivate { impl ObjectSubclass for GameReviewPrivate {
const NAME: &'static str = "GameReview"; const NAME: &'static str = "GameReview";
type Type = GameReview; type Type = GameReview;
type ParentType = gtk::Grid; type ParentType = gtk::Box;
} }
impl ObjectImpl for GameReviewPrivate {} impl ObjectImpl for GameReviewPrivate {}
impl WidgetImpl for GameReviewPrivate {} impl WidgetImpl for GameReviewPrivate {}
impl GridImpl for GameReviewPrivate {} impl BoxImpl for GameReviewPrivate {}
glib::wrapper! { glib::wrapper! {
pub struct GameReview(ObjectSubclass<GameReviewPrivate>) @extends gtk::Grid, gtk::Widget, @implements gtk::Accessible; pub struct GameReview(ObjectSubclass<GameReviewPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Accessible;
} }
impl GameReview { impl GameReview {
@ -54,10 +54,14 @@ impl GameReview {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
let board = Goban::new(otg_core::Goban::default()); let board = Goban::new(otg_core::Goban::default());
/*
s.attach(&board, 0, 0, 2, 2); s.attach(&board, 0, 0, 2, 2);
s.attach(&gtk::Label::new(Some("white player")), 0, 2, 1, 1); s.attach(&gtk::Label::new(Some("white player")), 0, 2, 1, 1);
s.attach(&gtk::Label::new(Some("black player")), 0, 2, 1, 2); s.attach(&gtk::Label::new(Some("black player")), 0, 2, 1, 2);
s.attach(&gtk::Label::new(Some("chat")), 1, 2, 2, 2); s.attach(&gtk::Label::new(Some("chat")), 1, 2, 2, 2);
*/
s.append(&board);
s s
} }