diff --git a/4PointCutting/4Pcut_execute.lua b/4PointCutting/4Pcut_execute.lua index aa1900d..0999793 100644 --- a/4PointCutting/4Pcut_execute.lua +++ b/4PointCutting/4Pcut_execute.lua @@ -22,6 +22,10 @@ local mark_dstin_pos = -1 local mark_dstin_id = -1 local mark_dstout_pos = -1 local mark_dstout_id = -1 +local edit_mode = 0 +local waitcount = 0 +-- edit modes: 0=not enough markers set; 1=src-in, src-out, dst-in; 2=src-in, src-out, dst-out; +-- 3=src-in, dst-in, dst-out; 4=src-out, dst-in, dst-out; 5=all four markers set; -- Send a message to the console function msg(m) @@ -97,17 +101,27 @@ for i = 0, nof_markers - 1 do end end +-- set edit mode depending on which markers are set (see comments next to definition of edit_mode) +if mark_srcin_id >= 0 and mark_srcout_id >= 0 and mark_dstin_id >= 0 and mark_dstout_id < 0 then + edit_mode = 1 +elseif mark_srcin_id >= 0 and mark_srcout_id >= 0 and mark_dstin_id < 0 and mark_dstout_id >= 0 then + edit_mode = 2 +elseif mark_srcin_id >= 0 and mark_srcout_id < 0 and mark_dstin_id >= 0 and mark_dstout_id >= 0 then + edit_mode = 3 +elseif mark_srcin_id < 0 and mark_srcout_id >= 0 and mark_dstin_id >= 0 and mark_dstout_id >= 0 then + edit_mode = 4 +elseif mark_srcin_id >= 0 and mark_srcout_id >= 0 and mark_dstin_id >= 0 and mark_dstout_id >= 0 then + edit_mode = 5 +end + -- error messages -if mark_srcin_pos < 0 then - msg('Please set Source-In Marker first!') - return -end -if mark_srcout_pos < 0 then - msg('Please set Source-Out Marker first!') -end -if mark_dstin_pos < 0 then - msg('Please set Destination-In Marker first!') +if edit_mode <= 0 then + msg('Please set at least 3 of the 4 markers first! Aborting.') return +elseif (edit_mode == 1 or edit_mode == 2 or edit_mode == 5) and (mark_srcin_pos >= mark_srcout_pos) then + msg('SRC-IN Marker must be set left of SRC-OUT marker! Aborting.') +elseif (edit_mode == 3 or edit_mode == 4 or edit_mode == 5) and (mark_dstin_pos >= mark_dstout_pos) then + msg('DST-IN Marker must be set left of DST-OUT marker! Aborting.') end -- Do stuff before actual edits... @@ -129,8 +143,25 @@ reaper.Main_OnCommand(40289, 0) -- Unselect all items reaper.Main_OnCommand(40297, 0) -- Unselect all tracks select_src_tracks() --- Set Source Time selection and Copy Items -reaper.GetSet_LoopTimeRange(true, false, mark_srcin_pos, mark_srcout_pos, false) +-- Set Source Time selection according to edit mode +if edit_mode == 1 then -- src-in, src-out and dst-in defined + reaper.GetSet_LoopTimeRange(true, false, mark_srcin_pos, mark_srcout_pos, false) +elseif edit_mode == 2 then -- src-in, src-out and dst-out defined + reaper.GetSet_LoopTimeRange(true, false, mark_srcin_pos, mark_srcout_pos, false) + mark_dstin_pos = mark_dstout_pos - (mark_srcout_pos - mark_srcin_pos) -- also set dst-in position, so we have a point to paste to +elseif edit_mode == 3 then -- src-in, dst-in and dst-out defined + reaper.GetSet_LoopTimeRange(true, false, mark_srcin_pos, mark_srcin_pos + (mark_dstout_pos - mark_dstin_pos), false) +elseif edit_mode == 4 then -- src-out, dst-in and dst-out defined + reaper.GetSet_LoopTimeRange(true, false, mark_srcout_pos - (mark_dstout_pos - mark_dstin_pos), mark_srcout_pos, false) +elseif edit_mode == 5 then -- all 4 markers defined, get lenght of shortest region + if (mark_srcout_pos - mark_srcin_pos) < (mark_dstout_pos - mark_dstin_pos) then + reaper.GetSet_LoopTimeRange(true, false, mark_srcin_pos, mark_srcout_pos, false) + else + reaper.GetSet_LoopTimeRange(true, false, mark_srcin_pos, mark_srcin_pos + (mark_dstout_pos - mark_dstin_pos), false) + end +end + +-- Copy items reaper.Main_OnCommand(40718, 0) -- select items on selected tracks under time selection reaper.Main_OnCommand(40060, 0) -- copy selected items under time selection 41383 @@ -138,7 +169,24 @@ reaper.Main_OnCommand(40060, 0) -- copy selected items under time selection 4138 reaper.MoveEditCursor(mark_dstin_pos - reaper.GetCursorPosition(), false) select_dst_track_only() reaper.Main_OnCommand(40058, 0) -- paste item -reaper.SetProjectMarker(mark_dstin_id, false, mark_dstin_pos + (mark_srcout_pos - mark_srcin_pos), 0, 'DST-IN_4Pcut') +--reaper.Main_OnCommand( reaper.NamedCommandLookup('_SWS_AWPASTE'), 0 ) -- SWS Paste +-- Move dstin marker according to edit mode +if edit_mode == 1 then + reaper.SetProjectMarker(mark_dstin_id, false, mark_dstin_pos + (mark_srcout_pos - mark_srcin_pos), 0, 'DST-IN_4Pcut') +elseif edit_mode == 2 then + reaper.SetProjectMarker(mark_dstout_id, false, mark_dstout_pos + (mark_srcout_pos - mark_srcin_pos), 0, 'DST-OUT_4Pcut') +elseif edit_mode == 3 or edit_mode == 4 then + reaper.SetProjectMarker(mark_dstin_id, false, mark_dstin_pos + (mark_dstout_pos - mark_dstin_pos), 0, 'DST-IN_4Pcut') + reaper.SetProjectMarker(mark_dstout_id, false, mark_dstout_pos + (mark_dstout_pos - mark_dstin_pos), 0, 'DST-OUT_4Pcut') +elseif edit_mode == 5 then + if (mark_srcout_pos - mark_srcin_pos) < (mark_dstout_pos - mark_dstin_pos) then + reaper.SetProjectMarker(mark_dstin_id, false, mark_dstin_pos + (mark_srcout_pos - mark_srcin_pos), 0, 'DST-IN_4Pcut') + reaper.SetProjectMarker(mark_dstout_id, false, mark_dstout_pos + (mark_srcout_pos - mark_srcin_pos), 0, 'DST-OUT_4Pcut') + else + reaper.SetProjectMarker(mark_dstin_id, false, mark_dstin_pos + (mark_dstout_pos - mark_dstin_pos), 0, 'DST-IN_4Pcut') + reaper.SetProjectMarker(mark_dstout_id, false, mark_dstout_pos + (mark_dstout_pos - mark_dstin_pos), 0, 'DST-OUT_4Pcut') + end +end -- Restore cursor position, time selection, selected tracks, selected items reaper.MoveEditCursor(stored_cursorPos - reaper.GetCursorPosition(), false)