Cleanup: deduplicate remap node pairing function

It's a bit unfortunate that the `node_map` in both cases has different
constness, so a conversion or `reinterpret_cast` is necessary. For now
a new temporary map is created as this is less error prone. That's not
ideal but better than the duplication from before.
This commit is contained in:
Jacques Lucke
2023-06-16 17:03:13 +02:00
parent 579a9d2e1f
commit b3641dae16
3 changed files with 13 additions and 31 deletions

View File

@@ -186,33 +186,6 @@ void NODE_OT_clipboard_copy(wmOperatorType *ot)
/** \name Paste
* \{ */
static void remap_pairing(bNodeTree &dst_tree, const Map<const bNode *, bNode *> &node_map)
{
/* We don't have the old tree for looking up output nodes by ID,
* so we have to build a map first to find copied output nodes in the new tree. */
Map<int32_t, bNode *> dst_output_node_map;
for (const auto &item : node_map.items()) {
if (item.key->type == GEO_NODE_SIMULATION_OUTPUT) {
dst_output_node_map.add_new(item.key->identifier, item.value);
}
}
for (bNode *dst_node : node_map.values()) {
if (dst_node->type == GEO_NODE_SIMULATION_INPUT) {
NodeGeometrySimulationInput &data = *static_cast<NodeGeometrySimulationInput *>(
dst_node->storage);
if (const bNode *output_node = dst_output_node_map.lookup_default(data.output_node_id,
nullptr)) {
data.output_node_id = output_node->identifier;
}
else {
data.output_node_id = 0;
blender::nodes::update_node_declaration_and_sockets(dst_tree, *dst_node);
}
}
}
}
static int node_clipboard_paste_exec(bContext *C, wmOperator *op)
{
SpaceNode &snode = *CTX_wm_space_node(C);
@@ -327,7 +300,7 @@ static int node_clipboard_paste_exec(bContext *C, wmOperator *op)
bke::nodeDeclarationEnsure(&tree, new_node);
}
remap_pairing(tree, node_map);
remap_node_pairing(tree, node_map);
tree.ensure_topology_cache();
for (bNode *new_node : node_map.values()) {

View File

@@ -1261,11 +1261,11 @@ static void node_duplicate_reparent_recursive(bNodeTree *ntree,
}
}
static void remap_pairing(bNodeTree &dst_tree, const Map<bNode *, bNode *> &node_map)
void remap_node_pairing(bNodeTree &dst_tree, const Map<const bNode *, bNode *> &node_map)
{
/* We don't have the old tree for looking up output nodes by ID,
* so we have to build a map first to find copied output nodes in the new tree. */
Map<uint32_t, bNode *> dst_output_node_map;
Map<int32_t, bNode *> dst_output_node_map;
for (const auto &item : node_map.items()) {
if (item.key->type == GEO_NODE_SIMULATION_OUTPUT) {
dst_output_node_map.add_new(item.key->identifier, item.value);
@@ -1376,7 +1376,14 @@ static int node_duplicate_exec(bContext *C, wmOperator *op)
}
}
remap_pairing(*ntree, node_map);
{
/* Use temporary map that has const key, because that's what the function below expects. */
Map<const bNode *, bNode *> const_node_map;
for (const auto item : node_map.items()) {
const_node_map.add(item.key, item.value);
}
remap_node_pairing(*ntree, const_node_map);
}
/* Deselect old nodes, select the copies instead. */
for (const auto item : node_map.items()) {

View File

@@ -336,6 +336,8 @@ bNodeSocket *node_find_indicated_socket(SpaceNode &snode,
float node_link_dim_factor(const View2D &v2d, const bNodeLink &link);
bool node_link_is_hidden_or_dimmed(const View2D &v2d, const bNodeLink &link);
void remap_node_pairing(bNodeTree &dst_tree, const Map<const bNode *, bNode *> &node_map);
void NODE_OT_duplicate(wmOperatorType *ot);
void NODE_OT_delete(wmOperatorType *ot);
void NODE_OT_delete_reconnect(wmOperatorType *ot);