-- @description Sexan ImGui FileManager
-- @author Sexan
-- @license GPL v3
-- @version 1.02
-- @changelog
-- Allow prenaming
local r = reaper
local os_separator = package.config:sub(1, 1)
FM_MAIN_PATH = r.GetResourcePath() .. os_separator .. "FXChains" .. os_separator
local path, filelist, entry, CAN_SAVE, CAN_LOAD
-- FOR SAVING
exstensions = {
".RfxChain",
}
-- FOR DROP DOWN MENU
exstensions_preview = {
".RfxChain",
".*",
}
exstension = exstensions[1]
local FLT_MIN, FLT_MAX = r.ImGui_NumericLimits_Float()
local function Ltrim(s)
if s == nil then return end
return (s:gsub("^%s*", ""))
end
local function GetDirEntries(path, enumerator, filter)
local entries = {}
enumerator(path, -1)
for index = 0, math.huge do
local path = enumerator(path, index)
if not path then break end
if path:sub(1, 1) ~= '.' then
if filter then
if path:match(filter) then
entries[#entries + 1] = path
end
else
entries[#entries + 1] = path
end
end
end
table.sort(entries, function(a, b) return a:lower() < b:lower() end)
return entries
end
local function GetFileList()
return {
dirs = GetDirEntries(path, r.EnumerateSubdirectories),
files = GetDirEntries(path, r.EnumerateFiles, exstension),
current = nil
}
end
local function RemoveLastPathComponent()
local last = #path - path:reverse():find(os_separator)
path = last > 0 and path:sub(1, last) or os_separator
end
local function DrawEntries(entries, is_dir)
for i, entry in ipairs(entries) do
if r.ImGui_Selectable(ctx, entry, filelist.current == entry, r.ImGui_SelectableFlags_AllowDoubleClick()) then
filelist.current = entry
filelist.current_is_dir = is_dir
current_text_input = is_dir and entry or
entry:reverse():match("%.(.+)"):reverse() -- REMOVE EXTENSTION FROM SELECTED ENTRY IF NOT FOLDER
if r.ImGui_IsMouseDoubleClicked(ctx, 0) then
if is_dir then
if path:sub(#path, 1) ~= os_separator then
path = path .. os_separator
end
path = path .. entry
filelist = GetFileList()
else
if FM_TYPE == "LOAD" then
LoadFile()
r.ImGui_CloseCurrentPopup(ctx)
elseif FM_TYPE == "SAVE" then
CAN_SAVE = true
DoSave()
end
end
end
end
end
end
local function Split_by_line(str)
local t, i = {}, 1
while true do
local ni = str:find(os_separator, i)
t[#t + 1] = str:sub(i, ni and ni - 1 or nil)
if not ni or ni == str:len() then break end
i = ni + 1
end
return t
end
local function autoWrap(n, sz, func)
local item_spacing_x =
r.ImGui_GetStyleVar(ctx, r.ImGui_StyleVar_ItemSpacing())
local window_visible_x2 =
r.ImGui_GetWindowPos(ctx) + r.ImGui_GetWindowContentRegionMax(ctx)
for i = 0, n do
func(i)
local x2 = r.ImGui_GetItemRectMax(ctx)
if i < n and x2 + item_spacing_x + sz < window_visible_x2 then
r.ImGui_SameLine(ctx)
end
end
end
local pad_x = r.ImGui_GetStyleVar(ctx, r.ImGui_StyleVar_FramePadding())
local inner_pad = (pad_x * 2)
local function PathButtons()
local path_buttons = Split_by_line(path)
local item_spacing_x = r.ImGui_GetStyleVar(ctx, r.ImGui_StyleVar_ItemSpacing())
local window_x2 = r.ImGui_GetWindowPos(ctx) + r.ImGui_GetWindowContentRegionMax(ctx)
local fx,fy = r.ImGui_GetStyleVar(ctx,r.ImGui_StyleVar_FramePadding())
r.ImGui_PushStyleVar(ctx, r.ImGui_StyleVar_ItemSpacing(),0,0)
r.ImGui_PushStyleVar(ctx, r.ImGui_StyleVar_FramePadding(),0,fy)
r.ImGui_PushStyleColor(ctx, r.ImGui_Col_Button(),0)
for i = 1, #path_buttons do
if r.ImGui_Button(ctx, path_buttons[i] .. os_separator) and i ~= #path_buttons then
path = ''
for j = 1, i do
if j > 1 then path = path .. os_separator end
path = path .. path_buttons[j]
end
if path:len() == 0 then path = os_separator end -- Unix root
filelist = GetFileList()
end
local button_x2 = r.ImGui_GetItemRectMax(ctx)
if i < #path_buttons then
local next_w = r.ImGui_CalcTextSize(ctx, path_buttons[i + 1])
local next_x2 = button_x2 + item_spacing_x + next_w + inner_pad
if next_x2 < window_x2 then
r.ImGui_SameLine(ctx)
end
end
end
r.ImGui_PopStyleVar(ctx)
r.ImGui_PopStyleVar(ctx)
r.ImGui_PopStyleColor(ctx)