diff --git a/doc/classes/GraphEdit.xml b/doc/classes/GraphEdit.xml
index 3451e427f34..b8c4c2fff1c 100644
--- a/doc/classes/GraphEdit.xml
+++ b/doc/classes/GraphEdit.xml
@@ -130,8 +130,10 @@
+
Create a connection between the [param from_port] of the [param from_node] [GraphNode] and the [param to_port] of the [param to_node] [GraphNode]. If the connection already exists, no connection is created.
+ Connections with [param keep_alive] set to [code]true[/code] are not automatically deleted if invalid during a redraw.
@@ -172,7 +174,7 @@
Returns the closest connection to the given point in screen space. If no connection is found within [param max_distance] pixels, an empty [Dictionary] is returned.
- A connection consists in a structure of the form [code]{ from_port: 0, from_node: "GraphNode name 0", to_port: 1, to_node: "GraphNode name 1" }[/code].
+ A connection consists in a structure of the form [code]{ from_port: 0, from_node: "GraphNode name 0", to_port: 1, to_node: "GraphNode name 1", keep_alive: true/false }[/code].
For example, getting a connection at a given mouse position can be achieved like this:
[codeblocks]
[gdscript]
@@ -189,17 +191,11 @@
Returns the points which would make up a connection between [param from_node] and [param to_node].
-
-
-
- Returns an [Array] containing the list of connections. A connection consists in a structure of the form [code]{ from_port: 0, from_node: "GraphNode name 0", to_port: 1, to_node: "GraphNode name 1" }[/code].
-
-
- Returns an [Array] containing the list of connections that intersect with the given [Rect2]. A connection consists in a structure of the form [code]{ from_port: 0, from_node: "GraphNode name 0", to_port: 1, to_node: "GraphNode name 1" }[/code].
+ Returns an [Array] containing the list of connections that intersect with the given [Rect2]. A connection consists in a structure of the form [code]{ from_port: 0, from_node: "GraphNode name 0", to_port: 1, to_node: "GraphNode name 1", keep_alive: true/false }[/code].
@@ -288,6 +284,11 @@
The thickness of the lines between the nodes.
+
+ The connections between [GraphNode]s.
+ A connection is a [Dictionary] in the form of [code]{ from_port: 0, from_node: "GraphNode name 0", to_port: 1, to_node: "GraphNode name 1", keep_alive: true/false }[/code].
+ Connections with [code]keep_alive[/code] set to [code]true[/code] are not automatically deleted if invalid during a redraw.
+
The pattern used for drawing the grid.
diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp
index 646757008a1..c59613abb8b 100644
--- a/scene/gui/graph_edit.cpp
+++ b/scene/gui/graph_edit.cpp
@@ -259,7 +259,7 @@ PackedStringArray GraphEdit::get_configuration_warnings() const {
return warnings;
}
-Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
+Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, bool p_keep_alive) {
ERR_FAIL_NULL_V_MSG(connections_layer, FAILED, "connections_layer is missing.");
if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
@@ -272,6 +272,7 @@ Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const S
c->to_node = p_to;
c->to_port = p_to_port;
c->activity = 0;
+ c->keep_alive = p_keep_alive;
connections.push_back(c);
connection_map[p_from].push_back(c);
connection_map[p_to].push_back(c);
@@ -317,23 +318,28 @@ bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, con
void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
- for (const List[>::Element *E = connections.front(); E; E = E->next()) {
- 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) {
- connection_map[p_from].erase(E->get());
- connection_map[p_to].erase(E->get());
- E->get()->_cache.line->queue_free();
- connections.erase(E);
-
- minimap->queue_redraw();
- queue_redraw();
- connections_layer->queue_redraw();
- callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
- return;
+ Ref conn_to_remove;
+ for (const Ref &conn : connections) {
+ if (conn->from_node == p_from && conn->from_port == p_from_port && conn->to_node == p_to && conn->to_port == p_to_port) {
+ conn_to_remove = conn;
+ break;
}
}
+
+ if (conn_to_remove.is_valid()) {
+ connection_map[p_from].erase(conn_to_remove);
+ connection_map[p_to].erase(conn_to_remove);
+ conn_to_remove->_cache.line->queue_free();
+ connections.erase(conn_to_remove);
+
+ minimap->queue_redraw();
+ queue_redraw();
+ connections_layer->queue_redraw();
+ callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
+ }
}
-const List][> &GraphEdit::get_connection_list() const {
+const Vector][> &GraphEdit::get_connections() const {
return connections;
}
@@ -575,8 +581,8 @@ void GraphEdit::_graph_node_rect_changed(GraphNode *p_node) {
return;
}
- for (Ref &c : connection_map[p_node->get_name()]) {
- c->_cache.dirty = true;
+ for (Ref &conn : connection_map[p_node->get_name()]) {
+ conn->_cache.dirty = true;
}
connections_layer->queue_redraw();
callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
@@ -1289,16 +1295,16 @@ Ref GraphEdit::get_closest_connection_at_point(const Vect
Ref closest_connection;
float closest_distance = p_max_distance;
- for (const Ref &c : connections) {
- if (c->_cache.aabb.distance_to(transformed_point) > p_max_distance) {
+ for (const Ref &conn : connections) {
+ if (conn->_cache.aabb.distance_to(transformed_point) > p_max_distance) {
continue;
}
- Vector points = get_connection_line(c->_cache.from_pos * zoom, c->_cache.to_pos * zoom);
+ Vector points = get_connection_line(conn->_cache.from_pos * zoom, conn->_cache.to_pos * zoom);
for (int i = 0; i < points.size() - 1; i++) {
float distance = Geometry2D::get_distance_to_segment(transformed_point, &points[i]);
if (distance <= lines_thickness * 0.5 + p_max_distance && distance < closest_distance) {
- closest_connection = c;
+ closest_connection = conn;
closest_distance = distance;
}
}
@@ -1312,15 +1318,15 @@ List][> GraphEdit::get_connections_intersecting_with_re
transformed_rect.position += get_scroll_offset();
List][> intersecting_connections;
- for (const Ref &c : connections) {
- if (!c->_cache.aabb.intersects(transformed_rect)) {
+ for (const Ref &conn : connections) {
+ if (!conn->_cache.aabb.intersects(transformed_rect)) {
continue;
}
- Vector points = get_connection_line(c->_cache.from_pos * zoom, c->_cache.to_pos * zoom);
+ Vector points = get_connection_line(conn->_cache.from_pos * zoom, conn->_cache.to_pos * zoom);
for (int i = 0; i < points.size() - 1; i++) {
if (Geometry2D::segment_intersects_rect(points[i], points[i + 1], transformed_rect)) {
- intersecting_connections.push_back(c);
+ intersecting_connections.push_back(conn);
break;
}
}
@@ -1352,47 +1358,49 @@ void GraphEdit::_draw_minimap_connection_line(const Vector2 &p_from_graph_positi
void GraphEdit::_update_connections() {
// Collect all dead connections and remove them.
- List]>::Element *> dead_connections;
+ LocalVector[> dead_connections;
- for (List][>::Element *E = connections.front(); E; E = E->next()) {
- Ref &c = E->get();
-
- if (c->_cache.dirty) {
- Node *from = get_node_or_null(NodePath(c->from_node));
+ for (const Ref &conn : connections) {
+ if (conn->_cache.dirty) {
+ Node *from = get_node_or_null(NodePath(conn->from_node));
GraphNode *gnode_from = Object::cast_to(from);
- if (!gnode_from) {
- dead_connections.push_back(E);
+ if (!gnode_from && !conn->keep_alive) {
+ dead_connections.push_back(conn);
continue;
}
- Node *to = get_node_or_null(NodePath(c->to_node));
+ Node *to = get_node_or_null(NodePath(conn->to_node));
GraphNode *gnode_to = Object::cast_to(to);
- if (!gnode_to) {
- dead_connections.push_back(E);
+ if (!gnode_to && !conn->keep_alive) {
+ dead_connections.push_back(conn);
continue;
}
- const Vector2 from_pos = gnode_from->get_output_port_position(c->from_port) + gnode_from->get_position_offset();
- const Vector2 to_pos = gnode_to->get_input_port_position(c->to_port) + gnode_to->get_position_offset();
+ if (conn->keep_alive && (!gnode_from || !gnode_to)) {
+ continue;
+ }
- const Color from_color = gnode_from->get_output_port_color(c->from_port);
- const Color to_color = gnode_to->get_input_port_color(c->to_port);
+ const Vector2 from_pos = gnode_from->get_output_port_position(conn->from_port) + gnode_from->get_position_offset();
+ const Vector2 to_pos = gnode_to->get_input_port_position(conn->to_port) + gnode_to->get_position_offset();
- const int from_type = gnode_from->get_output_port_type(c->from_port);
- const int to_type = gnode_to->get_input_port_type(c->to_port);
+ const Color from_color = gnode_from->get_output_port_color(conn->from_port);
+ const Color to_color = gnode_to->get_input_port_color(conn->to_port);
- c->_cache.from_pos = from_pos;
- c->_cache.to_pos = to_pos;
- c->_cache.from_color = from_color;
- c->_cache.to_color = to_color;
+ const int from_type = gnode_from->get_output_port_type(conn->from_port);
+ const int to_type = gnode_to->get_input_port_type(conn->to_port);
+
+ conn->_cache.from_pos = from_pos;
+ conn->_cache.to_pos = to_pos;
+ conn->_cache.from_color = from_color;
+ conn->_cache.to_color = to_color;
PackedVector2Array line_points = get_connection_line(from_pos * zoom, to_pos * zoom);
- c->_cache.line->set_points(line_points);
+ conn->_cache.line->set_points(line_points);
- Ref line_material = c->_cache.line->get_material();
+ Ref line_material = conn->_cache.line->get_material();
if (line_material.is_null()) {
line_material.instantiate();
- c->_cache.line->set_material(line_material);
+ conn->_cache.line->set_material(line_material);
}
float line_width = _get_shader_line_width();
@@ -1402,31 +1410,31 @@ void GraphEdit::_update_connections() {
line_material->set_shader_parameter("rim_color", theme_cache.connection_rim_color);
// Compute bounding box of the line, including the line width.
- c->_cache.aabb = Rect2(line_points[0], Vector2());
+ conn->_cache.aabb = Rect2(line_points[0], Vector2());
for (int i = 0; i < line_points.size(); i++) {
- c->_cache.aabb.expand_to(line_points[i]);
+ conn->_cache.aabb.expand_to(line_points[i]);
}
- c->_cache.aabb.grow_by(lines_thickness * 0.5);
+ conn->_cache.aabb.grow_by(lines_thickness * 0.5);
- c->_cache.dirty = false;
+ conn->_cache.dirty = false;
}
// Skip updating/drawing connections that are not visible.
Rect2 viewport_rect = get_viewport_rect();
viewport_rect.position += get_scroll_offset();
- if (!c->_cache.aabb.intersects(viewport_rect)) {
+ if (!conn->_cache.aabb.intersects(viewport_rect)) {
continue;
}
- Color from_color = c->_cache.from_color;
- Color to_color = c->_cache.to_color;
+ Color from_color = conn->_cache.from_color;
+ Color to_color = conn->_cache.to_color;
- if (c->activity > 0) {
- from_color = from_color.lerp(theme_cache.activity_color, c->activity);
- to_color = to_color.lerp(theme_cache.activity_color, c->activity);
+ if (conn->activity > 0) {
+ from_color = from_color.lerp(theme_cache.activity_color, conn->activity);
+ to_color = to_color.lerp(theme_cache.activity_color, conn->activity);
}
- if (c == hovered_connection) {
+ if (conn == hovered_connection) {
from_color = from_color.blend(theme_cache.connection_hover_tint_color);
to_color = to_color.blend(theme_cache.connection_hover_tint_color);
}
@@ -1435,21 +1443,21 @@ void GraphEdit::_update_connections() {
Ref line_gradient = memnew(Gradient);
float line_width = _get_shader_line_width();
- c->_cache.line->set_width(line_width);
+ conn->_cache.line->set_width(line_width);
line_gradient->set_color(0, from_color);
line_gradient->set_color(1, to_color);
- c->_cache.line->set_gradient(line_gradient);
+ conn->_cache.line->set_gradient(line_gradient);
}
- for (const List][>::Element *E : dead_connections) {
- List][> &connections_from = connection_map[E->get()->from_node];
- List][> &connections_to = connection_map[E->get()->to_node];
- connections_from.erase(E->get());
- connections_to.erase(E->get());
- E->get()->_cache.line->queue_free();
+ for (const Ref &dead_conn : dead_connections) {
+ List][> &connections_from = connection_map[dead_conn->from_node];
+ List][> &connections_to = connection_map[dead_conn->to_node];
+ connections_from.erase(dead_conn);
+ connections_to.erase(dead_conn);
+ dead_conn->_cache.line->queue_free();
- connections.erase(E->get());
+ connections.erase(dead_conn);
}
}
@@ -1601,15 +1609,15 @@ void GraphEdit::_minimap_draw() {
}
// Draw node connections.
- for (const Ref &c : connections) {
- Vector2 from_graph_position = c->_cache.from_pos * zoom - graph_offset;
- Vector2 to_graph_position = c->_cache.to_pos * zoom - graph_offset;
- Color from_color = c->_cache.from_color;
- Color to_color = c->_cache.to_color;
+ for (const Ref &conn : connections) {
+ Vector2 from_graph_position = conn->_cache.from_pos * zoom - graph_offset;
+ Vector2 to_graph_position = conn->_cache.to_pos * zoom - graph_offset;
+ Color from_color = conn->_cache.from_color;
+ Color to_color = conn->_cache.to_color;
- if (c->activity > 0) {
- from_color = from_color.lerp(theme_cache.activity_color, c->activity);
- to_color = to_color.lerp(theme_cache.activity_color, c->activity);
+ if (conn->activity > 0) {
+ from_color = from_color.lerp(theme_cache.activity_color, conn->activity);
+ to_color = to_color.lerp(theme_cache.activity_color, conn->activity);
}
_draw_minimap_connection_line(from_graph_position, to_graph_position, from_color, to_color);
@@ -2064,16 +2072,16 @@ void GraphEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref &c : connection_map[p_from]) {
- if (c->from_node == p_from && c->from_port == p_from_port && c->to_node == p_to && c->to_port == p_to_port) {
- if (!Math::is_equal_approx(c->activity, p_activity)) {
+ for (Ref &conn : connection_map[p_from]) {
+ if (conn->from_node == p_from && conn->from_port == p_from_port && conn->to_node == p_to && conn->to_port == p_to_port) {
+ if (!Math::is_equal_approx(conn->activity, p_activity)) {
// Update only if changed.
minimap->queue_redraw();
- c->_cache.dirty = true;
+ conn->_cache.dirty = true;
connections_layer->queue_redraw();
callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
}
- c->activity = p_activity;
+ conn->activity = p_activity;
return;
}
}
@@ -2098,8 +2106,8 @@ void GraphEdit::reset_all_connection_activity() {
void GraphEdit::clear_connections() {
ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
- for (Ref &c : connections) {
- c->_cache.line->queue_free();
+ for (Ref &conn : connections) {
+ conn->_cache.line->queue_free();
}
connections.clear();
@@ -2246,8 +2254,20 @@ void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
valid_left_disconnect_types.erase(p_type);
}
+void GraphEdit::set_connections(const TypedArray &p_connections) {
+ clear_connections();
+
+ bool is_editor = Engine::get_singleton()->is_editor_hint();
+
+ for (const Dictionary &d : p_connections) {
+ // Always keep the connection alive in case it is created using the inspector.
+ bool keep_alive = (is_editor && d.is_empty()) || d["keep_alive"];
+ connect_node(d["from_node"], d["from_port"], d["to_node"], d["to_port"], keep_alive);
+ }
+}
+
TypedArray GraphEdit::_get_connection_list() const {
- List][> conns = get_connection_list();
+ Vector][> conns = get_connections();
TypedArray arr;
for (const Ref &conn : conns) {
@@ -2256,6 +2276,7 @@ TypedArray GraphEdit::_get_connection_list() const {
d["from_port"] = conn->from_port;
d["to_node"] = conn->to_node;
d["to_port"] = conn->to_port;
+ d["keep_alive"] = conn->keep_alive;
arr.push_back(d);
}
return arr;
@@ -2269,6 +2290,7 @@ Dictionary GraphEdit::_get_closest_connection_at_point(const Vector2 &p_point, f
ret["from_port"] = c->from_port;
ret["to_node"] = c->to_node;
ret["to_port"] = c->to_port;
+ ret["keep_alive"] = c->keep_alive;
}
return ret;
}
@@ -2283,6 +2305,7 @@ TypedArray GraphEdit::_get_connections_intersecting_with_rect(const
d["from_port"] = conn->from_port;
d["to_node"] = conn->to_node;
d["to_port"] = conn->to_port;
+ d["keep_alive"] = conn->keep_alive;
arr.push_back(d);
}
return arr;
@@ -2307,8 +2330,8 @@ void GraphEdit::_update_zoom_label() {
}
void GraphEdit::_invalidate_connection_line_cache() {
- for (Ref &c : connections) {
- c->_cache.dirty = true;
+ for (Ref &conn : connections) {
+ conn->_cache.dirty = true;
}
}
@@ -2635,10 +2658,11 @@ void GraphEdit::arrange_nodes() {
}
void GraphEdit::_bind_methods() {
- ClassDB::bind_method(D_METHOD("connect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::connect_node);
+ ClassDB::bind_method(D_METHOD("connect_node", "from_node", "from_port", "to_node", "to_port", "keep_alive"), &GraphEdit::connect_node, DEFVAL(false));
ClassDB::bind_method(D_METHOD("is_node_connected", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::is_node_connected);
ClassDB::bind_method(D_METHOD("disconnect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::disconnect_node);
ClassDB::bind_method(D_METHOD("set_connection_activity", "from_node", "from_port", "to_node", "to_port", "amount"), &GraphEdit::set_connection_activity);
+ ClassDB::bind_method(D_METHOD("set_connections", "connections"), &GraphEdit::set_connections);
ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
ClassDB::bind_method(D_METHOD("get_closest_connection_at_point", "point", "max_distance"), &GraphEdit::_get_closest_connection_at_point, DEFVAL(4.0));
ClassDB::bind_method(D_METHOD("get_connections_intersecting_with_rect", "rect"), &GraphEdit::_get_connections_intersecting_with_rect);
@@ -2750,6 +2774,7 @@ void GraphEdit::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_curvature"), "set_connection_lines_curvature", "get_connection_lines_curvature");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_thickness", PROPERTY_HINT_RANGE, "0,100,0.1,suffix:px"), "set_connection_lines_thickness", "get_connection_lines_thickness");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_lines_antialiased"), "set_connection_lines_antialiased", "is_connection_lines_antialiased");
+ ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "connections", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::DICTIONARY, PROPERTY_HINT_NONE, String())), "set_connections", "get_connection_list");
ADD_GROUP("Zoom", "");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
diff --git a/scene/gui/graph_edit.h b/scene/gui/graph_edit.h
index 20c98c462c4..8231cf70f68 100644
--- a/scene/gui/graph_edit.h
+++ b/scene/gui/graph_edit.h
@@ -120,6 +120,7 @@ public:
int from_port = 0;
int to_port = 0;
float activity = 0.0;
+ bool keep_alive = true;
private:
struct Cache {
@@ -238,7 +239,7 @@ private:
bool updating = false;
bool awaiting_scroll_offset_update = false;
- List][> connections;
+ Vector][> connections;
HashMap>> connection_map;
Ref hovered_connection;
@@ -339,6 +340,7 @@ private:
bool is_in_port_hotzone(const Vector2 &p_pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left);
+ void set_connections(const TypedArray &p_connections);
TypedArray _get_connection_list() const;
Dictionary _get_closest_connection_at_point(const Vector2 &p_point, float p_max_distance = 4.0) const;
TypedArray _get_connections_intersecting_with_rect(const Rect2 &p_rect) const;
@@ -397,13 +399,13 @@ public:
void _update_graph_frame(GraphFrame *p_frame);
// Connection related methods.
- Error connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
+ Error connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, bool keep_alive = false);
bool is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
void disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
- void clear_connections();
void force_connection_drag_end();
- const List][> &get_connection_list() const;
+ const Vector][> &get_connections() const;
+ void clear_connections();
virtual PackedVector2Array get_connection_line(const Vector2 &p_from, const Vector2 &p_to) const;
Ref get_closest_connection_at_point(const Vector2 &p_point, float p_max_distance = 4.0) const;
List][> get_connections_intersecting_with_rect(const Rect2 &p_rect) const;
diff --git a/scene/gui/graph_edit_arranger.cpp b/scene/gui/graph_edit_arranger.cpp
index fa1059c6676..55fbb78fab8 100644
--- a/scene/gui/graph_edit_arranger.cpp
+++ b/scene/gui/graph_edit_arranger.cpp
@@ -65,7 +65,7 @@ void GraphEditArranger::arrange_nodes() {
float gap_v = 100.0f;
float gap_h = 100.0f;
- List][> connection_list = graph_edit->get_connection_list();
+ const Vector][> connection_list = graph_edit->get_connections();
for (int i = graph_edit->get_child_count() - 1; i >= 0; i--) {
GraphNode *graph_element = Object::cast_to(graph_edit->get_child(i));
@@ -438,7 +438,7 @@ float GraphEditArranger::_calculate_threshold(const StringName &p_v, const Strin
if (p_v == p_w) {
int min_order = MAX_ORDER;
Ref incoming;
- List][> connection_list = graph_edit->get_connection_list();
+ const Vector][> connection_list = graph_edit->get_connections();
for (const Ref &connection : connection_list) {
if (connection->to_node == p_w) {
ORDER(connection->from_node, r_layers);
@@ -469,7 +469,7 @@ float GraphEditArranger::_calculate_threshold(const StringName &p_v, const Strin
// This time, pick an outgoing edge and repeat as above!
int min_order = MAX_ORDER;
Ref outgoing;
- List][> connection_list = graph_edit->get_connection_list();
+ const Vector][> connection_list = graph_edit->get_connections();
for (const Ref &connection : connection_list) {
if (connection->from_node == p_w) {
ORDER(connection->to_node, r_layers);
]