Skip to content

Commit f8dbed4

Browse files
authored
Merge pull request #78547 from Chaosus/vs_drag_enhancement
2 parents dd05012 + 0110113 commit f8dbed4

7 files changed

+60
-6
lines changed

doc/classes/VisualShaderNode.xml

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
Clears the default input ports value.
1717
</description>
1818
</method>
19+
<method name="get_default_input_port" qualifiers="const">
20+
<return type="int" />
21+
<param index="0" name="type" type="int" enum="VisualShaderNode.PortType" />
22+
<description>
23+
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.
24+
</description>
25+
</method>
1926
<method name="get_default_input_values" qualifiers="const">
2027
<return type="Array" />
2128
<description>

doc/classes/VisualShaderNodeCustom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
Defining this method is [b]required[/b].
3838
</description>
3939
</method>
40+
<method name="_get_default_input_port" qualifiers="virtual const">
41+
<return type="int" />
42+
<param index="0" name="type" type="int" enum="VisualShaderNode.PortType" />
43+
<description>
44+
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.
45+
Defining this method is [b]optional[/b]. If not overridden, the connection will be created to the first valid port.
46+
</description>
47+
</method>
4048
<method name="_get_description" qualifiers="virtual const">
4149
<return type="String" />
4250
<description>

editor/plugins/visual_shader_editor_plugin.cpp

+19-6
Original file line numberDiff line numberDiff line change
@@ -3083,6 +3083,9 @@ void VisualShaderEditor::_add_node(int p_idx, const Vector<Variant> &p_ops, Stri
30833083
if (!is_native) {
30843084
vsnode->set_script(add_options[p_idx].script);
30853085
}
3086+
VisualShaderNodeCustom *custom_node = Object::cast_to<VisualShaderNodeCustom>(vsn);
3087+
ERR_FAIL_COND(!custom_node);
3088+
custom_node->update_ports();
30863089
}
30873090

30883091
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
32113214
undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot);
32123215
undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
32133216
} else {
3214-
// Attempting to connect to the first correct port.
3217+
int _to_slot = -1;
3218+
3219+
// Attempting to connect to the default input port or to the first correct port (if it's not found).
32153220
for (int i = 0; i < vsnode->get_input_port_count(); i++) {
32163221
if (visual_shader->is_port_types_compatible(output_port_type, vsnode->get_input_port_type(i))) {
3217-
undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, i);
3218-
undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, i);
3219-
undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, i);
3220-
undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, i);
3221-
break;
3222+
if (i == vsnode->get_default_input_port(output_port_type)) {
3223+
_to_slot = i;
3224+
break;
3225+
} else if (_to_slot == -1) {
3226+
_to_slot = i;
3227+
}
32223228
}
32233229
}
3230+
3231+
if (_to_slot >= 0) {
3232+
undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot);
3233+
undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
3234+
undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot);
3235+
undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
3236+
}
32243237
}
32253238

32263239
if (output_port_type == VisualShaderNode::PORT_TYPE_SAMPLER) {

scene/resources/visual_shader.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ bool VisualShaderNode::is_simple_decl() const {
4646
return simple_decl;
4747
}
4848

49+
int VisualShaderNode::get_default_input_port(PortType p_type) const {
50+
return 0;
51+
}
52+
4953
void VisualShaderNode::set_output_port_for_preview(int p_index) {
5054
port_preview = p_index;
5155
}
@@ -378,6 +382,8 @@ bool VisualShaderNode::is_input_port_default(int p_port, Shader::Mode p_mode) co
378382
}
379383

