From 5e9052fae3d7bec82330e88d65ab336fd3f15e56 Mon Sep 17 00:00:00 2001 From: Yuri Roubinski Date: Wed, 7 Aug 2019 16:46:47 +0300 Subject: [PATCH] Allow copy/paste nodes between vertex/fragment/light modes in visual shaders --- .../plugins/visual_shader_editor_plugin.cpp | 35 ++++++++++++++----- editor/plugins/visual_shader_editor_plugin.h | 2 +- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index 88c52040743..dc22507f39f 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -78,6 +78,7 @@ void VisualShaderEditor::edit(VisualShader *p_visual_shader) { hide(); } else { if (changed) { // to avoid tree collapse + _clear_buffer(); _update_options_menu(); } _update_graph(); @@ -1594,22 +1595,38 @@ void VisualShaderEditor::_dup_copy_nodes(int p_type, List &r_nodes, Set &r_nodes, Set &r_excluded, const Vector2 &p_offset, bool p_select) { +void VisualShaderEditor::_dup_paste_nodes(int p_type, int p_pasted_type, List &r_nodes, Set &r_excluded, const Vector2 &p_offset, bool p_select) { VisualShader::Type type = (VisualShader::Type)p_type; + VisualShader::Type pasted_type = (VisualShader::Type)p_pasted_type; int base_id = visual_shader->get_valid_node_id(type); int id_from = base_id; Map connection_remap; + Set unsupported_set; for (List::Element *E = r_nodes.front(); E; E = E->next()) { connection_remap[E->get()] = id_from; - Ref node = visual_shader->get_node(type, E->get()); + Ref node = visual_shader->get_node(pasted_type, E->get()); + + bool unsupported = false; + for (int i = 0; i < add_options.size(); i++) { + if (add_options[i].type == node->get_class_name()) { + if (!_is_available(add_options[i].mode)) { + unsupported = true; + } + break; + } + } + if (unsupported) { + unsupported_set.insert(E->get()); + continue; + } Ref dupli = node->duplicate(); - undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, dupli, visual_shader->get_node_position(type, E->get()) + p_offset, id_from); + undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, dupli, visual_shader->get_node_position(pasted_type, E->get()) + p_offset, id_from); undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_from); // duplicate size, inputs and outputs if node is group @@ -1629,9 +1646,12 @@ void VisualShaderEditor::_dup_paste_nodes(int p_type, List &r_nodes, Set conns; - visual_shader->get_node_connections(type, &conns); + visual_shader->get_node_connections(pasted_type, &conns); for (List::Element *E = conns.front(); E; E = E->next()) { + if (unsupported_set.has(E->get().from_node) || unsupported_set.has(E->get().to_node)) { + continue; + } if (connection_remap.has(E->get().from_node) && connection_remap.has(E->get().to_node)) { undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, connection_remap[E->get().from_node], E->get().from_port, connection_remap[E->get().to_node], E->get().to_port); } @@ -1678,7 +1698,7 @@ void VisualShaderEditor::_duplicate_nodes() { undo_redo->create_action(TTR("Duplicate Nodes")); - _dup_paste_nodes(type, nodes, excluded, Vector2(10, 10) * EDSCALE, true); + _dup_paste_nodes(type, type, nodes, excluded, Vector2(10, 10) * EDSCALE, true); } void VisualShaderEditor::_copy_nodes() { @@ -1701,7 +1721,7 @@ void VisualShaderEditor::_paste_nodes() { float scale = graph->get_zoom(); - _dup_paste_nodes(type, copy_nodes_buffer, copy_nodes_excluded_buffer, (graph->get_scroll_ofs() / scale + graph->get_local_mouse_position() / scale - selection_center), false); + _dup_paste_nodes(type, copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer, (graph->get_scroll_ofs() / scale + graph->get_local_mouse_position() / scale - selection_center), false); _dup_update_excluded(type, copy_nodes_excluded_buffer); // to prevent selection of previous copies at new paste } @@ -1780,9 +1800,6 @@ void VisualShaderEditor::_on_nodes_delete() { void VisualShaderEditor::_mode_selected(int p_id) { - copy_nodes_buffer.clear(); - copy_nodes_excluded_buffer.clear(); - _update_options_menu(); _update_graph(); } diff --git a/editor/plugins/visual_shader_editor_plugin.h b/editor/plugins/visual_shader_editor_plugin.h index 315ef1788e8..d2b1e8bc45c 100644 --- a/editor/plugins/visual_shader_editor_plugin.h +++ b/editor/plugins/visual_shader_editor_plugin.h @@ -178,7 +178,7 @@ class VisualShaderEditor : public VBoxContainer { void _dup_copy_nodes(int p_type, List &r_nodes, Set &r_excluded); void _dup_update_excluded(int p_type, Set &r_excluded); - void _dup_paste_nodes(int p_type, List &r_nodes, Set &r_excluded, const Vector2 &p_offset, bool p_select); + void _dup_paste_nodes(int p_type, int p_pasted_type, List &r_nodes, Set &r_excluded, const Vector2 &p_offset, bool p_select); void _duplicate_nodes();