Advertisement
Guest User

Trim left edge of item to edit cursor (changing fade length)

a guest
May 16th, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | Software | 0 0
  1. -- if difference between item current and new edge coordinates is greater than the fade length
  2. -- the original fade length is preserved
  3.  
  4. local r = reaper
  5.  
  6. function no_undo()
  7. do return end
  8. end
  9.  
  10.  
  11. function Error_Tooltip(text, caps, spaced, x2, y2, want_color, want_blink)
  12. local x, y = r.GetMousePosition()
  13. local text = caps and text:upper() or text
  14. local utf8 = '[\0-\127\194-\244][\128-\191]*'
  15. local text = spaced and text:gsub(utf8,'%0 ') or text
  16. local x2, y2 = x2 and math.floor(x2) or 0, y2 and math.floor(y2) or 0
  17. r.TrackCtl_SetToolTip(text, x+x2, y+y2, true)
  18.     if want_color then  
  19.     local color_init = r.GetThemeColor('col_tl_bg', 0)
  20.     local color = color_init ~= 255 and 255 or 65535
  21.         if want_blink then
  22.             for i = 1, 100 do    
  23.                 if i == 1 or i == 40 or i == 80 then
  24.                 r.SetThemeColor('col_tl_bg', color, 0)
  25.                 elseif i == 20 or i == 60 or i == 100 then
  26.                 r.SetThemeColor('col_tl_bg', color_init, 0)
  27.                 end
  28.             r.UpdateTimeline()
  29.             end    
  30.         else
  31.         r.SetThemeColor('col_tl_bg', color, 0)
  32.             for i = 1, 200 do
  33.             r.UpdateTimeline()
  34.             end
  35.         r.SetThemeColor('col_tl_bg', color_init, 0)
  36.         r.UpdateTimeline()
  37.         end
  38.     end
  39. r.UpdateTimeline()
  40. end
  41.  
  42.  
  43. local is_new_value, scr_name, sect_ID, cmd_ID, mode, resol, val, contextstr = r.get_action_context()
  44. scr_name = scr_name:match('([^\\/]+)%.%w+$')
  45. local cmd_ID = scr_name:match('left edge') and 41305 or scr_name:match('right edge') and 41311
  46. local sel_itms_cnt = r.CountSelectedMediaItems(0)
  47.  
  48. local err = not cmd_ID and 'wrong script name' or sel_itms_cnt == 0 and 'no selected items'
  49.    
  50.     if err then
  51.     Error_Tooltip('\n\n '..err..' \n\n', 1,1)
  52.     return r.defer(no_undo) end
  53.  
  54. local sel_items = {}
  55.  
  56.     for i=0, r.CountMediaItems(0)-1 do
  57.     local item = r.GetMediaItem(0,i)
  58.         if r.IsMediaItemSelected(item) then
  59.         sel_items[#sel_items+1] = item
  60.         end
  61.     end
  62.  
  63.    
  64. r.Undo_BeginBlock()
  65.    
  66. r.SelectAllMediaItems(0, false) -- selected false
  67.  
  68.  
  69. local FADE_PARM = cmd_ID == 41305 and 'D_FADEINLEN' or 'D_FADEOUTLEN'
  70. local fade_in = cmd_ID == 41305
  71.    
  72.     for k, item in ipairs(sel_items) do
  73.     r.SetMediaItemSelected(item, true) -- selected true
  74.     local pos = r.GetMediaItemInfo_Value(item, 'D_POSITION')
  75.     local edge = fade_in and pos or pos+r.GetMediaItemInfo_Value(item, 'D_LENGTH')
  76.     local fade_len = r.GetMediaItemInfo_Value(item, FADE_PARM)
  77.     r.Main_OnCommand(cmd_ID,0) -- Item edit: Trim left/right edge of item to edit cursor
  78.         if fade_len > 0 then
  79.         local pos_new = r.GetMediaItemInfo_Value(item, 'D_POSITION')
  80.         local edge_new = fade_in and pos_new or pos_new+r.GetMediaItemInfo_Value(item, 'D_LENGTH')
  81.         local diff = edge_new-edge
  82.         diff = math.abs(diff) > fade_len and 0 or diff*(fade_in and 1 or -1)
  83.         r.SetMediaItemInfo_Value(item, FADE_PARM, math.abs(fade_len-diff))
  84.         end
  85.     end
  86.    
  87.     for k, item in ipairs(sel_items) do
  88.     r.SetMediaItemSelected(item, true) -- selected true
  89.     end
  90.  
  91. r.Undo_EndBlock(scr_name,-1)    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement