Skip to content

Commit

Permalink
TimingBinds: Add commands to shift by end time
Browse files Browse the repository at this point in the history
Also, make the "shift all following" lines also include the selection
and clean up the code a bit
  • Loading branch information
arch1t3cht committed Aug 8, 2023
1 parent 4d1f029 commit 9619246
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions macros/arch.TimingBinds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ function has_video(subs, sel)
return true
end

function shift_frames_to_video(subs, sel, active_line, after)
active_line_frame = aegisub.frame_from_ms(subs[active_line].start_time)
function shift_frames_to_video(end_time, after) return function(subs, sel, active_line)
active_line_frame = aegisub.frame_from_ms(end_time and subs[active_line].end_time or subs[active_line].start_time)
video_frame = aegisub.project_properties().video_position
if video_frame == nil then
return false, "No video opened!"
Expand All @@ -67,42 +67,28 @@ function shift_frames_to_video(subs, sel, active_line, after)
frame_diff = video_frame - active_line_frame

if after then
newsel = {}
-- Some ugly code to make sure sel stays sorted and unique. This doesn't currently use depctrl,
-- so I don't want to depend on Functional
for i, line in ipairs(subs) do
if line.class == "dialogue" and line.start_time >= subs[active_line].start_time then
table.insert(newsel, i)
else
for _, si in ipairs(sel) do
if si == i then
table.insert(newsel, i)
break
end
end
table.insert(sel, i)
end
end
sel = newsel
end

table.sort(sel)
processed = {}

for i, s in ipairs(sel) do
line = subs[s]
if not processed[s] then
line = subs[s]

line.start_time = aegisub.ms_from_frame(aegisub.frame_from_ms(line.start_time) + frame_diff)
line.end_time = aegisub.ms_from_frame(aegisub.frame_from_ms(line.end_time) + frame_diff)
line.start_time = aegisub.ms_from_frame(aegisub.frame_from_ms(line.start_time) + frame_diff)
line.end_time = aegisub.ms_from_frame(aegisub.frame_from_ms(line.end_time) + frame_diff)

subs[s] = line
subs[s] = line
processed[s] = true
end
end
end

-- Lua (or is it Aegisub?) is being stupid, so no I can't use vararg for this
function shift_frames_sel(a, b, c)
return shift_frames_to_video(a, b, c, false)
end

function shift_frames_after(a, b, c)
return shift_frames_to_video(a, b, c, true)
end
end end

function join_previous(subs, sel, active_line)
sstart, send = aegisub.get_audio_selection()
Expand All @@ -122,5 +108,7 @@ end
aegisub.register_macro("Timing Binds/Snap Beginning to Frame","Snap the current line's beginning to the current frame, but also snap the previous line's end, if the lines were joined.",snap_beginning_to_video, has_video)
aegisub.register_macro("Timing Binds/Snap End to Frame","Snap the current line's end to the current frame, but also snap the following line's end, if the lines were joined.",snap_end_to_video, has_video)
aegisub.register_macro("Timing Binds/Join Previous Line","Joins the previous line's end to the current line's beginning.", join_previous)
aegisub.register_macro("Timing Binds/Shift by Frames to Video Position","Shifts the selection such that the active line starts at the video position, but shifts by frames instead of by milliseconds.", shift_frames_sel, has_video)
aegisub.register_macro("Timing Binds/Shift all following lines by Frames to Video Position","Shifts all following lines such that the active line starts at the video position, but shifts by frames instead of by milliseconds.", shift_frames_after, has_video)
aegisub.register_macro("Timing Binds/Shift by Frames to Video Position","Shifts the selection such that the active line starts at the video position, but shifts by frames instead of by milliseconds.", shift_frames_to_video(false, false), has_video)
aegisub.register_macro("Timing Binds/Shift by Frames to Video Position (End)","Shifts the selection such that the active line end at the video position, but shifts by frames instead of by milliseconds.", shift_frames_to_video(true, false), has_video)
aegisub.register_macro("Timing Binds/Shift all following lines by Frames to Video Position","Shifts all following lines such that the active line starts at the video position, but shifts by frames instead of by milliseconds.", shift_frames_to_video(false, true), has_video)
aegisub.register_macro("Timing Binds/Shift all following lines by Frames to Video Position (End)","Shifts all following lines such that the active line ends at the video position, but shifts by frames instead of by milliseconds.", shift_frames_to_video(true, true), has_video)

0 comments on commit 9619246

Please sign in to comment.