Skip to content

Commit a2f3048

Browse files
committed
crop: Refactor remove_crop() out of toggle_crop()
Another approach to occivink/mpv-scripts#77 The code for removing the last crop filter is already in toggle_crop(), I just have to break it down into smaller functions. Firstly, remove_delogo() is invoked to remove the last delogo filter. If a delogo filter exists, the function will remove it and return true; remove_hard() won't be called. Conversely, if no delogo filter is found, remove_hard() is invoked to determine the state of video-crop. Finally, if no delogo filter is present or video-crop is disabled, toggle_crop() will call start_crop(). The order of crop removal will be made configurable in the next few commits.
1 parent fb6bc2b commit a2f3048

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

scripts/crop.lua

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,41 @@ function cancel_crop()
357357
active = false
358358
end
359359

360+
function remove_crop()
361+
local remove_delogo = function()
362+
local vf_table = mp.get_property_native("vf")
363+
if #vf_table > 0 then
364+
for i = #vf_table, 1, -1 do
365+
if vf_table[i].name == "delogo" then
366+
for j = i, #vf_table-1 do
367+
vf_table[j] = vf_table[j+1]
368+
end
369+
vf_table[#vf_table] = nil
370+
mp.set_property_native("vf", vf_table)
371+
mp.osd_message("Removed delogo filter.")
372+
local subdata = mp.get_property_native("sub-ass-extradata")
373+
if subdata ~= nil and opts.fix_borders then
374+
local playresx = subdata:match("PlayResX:%s*(%d+)")
375+
mp.set_property_native("sub-ass-force-style", "PlayResX=" .. playresx)
376+
end
377+
return true
378+
end
379+
end
380+
end
381+
return false
382+
end
383+
local remove_hard = function()
384+
video_crop = mp.get_property_native("video-crop")
385+
if video_crop == "" then
386+
return false
387+
end
388+
mp.set_property_native("video-crop", "")
389+
mp.osd_message("Reset video-crop to empty.")
390+
return true
391+
end
392+
return remove_delogo() or remove_hard()
393+
end
394+
360395
function start_crop(mode)
361396
if active then return end
362397
if not mp.get_property_native("osd-dimensions") then return end
@@ -395,40 +430,7 @@ function toggle_crop(mode)
395430
end
396431
local toggle_mode = mode or opts.mode
397432
if toggle_mode == "soft" then return end -- can't toggle soft mode
398-
399-
local remove_delogo = function()
400-
local vf_table = mp.get_property_native("vf")
401-
if #vf_table > 0 then
402-
for i = #vf_table, 1, -1 do
403-
if vf_table[i].name == "delogo" then
404-
for j = i, #vf_table-1 do
405-
vf_table[j] = vf_table[j+1]
406-
end
407-
vf_table[#vf_table] = nil
408-
mp.set_property_native("vf", vf_table)
409-
local subdata = mp.get_property_native("sub-ass-extradata")
410-
if subdata ~= nil and opts.fix_borders then
411-
local playresx = subdata:match("PlayResX:%s*(%d+)")
412-
mp.set_property_native("sub-ass-force-style", "PlayResX=" .. playresx)
413-
end
414-
return true
415-
end
416-
end
417-
end
418-
return false
419-
end
420-
if toggle_mode == "delogo" and not remove_delogo() then
421-
start_crop(mode)
422-
end
423-
local remove_hard = function()
424-
video_crop = mp.get_property_native("video-crop")
425-
if video_crop == "" then
426-
return false
427-
end
428-
mp.set_property_native("video-crop", "")
429-
return true
430-
end
431-
if toggle_mode == "hard" and not remove_hard() then
433+
if not remove_crop() then -- only start_crop if no crops are removed
432434
start_crop(mode)
433435
end
434436
end
@@ -459,6 +461,6 @@ bindings_repeat[opts.right_fine] = movement_func(opts.fine_movement, 0)
459461
bindings_repeat[opts.up_fine] = movement_func(0, -opts.fine_movement)
460462
bindings_repeat[opts.down_fine] = movement_func(0, opts.fine_movement)
461463

462-
464+
mp.add_key_binding(nil, "remove-crop", remove_crop)
463465
mp.add_key_binding(nil, "start-crop", start_crop)
464466
mp.add_key_binding(nil, "toggle-crop", toggle_crop)

0 commit comments

Comments
 (0)