Skip to content

Commit

Permalink
GH-722 Correctly copy node state using ui_copy action
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Aug 12, 2024
1 parent c7c0df6 commit c2f475d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/editor/graph/graph_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1898,8 +1898,7 @@ void OrchestratorGraphEdit::_on_copy_nodes_request()

const int id = script_node->get_id();
_clipboard->positions[id] = script_node->get_position();
// Be sure to clone to create new copies of pins
_clipboard->nodes[id] = script_node->duplicate(true);
_clipboard->nodes[id] = _script_graph->copy_node(id, true);
}
});

Expand Down
19 changes: 19 additions & 0 deletions src/script/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,25 @@ void OScriptGraph::move_node_to(const Ref<OScriptNode>& p_node, const Ref<OScrip
p_target->add_node(p_node);
}

Ref<OScriptNode> OScriptGraph::copy_node(int p_node_id, bool p_duplicate_resources)
{
const Ref<OScriptNode> node = get_node(p_node_id);
if (!node.is_valid())
{
ERR_PRINT("Cannot copy node with id " + itos(p_node_id));
return nullptr;
}

// Duplicate node
Ref<OScriptNode> duplicate = node->duplicate(p_duplicate_resources);

// Partially initialize, only so state gets copied/set
duplicate->_orchestration = _orchestration;
duplicate->post_initialize();

return duplicate;
}

Ref<OScriptNode> OScriptGraph::duplicate_node(int p_node_id, const Vector2& p_delta, bool p_duplicate_resources)
{
const Ref<OScriptNode> node = get_node(p_node_id);
Expand Down
6 changes: 6 additions & 0 deletions src/script/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ class OScriptGraph : public Resource
/// @param p_target the target graph
void move_node_to(const Ref<OScriptNode>& p_node, const Ref<OScriptGraph>& p_target);

/// Creates a copy of the specified node
/// @param p_node_id the node to copy
/// @param p_duplicate_resources whether to duplicate sub-resources, defaults o false
/// @return the copied node reference
Ref<OScriptNode> copy_node(int p_node_id, bool p_duplicate_resources = false);

/// Duplicate the specified node
/// @param p_node_id the node to duplicate
/// @param p_delta the position to add to the existing node's position
Expand Down

0 comments on commit c2f475d

Please sign in to comment.