unhide() + better spring margins

This commit is contained in:
mars 2022-11-03 18:00:10 -06:00
parent 420fc2089a
commit 647b6ec59a
1 changed files with 26 additions and 1 deletions

View File

@ -46,6 +46,7 @@ pub struct NodeInfo {
pub label_offset: Vec2,
pub text_size: f32,
pub hidden: bool,
pub unhide: Vec<usize>,
}
impl NodeInfo {
@ -60,6 +61,7 @@ impl NodeInfo {
label_offset,
text_size,
hidden: false,
unhide: Vec::new(),
}
}
}
@ -160,6 +162,7 @@ impl MenuNode {
let parent = graph.nodes.len();
graph.nodes.push(root);
self.build_force_graph(&mut graph, parent);
graph.unhide(parent);
graph
}
@ -175,7 +178,10 @@ impl MenuNode {
position: Vec2::from_angle(id as f32),
..Default::default()
},
info: NodeInfo::new(&child.label),
info: NodeInfo {
hidden: true,
..NodeInfo::new(&child.label)
},
});
graph.springs.push(Spring {
@ -207,6 +213,8 @@ impl MenuNode {
child.build_force_graph(graph, id);
}
graph.nodes[parent].info.unhide.extend_from_slice(&siblings);
}
}
@ -233,6 +241,10 @@ impl Spring {
} else {
displacement += self.margin;
}*/
if displacement.abs() < self.margin {
displacement *= displacement.abs() / self.margin;
}
let displacement = displacement.clamp(-MAX_DISPLACE, MAX_DISPLACE);
let force = displacement * self.stiffness * delta.normalize_or_zero();
@ -292,6 +304,19 @@ impl ForceGraph {
}
}
pub fn unhide(&mut self, node: usize) {
let node = &mut self.nodes[node];
node.info.hidden = false;
let position = node.body.position;
let children = node.info.unhide.to_owned();
for child in children {
let child = &mut self.nodes[child];
child.info.hidden = false;
child.body.position = position;
}
}
pub fn draw(&self, ctx: &DrawContext) {
let radius = 5.0;
let thickness = 1.0;