Skip to content

Commit

Permalink
fixed panic
Browse files Browse the repository at this point in the history
  • Loading branch information
blitzarx1 committed May 5, 2023
1 parent 92e9fa2 commit 8b72b0b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/drawer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<'a, N: Clone, E: Clone> Drawer<'a, N, E> {
edge_map
.entry((source, target))
.or_insert_with(Vec::new)
.push((e.id(), edge.clone(), self.comp.edge_state(e.id()).unwrap()));
.push((e.id(), edge.clone(), self.comp.edge_state(&e.id()).unwrap()));
});

let edges_by_nodes = edge_map
Expand Down Expand Up @@ -206,7 +206,7 @@ impl<'a, N: Clone, E: Clone> Drawer<'a, N, E> {
order: usize,
) -> Vec<CubicBezierShape> {
let n: Node<N> = self.g.node(*n_idx).unwrap().screen_transform(self.meta);
let comp_node = self.comp.node_state(*n_idx).unwrap();
let comp_node = self.comp.node_state(n_idx).unwrap();

let pos_start_and_end = n.location.to_pos2();
let loop_size = comp_node.radius(self.meta) * (4. + 1. + order as f32);
Expand Down Expand Up @@ -282,8 +282,8 @@ impl<'a, N: Clone, E: Clone> Drawer<'a, N, E> {
.location
.to_pos2();

let comp_start = self.comp.node_state(*start_idx).unwrap();
let comp_end = self.comp.node_state(*end_idx).unwrap();
let comp_start = self.comp.node_state(start_idx).unwrap();
let comp_end = self.comp.node_state(end_idx).unwrap();

let vec = pos_end - pos_start;
let l = vec.length();
Expand Down
25 changes: 18 additions & 7 deletions src/graph_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use crate::{
metadata::Metadata,
selections::Selections,
settings::{SettingsInteraction, SettingsStyle},
state_computed::StateComputed,
state_computed::{StateComputed, StateComputedEdge, StateComputedNode},
Edge, SettingsNavigation,
};
use egui::{Painter, Pos2, Rect, Response, Sense, Ui, Vec2, Widget};
use petgraph::stable_graph::{NodeIndex, StableGraph};
use petgraph::{stable_graph::{NodeIndex, StableGraph}, visit::EdgeRef};

/// `GraphView` is a widget for visualizing and interacting with graphs.
///
Expand Down Expand Up @@ -337,17 +337,28 @@ impl<'a, N: Clone, E: Clone> GraphView<'a, N, E> {
}

fn precompute_state(&mut self) -> StateComputed {
let mut state = StateComputed::new(self.g.node_count(), self.g.edge_count());

let mut selections = Selections::default();
let nodes_computed = self.g.nodes().map(|(idx, _)| {
let node_state = StateComputedNode::default();
(idx, node_state)
});

let edges_computed = self.g.edges().map(|e| {
let edge_state = StateComputedEdge::default();
(e.id(), edge_state)
});

let mut state = StateComputed::default();
state.nodes = nodes_computed.collect();
state.edges = edges_computed.collect();

// compute radiuses and selections
let child_mode = self.settings_interaction.selection_depth > 0;
self.g.nodes().for_each(|(root_idx, root_n)| {
// compute radii
let num = self.g.edges_num(root_idx);
state
.node_state_mut(root_idx)
.node_state_mut(&root_idx)
.unwrap()
.inc_radius(self.settings_style.edge_radius_weight * num as f32);

Expand All @@ -370,7 +381,7 @@ impl<'a, N: Clone, E: Clone> GraphView<'a, N, E> {
return;
}

let computed = state.node_state_mut(*idx).unwrap();
let computed = state.node_state_mut(idx).unwrap();
if child_mode {
computed.selected_child = true;
return;
Expand All @@ -379,7 +390,7 @@ impl<'a, N: Clone, E: Clone> GraphView<'a, N, E> {
});

edges.iter().for_each(|idx| {
let mut computed = state.edge_state_mut(*idx).unwrap();
let mut computed = state.edge_state_mut(idx).unwrap();
if child_mode {
computed.selected_child = true;
return;
Expand Down
2 changes: 1 addition & 1 deletion src/graph_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<'a, N: Clone, E: Clone> GraphWrapper<'a, N, E> {
) -> impl Iterator<Item = (NodeIndex, &Node<N>, &StateComputedNode)> {
self.g
.node_references()
.map(|(i, n)| (i, n, comp.node_state(i).unwrap()))
.map(|(i, n)| (i, n, comp.node_state(&i).unwrap()))
}

pub fn nodes(&'a self) -> impl Iterator<Item = (NodeIndex, &Node<N>)> {
Expand Down
35 changes: 14 additions & 21 deletions src/state_computed.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
use std::collections::HashMap;

use petgraph::{stable_graph::EdgeIndex, stable_graph::NodeIndex};

use crate::{metadata::Metadata, selections::Selections};

/// `StateComputed` is a utility struct for managing ephemerial state which is created and destroyed in one frame.
///
/// The struct stores the selected nodes, dragged node, and cached edges by nodes.
#[derive(Debug, Clone)]
/// The struct stores selections, dragged node and computed elements states.
#[derive(Default, Debug, Clone)]
pub struct StateComputed {
pub dragged: Option<NodeIndex>,
pub selections: Option<Selections>,
nodes: Vec<StateComputedNode>,
edges: Vec<StateComputedEdge>,
pub nodes: HashMap<NodeIndex,StateComputedNode>,
pub edges: HashMap<EdgeIndex,StateComputedEdge>,
}

impl StateComputed {
pub fn new(node_count: usize, edge_count: usize) -> Self {
Self {
dragged: None,
selections: None,
nodes: vec![Default::default(); node_count],
edges: vec![Default::default(); edge_count],
}
}

pub fn node_state(&self, idx: NodeIndex) -> Option<&StateComputedNode> {
self.nodes.get(idx.index())
pub fn node_state(&self, idx: &NodeIndex) -> Option<&StateComputedNode> {
self.nodes.get(idx)
}

pub fn node_state_mut(&mut self, idx: NodeIndex) -> Option<&mut StateComputedNode> {
self.nodes.get_mut(idx.index())
pub fn node_state_mut(&mut self, idx: &NodeIndex) -> Option<&mut StateComputedNode> {
self.nodes.get_mut(idx)
}

pub fn edge_state(&self, idx: EdgeIndex) -> Option<&StateComputedEdge> {
self.edges.get(idx.index())
pub fn edge_state(&self, idx: &EdgeIndex) -> Option<&StateComputedEdge> {
self.edges.get(idx)
}

pub fn edge_state_mut(&mut self, idx: EdgeIndex) -> Option<&mut StateComputedEdge> {
self.edges.get_mut(idx.index())
pub fn edge_state_mut(&mut self, idx: &EdgeIndex) -> Option<&mut StateComputedEdge> {
self.edges.get_mut(idx)
}
}

Expand Down

0 comments on commit 8b72b0b

Please sign in to comment.