Skip to content

Commit c427965

Browse files
po5dexeonify
authored andcommitted
crop: Improve click-and-drag crop selection
Second take of d5306ca because I noticed Sneakpeakcss didn't fully implement click-and-drag crop selection exactly as in the original PR. Previously, you had to click the first point before you can hold and drag to crop. This commit integrates po5's complex keybinds changes, allowing you to start, select and submit the crop in a single hold-and-drag operation! One of the roadblock to patching po5's PR directly was the condition `or (crop_first_corner.x == corner_video.x and crop_first_corner.y == corner_video.y)`. It was causing errors due to a missing `corner_video` variable, which was removed in occivink/mpv-scripts@af360f3. Thus, I've partially restored the variable to fix this. Note: Unlike Sneakpeakcss's version, click-and-drag and point-and-click are mutually exclusive. You can't click to start the crop, then hold-and-drag to select the area. It was unintuitive anyways. Ref: occivink/mpv-scripts#62
1 parent d5306ca commit c427965

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

scripts/crop.lua

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,11 @@ function update_crop_zone_state()
345345
return
346346
end
347347
cursor = clamp_point(cursor, dim)
348-
if crop_first_corner == nil then
349-
crop_first_corner = screen_to_video_norm(cursor, dim)
348+
local corner_video = screen_to_video_norm(cursor, dim)
349+
if crop_first_corner == nil or
350+
(crop_first_corner.x == corner_video.x and
351+
crop_first_corner.y == corner_video.y) then
352+
crop_first_corner = corner_video
350353
redraw()
351354
else
352355
local c1, c2 = rect_from_two_points(
@@ -362,23 +365,24 @@ function update_crop_zone_state()
362365
end
363366

364367
local bindings = {}
368+
local bindings_complex = {}
365369
local bindings_repeat = {}
366370

367371
function cancel_crop()
368372
crop_first_corner = nil
369373
for key, _ in pairs(bindings) do
370374
mp.remove_key_binding("crop-"..key)
371375
end
376+
for key, _ in pairs(bindings_complex) do
377+
mp.remove_key_binding("crop-"..key)
378+
end
372379
for key, _ in pairs(bindings_repeat) do
373380
mp.remove_key_binding("crop-"..key)
374381
end
375382
mp.unobserve_property(redraw)
376383
mp.unregister_idle(draw_crop_zone)
377384
mp.set_osd_ass(1280, 720, '')
378385
active = false
379-
if opts.disable_window_dragging and not mp.get_property_bool("window-dragging") then
380-
mp.set_property_bool("window-dragging", true)
381-
end
382386
end
383387

384388
-- adjust coordinates based on previous values
@@ -539,6 +543,9 @@ function start_crop(mode)
539543
for key, func in pairs(bindings) do
540544
mp.add_forced_key_binding(key, "crop-"..key, func)
541545
end
546+
for key, func in pairs(bindings_complex) do
547+
mp.add_forced_key_binding(key, "crop-"..key, func, { complex = true })
548+
end
542549
for key, func in pairs(bindings_repeat) do
543550
mp.add_forced_key_binding(key, "crop-"..key, func, { repeatable = true })
544551
end
@@ -577,7 +584,18 @@ if opts.mouse_support then
577584
bindings["MOUSE_MOVE"] = function() cursor.x, cursor.y = mp.get_mouse_pos(); redraw() end
578585
end
579586
for _, key in ipairs(opts.accept) do
580-
bindings[key] = update_crop_zone_state
587+
if string.find(key:lower(), "btn") then
588+
bindings_complex[key] = function(mouse)
589+
if opts.disable_window_dragging then
590+
mp.set_property_bool("window-dragging", mouse["event"] ~= "down")
591+
end
592+
if crop_first_corner == nil or mouse["event"] ~= "down" then
593+
update_crop_zone_state()
594+
end
595+
end
596+
else
597+
bindings[key] = update_crop_zone_state
598+
end
581599
end
582600
for _, key in ipairs(opts.cancel) do
583601
bindings[key] = cancel_crop

0 commit comments

Comments
 (0)