380384
void VisualShaderNode::_bind_methods() {
385+
ClassDB::bind_method(D_METHOD("get_default_input_port", "type"), &VisualShaderNode::get_default_input_port);
386+
381387
ClassDB::bind_method(D_METHOD("set_output_port_for_preview", "port"), &VisualShaderNode::set_output_port_for_preview);
382388
ClassDB::bind_method(D_METHOD("get_output_port_for_preview"), &VisualShaderNode::get_output_port_for_preview);
383389

@@ -481,6 +487,12 @@ String VisualShaderNodeCustom::get_input_port_name(int p_port) const {
481487
return input_ports[p_port].name;
482488
}
483489

490+
int VisualShaderNodeCustom::get_default_input_port(PortType p_type) const {
491+
int ret = 0;
492+
GDVIRTUAL_CALL(_get_default_input_port, p_type, ret);
493+
return ret;
494+
}
495+
484496
int VisualShaderNodeCustom::get_output_port_count() const {
485497
return output_ports.size();
486498
}
@@ -649,6 +661,7 @@ void VisualShaderNodeCustom::_bind_methods() {
649661
GDVIRTUAL_BIND(_get_input_port_count);
650662
GDVIRTUAL_BIND(_get_input_port_type, "port");
651663
GDVIRTUAL_BIND(_get_input_port_name, "port");
664+
GDVIRTUAL_BIND(_get_default_input_port, "type");
652665
GDVIRTUAL_BIND(_get_output_port_count);
653666
GDVIRTUAL_BIND(_get_output_port_type, "port");
654667
GDVIRTUAL_BIND(_get_output_port_name, "port");

scene/resources/visual_shader.h

+3
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ class VisualShaderNode : public Resource {
289289
virtual int get_input_port_count() const = 0;
290290
virtual PortType get_input_port_type(int p_port) const = 0;
291291
virtual String get_input_port_name(int p_port) const = 0;
292+
virtual int get_default_input_port(PortType p_type) const;
292293

293294
virtual void set_input_port_default_value(int p_port, const Variant &p_value, const Variant &p_prev_value = Variant());
294295
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 @@ class VisualShaderNodeCustom : public VisualShaderNode {
367368
virtual int get_input_port_count() const override;
368369
virtual PortType get_input_port_type(int p_port) const override;
369370
virtual String get_input_port_name(int p_port) const override;
371+
virtual int get_default_input_port(PortType p_type) const override;
370372

371373
virtual int get_output_port_count() const override;
372374
virtual PortType get_output_port_type(int p_port) const override;
@@ -384,6 +386,7 @@ class VisualShaderNodeCustom : public VisualShaderNode {
384386
GDVIRTUAL0RC(int, _get_input_port_count)
385387
GDVIRTUAL1RC(PortType, _get_input_port_type, int)
386388
GDVIRTUAL1RC(String, _get_input_port_name, int)
389+
GDVIRTUAL1RC(int, _get_default_input_port, PortType)
387390
GDVIRTUAL0RC(int, _get_output_port_count)
388391
GDVIRTUAL1RC(PortType, _get_output_port_type, int)
389392
GDVIRTUAL1RC(String, _get_output_port_name, int)

scene/resources/visual_shader_nodes.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -4137,6 +4137,10 @@ String VisualShaderNodeStep::get_input_port_name(int p_port) const {
41374137
return String();
41384138
}
41394139

4140+
int VisualShaderNodeStep::get_default_input_port(PortType p_type) const {
4141+
return 1;
4142+
}
4143+
41404144
int VisualShaderNodeStep::get_output_port_count() const {
41414145
return 1;
41424146
}
@@ -4292,6 +4296,10 @@ String VisualShaderNodeSmoothStep::get_input_port_name(int p_port) const {
42924296
return String();
42934297
}
42944298

4299+
int VisualShaderNodeSmoothStep::get_default_input_port(PortType p_type) const {
4300+
return 2;
4301+
}
4302+
42954303
int VisualShaderNodeSmoothStep::get_output_port_count() const {
42964304
return 1;
42974305
}

scene/resources/visual_shader_nodes.h

+2
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,7 @@ class VisualShaderNodeStep : public VisualShaderNode {
16611661
virtual int get_input_port_count() const override;
16621662
virtual PortType get_input_port_type(int p_port) const override;
16631663
virtual String get_input_port_name(int p_port) const override;
1664+
virtual int get_default_input_port(PortType p_type) const override;
16641665

16651666
virtual int get_output_port_count() const override;
16661667
virtual PortType get_output_port_type(int p_port) const override;
@@ -1707,6 +1708,7 @@ class VisualShaderNodeSmoothStep : public VisualShaderNode {
17071708
virtual int get_input_port_count() const override;
17081709
virtual PortType get_input_port_type(int p_port) const override;
17091710
virtual String get_input_port_name(int p_port) const override;
1711+
virtual int get_default_input_port(PortType p_type) const override;
17101712

17111713
virtual int get_output_port_count() const override;
17121714
virtual PortType get_output_port_type(int p_port) const override;

0 commit comments

Comments
 (0)