From 0505b08a5607d2f488ec69cf2bd4e8b83bd97ede Mon Sep 17 00:00:00 2001 From: Johannes Witt Date: Mon, 26 Jul 2021 16:31:31 +0200 Subject: Add `GraphEdit` drag notifications This commit adds two signals: * connection_drag_begun, which is emitted when a connection is started to be created by the user and * `connection_drag_ended`, which is emitted when no longer a connection is created. Additionally `force_connection_drag_end()` adds the possibility to end the connection dragging. If called from user code, no other connection request signals are invoked. This is useful to add `GraphNode`s via shortcuts while the user is dragging a connection to directly connect the newly added node. --- scene/gui/graph_edit.cpp | 31 ++++++++++++++++++++++--------- scene/gui/graph_edit.h | 1 + 2 files changed, 23 insertions(+), 9 deletions(-) (limited to 'scene/gui') diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp index 1ddec4f587..92c770429b 100644 --- a/scene/gui/graph_edit.cpp +++ b/scene/gui/graph_edit.cpp @@ -600,6 +600,7 @@ void GraphEdit::_top_layer_input(const Ref &p_ev) { to = get_node(String(connecting_from)); //maybe it was erased if (Object::cast_to(to)) { connecting = true; + emit_signal(SNAME("connection_drag_begun"), connecting_from, connecting_index, false); } return; } @@ -616,6 +617,7 @@ void GraphEdit::_top_layer_input(const Ref &p_ev) { connecting_target = false; connecting_to = pos; just_disconnected = false; + emit_signal(SNAME("connection_drag_begun"), connecting_from, connecting_index, true); return; } } @@ -642,6 +644,7 @@ void GraphEdit::_top_layer_input(const Ref &p_ev) { fr = get_node(String(connecting_from)); //maybe it was erased if (Object::cast_to(fr)) { connecting = true; + emit_signal(SNAME("connection_drag_begun"), connecting_from, connecting_index, true); } return; } @@ -658,7 +661,7 @@ void GraphEdit::_top_layer_input(const Ref &p_ev) { connecting_target = false; connecting_to = pos; just_disconnected = false; - + emit_signal(SNAME("connection_drag_begun"), connecting_from, connecting_index, false); return; } } @@ -740,11 +743,9 @@ void GraphEdit::_top_layer_input(const Ref &p_ev) { } } - connecting = false; - top_layer->update(); - minimap->update(); - update(); - connections_layer->update(); + if (connecting) { + force_connection_drag_end(); + } } } @@ -1162,9 +1163,7 @@ void GraphEdit::gui_input(const Ref &p_ev) { minimap->update(); } else { if (connecting) { - connecting = false; - top_layer->update(); - minimap->update(); + force_connection_drag_end(); } else { emit_signal(SNAME("popup_request"), get_screen_position() + b->get_position()); } @@ -1394,6 +1393,17 @@ void GraphEdit::clear_connections() { connections_layer->update(); } +void GraphEdit::force_connection_drag_end() { + ERR_FAIL_COND_MSG(!connecting, "Drag end requested without active drag!"); + connecting = false; + connecting_valid = false; + top_layer->update(); + minimap->update(); + update(); + connections_layer->update(); + emit_signal(SNAME("connection_drag_ended")); +} + void GraphEdit::set_zoom(float p_zoom) { set_zoom_custom(p_zoom, get_size() / 2); } @@ -2165,6 +2175,7 @@ void GraphEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("set_connection_activity", "from", "from_port", "to", "to_port", "amount"), &GraphEdit::set_connection_activity); ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list); ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections); + ClassDB::bind_method(D_METHOD("force_connection_drag_end"), &GraphEdit::force_connection_drag_end); ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs); ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs); @@ -2262,6 +2273,8 @@ void GraphEdit::_bind_methods() { ADD_SIGNAL(MethodInfo("begin_node_move")); ADD_SIGNAL(MethodInfo("end_node_move")); ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs"))); + ADD_SIGNAL(MethodInfo("connection_drag_begun", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::STRING, "slot"), PropertyInfo(Variant::BOOL, "is_output"))); + ADD_SIGNAL(MethodInfo("connection_drag_ended")); } GraphEdit::GraphEdit() { diff --git a/scene/gui/graph_edit.h b/scene/gui/graph_edit.h index 7cbd0d179d..f1c4e95e38 100644 --- a/scene/gui/graph_edit.h +++ b/scene/gui/graph_edit.h @@ -267,6 +267,7 @@ public: 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(); void set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity); -- cgit v1.2.3