Improve ident calculation to help with tree drawing

This commit is contained in:
Savanni D'Gerinel 2024-03-31 16:19:09 -04:00
parent b982f2c1cc
commit 5cdcf0499c
2 changed files with 13 additions and 11 deletions

View File

@ -307,11 +307,13 @@ impl<T> Tree<T> {
//
// indent represents the indentation that should be applied to all children in this tree. It
// amounts to the position of the parent node.
pub fn position(&self, indent: usize, idx: usize) -> (usize, usize) {
println!("[{}]", idx);
//
// When drawing nodes, I don't know how to persist the level of indent.
pub fn position(&self, idx: usize) -> (usize, usize) {
let node = &self.nodes[idx];
match node.parent {
Some(parent_idx) => {
let (_parent_row, parent_column) = self.position(parent_idx);
let parent = &self.nodes[parent_idx];
let sibling_width = parent
.children
@ -319,7 +321,7 @@ impl<T> Tree<T> {
.take_while(|n| **n != node.id)
.fold(0, |acc, n| acc + self.width(*n));
println!("[{}] sibling width {}", idx, sibling_width);
(node.depth, indent + sibling_width)
(node.depth, parent_column + sibling_width)
}
// Root nodes won't have a parent, so just put them in the first column
@ -538,13 +540,13 @@ mod test {
let tree = Tree::from(&game_tree);
assert_eq!(tree.position(0, 2), (2, 0));
assert_eq!(tree.position(0, 1), (1, 0));
assert_eq!(tree.position(0, 0), (0, 0));
assert_eq!(tree.position(0, 4), (3, 1));
assert_eq!(tree.position(0, 5), (3, 2));
assert_eq!(tree.position(0, 6), (1, 3));
assert_eq!(tree.position(0, 7), (1, 4));
assert_eq!(tree.position(2), (2, 0));
assert_eq!(tree.position(1), (1, 0));
assert_eq!(tree.position(0), (0, 0));
assert_eq!(tree.position(4), (3, 1));
assert_eq!(tree.position(5), (3, 2));
assert_eq!(tree.position(6), (1, 3));
assert_eq!(tree.position(7), (1, 4));
}
#[test]

View File

@ -77,7 +77,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(0, node.id);
let (row, column) = tree.position(node.id);
println!("[{}] {} x {}", node.id, row, column);
let y = (row as f64) * 20. + 10.;
let x = (column as f64) * 20. + 10.;