Merge pull request #45226 from Chaosus/vs_expression_port

Auto-creates a port in visual shader expression on dragging from (and to)
This commit is contained in:
Rémi Verschelde 2021-01-26 12:50:40 +01:00 committed by GitHub
commit 268f32bada
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);