diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-01-26 12:50:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-26 12:50:40 +0100 |
commit | 268f32badaa0b8a6843f6f36c1284a41b4d7831d (patch) | |
tree | cf9adb8b2acca1586c4d97253f2db9a96f0ef8e2 /editor | |
parent | bdfc7a6b40f21817894fa2652303931b0b2fc180 (diff) | |
parent | d87303d058f53a6b3c035dfb92385f84cb49f035 (diff) |
Merge pull request #45226 from Chaosus/vs_expression_port
Auto-creates a port in visual shader expression on dragging from (and to)
Diffstat (limited to 'editor')
-rw-r--r-- | editor/plugins/visual_shader_editor_plugin.cpp | 53 |
1 files changed, 49 insertions, 4 deletions
diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index edd0f7eeba..fb821a0856 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -1883,12 +1883,47 @@ VisualShaderNode *VisualShaderEditor::_add_node(int p_idx, int p_op_idx) { undo_redo->add_do_method(expr, "set_size", Size2(250 * EDSCALE, 150 * EDSCALE)); } + bool created_expression_port = false; + if (to_node != -1 && to_slot != -1) { - if (vsnode->get_output_port_count() > 0) { + VisualShaderNode::PortType input_port_type = visual_shader->get_node(type, to_node)->get_input_port_type(to_slot); + + if (expr && expr->is_editable() && input_port_type != VisualShaderNode::PORT_TYPE_SAMPLER) { + undo_redo->add_do_method(expr, "add_output_port", 0, input_port_type, "output0"); + undo_redo->add_undo_method(expr, "remove_output_port", 0); + + String initial_expression_code; + + switch (input_port_type) { + case VisualShaderNode::PORT_TYPE_SCALAR: + initial_expression_code = "output0 = 1.0;"; + break; + case VisualShaderNode::PORT_TYPE_SCALAR_INT: + initial_expression_code = "output0 = 1;"; + break; + case VisualShaderNode::PORT_TYPE_VECTOR: + initial_expression_code = "output0 = vec3(1.0, 1.0, 1.0);"; + break; + case VisualShaderNode::PORT_TYPE_BOOLEAN: + initial_expression_code = "output0 = true;"; + break; + case VisualShaderNode::PORT_TYPE_TRANSFORM: + initial_expression_code = "output0 = mat4(1.0);"; + break; + default: + break; + } + + undo_redo->add_do_method(expr, "set_expression", initial_expression_code); + undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, id_to_use); + + created_expression_port = true; + } + if (vsnode->get_output_port_count() > 0 || created_expression_port) { int _from_node = id_to_use; int _from_slot = 0; - if (visual_shader->is_port_types_compatible(vsnode->get_output_port_type(_from_slot), visual_shader->get_node(type, to_node)->get_input_port_type(to_slot))) { + if (created_expression_port || visual_shader->is_port_types_compatible(vsnode->get_output_port_type(_from_slot), input_port_type)) { undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, _from_node, _from_slot, to_node, to_slot); undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, _from_node, _from_slot, to_node, to_slot); undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, _from_node, _from_slot, to_node, to_slot); @@ -1896,11 +1931,21 @@ VisualShaderNode *VisualShaderEditor::_add_node(int p_idx, int p_op_idx) { } } } else if (from_node != -1 && from_slot != -1) { - if (vsnode->get_input_port_count() > 0) { + VisualShaderNode::PortType output_port_type = visual_shader->get_node(type, from_node)->get_output_port_type(from_slot); + + if (expr && expr->is_editable()) { + undo_redo->add_do_method(expr, "add_input_port", 0, output_port_type, "input0"); + undo_redo->add_undo_method(expr, "remove_input_port", 0); + undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, id_to_use); + + created_expression_port = true; + } + + if (vsnode->get_input_port_count() > 0 || created_expression_port) { int _to_node = id_to_use; int _to_slot = 0; - if (visual_shader->is_port_types_compatible(visual_shader->get_node(type, from_node)->get_output_port_type(from_slot), vsnode->get_input_port_type(_to_slot))) { + if (created_expression_port || visual_shader->is_port_types_compatible(output_port_type, vsnode->get_input_port_type(_to_slot))) { undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot); undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot); undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot); |