@@ -574,13 +574,16 @@ void Polygon2DEditor::_canvas_input(const Ref<InputEvent> &p_input) {
574574 return ;
575575 }
576576
577+ const real_t grab_threshold = EDITOR_GET (" editors/polygon_editor/point_grab_radius" );
577578 int closest = -1 ;
579+ real_t closest_dist = Math::INF;
578580
579- for (int i = editing_points.size () - 1 ; i >= editing_points.size () - internal_vertices; i--) {
581+ for (int i = editing_points.size () - 1 ; i >= editing_points.size () - internal_vertices && closest_dist >= 8 ; i--) {
580582 Vector2 tuv = mtx.xform (previous_polygon[i]);
581- if (tuv.distance_to (mb->get_position ()) < 8 ) {
583+ const real_t dist = tuv.distance_to (mb->get_position ());
584+ if (dist < grab_threshold && dist < closest_dist) {
582585 closest = i;
583- break ;
586+ closest_dist = dist ;
584587 }
585588 }
586589
@@ -625,11 +628,15 @@ void Polygon2DEditor::_canvas_input(const Ref<InputEvent> &p_input) {
625628 }
626629
627630 if (current_action == ACTION_EDIT_POINT) {
631+ const real_t grab_threshold = EDITOR_GET (" editors/polygon_editor/point_grab_radius" );
628632 point_drag_index = -1 ;
629- for (int i = 0 ; i < editing_points.size (); i++) {
630- if (mtx.xform (editing_points[i]).distance_to (mb->get_position ()) < 8 ) {
633+ real_t closest_dist = Math::INF;
634+ for (int i = editing_points.size () - 1 ; i >= 0 && closest_dist >= 8 ; i--) {
635+ const real_t dist = mtx.xform (editing_points[i]).distance_to (mb->get_position ());
636+ if (dist < grab_threshold && dist < closest_dist) {
631637 drag_from = mb->get_position ();
632638 point_drag_index = i;
639+ closest_dist = dist;
633640 }
634641 }
635642
@@ -639,13 +646,16 @@ void Polygon2DEditor::_canvas_input(const Ref<InputEvent> &p_input) {
639646 }
640647
641648 if (current_action == ACTION_ADD_POLYGON) {
649+ const real_t grab_threshold = EDITOR_GET (" editors/polygon_editor/point_grab_radius" );
642650 int closest = -1 ;
651+ real_t closest_dist = Math::INF;
643652
644- for (int i = editing_points.size () - 1 ; i >= 0 ; i--) {
653+ for (int i = editing_points.size () - 1 ; i >= 0 && closest_dist >= 8 ; i--) {
645654 Vector2 tuv = mtx.xform (editing_points[i]);
646- if (tuv.distance_to (mb->get_position ()) < 8 ) {
655+ const real_t dist = tuv.distance_to (mb->get_position ());
656+ if (dist < grab_threshold && dist < closest_dist) {
647657 closest = i;
648- break ;
658+ closest_dist = dist ;
649659 }
650660 }
651661
0 commit comments