Focus last selected node

This commit is contained in:
mars 2022-11-04 21:56:15 -06:00
parent ca3877f96c
commit 69355204d7
1 changed files with 33 additions and 3 deletions

View File

@ -35,6 +35,11 @@ impl Body {
}
pub fn update(&mut self, dt: f32) {
if self.fixed {
self.velocity = Vec2::ZERO;
return;
}
let pos = self.position;
let vel = self.velocity;
let acc = self.acceleration;
@ -293,7 +298,7 @@ impl ForceGraph {
let from = from.body.position + from.body.velocity * dt;
let to = to.body.position + to.body.velocity * dt;
let delta = to - from;
spring.apply(delta) // * dt
spring.apply(delta)
});
if let Some(vel) = vel {
@ -446,6 +451,10 @@ export_abi!(ForceDirectedGraphPanel);
pub struct ForceDirectedGraphPanel {
panel: Panel,
graph: ForceGraph,
center: Vec2,
last_focus: Vec2,
focus_anim: f32,
focus_target: usize,
size: Vec2,
}
@ -456,6 +465,10 @@ impl BindPanel for ForceDirectedGraphPanel {
let panel = Self {
panel,
graph,
last_focus: Vec2::ZERO,
center: Vec2::ZERO,
focus_anim: 1.0,
focus_target: 0,
size: Vec2::splat(100.0),
};
Box::new(panel)
@ -465,10 +478,23 @@ impl BindPanel for ForceDirectedGraphPanel {
impl PanelImpl for ForceDirectedGraphPanel {
fn update(&mut self, dt: f32) {
self.graph.update(dt);
self.focus_anim += dt;
let target_pos = self.graph.nodes[self.focus_target].body.position;
let anim = self.focus_anim / 0.5;
if anim < 0.0 {
self.center = self.last_focus;
} else if anim < 1.0 {
let anim = -(anim * (anim - 2.0));
self.center = target_pos * anim + self.last_focus * (1.0 - anim);
} else {
self.center = target_pos;
}
}
fn draw(&mut self) {
let ctx = DrawContext::new(self.panel).with_offset(self.size / 2.0);
let offset = self.size / 2.0 - self.center;
let ctx = DrawContext::new(self.panel).with_offset(offset);
self.graph.draw(&ctx);
}
@ -477,7 +503,8 @@ impl PanelImpl for ForceDirectedGraphPanel {
}
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {
let at = at - self.size / 2.0;
let offset = self.size / 2.0 - self.center;
let at = at - offset;
if let CursorEventKind::Deselect = kind {
let node = self
.graph
@ -486,6 +513,9 @@ impl PanelImpl for ForceDirectedGraphPanel {
.position(|node| !node.info.hidden && node.body.position.distance(at) < 6.0);
if let Some(node) = node {
self.graph.on_select(node);
self.last_focus = self.center;
self.focus_target = node;
self.focus_anim = 0.0;
}
}
}