From 5441a3c4417687aad3451a1848372697c60ae94d Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 30 Apr 2024 20:31:17 -0400 Subject: [PATCH] Adapt the app to the new slab tree --- otg/core/src/lib.rs | 5 +++-- otg/core/src/types.rs | 25 +++++++++++-------------- otg/gtk/src/components/review_tree.rs | 12 +++++++----- otg/gtk/src/views/game_review.rs | 7 ++++--- sgf/src/game.rs | 1 - 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/otg/core/src/lib.rs b/otg/core/src/lib.rs index 5fde824..5964ce9 100644 --- a/otg/core/src/lib.rs +++ b/otg/core/src/lib.rs @@ -29,5 +29,6 @@ pub mod library; pub mod settings; mod types; -pub use types::{BoardError, Color, Config, ConfigOption, LibraryPath, Player, Rank, Size}; - +pub use types::{ + BoardError, Color, Config, ConfigOption, DepthTree, LibraryPath, Player, Rank, Size, +}; diff --git a/otg/core/src/types.rs b/otg/core/src/types.rs index fb1c364..c48cc7f 100644 --- a/otg/core/src/types.rs +++ b/otg/core/src/types.rs @@ -242,7 +242,7 @@ pub struct Tree { } */ -struct DepthTree(slab_tree::Tree); +pub struct DepthTree(slab_tree::Tree); impl Deref for DepthTree { type Target = slab_tree::Tree; @@ -393,13 +393,13 @@ impl DepthTree { width } + */ - pub fn bfs_iter(&self) -> BFSIter { + pub fn bfs_iter(&self) -> BFSIter<'_, SizeNode> { let mut queue = VecDeque::new(); - queue.push_back(&self.nodes[0]); + queue.push_back(self.0.root().unwrap()); BFSIter { tree: self, queue } } - */ } impl<'a> From<&'a GameTree> for DepthTree { @@ -520,27 +520,24 @@ impl<'a> From<&'a GameNode> for Tree { } */ -/* pub struct BFSIter<'a, T> { - tree: &'a Tree, - queue: VecDeque<&'a Node>, + tree: &'a DepthTree, + queue: VecDeque>, } impl<'a, T> Iterator for BFSIter<'a, T> { - type Item = &'a Node; + type Item = &'a T; fn next(&mut self) -> Option { let retval = self.queue.pop_front(); - if let Some(retval) = retval { + if let Some(ref retval) = retval { retval - .children - .iter() - .for_each(|idx| self.queue.push_back(&self.tree.nodes[*idx])); + .children() + .for_each(|noderef| self.queue.push_back(noderef)); } - retval + retval.map(|retval| retval.data()) } } -*/ #[cfg(test)] mod test { diff --git a/otg/gtk/src/components/review_tree.rs b/otg/gtk/src/components/review_tree.rs index f759a34..c2297ff 100644 --- a/otg/gtk/src/components/review_tree.rs +++ b/otg/gtk/src/components/review_tree.rs @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with On use cairo::Context; use glib::Object; use gtk::{prelude::*, subclass::prelude::*}; -use otg_core::Tree; +use otg_core::DepthTree; use sgf::GameRecord; use std::{cell::RefCell, rc::Rc}; use uuid::Uuid; @@ -28,7 +28,7 @@ const HEIGHT: i32 = 800; #[derive(Default)] pub struct ReviewTreePrivate { record: Rc>>, - tree: Rc>>>, + tree: Rc>>, } #[glib::object_subclass] @@ -50,7 +50,9 @@ impl ReviewTree { pub fn new(record: GameRecord) -> Self { let s: Self = Object::new(); - *s.imp().tree.borrow_mut() = Some(Tree::from(&record.children[0])); + // TODO: there can be more than one tree, especially in instructional files. Either unify + // them into a single tree in the GameTree, or draw all of them here. + *s.imp().tree.borrow_mut() = Some(DepthTree::from(&record.trees[0])); *s.imp().record.borrow_mut() = Some(record); s.set_width_request(WIDTH); @@ -67,7 +69,7 @@ impl ReviewTree { } pub fn redraw(&self, ctx: &Context, _width: i32, _height: i32) { - let tree: &Option> = &self.imp().tree.borrow(); + let tree: &Option = &self.imp().tree.borrow(); match tree { Some(ref tree) => { for node in tree.bfs_iter() { @@ -76,7 +78,7 @@ impl ReviewTree { // the parent? do I need to just make it more intrinsically a part of the position // code? ctx.set_source_rgb(0.7, 0.7, 0.7); - let (row, column) = tree.position(node.id); + let (row, column) = node.position(); let y = (row as f64) * 20. + 10.; let x = (column as f64) * 20. + 10.; ctx.arc(x, y, 5., 0., 2. * std::f64::consts::PI); diff --git a/otg/gtk/src/views/game_review.rs b/otg/gtk/src/views/game_review.rs index 721c5d9..101c53c 100644 --- a/otg/gtk/src/views/game_review.rs +++ b/otg/gtk/src/views/game_review.rs @@ -55,9 +55,10 @@ impl GameReview { // It's actually really bad to be just throwing away errors. Panics make everyone unhappy. // This is not a fatal error, so I'll replace this `unwrap` call with something that // renders the board and notifies the user of a problem that cannot be resolved. - let board_repr = otg_core::Goban::default() - .apply_moves(record.mainline()) - .unwrap(); + let board_repr = match record.mainline() { + Some(iter) => otg_core::Goban::default().apply_moves(iter).unwrap(), + None => otg_core::Goban::default(), + }; let board = Goban::new(board_repr, resources); /* diff --git a/sgf/src/game.rs b/sgf/src/game.rs index 5d64737..ae11f54 100644 --- a/sgf/src/game.rs +++ b/sgf/src/game.rs @@ -136,7 +136,6 @@ impl GameRecord { /// was actually played out, and by convention consists of the first node in each list of /// children. pub fn mainline(&self) -> Option> { - println!("number of trees: {}", self.trees.len()); if !self.trees.is_empty() { Some(MainlineIter { next: self.trees[0].root(),