ReaLive-ProjectTabsList/Realive_ProjectTabsList_Open.lua

306 lines
9.6 KiB
Lua

--------------------------------------
----------// HEADER //----------------
--------------------------------------
--[[
* REALIVE: Reaper Setup for a Complete Live Playback Engine
* ReaScript Name: Realive_ProjectTabsList_Open.lua
* Description: Open RPL File
* Instructions:
* Author: Alex Paquette, Thanks to Claude Fillion for the Large contribution
* Repository:
* Repository URI:
* File URI:
* Licence: GPL v3
* Forum Thread:
* Forum Thread URI: https://forum.cockos.com/showthread.php?p=1854775#post1854775
* REAPER: 5
* Extensions: SWS/S&M
* Version: 2.00
--]]
--[[
* Changelog:
* v1.0 (2019-06-02)
+ Initial Release
--]]
--------------------------------------
----------// VARIABLES //-------------
--------------------------------------
local reaper = reaper -------// Performance Enhancement //----------
local EXT_SECTION = 'ProjectTabs_Reaper_list'
local EXT_SECTION_PLAYLIST = 'ProjectTabs_Realive_list'
-----// Window Set Numbers //------------------------------
local PlayViewWindowSet = 01 -- Enter Window Set Number Used for the "Live Play" View
local AllTracksViewWindowSet = 02 -- Enter the Window Set Number for the "Show All Tracks" View
local WindowSetBaseNumber = 40453
local PlayView_cmdID = WindowSetBaseNumber+PlayViewWindowSet
local AllView_cmdID = WindowSetBaseNumber+AllTracksViewWindowSet
local RegionWindow_cmdID = reaper.NamedCommandLookup("_RS05396ad2c5c53e928799e707652dea37cc588b3c")
local ResetPlayView_cmdID = reaper.NamedCommandLookup("_RS08d5f2749008cc0f6f53feb223d1638fde598566")
local PlaylistWindow_cmdID = reaper.NamedCommandLookup('_RS2071ccff7199c897478c49da97ca1422fc732d4f')
local debug = false
-----// gmem Variables //------------------------------
local gmemSect = 'Realive_Gmem'
local gmem_idx_PlaylistState = 11
local gmem_idx_Management = 12
local gmem_idx_PlaylistState_Clean = 32
local gmem_idx_PlaylistCount_Reaper = 30
local gmem_idx_PlaylistCount_Realive = 31
local gmemPlaylist_SaveRequest = 1
local gmemPlaylist_SaveRefused = 2
local gmemPlaylist_Initialize = 3
local gmemPlaylist_RealiveListItem_Remove = 4
local gmemPlaylist_RealiveListItem_Add = 5
local gmemPlaylist_RealiveListItem_Update = 6
local gmemPlaylist_ProjectTabs_CleanStatus = 9
local gmem_idxRead_Mngmt = reaper.gmem_read(gmem_idx_Management)
local playlistState = reaper.GetExtState('Realive_Playlist', 'State')
local realivePlaylist_compare = 0
-----// Zoom Variables //------------------------------
local Zoom2Amount = 100
local Zoom2Forceset = 1
local Zoom2Doupd = false
local Zoom2Centermode = -1-- 0=EditCursor,
-----------------------------------------------------------
-----------------------------------------------------------
-----// DEBUG FUNCTION //----------------------------------
-----------------------------------------------------------
if debug then reaper.ShowConsoleMsg("") end
function Msg(value)
if debug then
reaper.ShowConsoleMsg(tostring(value) .. "\n")
end
end
-----------------------------------------------------------
-----// FUNCTIONS //---------------------------------------
-----------------------------------------------------------
function Playlist_CountProjectTabs()
local ProjCount=-1
local Aretval="t"
while Aretval~=nil do
local Aretval, Aprojfn = reaper.EnumProjects(ProjCount+1, "")
if Aretval~=nil then ProjCount=ProjCount+1
else break
end
end
return ProjCount+1
end
function Actions_ResetAllProjects()
local prjct_Counter = Playlist_CountProjectTabs()
for p = 0, prjct_Counter do
local proj = reaper.EnumProjects(p, 0)
if not proj then break
else
reaper.SetEditCurPos2(proj, 0, true, true)
end
end
end
-----------------------------------------------------------
-----// Realive List Management //-------------------------
-----------------------------------------------------------
function makeKey(i)
return string.format('project%d', i)
end
function enum_RealivePlaylist()
local i = 1
return function()
local key = makeKey(i)
i = i + 1
if reaper.HasExtState(EXT_SECTION_PLAYLIST, key) then
return key
end
end
end
function get_RealivePlaylist()
local Realive_list = {}
for Realive_key in enum_RealivePlaylist() do
local Realive_projfn = reaper.GetExtState(EXT_SECTION_PLAYLIST, Realive_key)
table.insert(Realive_list, Realive_projfn)
end
return Realive_list
end
-----------------------------------------------------------
-----// SAVE RPL FUNCTION //-------------------------------
-----------------------------------------------------------
function SaveRPL()
local out_str = ''
local save_targetPath = ''
local save_lastPath = ''
if reaper.HasExtState('Realive_PlaylistFolder', 'lastPath') then
save_lastPath = reaper.GetExtState('Realive_PlaylistFolder', 'lastPath')
end
if reaper.HasExtState('Realive_PlaylistFolder', 'defaultPath') then
save_defaultPath = reaper.GetExtState('Realive_PlaylistFolder', 'defaultPath')
end
if save_lastPath ~= '' then save_targetPath = save_lastPath
elseif save_defaultPath ~= '' then save_targetPath = save_defaultPath
end
local unsavedConfirm = 0
local playlist = get_RealivePlaylist()
local projcnt = Playlist_CountProjectTabs()
local UnsavedProj_cnt = 0
for i = 1, projcnt do
local retval, projfn= reaper.EnumProjects( i-1, '' )
if projfn =='' then UnsavedProj_cnt = UnsavedProj_cnt+1 end
Msg('projfn: '..projfn)
end
Msg('UnsavedProject: '..UnsavedProj_cnt)
if UnsavedProj_cnt > 0 then
unsavedConfirm = reaper.ShowMessageBox("Your list contains project(s) that have no name. They will not be saved in the playlist. Do you still want to proceed? ", 'Warning', 4) -- Yes = 6, No = 7, Cancel = 2
Msg('unsavedConfirm: '..unsavedConfirm)
if unsavedConfirm ~= 6 then return end
end
local SaveDialogAnswer, saving_folder = reaper.JS_Dialog_BrowseForSaveFile('Save RPL', save_targetPath, '', 'Reaper Playlist files (.RPL)\0*.RPL\0\0')
Msg('SaveDialogAnswer: '..SaveDialogAnswer)
if SaveDialogAnswer == 1 then -- if a file was selected
extst_path = string.match(saving_folder, ".+/")
reaper.SetExtState('Realive_PlaylistFolder', 'lastPath', extst_path, false)
Msg('path: '..extst_path)
for i = 1, #playlist do
out_str = out_str..playlist[i]..'\n'
end
local f = io.open(saving_folder, 'w')
f:write(out_str)
f:close()
OpenRPL()
return true
else
return false
end
end
-----------------------------------------------------------
-----------------------------------------------------------
-----------------------------------------------------------
-----// OPEN RPL FUNCTION //-------------------------------
-----------------------------------------------------------
function OpenRPL()
local open_targetPath = ''
local open_lastPath = ''
if reaper.HasExtState('Realive_PlaylistFolder', 'lastPath') then
open_lastPath = reaper.GetExtState('Realive_PlaylistFolder', 'lastPath')
end
if reaper.HasExtState('Realive_PlaylistFolder', 'defaultPath') then
open_defaultPath = reaper.GetExtState('Realive_PlaylistFolder', 'defaultPath')
end
if open_lastPath ~= '' then open_targetPath = open_lastPath
elseif open_defaultPath ~= '' then open_targetPath = open_defaultPath
end
local r, fn = reaper.JS_Dialog_BrowseForOpenFiles('Open RPL', open_targetPath, '', 'Reaper Playlist files (.RPL)\0*.RPL\0\0', false)
Msg('r: '..r)
if r == 1 then
if fn ~= '' then
extst_path = string.match(fn, ".+/")
reaper.SetExtState('Realive_PlaylistFolder', 'lastPath', extst_path, false)
Msg('path: '..extst_path)
end
else
return
end
local f = io.open(fn, 'r')
if not f then return end
local context = f:read('a')
f:close()
CloseAllTabs()
for line in context:gmatch('[^\r\n]+') do
reaper.Main_OnCommand(41929, 0 ) -- New project tab (ignore default template)
reaper.Main_openProject( line )
end
reaper.SelectProjectInstance( reaper.EnumProjects( 0, '') )
reaper.Main_OnCommand(40860,0) -- Close current project tab
Playlist_Init()
end
-----------------------------------------------------------
-----------------------------------------------------------
function RestartPlaylistWindow()
local state = reaper.GetToggleCommandState(PlaylistWindow_cmdID)
if state == 1 then
reaper.Main_OnCommand(PlaylistWindow_cmdID,0) --Close Playlist Window
reaper.Main_OnCommand(PlaylistWindow_cmdID,0) --Restart Playlist Window
end
end
function CloseAllTabs()
reaper.Main_OnCommand(40886, 0) -- Close All Projects
reaper.Main_OnCommand(41929, 0) -- Open empty project
reaper.Main_OnCommand(reaper.NamedCommandLookup("_SWS_FIRSTPROJTAB"), 0) -- Switch to 1st Tab
reaper.Main_OnCommand(40860, 0) -- Close tab
end
function Playlist_Init()
RestartPlaylistWindow()
Actions_ResetAllProjects()
reaper.Main_OnCommand(reaper.NamedCommandLookup("_SWS_PROJTAB2"), 0) -- Switch to 2nd Tab
-- reaper.Main_OnCommand(RegionWindow_cmdID,0) --Initialize Region Window
reaper.Main_OnCommand(ResetPlayView_cmdID,0) --Reset Live Playing View
reaper.adjustZoom(Zoom2Amount, Zoom2Forceset, Zoom2Doupd, Zoom2Centermode)
reaper.SetEditCurPos(0, true, true)
end
------------------------------------------
---------// RUN FUNCTION //--------------
------------------------------------------
function run()
if playlistState == 'Drty' then
realivePlaylist_compare = reaper.ShowMessageBox("Do you want to save your Playlist? ", 'Playlist was Changed', 3) -- Yes = 6, No = 7, Cancel = 2
end
if realivePlaylist_compare == 2 then return end -- if Cancel then abort the whole thing
if realivePlaylist_compare == 6 then -- if Yes then save RPL
SaveRPL()
else
OpenRPL()
end
end
------------------------------------------
---------// RUNTIME //-------------------
------------------------------------------
reaper.gmem_attach(gmemSect)
run()