From c5199a50171a523c0d4fbc87cdf1abe0e808b864 Mon Sep 17 00:00:00 2001 From: lullabyist Date: Tue, 22 Oct 2024 04:46:59 +0530 Subject: [PATCH] Prioritize proximity to green arrow when selecting overlapping transitions in AnimationNodeStateMachine Applied suggestions from code review --- .../animation_state_machine_editor.cpp | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/editor/plugins/animation_state_machine_editor.cpp b/editor/plugins/animation_state_machine_editor.cpp index e9dd54f73be..ef1ff01ac80 100644 --- a/editor/plugins/animation_state_machine_editor.cpp +++ b/editor/plugins/animation_state_machine_editor.cpp @@ -228,9 +228,12 @@ void AnimationNodeStateMachineEditor::_state_machine_gui_input(const Ref close_candidates; + + // First find closest lines using point-to-segment distance. for (int i = 0; i < transition_lines.size(); i++) { Vector2 s[2] = { transition_lines[i].from, @@ -238,13 +241,34 @@ void AnimationNodeStateMachineEditor::_state_machine_gui_input(const Refget_position(), s); float d = cpoint.distance_to(mb->get_position()); + if (d > transition_lines[i].width) { continue; } - if (d < closest_d) { - closest = i; + // If this is very close to our current closest distance, add it to candidates. + if (Math::abs(d - closest_d) < 2.0) { // Within 2 pixels. + close_candidates.push_back(i); + } else if (d < closest_d) { closest_d = d; + closest = i; + close_candidates.clear(); + close_candidates.push_back(i); + } + } + + // Use midpoint distance as bias. + if (close_candidates.size() > 1) { + float best_midpoint_dist = 1e20; + + for (int idx : close_candidates) { + Vector2 midpoint = (transition_lines[idx].from + transition_lines[idx].to) / 2.0; + float midpoint_dist = midpoint.distance_to(mb->get_position()); + + if (midpoint_dist < best_midpoint_dist) { + best_midpoint_dist = midpoint_dist; + closest = idx; + } } }