Advertisement
poulhoi

phoi_Create send from selected tracks (search GUI).lua

Jul 5th, 2023 (edited)
1,164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.80 KB | None | 0 0
  1. -- Creates search gui for adding sends the selected track.
  2. -- Keyboard shortcuts:
  3.     -- Enter applies the topmost search result
  4.     -- Cmd+[1-9] applies the equivalent result
  5.     -- "a" toggles auto-close
  6. local searchRate = 0.05
  7. local autoCloseDefault = true
  8. local autoNameEmpty = false
  9. local tracks  = {}
  10. local trackNames = {}
  11. local trackNamesLower = {}
  12. local trackColors = {}
  13. local hits = {}
  14. local first, refresh = true, true
  15. local clock = os.clock()
  16. local autoClose = autoCloseDefault
  17.  
  18. function init()
  19.     for i = 1, reaper.CountTracks(0) do
  20.         local track = reaper.GetTrack(0, i-1)
  21.         tracks[i] = track
  22.         local _, name = reaper.GetSetMediaTrackInfo_String(track, "P_NAME", '', false)
  23.         if autoNameEmpty and name == '' then name = "Track " .. tostring(i+1) end
  24.         trackNames[i] = name
  25.         trackNamesLower[i] = name:lower() --make all lowercase
  26.         trackColors[i] = reaper.GetTrackColor(track)
  27.     end
  28. end
  29.  
  30. function ShowWindow()
  31.     if not ctx then
  32.         ctx = reaper.ImGui_CreateContext('phoi_Send to track', reaper.ImGui_ConfigFlags_None())
  33.     end
  34.     local rv, open = nil, true
  35.     rv, open = reaper.ImGui_Begin(ctx, 'phoi_Send to track', open, reaper.ImGui_WindowFlags_None())
  36.     if not rv then return open end
  37.  
  38.     local openSendPrefs = reaper.ImGui_Button(ctx, 'Send preferences...') -- button to open send preferences
  39.     if openSendPrefs then
  40.         reaper.ViewPrefs(0x0b2, '')
  41.     end
  42.     reaper.ImGui_SameLine(ctx)
  43.     local autoCloseSwitch = reaper.ImGui_Checkbox(ctx, 'Auto-close?', autoClose)
  44.     if autoCloseSwitch then
  45.         autoClose = not autoClose
  46.     end
  47.  
  48.     if not oldText then oldText = '' end
  49.     if first then reaper.ImGui_SetKeyboardFocusHere(ctx, 0) end
  50.     reaper.ImGui_Dummy(ctx, 100, 10)
  51.     local rv, text = reaper.ImGui_InputText(ctx, '<- Search!', oldText, reaper.ImGui_InputTextFlags_None())
  52.     if refresh then
  53.         local search = text:lower()
  54.         local searchWords = {}
  55.         for word in search:gmatch('[^%s]*') do
  56.             searchWords[#searchWords+1] = word
  57.         end
  58.        
  59.         hits = {}
  60.         for i = 1, #tracks do
  61.             local name = trackNamesLower[i]
  62.             for j = 1, #searchWords do
  63.                 if name:find(searchWords[j]) then
  64.                     hits[#hits+1] = i -- store only the index
  65.                     break
  66.                 end
  67.             end
  68.         end
  69.         refresh = false
  70.     else
  71.         local delta = os.clock() - clock
  72.         if delta >= searchRate then
  73.             refresh = true
  74.             clock = os.clock()
  75.         end
  76.     end
  77.  
  78.     oldText = text
  79.     reaper.ImGui_Dummy(ctx, 100, 10)
  80.     local oops = false
  81.     local function CreateSend(destTrack)
  82.         local trackCount = reaper.CountSelectedTracks(0)
  83.         if trackCount < 1 then
  84.             oops = true
  85.             return
  86.         end
  87.         reaper.Undo_BeginBlock()
  88.         local targetTrackName
  89.  
  90.         if trackCount > 1 then
  91.             targetTrackName = 'multiple tracks'
  92.         else
  93.             _, targetTrackName = reaper.GetSetMediaTrackInfo_String(reaper.GetSelectedTrack(0, 0), 'P_NAME', '', false)
  94.         end
  95.  
  96.         local _, destTrackName = reaper.GetSetMediaTrackInfo_String(destTrack, 'P_NAME', '', false)
  97.  
  98.         for j = 0, trackCount - 1 do
  99.             local targetTrack = reaper.GetSelectedTrack(0, j)
  100.             reaper.CreateTrackSend(targetTrack, destTrack)
  101.         end
  102.         reaper.Undo_EndBlock("Create track send from " .. targetTrackName .. ' to ' .. destTrackName, 1)
  103.     end
  104.  
  105.     for i = 1, #hits do
  106.         local prefix = tostring(i):format("%i") .. ": "
  107.         local track_color = reaper.ImGui_ColorConvertNative(trackColors[hits[i]])
  108.         reaper.ImGui_ColorButton(ctx, 'color', track_color,
  109.             reaper.ImGui_ColorEditFlags_NoAlpha() | reaper.ImGui_ColorEditFlags_NoTooltip(), 12, 12)
  110.         reaper.ImGui_SameLine(ctx)
  111.         local trackChosen = reaper.ImGui_Button(ctx, prefix .. trackNames[hits[i]])
  112.         if trackChosen then
  113.             local destTrack = tracks[hits[i]]
  114.             CreateSend(destTrack)
  115.             if autoClose and not oops then
  116.                 reaper.ImGui_End(ctx)  
  117.                 return false
  118.             end
  119.         end
  120.     end
  121.  
  122.     local function CreateSendToHit(x)
  123.         if x > #hits then return true end
  124.         local destTrack = tracks[hits[x]]
  125.         CreateSend(destTrack)  
  126.         if autoClose and not oops then
  127.             reaper.ImGui_End(ctx)  
  128.             return false
  129.         else
  130.             return true
  131.         end
  132.     end
  133.  
  134.     if reaper.ImGui_IsKeyPressed(ctx, 13, false) then -- if enter is pressed, send to top track
  135.         local val = CreateSendToHit(1)
  136.         if not val then return false end
  137.     end
  138.     local ctrl = reaper.ImGui_GetKeyMods(ctx) & reaper.ImGui_Mod_Super()
  139.     if ctrl > 0 then
  140.         for i = 49, 57 do -- create send with number keys
  141.             if reaper.ImGui_IsKeyPressed(ctx, i, false) then
  142.                 local val = CreateSendToHit(i-48)
  143.                 if not val then return false end
  144.             end
  145.         end
  146.     end
  147.     if reaper.ImGui_IsKeyDown(ctx, 27) then -- if escape is pressed, close window
  148.         reaper.ImGui_End(ctx)
  149.         return false
  150.     end
  151.  
  152.     if reaper.ImGui_IsKeyPressed(ctx, 9, false) then
  153.         autoClose = not autoClose
  154.     end
  155.  
  156.     reaper.ImGui_End(ctx)
  157.     first = false
  158.     return open
  159. end
  160.  
  161.  
  162. function loop()
  163.     open = ShowWindow()
  164.     if open then
  165.         reaper.defer(loop)
  166.     else
  167.         reaper.ImGui_DestroyContext(ctx)
  168.     end
  169. end
  170.  
  171. init()
  172. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement