Merge pull request #50896 from HaSa1002/add-graphedit-drag-signals-4

Add `GraphEdit` drag notifications
This commit is contained in:
Rémi Verschelde 2022-01-06 19:31:22 +01:00 committed by GitHub
commit f5d281d55f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 9 deletions

View file

@ -109,6 +109,14 @@
Removes the connection between the [code]from_port[/code] slot of the [code]from[/code] GraphNode and the [code]to_port[/code] slot of the [code]to[/code] GraphNode. If the connection does not exist, no connection is removed. Removes the connection between the [code]from_port[/code] slot of the [code]from[/code] GraphNode and the [code]to_port[/code] slot of the [code]to[/code] GraphNode. If the connection does not exist, no connection is removed.
</description> </description>
</method> </method>
<method name="force_connection_drag_end">
<return type="void" />
<description>
Ends the creation of the current connection. In other words, if you are dragging a connection you can use this method to abort the process and remove the line that followed your cursor.
This is best used together with [signal connection_drag_begun] and [signal connection_drag_ended] to add custom behavior like node addition through shortcuts.
[b]Note:[/b] This method suppresses any other connection request signals apart from [signal connection_drag_ended].
</description>
</method>
<method name="get_connection_line"> <method name="get_connection_line">
<return type="PackedVector2Array" /> <return type="PackedVector2Array" />
<argument index="0" name="from" type="Vector2" /> <argument index="0" name="from" type="Vector2" />
@ -241,6 +249,19 @@
Emitted at the beginning of a GraphNode movement. Emitted at the beginning of a GraphNode movement.
</description> </description>
</signal> </signal>
<signal name="connection_drag_begun">
<argument index="0" name="from" type="String" />
<argument index="1" name="slot" type="String" />
<argument index="2" name="is_output" type="bool" />
<description>
Emitted at the beginning of a connection drag.
</description>
</signal>
<signal name="connection_drag_ended">
<description>
Emitted at the end of a connection drag.
</description>
</signal>
<signal name="connection_from_empty"> <signal name="connection_from_empty">
<argument index="0" name="to" type="StringName" /> <argument index="0" name="to" type="StringName" />
<argument index="1" name="to_slot" type="int" /> <argument index="1" name="to_slot" type="int" />

View file

@ -600,6 +600,7 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
to = get_node(String(connecting_from)); //maybe it was erased to = get_node(String(connecting_from)); //maybe it was erased
if (Object::cast_to<GraphNode>(to)) { if (Object::cast_to<GraphNode>(to)) {
connecting = true; connecting = true;
emit_signal(SNAME("connection_drag_begun"), connecting_from, connecting_index, false);
} }
return; return;
} }
@ -616,6 +617,7 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
connecting_target = false; connecting_target = false;
connecting_to = pos; connecting_to = pos;
just_disconnected = false; just_disconnected = false;
emit_signal(SNAME("connection_drag_begun"), connecting_from, connecting_index, true);
return; return;
} }
} }
@ -642,6 +644,7 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
fr = get_node(String(connecting_from)); //maybe it was erased fr = get_node(String(connecting_from)); //maybe it was erased
if (Object::cast_to<GraphNode>(fr)) { if (Object::cast_to<GraphNode>(fr)) {
connecting = true; connecting = true;
emit_signal(SNAME("connection_drag_begun"), connecting_from, connecting_index, true);
} }
return; return;
} }
@ -658,7 +661,7 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
connecting_target = false; connecting_target = false;
connecting_to = pos; connecting_to = pos;
just_disconnected = false; just_disconnected = false;
emit_signal(SNAME("connection_drag_begun"), connecting_from, connecting_index, false);
return; return;
} }
} }
@ -740,11 +743,9 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
} }
} }
connecting = false; if (connecting) {
top_layer->update(); force_connection_drag_end();
minimap->update(); }
update();
connections_layer->update();
} }
} }
@ -1162,9 +1163,7 @@ void GraphEdit::gui_input(const Ref<InputEvent> &p_ev) {
minimap->update(); minimap->update();
} else { } else {
if (connecting) { if (connecting) {
connecting = false; force_connection_drag_end();
top_layer->update();
minimap->update();
} else { } else {
emit_signal(SNAME("popup_request"), get_screen_position() + b->get_position()); emit_signal(SNAME("popup_request"), get_screen_position() + b->get_position());
} }
@ -1394,6 +1393,17 @@ void GraphEdit::clear_connections() {
connections_layer->update(); 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) { void GraphEdit::set_zoom(float p_zoom) {
set_zoom_custom(p_zoom, get_size() / 2); 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("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("get_connection_list"), &GraphEdit::_get_connection_list);
ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections); 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("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_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("begin_node_move"));
ADD_SIGNAL(MethodInfo("end_node_move")); ADD_SIGNAL(MethodInfo("end_node_move"));
ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs"))); 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() { GraphEdit::GraphEdit() {

View file

@ -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); 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 disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port);
void clear_connections(); 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); void set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity);