From 25112d496be50b33f3484c44fbdadcb53521e9dd Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Tue, 23 Dec 2025 09:04:39 +0100 Subject: [PATCH] Refactor border detection logic in crop functionality to target nearest edge instead of whatever the fuck random one Enhance the border detection mechanism in the VideoEditor class by calculating distances to each border and identifying the closest one. This change ensures that the closest border is returned only if within the specified threshold, improving user interaction during crop adjustments. --- croppa/main.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/croppa/main.py b/croppa/main.py index f3912a6..13b166d 100644 --- a/croppa/main.py +++ b/croppa/main.py @@ -3773,23 +3773,35 @@ class VideoEditor: # Handle crop border dragging (only when Shift and Ctrl are NOT pressed) if not (flags & cv2.EVENT_FLAG_SHIFTKEY) and not (flags & cv2.EVENT_FLAG_CTRLKEY) and self.crop_rect: - border_threshold = 800 # pixels + border_threshold = 800 # pixels - maximum distance to consider # Get effective crop in rotated coords and map to screen eff_x, eff_y, eff_w, eff_h = self._get_effective_crop_rect_for_frame(getattr(self, 'current_frame', 0)) sx1, sy1 = self._map_rotated_to_screen(eff_x, eff_y) sx2, sy2 = self._map_rotated_to_screen(eff_x + eff_w, eff_y + eff_h) - # Detect which border is near + # Detect which border is closest to cursor def detect_border(): - if abs(x - sx1) < border_threshold and sy1 <= y <= sy2: - return 'left' - elif abs(x - sx2) < border_threshold and sy1 <= y <= sy2: - return 'right' - elif abs(y - sy1) < border_threshold and sx1 <= x <= sx2: - return 'top' - elif abs(y - sy2) < border_threshold and sx1 <= x <= sx2: - return 'bottom' + # Calculate distances to each border + dist_left = abs(x - sx1) if sy1 <= y <= sy2 else float('inf') + dist_right = abs(x - sx2) if sy1 <= y <= sy2 else float('inf') + dist_top = abs(y - sy1) if sx1 <= x <= sx2 else float('inf') + dist_bottom = abs(y - sy2) if sx1 <= x <= sx2 else float('inf') + + # Find closest border + distances = { + 'left': dist_left, + 'right': dist_right, + 'top': dist_top, + 'bottom': dist_bottom + } + + closest_edge = min(distances, key=distances.get) + closest_dist = distances[closest_edge] + + # Only return if within threshold + if closest_dist < border_threshold: + return closest_edge return None if event == cv2.EVENT_LBUTTONDOWN: