Skip to content

Commit 8ca7f0e

Browse files
committed
Prevent crash after removing GraphEdit's connection layer and add additional warnings
1 parent fd7239c commit 8ca7f0e

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

scene/gui/graph_edit.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ PackedStringArray GraphEdit::get_configuration_warnings() const {
260260
}
261261

262262
Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
263+
ERR_FAIL_NULL_V_MSG(connections_layer, FAILED, "connections_layer is missing.");
264+
263265
if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
264266
return OK;
265267
}
@@ -313,6 +315,8 @@ bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, con
313315
}
314316

315317
void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
318+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
319+
316320
for (const List<Ref<Connection>>::Element *E = connections.front(); E; E = E->next()) {
317321
if (E->get()->from_node == p_from && E->get()->from_port == p_from_port && E->get()->to_node == p_to && E->get()->to_port == p_to_port) {
318322
connection_map[p_from].erase(E->get());
@@ -356,6 +360,8 @@ void GraphEdit::_scroll_moved(double) {
356360
}
357361

358362
void GraphEdit::_update_scroll_offset() {
363+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
364+
359365
set_block_minimum_size_adjust(true);
360366

361367
for (int i = 0; i < get_child_count(); i++) {
@@ -524,6 +530,8 @@ void GraphEdit::_graph_element_resize_request(const Vector2 &p_new_minsize, Node
524530
}
525531

526532
void GraphEdit::_graph_frame_autoshrink_changed(const Vector2 &p_new_minsize, GraphFrame *p_frame) {
533+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
534+
527535
_update_graph_frame(p_frame);
528536

529537
minimap->queue_redraw();
@@ -535,6 +543,7 @@ void GraphEdit::_graph_frame_autoshrink_changed(const Vector2 &p_new_minsize, Gr
535543
void GraphEdit::_graph_element_moved(Node *p_node) {
536544
GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
537545
ERR_FAIL_NULL(graph_element);
546+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
538547

539548
minimap->queue_redraw();
540549
queue_redraw();
@@ -543,6 +552,7 @@ void GraphEdit::_graph_element_moved(Node *p_node) {
543552
}
544553

545554
void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_node) {
555+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
546556
GraphNode *graph_node = Object::cast_to<GraphNode>(p_node);
547557
ERR_FAIL_NULL(graph_node);
548558

@@ -558,6 +568,8 @@ void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_node) {
558568
}
559569

560570
void GraphEdit::_graph_node_rect_changed(GraphNode *p_node) {
571+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
572+
561573
// Only invalidate the cache when zooming or the node is moved/resized in graph space.
562574
if (panner->is_panning()) {
563575
return;
@@ -566,7 +578,6 @@ void GraphEdit::_graph_node_rect_changed(GraphNode *p_node) {
566578
for (Ref<Connection> &c : connection_map[p_node->get_name()]) {
567579
c->_cache.dirty = true;
568580
}
569-
570581
connections_layer->queue_redraw();
571582
callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
572583

@@ -623,7 +634,9 @@ void GraphEdit::add_child_notify(Node *p_child) {
623634
}
624635
graph_element->connect("raise_request", callable_mp(this, &GraphEdit::_ensure_node_order_from).bind(graph_element));
625636
graph_element->connect("resize_request", callable_mp(this, &GraphEdit::_graph_element_resize_request).bind(graph_element));
626-
graph_element->connect(SceneStringName(item_rect_changed), callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
637+
if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
638+
graph_element->connect(SceneStringName(item_rect_changed), callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
639+
}
627640
graph_element->connect(SceneStringName(item_rect_changed), callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
628641

629642
graph_element->set_scale(Vector2(zoom, zoom));
@@ -640,6 +653,7 @@ void GraphEdit::remove_child_notify(Node *p_child) {
640653
minimap = nullptr;
641654
} else if (p_child == connections_layer) {
642655
connections_layer = nullptr;
656+
WARN_PRINT("GraphEdit's connection_layer removed. This should not be done. If you like to remove all GraphElements from a GraphEdit node, do not simply remove all non-internal children but check their type since the connection layer has to be kept non-internal due to technical reasons.");
643657
}
644658

645659
if (top_layer != nullptr && is_inside_tree()) {
@@ -662,7 +676,9 @@ void GraphEdit::remove_child_notify(Node *p_child) {
662676
for (const Ref<Connection> &conn : connection_map[graph_node->get_name()]) {
663677
conn->_cache.dirty = true;
664678
}
665-
connections_layer->queue_redraw();
679+
if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
680+
connections_layer->queue_redraw();
681+
}
666682
}
667683

668684
GraphFrame *frame = Object::cast_to<GraphFrame>(graph_element);
@@ -1680,6 +1696,8 @@ void GraphEdit::set_selected(Node *p_child) {
16801696
}
16811697

16821698
void GraphEdit::gui_input(const Ref<InputEvent> &p_ev) {
1699+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
1700+
16831701
ERR_FAIL_COND(p_ev.is_null());
16841702
if (panner->gui_input(p_ev, warped_panning ? get_global_rect() : Rect2())) {
16851703
return;
@@ -2012,6 +2030,8 @@ void GraphEdit::gui_input(const Ref<InputEvent> &p_ev) {
20122030
}
20132031

20142032
void GraphEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
2033+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
2034+
20152035
h_scrollbar->set_value(h_scrollbar->get_value() - p_scroll_vec.x);
20162036
v_scrollbar->set_value(v_scrollbar->get_value() - p_scroll_vec.y);
20172037

@@ -2027,6 +2047,8 @@ void GraphEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputE
20272047
}
20282048

20292049
void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
2050+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
2051+
20302052
for (Ref<Connection> &c : connection_map[p_from]) {
20312053
if (c->from_node == p_from && c->from_port == p_from_port && c->to_node == p_to && c->to_port == p_to_port) {
20322054
if (!Math::is_equal_approx(c->activity, p_activity)) {
@@ -2043,6 +2065,8 @@ void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_por
20432065
}
20442066

20452067
void GraphEdit::reset_all_connection_activity() {
2068+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
2069+
20462070
bool changed = false;
20472071
for (Ref<Connection> &conn : connections) {
20482072
if (conn->activity > 0) {
@@ -2057,6 +2081,8 @@ void GraphEdit::reset_all_connection_activity() {
20572081
}
20582082

20592083
void GraphEdit::clear_connections() {
2084+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
2085+
20602086
for (Ref<Connection> &c : connections) {
20612087
c->_cache.line->queue_free();
20622088
}
@@ -2070,7 +2096,9 @@ void GraphEdit::clear_connections() {
20702096
}
20712097

20722098
void GraphEdit::force_connection_drag_end() {
2099+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
20732100
ERR_FAIL_COND_MSG(!connecting, "Drag end requested without active drag!");
2101+
20742102
connecting = false;
20752103
connecting_valid = false;
20762104
minimap->queue_redraw();
@@ -2100,6 +2128,8 @@ void GraphEdit::set_zoom(float p_zoom) {
21002128
}
21012129

21022130
void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
2131+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
2132+
21032133
p_zoom = CLAMP(p_zoom, zoom_min, zoom_max);
21042134
if (zoom == p_zoom) {
21052135
return;
@@ -2508,6 +2538,8 @@ bool GraphEdit::is_showing_arrange_button() const {
25082538
}
25092539

25102540
void GraphEdit::override_connections_shader(const Ref<Shader> &p_shader) {
2541+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
2542+
25112543
connections_shader = p_shader;
25122544

25132545
_invalidate_connection_line_cache();
@@ -2526,6 +2558,8 @@ void GraphEdit::_minimap_toggled() {
25262558
}
25272559

25282560
void GraphEdit::set_connection_lines_curvature(float p_curvature) {
2561+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
2562+
25292563
lines_curvature = p_curvature;
25302564
_invalidate_connection_line_cache();
25312565
connections_layer->queue_redraw();
@@ -2537,7 +2571,9 @@ float GraphEdit::get_connection_lines_curvature() const {
25372571
}
25382572

25392573
void GraphEdit::set_connection_lines_thickness(float p_thickness) {
2574+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
25402575
ERR_FAIL_COND_MSG(p_thickness < 0, "Connection lines thickness must be greater than or equal to 0.");
2576+
25412577
if (lines_thickness == p_thickness) {
25422578
return;
25432579
}
@@ -2552,6 +2588,8 @@ float GraphEdit::get_connection_lines_thickness() const {
25522588
}
25532589

25542590
void GraphEdit::set_connection_lines_antialiased(bool p_antialiased) {
2591+
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
2592+
25552593
if (lines_antialiased == p_antialiased) {
25562594
return;
25572595
}

0 commit comments

Comments
 (0)