forked from setzer22/egui_node_graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
use super::*; | ||
|
||
impl<NodeData, DataType, ValueType> Graph<NodeData, DataType, ValueType> { | ||
impl<NodeData, DataType, ValueType> Graph<NodeData, DataType, ValueType> | ||
where DataType: DataTypeTrait { | ||
pub fn new() -> Self { | ||
Self { | ||
nodes: SlotMap::default(), | ||
inputs: SlotMap::default(), | ||
outputs: SlotMap::default(), | ||
connections: SecondaryMap::default(), | ||
incoming: SecondaryMap::default(), | ||
outgoing: SecondaryMap::default(), | ||
} | ||
} | ||
|
||
|
@@ -64,37 +66,86 @@ impl<NodeData, DataType, ValueType> Graph<NodeData, DataType, ValueType> { | |
} | ||
|
||
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 { | ||
self.inputs.remove(input); | ||
self.remove_incoming_connections(input); | ||
} | ||
let outputs: SVec<_> = self[node_id].output_ids().collect(); | ||
for output in outputs { | ||
self.outputs.remove(output); | ||
self.remove_outgoing_connections(output); | ||
} | ||
self.nodes.remove(node_id); | ||
} | ||
|
||
pub fn remove_connection(&mut self, input_id: InputId) -> Option<OutputId> { | ||
self.connections.remove(input_id) | ||
pub fn remove_connection(&mut self, input_id: InputId, output_id: OutputId) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
gmorenz
Author
Owner
|
||
self.outgoing[output_id].retain(|&mut x| x != input_id); | ||
self.incoming[input_id].retain(|&mut x| x != output_id); | ||
} | ||
|
||
pub fn remove_incoming_connections(&mut self, input_id: InputId) { | ||
if let Some(outputs) = self.incoming.get(input_id) { | ||
for &output in outputs { | ||
self.outgoing[output].retain(|&mut x| x != input_id); | ||
} | ||
} | ||
self.incoming.remove(input_id); | ||
} | ||
|
||
pub fn remove_outgoing_connections(&mut self, output_id: OutputId) { | ||
if let Some(inputs) = self.outgoing.get(output_id) { | ||
for &input in inputs { | ||
self.incoming[input].retain(|&mut x| x != output_id); | ||
} | ||
} | ||
self.outgoing.remove(output_id); | ||
} | ||
|
||
pub fn iter_nodes(&self) -> impl Iterator<Item = NodeId> + '_ { | ||
self.nodes.iter().map(|(id, _)| id) | ||
} | ||
|
||
pub fn add_connection(&mut self, output: OutputId, input: InputId) { | ||
self.connections.insert(input, output); | ||
if self.get_input(input).typ.mergeable() { | ||
self.incoming.entry(input) | ||
.expect("Old InputId") | ||
.or_default() | ||
.push(output); | ||
} else { | ||
self.remove_incoming_connections(input); | ||
let mut v = SVec::new(); | ||
v.push(output); | ||
self.incoming.insert(input, v); | ||
} | ||
|
||
if self.get_output(output).typ.splittable() { | ||
self.outgoing.entry(output) | ||
.expect("Old OutputId") | ||
.or_default() | ||
.push(input); | ||
} else { | ||
self.remove_outgoing_connections(output); | ||
let mut v = SVec::new(); | ||
v.push(input); | ||
self.outgoing.insert(output, v); | ||
} | ||
} | ||
|
||
pub fn iter_connections(&self) -> impl Iterator<Item = (InputId, OutputId)> + '_ { | ||
self.connections.iter().map(|(o, i)| (o, *i)) | ||
self.incoming.iter().flat_map(|(o, inputs)| | ||
inputs.iter().map(move |&i| (o, i)) | ||
) | ||
} | ||
|
||
pub fn incoming(&self, input: InputId) -> &[OutputId] { | ||
self.incoming.get(input) | ||
.map(|x| x.as_slice()) | ||
.unwrap_or(&[]) | ||
} | ||
|
||
pub fn connection(&self, input: InputId) -> Option<OutputId> { | ||
self.connections.get(input).copied() | ||
pub fn outgoing(&self, output: OutputId) -> &[InputId] { | ||
self.outgoing.get(output) | ||
.map(|x| x.as_slice()) | ||
.unwrap_or(&[]) | ||
} | ||
|
||
pub fn any_param_type(&self, param: AnyParameterId) -> Result<&DataType, EguiGraphError> { | ||
|
@@ -114,21 +165,21 @@ impl<NodeData, DataType, ValueType> Graph<NodeData, DataType, ValueType> { | |
} | ||
} | ||
|
||
impl<NodeData, DataType, ValueType> Default for Graph<NodeData, DataType, ValueType> { | ||
impl<NodeData, DataType: DataTypeTrait, ValueType> Default for Graph<NodeData, DataType, ValueType> { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl<NodeData> Node<NodeData> { | ||
pub fn inputs<'a, DataType, DataValue>( | ||
pub fn inputs<'a, DataType: DataTypeTrait, DataValue>( | ||
&'a self, | ||
graph: &'a Graph<NodeData, DataType, DataValue>, | ||
) -> impl Iterator<Item = &InputParam<DataType, DataValue>> + 'a { | ||
self.input_ids().map(|id| graph.get_input(id)) | ||
} | ||
|
||
pub fn outputs<'a, DataType, DataValue>( | ||
pub fn outputs<'a, DataType: DataTypeTrait, DataValue>( | ||
&'a self, | ||
graph: &'a Graph<NodeData, DataType, DataValue>, | ||
) -> impl Iterator<Item = &OutputParam<DataType>> + 'a { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Minor API design thing I noticed while using this:
Would suggest swapping the arguments on
remove_connection
to be consistent.