diff options
author | kobewi <kobewi4e@gmail.com> | 2022-08-12 13:06:08 +0200 |
---|---|---|
committer | kobewi <kobewi4e@gmail.com> | 2022-12-02 22:57:23 +0100 |
commit | 5490c22ffbb41ac31768ac4667b9d053886c6e6c (patch) | |
tree | bc6a632d4161637692bff80ada4d444ecada5e07 /scene/gui | |
parent | 6f1d4fd8871155efcb29d115a7168879948e1cf3 (diff) |
Improve usage of StringNames in GraphEdit
Diffstat (limited to 'scene/gui')
-rw-r--r-- | scene/gui/graph_edit.cpp | 89 | ||||
-rw-r--r-- | scene/gui/graph_edit.h | 4 |
2 files changed, 31 insertions, 62 deletions
diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp index 40792dd43f..1c4c8c2574 100644 --- a/scene/gui/graph_edit.cpp +++ b/scene/gui/graph_edit.cpp @@ -623,7 +623,7 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) { //check disconnect for (const Connection &E : connections) { if (E.from == gn->get_name() && E.from_port == j) { - Node *to = get_node(String(E.to)); + Node *to = get_node(NodePath(E.to)); if (Object::cast_to<GraphNode>(to)) { connecting_from = E.to; connecting_index = E.to_port; @@ -637,7 +637,7 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) { just_disconnected = true; emit_signal(SNAME("disconnection_request"), E.from, E.from_port, E.to, E.to_port); - to = get_node(String(connecting_from)); //maybe it was erased + to = get_node(NodePath(connecting_from)); // Maybe it was erased. if (Object::cast_to<GraphNode>(to)) { connecting = true; emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, false); @@ -673,10 +673,10 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) { if (is_in_input_hotzone(gn, j, click_pos, port_size)) { if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) { - //check disconnect + // Check disconnect. for (const Connection &E : connections) { if (E.to == gn->get_name() && E.to_port == j) { - Node *fr = get_node(String(E.from)); + Node *fr = get_node(NodePath(E.from)); if (Object::cast_to<GraphNode>(fr)) { connecting_from = E.from; connecting_index = E.from_port; @@ -689,7 +689,7 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) { if (connecting_type >= 0) { emit_signal(SNAME("disconnection_request"), E.from, E.from_port, E.to, E.to_port); - fr = get_node(String(connecting_from)); //maybe it was erased + fr = get_node(NodePath(connecting_from)); // Maybe it was erased. if (Object::cast_to<GraphNode>(fr)) { connecting = true; emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, true); @@ -780,26 +780,16 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) { if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) { if (connecting_valid) { if (connecting && connecting_target) { - String from = connecting_from; - int from_port = connecting_index; - String to = connecting_target_to; - int to_port = connecting_target_index; - - if (!connecting_out) { - SWAP(from, to); - SWAP(from_port, to_port); + if (connecting_out) { + emit_signal(SNAME("connection_request"), connecting_from, connecting_index, connecting_target_to, connecting_target_index); + } else { + emit_signal(SNAME("connection_request"), connecting_target_to, connecting_target_index, connecting_from, connecting_index); } - emit_signal(SNAME("connection_request"), from, from_port, to, to_port); - } else if (!just_disconnected) { - String from = connecting_from; - int from_port = connecting_index; - Vector2 ofs = mb->get_position(); - - if (!connecting_out) { - emit_signal(SNAME("connection_from_empty"), from, from_port, ofs); + if (connecting_out) { + emit_signal(SNAME("connection_to_empty"), connecting_from, connecting_index, mb->get_position()); } else { - emit_signal(SNAME("connection_to_empty"), from, from_port, ofs); + emit_signal(SNAME("connection_from_empty"), connecting_from, connecting_index, mb->get_position()); } } } @@ -935,17 +925,12 @@ void GraphEdit::_draw_connection_line(CanvasItem *p_where, const Vector2 &p_from void GraphEdit::_connections_layer_draw() { Color activity_color = get_theme_color(SNAME("activity")); - //draw connections + // Draw connections. List<List<Connection>::Element *> to_erase; for (List<Connection>::Element *E = connections.front(); E; E = E->next()) { - NodePath fromnp(E->get().from); - - Node *from = get_node(fromnp); - if (!from) { - to_erase.push_back(E); - continue; - } + const Connection &c = E->get(); + Node *from = get_node(NodePath(c.from)); GraphNode *gfrom = Object::cast_to<GraphNode>(from); if (!gfrom) { @@ -953,13 +938,7 @@ void GraphEdit::_connections_layer_draw() { continue; } - NodePath tonp(E->get().to); - Node *to = get_node(tonp); - if (!to) { - to_erase.push_back(E); - continue; - } - + Node *to = get_node(NodePath(c.to)); GraphNode *gto = Object::cast_to<GraphNode>(to); if (!gto) { @@ -967,21 +946,20 @@ void GraphEdit::_connections_layer_draw() { continue; } - Vector2 frompos = gfrom->get_connection_output_position(E->get().from_port) + gfrom->get_position_offset() * zoom; - Color color = gfrom->get_connection_output_color(E->get().from_port); - Vector2 topos = gto->get_connection_input_position(E->get().to_port) + gto->get_position_offset() * zoom; - Color tocolor = gto->get_connection_input_color(E->get().to_port); + Vector2 frompos = gfrom->get_connection_output_position(c.from_port) + gfrom->get_position_offset() * zoom; + Color color = gfrom->get_connection_output_color(c.from_port); + Vector2 topos = gto->get_connection_input_position(c.to_port) + gto->get_position_offset() * zoom; + Color tocolor = gto->get_connection_input_color(c.to_port); - if (E->get().activity > 0) { - color = color.lerp(activity_color, E->get().activity); - tocolor = tocolor.lerp(activity_color, E->get().activity); + if (c.activity > 0) { + color = color.lerp(activity_color, c.activity); + tocolor = tocolor.lerp(activity_color, c.activity); } _draw_connection_line(connections_layer, frompos, topos, color, tocolor, lines_thickness, zoom); } - while (to_erase.size()) { - connections.erase(to_erase.front()->get()); - to_erase.pop_front(); + for (List<Connection>::Element *&E : to_erase) { + connections.erase(E); } } @@ -989,7 +967,7 @@ void GraphEdit::_top_layer_draw() { _update_scroll(); if (connecting) { - Node *fromn = get_node(connecting_from); + Node *fromn = get_node(NodePath(connecting_from)); ERR_FAIL_COND(!fromn); GraphNode *from = Object::cast_to<GraphNode>(fromn); ERR_FAIL_COND(!from); @@ -1087,22 +1065,13 @@ void GraphEdit::_minimap_draw() { // Draw node connections. Color activity_color = get_theme_color(SNAME("activity")); for (const Connection &E : connections) { - NodePath fromnp(E.from); - - Node *from = get_node(fromnp); - if (!from) { - continue; - } + Node *from = get_node(NodePath(E.from)); GraphNode *gfrom = Object::cast_to<GraphNode>(from); if (!gfrom) { continue; } - NodePath tonp(E.to); - Node *to = get_node(tonp); - if (!to) { - continue; - } + Node *to = get_node(NodePath(E.to)); GraphNode *gto = Object::cast_to<GraphNode>(to); if (!gto) { continue; @@ -2428,7 +2397,7 @@ 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, "offset"))); - ADD_SIGNAL(MethodInfo("connection_drag_started", PropertyInfo(Variant::STRING, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::BOOL, "is_output"))); + ADD_SIGNAL(MethodInfo("connection_drag_started", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::BOOL, "is_output"))); ADD_SIGNAL(MethodInfo("connection_drag_ended")); BIND_ENUM_CONSTANT(SCROLL_ZOOMS); diff --git a/scene/gui/graph_edit.h b/scene/gui/graph_edit.h index 101087bdbd..eda7ddd824 100644 --- a/scene/gui/graph_edit.h +++ b/scene/gui/graph_edit.h @@ -136,14 +136,14 @@ private: bool arrange_nodes_button_hidden = false; bool connecting = false; - String connecting_from; + StringName connecting_from; bool connecting_out = false; int connecting_index = 0; int connecting_type = 0; Color connecting_color; bool connecting_target = false; Vector2 connecting_to; - String connecting_target_to; + StringName connecting_target_to; int connecting_target_index = 0; bool just_disconnected = false; bool connecting_valid = false; |