Skip to content

Commit 429bf02

Browse files
committed
Merge pull request godotengine#89810 from Geometror/fix-79417
[VisualShader] Remove invalid graph connections when ports are removed
2 parents c3a8da6 + 0b9b479 commit 429bf02

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

editor/plugins/visual_shader_editor_plugin.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7183,8 +7183,35 @@ class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
71837183
undo_redo->add_do_property(node.ptr(), p_property, p_value);
71847184
undo_redo->add_undo_property(node.ptr(), p_property, node->get(p_property));
71857185

7186+
Ref<VisualShaderNode> vsnode = editor->get_visual_shader()->get_node(shader_type, node_id);
7187+
ERR_FAIL_COND(vsnode.is_null());
7188+
7189+
// Check for invalid connections due to removed ports.
7190+
// We need to know the new state of the node to generate the proper undo/redo instructions.
7191+
// Quite hacky but the best way I could come up with for now.
7192+
Ref<VisualShaderNode> vsnode_new = vsnode->duplicate();
7193+
vsnode_new->set(p_property, p_value);
7194+
const int input_port_count = vsnode_new->get_input_port_count();
7195+
const int output_port_count = vsnode_new->get_output_port_count();
7196+
7197+
List<VisualShader::Connection> conns;
7198+
editor->get_visual_shader()->get_node_connections(shader_type, &conns);
7199+
VisualShaderGraphPlugin *graph_plugin = editor->get_graph_plugin();
7200+
bool undo_node_already_updated = false;
7201+
for (const VisualShader::Connection &c : conns) {
7202+
if ((c.from_node == node_id && c.from_port >= output_port_count) || (c.to_node == node_id && c.to_port >= input_port_count)) {
7203+
undo_redo->add_do_method(editor->get_visual_shader().ptr(), "disconnect_nodes", shader_type, c.from_node, c.from_port, c.to_node, c.to_port);
7204+
undo_redo->add_do_method(graph_plugin, "disconnect_nodes", shader_type, c.from_node, c.from_port, c.to_node, c.to_port);
7205+
// We need to update the node before reconnecting to avoid accessing a non-existing port.
7206+
undo_redo->add_undo_method(graph_plugin, "update_node_deferred", shader_type, node_id);
7207+
undo_node_already_updated = true;
7208+
undo_redo->add_undo_method(editor->get_visual_shader().ptr(), "connect_nodes", shader_type, c.from_node, c.from_port, c.to_node, c.to_port);
7209+
undo_redo->add_undo_method(graph_plugin, "connect_nodes", shader_type, c.from_node, c.from_port, c.to_node, c.to_port);
7210+
}
7211+
}
7212+
71867213
if (p_value.get_type() == Variant::OBJECT) {
7187-
Ref<Resource> prev_res = node->get(p_property);
7214+
Ref<Resource> prev_res = vsnode->get(p_property);
71887215
Ref<Resource> curr_res = p_value;
71897216

71907217
if (curr_res.is_null()) {
@@ -7199,14 +7226,16 @@ class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
71997226
}
72007227
}
72017228
if (p_property != "constant") {
7202-
VisualShaderGraphPlugin *graph_plugin = editor->get_graph_plugin();
72037229
if (graph_plugin) {
72047230
undo_redo->add_do_method(editor, "_update_next_previews", node_id);
72057231
undo_redo->add_undo_method(editor, "_update_next_previews", node_id);
72067232
undo_redo->add_do_method(graph_plugin, "update_node_deferred", shader_type, node_id);
7207-
undo_redo->add_undo_method(graph_plugin, "update_node_deferred", shader_type, node_id);
7233+
if (!undo_node_already_updated) {
7234+
undo_redo->add_undo_method(graph_plugin, "update_node_deferred", shader_type, node_id);
7235+
}
72087236
}
72097237
}
7238+
72107239
undo_redo->commit_action();
72117240

72127241
updating = false;

editor/plugins/visual_shader_editor_plugin.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,10 @@ class VisualShaderEditor : public VBoxContainer {
578578
void update_custom_type(const Ref<Resource> &p_resource);
579579

580580
virtual Size2 get_minimum_size() const override;
581+
581582
void edit(VisualShader *p_visual_shader);
583+
Ref<VisualShader> get_visual_shader() const { return visual_shader; }
584+
582585
VisualShaderEditor();
583586
};
584587

0 commit comments

Comments
 (0)