Start on the new Goban component

This commit is contained in:
Savanni D'Gerinel 2024-03-23 14:41:50 -04:00
parent d964ab0d2f
commit 894575b0fb
6 changed files with 118 additions and 15 deletions

View File

@ -21,7 +21,7 @@ use otg_core::{
settings::{SettingsRequest, SettingsResponse},
Config, CoreRequest, CoreResponse,
};
use sgf::Game;
use sgf::GameRecord;
use std::sync::{Arc, RwLock};
use crate::views::{GameReview, HomeView, SettingsView};
@ -84,7 +84,7 @@ impl AppWindow {
s
}
pub fn open_game_review(&self, _game: Game) {
pub fn open_game_review(&self, _game: GameRecord) {
let header = adw::HeaderBar::new();
let game_review = GameReview::new(self.core.clone());

View File

@ -0,0 +1,84 @@
/*
Copyright 2024, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of On the Grid.
On the Grid is free software: you can redistribute it and/or modify it under the terms of
the GNU General Public License as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
On the Grid is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with On the Grid. If not, see <https://www.gnu.org/licenses/>.
*/
// I have an old Board class which I'm going to update. I'll just copy over the rendering code, but
// at the same time I am going to work pretty heavily on the API.
//
// For a game review, the board needs to interact very well with a game record. So I have to keep
// in mind all of that as I work on the API.
//
// Also, this is going to be a cross-platform application. Today it is Gnome + Rust, but as I
// progress I will also need a Progressive Web App so that I can run this on my tablet. Especially
// useful if I'm out socializing and happen to be talking to somebody who would enjoy a relaxing
// game. Anyway, that is going to impact some of my API decisions.
//
// First, though, I need to rename my game record.
//
// Now, let's get the existing code compiling again.
//
// 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.
// Now, we know what kind of object we have for the current board representation. Let's make use of
// that.
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use std::{rc::Rc, cell::RefCell};
// Internal representation of the Goban drawing area.
#[derive(Default)]
pub struct GobanPrivate {
board_state: Rc<RefCell<otg_core::Goban>>,
}
#[glib::object_subclass]
impl ObjectSubclass for GobanPrivate {
const NAME: &'static str = "Goban";
type Type = Goban;
type ParentType = gtk::DrawingArea;
}
impl ObjectImpl for GobanPrivate {}
impl WidgetImpl for GobanPrivate {}
impl DrawingAreaImpl for GobanPrivate {}
/// This Goban, being in the `components` crate, is merely the rendering of a board. This is not
/// the primary representation of the board.
///
/// 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.
///
/// - 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)
/// - We can never play a suicidal move
///
/// 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.
glib::wrapper! {
pub struct Goban(ObjectSubclass<GobanPrivate>) @extends gtk::Widget, gtk::DrawingArea;
}
impl Goban {
pub fn new(board_state: otg_core::Goban) -> Self {
let s: Self = Object::new();
*s.imp().board_state.borrow_mut() = board_state;
s
}
}

View File

@ -2,12 +2,12 @@ use adw::{prelude::*, subclass::prelude::*};
use glib::Object;
use gtk::glib;
// use otg_core::ui::GamePreviewElement;
use sgf::Game;
use sgf::GameRecord;
use std::{cell::RefCell, rc::Rc};
#[derive(Default)]
pub struct GameObjectPrivate {
game: Rc<RefCell<Option<Game>>>,
game: Rc<RefCell<Option<GameRecord>>>,
}
#[glib::object_subclass]
@ -23,13 +23,13 @@ glib::wrapper! {
}
impl GameObject {
pub fn new(game: Game) -> Self {
pub fn new(game: GameRecord) -> Self {
let s: Self = Object::builder().build();
*s.imp().game.borrow_mut() = Some(game);
s
}
pub fn game(&self) -> Option<Game> {
pub fn game(&self) -> Option<GameRecord> {
self.imp().game.borrow().clone()
}
}
@ -54,7 +54,7 @@ impl Default for LibraryPrivate {
fn make_factory<F>(bind: F) -> gtk::SignalListItemFactory
where
F: Fn(Game) -> String + 'static,
F: Fn(GameRecord) -> String + 'static,
{
let factory = gtk::SignalListItemFactory::new();
factory.connect_setup(|_, list_item| {
@ -155,7 +155,7 @@ impl Default for Library {
}
impl Library {
pub fn new(on_select: impl Fn(Game) + 'static) -> Library {
pub fn new(on_select: impl Fn(GameRecord) + 'static) -> Library {
let s = Library::default();
s.imp().list_view.connect_activate({
@ -177,7 +177,7 @@ impl Library {
s
}
pub fn set_games(&self, games: Vec<Game>) {
pub fn set_games(&self, games: Vec<GameRecord>) {
let games = games
.into_iter()
.map(GameObject::new)

View File

@ -1,5 +1,21 @@
mod board;
pub use board::Board;
/*
Copyright 2024, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of On the Grid.
On the Grid is free software: you can redistribute it and/or modify it under the terms of
the GNU General Public License as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
On the Grid is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with On the Grid. If not, see <https://www.gnu.org/licenses/>.
*/
// mod board;
// pub use board::Board;
// mod chat;
// pub use chat::Chat;
@ -10,6 +26,9 @@ pub use board::Board;
// mod game_preview;
// pub use game_preview::GamePreview;
mod goban;
pub use goban::Goban;
mod library;
pub use library::Library;

View File

@ -24,7 +24,7 @@ You should have received a copy of the GNU General Public License along with On
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use crate::{components::Board, CoreApi};
use crate::{components::Goban, CoreApi};
pub struct GameReviewPrivate {}
@ -53,7 +53,7 @@ impl GameReview {
pub fn new(api: CoreApi) -> Self {
let s: Self = Object::builder().build();
let board = Board::new(api);
let board = Goban::new(otg_core::Goban::default());
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("black player")), 0, 2, 1, 2);

View File

@ -21,7 +21,7 @@ use otg_core::{
library::{LibraryRequest, LibraryResponse},
CoreRequest, CoreResponse,
};
use sgf::Game;
use sgf::GameRecord;
use std::{cell::RefCell, rc::Rc};
/*
@ -131,7 +131,7 @@ glib::wrapper! {
}
impl HomeView {
pub fn new(api: CoreApi, on_select_game: impl Fn(Game) + 'static) -> Self {
pub fn new(api: CoreApi, on_select_game: impl Fn(GameRecord) + 'static) -> Self {
let s: Self = Object::builder().build();
s.set_spacing(4);
s.set_homogeneous(false);