-- Include necessary sub-functions
function GetSubdirectories(dir)
local subdirs = {}
local i = 0
repeat
local subdir = reaper.EnumerateSubdirectories(dir, i)
if subdir then
table.insert(subdirs, dir .. "/" .. subdir)
end
i = i + 1
until not subdir
return subdirs
end
function GetFiles(dir)
local files = {}
local i = 0
repeat
local file = reaper.EnumerateFiles(dir, i)
if file then
table.insert(files, dir .. "/" .. file) -- Ensure full file path is stored
end
i = i + 1
until not file
return files
end
function DrawDirectoryTree(dir)
if reaper.file_exists(dir) then
reaper.ImGui_Text(ctx, "File: " .. dir)
return
end
code
local open = reaper.ImGui_TreeNode(ctx, dir)
if open then
local subdirs = GetSubdirectories(dir)
for _, subdir in ipairs(subdirs) do
DrawDirectoryTree(subdir)
end
local files = GetFiles(dir)
for _, file in ipairs(files) do
if reaper.ImGui_Selectable(ctx, file) then
selected = file
end
if reaper.ImGui_BeginDragDropSource(ctx, reaper.ImGui_DragDropFlags_None()) then
reaper.ImGui_SetDragDropPayload(ctx, "DND_TEXT", file)
reaper.ImGui_Text(ctx, file)
reaper.ImGui_EndDragDropSource(ctx)
dragging = true
end
end
reaper.ImGui_TreePop(ctx)
end
end
function InsertMediaToTrackAtMouse(file)
-- Check if the file path is valid
if not file or file == "" then return end
code
Assistant
User
now give me the whole script with that correction
Assistant
-- Get mouse position in arrange view and convert to project time position
local mouse_x, mouse_y = reaper.GetMousePosition()
local trackview = reaper.JS_Window_FindChildByID(reaper.GetMainHwnd(), 1000)
if trackview then
local proj_x, proj_y = reaper.JS_Window_ScreenToClient(trackview, mouse_x, mouse_y)
local pos = reaper.BR_GetMouseCursorContext_Position()
local track = reaper.GetSelectedTrack(0, 0)
if not track then track = reaper.GetTrack(0, 0) end -- Fallback to the first track if none selected
if track then
reaper.Undo_BeginBlock()
reaper.InsertMedia(file, 0)
reaper.ShowConsoleMsg("oi")
local item = reaper.GetSelectedMediaItem(0, 0)
if item then
reaper.SetMediaItemPosition(item, pos, false)
end
reaper.Undo_EndBlock("Insert Media at Mouse Position", -1)
end
end
end
function main()
reaper.ImGui_SetNextWindowDockID(ctx, -7)
local retval, is_open = reaper.ImGui_Begin(ctx, "Media Explorer")
code
if retval then
local search_buf = reaper.ImGui_InputText(ctx, "##search", "")
local root_dir = "E:\\Sample Packs\\COLLECTION"
DrawDirectoryTree(root_dir)
-- Check mouse release outside ImGui window
if dragging and not reaper.ImGui_IsWindowHovered(ctx, reaper.ImGui_HoveredFlags_AnyWindow()) then
if not reaper.ImGui_IsMouseDown(ctx, 0) then
if selected then
InsertMediaToTrackAtMouse(selected)
end
dragging = false -- Reset dragging state
end
end
reaper.defer(main)
end
reaper.ImGui_End(ctx)
end
if not reaper.APIExists("JS_ReaScriptAPI_Version") then
reaper.ShowMessageBox("This script requires the JS ReaScriptAPI extension. Please install it and try again.", "Error", 0)
else
ctx = reaper.ImGui_CreateContext("ctx")
dragging = false
selected = nil
main()
end
the drag and dropping into reaper timeline is not working. give me only the part of the solution to this script
The issue of drag and drop not working might be due to how the dropping logic is handled. The current script usually checks for dragging flag instead of directly checking if the mouse button is released. Let's refine the drag and drop logic by directly managing the state of mouse button clicks and ensuring the drop is properly detected and acted upon.
Here’s the snippet addressing the drag and drop functionality more reliably:
lua
function InsertMediaToTrackAtMouse(file)
if not file or file == "" then return end
local mouse_x, mouse_y = reaper.GetMousePosition()
local trackview = reaper.JS_Window_FindChildByID(reaper.GetMainHwnd(), 1000)
if trackview then
local proj_x, proj_y = reaper.JS_Window_ScreenToClient(trackview, mouse_x, mouse_y)
local pos = reaper.BR_GetMouseCursorContext_Position()
local track = reaper.GetSelectedTrack(0, 0)
if not track then track = reaper.GetTrack(0, 0) end
if track then
reaper.Undo_BeginBlock()
reaper.InsertMedia(file, 0)
local item = reaper.GetSelectedMediaItem(0, 0)
if item then
reaper.SetMediaItemPosition(item, pos, false)
end
reaper.Undo_EndBlock("Insert Media at Mouse Position", -1)
end
end
end
local is_dragging = false
local drag_file = nil
function DrawDirectoryTree(dir)
if reaper.file_exists(dir) then
reaper.ImGui_Text(ctx, "File: " .. dir)
return
end
local open = reaper.ImGui_TreeNode(ctx, dir)
if open then
local subdirs = GetSubdirectories(dir)
for _, subdir in ipairs(subdirs) do
DrawDirectoryTree(subdir)
end
local files = GetFiles(dir)
for _, file in ipairs(files) do
if reaper.ImGui_Selectable(ctx, file) then
selected = file
end
if reaper.ImGui_BeginDragDropSource(ctx, reaper.ImGui_DragDropFlags_None()) then
reaper.ImGui_SetDragDropPayload(ctx, "DND_TEXT", file)
reaper.ImGui_Text(ctx, file)
reaper.ImGui_EndDragDropSource(ctx)
is_dragging = true
drag_file = file
end
end
reaper.ImGui_TreePop(ctx)
end
end
function main()
reaper.ImGui_SetNextWindowDockID(ctx, -7)
local retval, is_open = reaper.ImGui_Begin(ctx, "Media Explorer")
if retval then
local search_buf = reaper.ImGui_InputText(ctx, "##search", "")
local root_dir = "E:\\Sample Packs\\COLLECTION"
DrawDirectoryTree(root_dir)
if is_dragging and not reaper.ImGui_IsMouseDown(nil) then
InsertMediaToTrackAtMouse(drag_file)
is_dragging = false
drag_file = nil
end
reaper.defer(main)
end
reaper.ImGui_End(ctx)
end
if not reaper.APIExists("JS_ReaScriptAPI_Version") then
reaper.ShowMessageBox("This script requires the JS ReaScriptAPI extension. Please install it and try again.", "Error", 0)
else
ctx = reaper.ImGui_CreateContext("ctx")
main()
end
In this revised code:
The dragging flag is now replaced by is_dragging.
The logic to check if the item is being dragged is simplified.
The function InsertMediaToTrackAtMouse() now directly responds to drag_file which holds the file being dragged.
The final dropping detection is done based on not reaper.ImGui_IsMouseDown(nil) which directly checks if the mouse button is released to determine the drop.
This change should ensure the dragging and dropping mechanism into the Reaper timeline is working correctly.
Certainly! Below is the complete corrected script with the improved drag and drop functionality:
lua
-- Rykord - ImGui Media Explorer
-- Include necessary sub-functions
function GetSubdirectories(dir)
local subdirs = {}
local i = 0
repeat
local subdir = reaper.EnumerateSubdirectories(dir, i)
if subdir then
table.insert(subdirs, dir .. "/" .. subdir)
end
i = i + 1
until not subdir
return subdirs
end
function GetFiles(dir)
local files = {}
local i = 0
repeat
local file = reaper.EnumerateFiles(dir, i)
if file then
table.insert(files, dir .. "/" .. file) -- Ensure full file path is stored
end
i = i + 1
until not file
return files
end
function InsertMediaToTrackAtMouse(file)
if not file or file == "" then return end
local mouse_x, mouse_y = reaper.GetMousePosition()
local trackview = reaper.JS_Window_FindChildByID(reaper.GetMainHwnd(), 1000)
if trackview then
local proj_x, proj_y = reaper.JS_Window_ScreenToClient(trackview, mouse_x, mouse_y)
local pos = reaper.BR_GetMouseCursorContext_Position()
local track = reaper.GetSelectedTrack(0, 0)
if not track then track = reaper.GetTrack(0, 0) end
if track then
reaper.Undo_BeginBlock()
reaper.InsertMedia(file, 0)
local item = reaper.GetSelectedMediaItem(0, 0)
if item then
reaper.SetMediaItemPosition(item, pos, false)
end
reaper.Undo_EndBlock("Insert Media at Mouse Position", -1)
end
end
end
local is_dragging = false
local drag_file = nil
function DrawDirectoryTree(dir)
if reaper.file_exists(dir) then
reaper.ImGui_Text(ctx, "File: " .. dir)
return
end
local open = reaper.ImGui_TreeNode(ctx, dir)
if open then
local subdirs = GetSubdirectories(dir)
for _, subdir in ipairs(subdirs) do
DrawDirectoryTree(subdir)
end
local files = GetFiles(dir)
for _, file in ipairs(files) do
if reaper.ImGui_Selectable(ctx, file) then
selected = file
end
if reaper.ImGui_BeginDragDropSource(ctx, reaper.ImGui_DragDropFlags_None()) then
reaper.ImGui_SetDragDropPayload(ctx, "DND_TEXT", file)
reaper.ImGui_Text(ctx, file)
reaper.ImGui_EndDragDropSource(ctx)
is_dragging = true
drag_file = file
end
end
reaper.ImGui_TreePop(ctx)
end
end
function main()
reaper.ImGui_SetNextWindowDockID(ctx, -7)
local retval, is_open = reaper.ImGui_Begin(ctx, "Media Explorer")
if retval then
local search_buf = reaper.ImGui_InputText(ctx, "##search", "")
local root_dir = "E:\\Sample Packs\\COLLECTION"
DrawDirectoryTree(root_dir)
if is_dragging and not reaper.ImGui_IsMouseDown(nil) then
InsertMediaToTrackAtMouse(drag_file)
is_dragging = false
drag_file = nil
end
reaper.defer(main)
end
reaper.ImGui_End(ctx)
end
if not reaper.APIExists("JS_ReaScriptAPI_Version") then
reaper.ShowMessageBox("This script requires the JS ReaScriptAPI extension. Please install it and try again.", "Error", 0)
else
ctx = reaper.ImGui_CreateContext("ctx")
main()
end
This script should now correctly implement the drag-and-drop functionality to insert media into the Reaper timeline based on the improved checking and handling of drag-and-drop events.