Skip to content

Commit b8b090e

Browse files
committed
CraterCrashGH-369 Fix highlight selected nodes feature
1 parent 3f2f461 commit b8b090e

File tree

2 files changed

+117
-65
lines changed

2 files changed

+117
-65
lines changed

src/editor/graph/graph_edit.cpp

Lines changed: 99 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,15 @@ void OrchestratorGraphEdit::for_each_graph_node(std::function<void(OrchestratorG
382382
}
383383
}
384384

385+
void OrchestratorGraphEdit::for_each_graph_knot(std::function<void(OrchestratorGraphKnot*)> p_func)
386+
{
387+
for (int i = 0; i < get_child_count(); i++)
388+
{
389+
if (OrchestratorGraphKnot* knot = Object::cast_to<OrchestratorGraphKnot>(get_child(i)))
390+
p_func(knot);
391+
}
392+
}
393+
385394
void OrchestratorGraphEdit::execute_action(const String& p_action_name)
386395
{
387396
Ref<InputEventAction> action = memnew(InputEventAction);
@@ -709,6 +718,29 @@ void OrchestratorGraphEdit::_drop_data(const Vector2& p_position, const Variant&
709718
}
710719
}
711720

721+
bool OrchestratorGraphEdit::highlight_selected_connections() const
722+
{
723+
return OrchestratorSettings::get_singleton()->get_setting("ui/nodes/highlight_selected_connections", false);
724+
}
725+
726+
Vector<Ref<OScriptNode>> OrchestratorGraphEdit::_get_linked_script_nodes(const Vector<Ref<OScriptNode>>& p_selected)
727+
{
728+
Vector<Ref<OScriptNode>> linked;
729+
for(const Ref<OScriptNode>& selected : p_selected)
730+
{
731+
for (const Ref<OScriptNodePin>& pin : selected->get_all_pins())
732+
{
733+
for (const Ref<OScriptNodePin>& connection : pin->get_connections())
734+
{
735+
const Ref<OScriptNode> node = connection->get_owning_node();
736+
if (!p_selected.has(node) && !linked.has(node))
737+
linked.push_back(node);
738+
}
739+
}
740+
}
741+
return linked;
742+
}
743+
712744
void OrchestratorGraphEdit::_cache_connection_knots()
713745
{
714746
_knots.clear();
@@ -761,6 +793,10 @@ PackedVector2Array OrchestratorGraphEdit::_get_connection_knot_points(const OScr
761793

762794
void OrchestratorGraphEdit::_create_connection_knot(const Dictionary& p_connection, const Vector2& p_position)
763795
{
796+
// Deselect all nodes
797+
for (OrchestratorGraphNode* node : get_selected_nodes())
798+
node->set_selected(false);
799+
764800
// Knots should be stored within any zoom applied.
765801
const Vector2 position = p_position / get_zoom();
766802
const Vector2 transformed_position = position + (get_scroll_offset() / get_zoom());
@@ -1073,6 +1109,8 @@ void OrchestratorGraphEdit::_synchronize_graph_knots()
10731109
if (!source)
10741110
continue;
10751111

1112+
Color color = source->get_output_port_color(connection.from_port);
1113+
10761114
for (int i = 0; i < E.value.size(); i++)
10771115
{
10781116
const Ref<KnotPoint>& point = E.value[i];
@@ -1081,7 +1119,7 @@ void OrchestratorGraphEdit::_synchronize_graph_knots()
10811119
graph_knot->set_owning_script(_script);
10821120
graph_knot->set_connection(connection);
10831121
graph_knot->set_knot(point);
1084-
graph_knot->set_color(source->get_output_port_color(connection.from_port));
1122+
graph_knot->set_color(color);
10851123
add_child(graph_knot);
10861124

10871125
graph_knot->connect("knot_position_changed", callable_mp_lambda(this, [&](const Vector2& position) {
@@ -1372,9 +1410,6 @@ void OrchestratorGraphEdit::_on_attempt_connection_to_empty(const StringName& p_
13721410

13731411
void OrchestratorGraphEdit::_on_node_selected(Node* p_node)
13741412
{
1375-
if (!p_node)
1376-
return;
1377-
13781413
OrchestratorGraphNode* graph_node = Object::cast_to<OrchestratorGraphNode>(p_node);
13791414
if (!graph_node)
13801415
return;
@@ -1383,43 +1418,48 @@ void OrchestratorGraphEdit::_on_node_selected(Node* p_node)
13831418
if (node.is_null())
13841419
return;
13851420

1386-
OrchestratorSettings* os = OrchestratorSettings::get_singleton();
1387-
if (os->get_setting("ui/nodes/highlight_selected_connections", false))
1421+
if (highlight_selected_connections())
13881422
{
1389-
// Get list of all selected nodes
1390-
List<Ref<OScriptNode>> selected_nodes;
1391-
for_each_graph_node([&](OrchestratorGraphNode* other) {
1392-
if (other && other->is_selected())
1393-
selected_nodes.push_back(other->get_script_node());
1394-
});
1395-
1423+
Vector<Ref<OScriptNode>> selected_nodes = get_selected_script_nodes();
13961424
if (!selected_nodes.is_empty())
13971425
{
1398-
for_each_graph_node([&](OrchestratorGraphNode* loop_node) {
1399-
loop_node->set_all_inputs_opacity(0.3f);
1400-
loop_node->set_all_outputs_opacity(0.3f);
1426+
// For each graph node, dim connection pins and wires
1427+
for_each_graph_node([&](OrchestratorGraphNode* gn) {
1428+
gn->set_all_inputs_opacity(0.3f);
1429+
gn->set_all_outputs_opacity(0.3f);
14011430
});
1402-
}
14031431

1404-
List<Ref<OScriptNode>> linked_nodes;
1405-
for (const Ref<OScriptNode>& selected : selected_nodes)
1432+
// For all knots, dim them.
1433+
for_each_graph_knot([&](OrchestratorGraphKnot* gk) {
1434+
gk->set_modulate(Color(1, 1, 1, 0.3f));
1435+
});
1436+
1437+
// Get collection of all nodes linked to the selected nodes
1438+
const Vector<Ref<OScriptNode>> linked_nodes = _get_linked_script_nodes(selected_nodes);
1439+
1440+
// For each graph node selected, dim non-connected nodes
1441+
for_each_graph_node([&](OrchestratorGraphNode* gn) {
1442+
// Initially set each node dimmed
1443+
gn->set_modulate(Color(1, 1, 1, 0.5));
1444+
1445+
// If linked, undim
1446+
if (selected_nodes.has(gn->get_script_node()) || linked_nodes.has(gn->get_script_node()))
1447+
gn->set_modulate(Color(1, 1, 1, 1));
1448+
});
1449+
}
1450+
else
14061451
{
1407-
Vector<Ref<OScriptNodePin>> pins = selected->get_all_pins();
1408-
for (const Ref<OScriptNodePin>& pin : pins)
1409-
{
1410-
const Vector<Ref<OScriptNodePin>> connections = pin->get_connections();
1411-
for (const Ref<OScriptNodePin>& connection : connections)
1412-
{
1413-
if (!selected_nodes.find(connection->get_owning_node()))
1414-
linked_nodes.push_back(connection->get_owning_node());
1415-
}
1416-
}
1452+
// For each graph node, undim connection pins and wires
1453+
for_each_graph_node([&](OrchestratorGraphNode* gn) {
1454+
gn->set_all_inputs_opacity();
1455+
gn->set_all_outputs_opacity();
1456+
});
1457+
1458+
// For all knots, undim them.
1459+
for_each_graph_knot([&](OrchestratorGraphKnot* gk) {
1460+
gk->set_modulate(Color(1, 1, 1, 1));
1461+
});
14171462
}
1418-
for_each_graph_node([&](OrchestratorGraphNode* other) {
1419-
other->set_modulate(Color(1, 1, 1, 0.5));
1420-
if (selected_nodes.find(other->get_script_node()) || linked_nodes.find(other->get_script_node()))
1421-
other->set_modulate(Color(1, 1, 1, 1));
1422-
});
14231463
}
14241464

14251465
if (!node->can_inspect_node_properties())
@@ -1440,46 +1480,40 @@ void OrchestratorGraphEdit::_on_node_selected(Node* p_node)
14401480

14411481
void OrchestratorGraphEdit::_on_node_deselected(Node* p_node)
14421482
{
1483+
OrchestratorGraphNode* graph_node = Object::cast_to<OrchestratorGraphNode>(p_node);
1484+
if (!graph_node)
1485+
return;
1486+
14431487
_plugin->get_editor_interface()->inspect_object(nullptr);
14441488

1445-
OrchestratorSettings* os = OrchestratorSettings::get_singleton();
1446-
if (os->get_setting("ui/nodes/highlight_selected_connections", false))
1489+
if (highlight_selected_connections())
14471490
{
1448-
// Get list of all selected nodes
1449-
List<Ref<OScriptNode>> selected_nodes;
1450-
for_each_graph_node([&](OrchestratorGraphNode* other) {
1451-
if (other && other->is_selected())
1452-
selected_nodes.push_back(other->get_script_node());
1453-
});
1454-
1491+
Vector<Ref<OScriptNode>> selected_nodes = get_selected_script_nodes();
14551492
if (selected_nodes.is_empty())
14561493
{
1457-
for_each_graph_node([&](OrchestratorGraphNode* other) {
1458-
other->set_modulate(Color(1, 1, 1, 1.0));
1459-
other->set_all_inputs_opacity();
1460-
other->set_all_outputs_opacity();
1494+
// For all graph nodes, undim them, their pins and connection wires
1495+
for_each_graph_node([&](OrchestratorGraphNode* gn) {
1496+
gn->set_modulate(Color(1, 1, 1, 1));
1497+
gn->set_all_inputs_opacity();
1498+
gn->set_all_outputs_opacity();
1499+
});
1500+
1501+
// For all knots, undim them.
1502+
for_each_graph_knot([&](OrchestratorGraphKnot* gk) {
1503+
gk->set_modulate(Color(1, 1, 1, 1));
14611504
});
14621505
}
14631506
else
14641507
{
1465-
List<Ref<OScriptNode>> linked_nodes;
1466-
for (const Ref<OScriptNode>& selected : selected_nodes)
1467-
{
1468-
Vector<Ref<OScriptNodePin>> pins = selected->get_all_pins();
1469-
for (const Ref<OScriptNodePin>& pin : pins)
1470-
{
1471-
const Vector<Ref<OScriptNodePin>> connections = pin->get_connections();
1472-
for (const Ref<OScriptNodePin>& connection : connections)
1473-
{
1474-
if (!selected_nodes.find(connection->get_owning_node()))
1475-
linked_nodes.push_back(connection->get_owning_node());
1476-
}
1477-
}
1478-
}
1479-
for_each_graph_node([&](OrchestratorGraphNode* other) {
1480-
other->set_modulate(Color(1, 1, 1, 0.5));
1481-
if (selected_nodes.find(other->get_script_node()) || linked_nodes.find(other->get_script_node()))
1482-
other->set_modulate(Color(1, 1, 1, 1));
1508+
const Vector<Ref<OScriptNode>> linked_nodes = _get_linked_script_nodes(selected_nodes);
1509+
// For each graph node selected, dim non-connected nodes
1510+
for_each_graph_node([&](OrchestratorGraphNode* gn) {
1511+
// Initially set each node dimmed
1512+
gn->set_modulate(Color(1, 1, 1, 0.5));
1513+
1514+
// If linked, undim
1515+
if (selected_nodes.has(gn->get_script_node()) || linked_nodes.has(gn->get_script_node()))
1516+
gn->set_modulate(Color(1, 1, 1, 1));
14831517
});
14841518
}
14851519
}

src/editor/graph/graph_edit.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,12 @@ class OrchestratorGraphEdit : public GraphEdit
178178
/// Clear all selected nodes
179179
void clear_selection();
180180

181+
/// Get a list of selected graph nodes
182+
/// @return collection of selected graph nodes
181183
Vector<OrchestratorGraphNode*> get_selected_nodes();
184+
185+
/// Get a list of selected script nodes
186+
/// @return collection of selected script nodes
182187
Vector<Ref<OScriptNode>> get_selected_script_nodes();
183188

184189
/// Causes the graph to tween focus the specified node in the graph.
@@ -212,6 +217,10 @@ class OrchestratorGraphEdit : public GraphEdit
212217
/// @param p_func the lambda to be applied
213218
void for_each_graph_node(std::function<void(OrchestratorGraphNode*)> p_func);
214219

220+
/// Perform an action for each graph knot
221+
/// @param p_func the lambda to the applied
222+
void for_each_graph_knot(std::function<void(OrchestratorGraphKnot*)> p_func);
223+
215224
/// Execute the specified action
216225
/// @param p_action_name the action to execute
217226
void execute_action(const String& p_action_name);
@@ -233,6 +242,15 @@ class OrchestratorGraphEdit : public GraphEdit
233242
//~ End GraphEdit overrides
234243

235244
private:
245+
/// Return whether to highlight selected connections
246+
/// @return true if nodes connected to selected nodes should be highlighted
247+
bool highlight_selected_connections() const;
248+
249+
/// Retrieve a collection of all linked script nodes
250+
/// @param p_selected collection of selected nodes to get links
251+
/// @return collection of linked nodes to the selected nodes
252+
Vector<Ref<OScriptNode>> _get_linked_script_nodes(const Vector<Ref<OScriptNode>>& p_selected);
253+
236254
/// Caches the graph knots for use.
237255
/// Copies the knot data from the OScriptGraph to this GraphEdit instance.
238256
void _cache_connection_knots();

0 commit comments

Comments
 (0)