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.
This commit is contained in:
2025-12-23 09:04:39 +01:00
parent 958e066042
commit 25112d496b

View File

@@ -3773,23 +3773,35 @@ class VideoEditor:
# Handle crop border dragging (only when Shift and Ctrl are NOT pressed) # 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: 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 # 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)) 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) 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) 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(): def detect_border():
if abs(x - sx1) < border_threshold and sy1 <= y <= sy2: # Calculate distances to each border
return 'left' dist_left = abs(x - sx1) if sy1 <= y <= sy2 else float('inf')
elif abs(x - sx2) < border_threshold and sy1 <= y <= sy2: dist_right = abs(x - sx2) if sy1 <= y <= sy2 else float('inf')
return 'right' dist_top = abs(y - sy1) if sx1 <= x <= sx2 else float('inf')
elif abs(y - sy1) < border_threshold and sx1 <= x <= sx2: dist_bottom = abs(y - sy2) if sx1 <= x <= sx2 else float('inf')
return 'top'
elif abs(y - sy2) < border_threshold and sx1 <= x <= sx2: # Find closest border
return 'bottom' 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 return None
if event == cv2.EVENT_LBUTTONDOWN: if event == cv2.EVENT_LBUTTONDOWN: