Merge pull request #78547 from Chaosus/vs_drag_enhancement

This commit is contained in:
Yuri Rubinsky 2023-07-20 21:42:04 +03:00 committed by GitHub
commit f8dbed4d0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 6 deletions

View File

@ -16,6 +16,13 @@
Clears the default input ports value.
</description>
</method>
<method name="get_default_input_port" qualifiers="const">
<return type="int" />
<param index="0" name="type" type="int" enum="VisualShaderNode.PortType" />
<description>
Returns the input port which should be connected by default when this node is created as a result of dragging a connection from an existing node to the empty space on the graph.
</description>
</method>
<method name="get_default_input_values" qualifiers="const">
<return type="Array" />
<description>

View File

@ -37,6 +37,14 @@
Defining this method is [b]required[/b].
</description>
</method>
<method name="_get_default_input_port" qualifiers="virtual const">
<return type="int" />
<param index="0" name="type" type="int" enum="VisualShaderNode.PortType" />
<description>
Override this method to define the input port which should be connected by default when this node is created as a result of dragging a connection from an existing node to the empty space on the graph.
Defining this method is [b]optional[/b]. If not overridden, the connection will be created to the first valid port.
</description>
</method>
<method name="_get_description" qualifiers="virtual const">
<return type="String" />
<description>

View File

@ -3083,6 +3083,9 @@ void VisualShaderEditor::_add_node(int p_idx, const Vector<Variant> &p_ops, Stri
if (!is_native) {
vsnode->set_script(add_options[p_idx].script);
}
VisualShaderNodeCustom *custom_node = Object::cast_to<VisualShaderNodeCustom>(vsn);
ERR_FAIL_COND(!custom_node);
custom_node->update_ports();
}
bool is_texture2d = (Object::cast_to<VisualShaderNodeTexture>(vsnode.ptr()) != nullptr);
@ -3211,16 +3214,26 @@ void VisualShaderEditor::_add_node(int p_idx, const Vector<Variant> &p_ops, Stri
undo_redo->add_undo_method(graph_plugin.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);
} else {
// Attempting to connect to the first correct port.
int _to_slot = -1;
// Attempting to connect to the default input port or to the first correct port (if it's not found).
for (int i = 0; i < vsnode->get_input_port_count(); i++) {
if (visual_shader->is_port_types_compatible(output_port_type, vsnode->get_input_port_type(i))) {
undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, i);
undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, i);
undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, i);
undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, i);
break;
if (i == vsnode->get_default_input_port(output_port_type)) {
_to_slot = i;
break;
} else if (_to_slot == -1) {
_to_slot = i;
}
}
}
if (_to_slot >= 0) {
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);
undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
}
}
if (output_port_type == VisualShaderNode::PORT_TYPE_SAMPLER) {

View File

@ -46,6 +46,10 @@ bool VisualShaderNode::is_simple_decl() const {
return simple_decl;
}
int VisualShaderNode::get_default_input_port(PortType p_type) const {
return 0;
}
void VisualShaderNode::set_output_port_for_preview(int p_index) {
port_preview = p_index;
}
@ -378,6 +382,8 @@ bool VisualShaderNode::is_input_port_default(int p_port, Shader::Mode p_mode) co
}
void VisualShaderNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_default_input_port", "type"), &VisualShaderNode::get_default_input_port);
ClassDB::bind_method(D_METHOD("set_output_port_for_preview", "port"), &VisualShaderNode::set_output_port_for_preview);
ClassDB::bind_method(D_METHOD("get_output_port_for_preview"), &VisualShaderNode::get_output_port_for_preview);
@ -481,6 +487,12 @@ String VisualShaderNodeCustom::get_input_port_name(int p_port) const {
return input_ports[p_port].name;
}
int VisualShaderNodeCustom::get_default_input_port(PortType p_type) const {
int ret = 0;
GDVIRTUAL_CALL(_get_default_input_port, p_type, ret);
return ret;
}
int VisualShaderNodeCustom::get_output_port_count() const {
return output_ports.size();
}
@ -649,6 +661,7 @@ void VisualShaderNodeCustom::_bind_methods() {
GDVIRTUAL_BIND(_get_input_port_count);
GDVIRTUAL_BIND(_get_input_port_type, "port");
GDVIRTUAL_BIND(_get_input_port_name, "port");
GDVIRTUAL_BIND(_get_default_input_port, "type");
GDVIRTUAL_BIND(_get_output_port_count);
GDVIRTUAL_BIND(_get_output_port_type, "port");
GDVIRTUAL_BIND(_get_output_port_name, "port");

View File

@ -289,6 +289,7 @@ public:
virtual int get_input_port_count() const = 0;
virtual PortType get_input_port_type(int p_port) const = 0;
virtual String get_input_port_name(int p_port) const = 0;
virtual int get_default_input_port(PortType p_type) const;
virtual void set_input_port_default_value(int p_port, const Variant &p_value, const Variant &p_prev_value = Variant());
Variant get_input_port_default_value(int p_port) const; // if NIL (default if node does not set anything) is returned, it means no default value is wanted if disconnected, thus no input var must be supplied (empty string will be supplied)
@ -367,6 +368,7 @@ protected:
virtual int get_input_port_count() const override;
virtual PortType get_input_port_type(int p_port) const override;
virtual String get_input_port_name(int p_port) const override;
virtual int get_default_input_port(PortType p_type) const override;
virtual int get_output_port_count() const override;
virtual PortType get_output_port_type(int p_port) const override;
@ -384,6 +386,7 @@ protected:
GDVIRTUAL0RC(int, _get_input_port_count)
GDVIRTUAL1RC(PortType, _get_input_port_type, int)
GDVIRTUAL1RC(String, _get_input_port_name, int)
GDVIRTUAL1RC(int, _get_default_input_port, PortType)
GDVIRTUAL0RC(int, _get_output_port_count)
GDVIRTUAL1RC(PortType, _get_output_port_type, int)
GDVIRTUAL1RC(String, _get_output_port_name, int)

View File

@ -4137,6 +4137,10 @@ String VisualShaderNodeStep::get_input_port_name(int p_port) const {
return String();
}
int VisualShaderNodeStep::get_default_input_port(PortType p_type) const {
return 1;
}
int VisualShaderNodeStep::get_output_port_count() const {
return 1;
}
@ -4292,6 +4296,10 @@ String VisualShaderNodeSmoothStep::get_input_port_name(int p_port) const {
return String();
}
int VisualShaderNodeSmoothStep::get_default_input_port(PortType p_type) const {
return 2;
}
int VisualShaderNodeSmoothStep::get_output_port_count() const {
return 1;
}

View File

@ -1661,6 +1661,7 @@ public:
virtual int get_input_port_count() const override;
virtual PortType get_input_port_type(int p_port) const override;
virtual String get_input_port_name(int p_port) const override;
virtual int get_default_input_port(PortType p_type) const override;
virtual int get_output_port_count() const override;
virtual PortType get_output_port_type(int p_port) const override;
@ -1707,6 +1708,7 @@ public:
virtual int get_input_port_count() const override;
virtual PortType get_input_port_type(int p_port) const override;
virtual String get_input_port_name(int p_port) const override;
virtual int get_default_input_port(PortType p_type) const override;
virtual int get_output_port_count() const override;
virtual PortType get_output_port_type(int p_port) const override;