Make copying/duplicating nodes not preserve the incoming connection (#917)

* Remove wires to nodes outside of copy

* Fix logic error

* Shift pasted nodes

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
0HyperCube 2022-12-28 20:00:53 +00:00 committed by Keavon Chambers
parent c552edd525
commit b408bef14b
4 changed files with 88 additions and 40 deletions

View file

@ -6,6 +6,7 @@ use std::sync::Mutex;
pub mod value;
use dyn_any::{DynAny, StaticType};
use glam::IVec2;
use rand_chacha::{
rand_core::{RngCore, SeedableRng},
ChaCha20Rng,
@ -33,7 +34,7 @@ fn merge_ids(a: u64, b: u64) -> u64 {
#[derive(Clone, Debug, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DocumentNodeMetadata {
pub position: (i32, i32),
pub position: IVec2,
}
#[derive(Clone, Debug, PartialEq)]
@ -94,6 +95,28 @@ impl DocumentNode {
unreachable!("tried to resolve not flattened node on resolved node");
}
}
/// Converts all node id inputs to a new id based on a HashMap.
///
/// If the node is not in the hashmap then a default input is found based on the node name and input index.
pub fn map_ids<P>(mut self, default_input: P, new_ids: &HashMap<NodeId, NodeId>) -> Self
where
P: Fn(String, usize) -> Option<NodeInput>,
{
for (index, input) in self.inputs.iter_mut().enumerate() {
let &mut NodeInput::Node(id) = input else {
continue;
};
if let Some(&new_id) = new_ids.get(&id) {
*input = NodeInput::Node(new_id);
} else if let Some(new_input) = default_input(self.name.clone(), index) {
*input = new_input;
} else {
warn!("Node does not exist in library with that many inputs");
}
}
self
}
}
#[derive(Clone, Debug)]