diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index 8285211..de015d5 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -174,6 +174,10 @@ where /* Handle responses from drawing nodes */ + // Some responses generate additional responses when processed. These + // are stored here to report them back to the user. + let mut extra_responses: Vec> = Vec::new(); + for response in delayed_responses.iter().copied() { match response { NodeResponse::ConnectEventStarted(node_id, port) => { @@ -205,7 +209,14 @@ where self.selected_node = Some(node_id); } NodeResponse::DeleteNode(node_id) => { - self.graph.remove_node(node_id); + let removed = self.graph.remove_node(node_id); + extra_responses.extend( + removed + .into_iter() + .map(|x| x.0) + .into_iter() + .map(NodeResponse::DisconnectEvent), + ); self.node_positions.remove(node_id); // Make sure to not leave references to old nodes hanging if self.selected_node.map(|x| x == node_id).unwrap_or(false) { @@ -238,6 +249,11 @@ where } } + // Push any responses that were generated during response handling. + // These are only informative for the end-user and need no special + // treatment here. + delayed_responses.extend(extra_responses); + /* Mouse input handling */ // This locks the context, so don't hold on to it for too long. diff --git a/egui_node_graph/src/graph_impls.rs b/egui_node_graph/src/graph_impls.rs index 7f898a6..9c26881 100644 --- a/egui_node_graph/src/graph_impls.rs +++ b/egui_node_graph/src/graph_impls.rs @@ -63,18 +63,36 @@ impl Graph { output_id } - pub fn remove_node(&mut self, node_id: NodeId) { - self.connections - .retain(|i, o| !(self.outputs[*o].node == node_id || self.inputs[i].node == node_id)); - let inputs: SVec<_> = self[node_id].input_ids().collect(); - for input in inputs { + /// Removes a node from the graph with given `node_id`. This also removes + /// any incoming or outgoing connections from that node + /// + /// This function returns the list of connections that has been removed + /// after deleting this node as input-output pairs. Note that one of the two + /// ids in the pair (the one on `node_id`'s end) will be invalid after + /// calling this function. + pub fn remove_node(&mut self, node_id: NodeId) -> Vec<(InputId, OutputId)> { + let mut disconnect_events = vec![]; + + self.connections.retain(|i, o| { + if self.outputs[*o].node == node_id || self.inputs[i].node == node_id { + disconnect_events.push((i, *o)); + false + } else { + true + } + }); + + // NOTE: Collect is needed because we can't borrow the input ids while + // we remove them inside the loop. + for input in self[node_id].input_ids().collect::>() { self.inputs.remove(input); } - let outputs: SVec<_> = self[node_id].output_ids().collect(); - for output in outputs { + for output in self[node_id].output_ids().collect::>() { self.outputs.remove(output); } self.nodes.remove(node_id); + + disconnect_events } pub fn remove_connection(&mut self, input_id: InputId) -> Option {