Old 06-20-2017, 05:18 PM   #1
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 3,458
Default How to get and set coordinates of a floating fx?

Hello!

How can I get the coordinates of a floating fx window? And how can I set them? (so that I place the floating fx somewhere else on screen - preferably centered on the screen)

Thanks!
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)
amagalma is offline   Reply With Quote
Old 06-20-2017, 05:42 PM   #2
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,964
Default

GetTrackStateChunk, some string parsing and manipulation then SetTrackStateChunk.

Quick and dirty example:
Code:
local PATTERN = 'FLOAT' .. (' (%d+)'):rep(4)

local t = reaper.GetTrack(0, 0)
local ok, chunk = reaper.GetTrackStateChunk(t, '')
if not ok then return end

local x, y, w, h = chunk:match(PATTERN)
reaper.ShowConsoleMsg(string.format("was: position = %dx%d, size = %dx%d\n", x, y, w, h))

chunk = chunk:gsub(PATTERN, 'FLOAT 10, 10, 300, 300')
reaper.SetTrackStateChunk(t, chunk)

Last edited by cfillion; 06-21-2017 at 12:24 AM.
cfillion is offline   Reply With Quote
Old 06-21-2017, 12:09 AM   #3
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 3,458
Default

Thank you so much cfillion!!

It works perfect!
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)

Last edited by amagalma; 06-21-2017 at 07:47 AM.
amagalma is offline   Reply With Quote
Old 06-21-2017, 07:29 AM   #4
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 3,458
Default

And another question..

Is there any way to know if an FX belonging to a track is floating (focused or not), by other means than getting the track's chunk?

Edit.. Found it!
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)

Last edited by amagalma; 06-21-2017 at 10:15 AM.
amagalma is offline   Reply With Quote
Old 06-21-2017, 10:16 AM   #5
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 3,458
Default

Here is my code:

Code:
function NoUndoPoint() end
local reaper = reaper

function Main()
    local track = reaper.GetSelectedTrack(0, 0)
    local fxcount = reaper.TrackFX_GetCount(track)
    if fxcount == 0 then
        reaper.Main_OnCommand(40291,0) -- add FX
        reaper.TrackFX_Show(track, 0, 0) -- Hide FX Chain
    else
        local t = {}
        local nextfx = 0
        for i = 0, fxcount-1 do -- Find the first open FX, if any
            local open = reaper.TrackFX_GetOpen(track, i)
            if open then t[#t+1] = i end
        end
        if #t ~= 0 then nextfx = t[1]+1 end
        if nextfx == fxcount then nextfx = 0 end
        reaper.TrackFX_Show(track, nextfx, 3) -- Show "next" FX
        for i,v in ipairs(t) do -- Close other open FX
            if tonumber(v) ~= tonumber(nextfx) then reaper.TrackFX_Show(track, v, 2) end
        end
        reaper.TrackFX_Show(track, 0, 0) -- Hide FX Chain
        local PATTERN = "FLOAT" .. (" (%d+)"):rep(4)
        local ok, chunk = reaper.GetTrackStateChunk(track, "")
        if not ok then return end
        local plx, ply, plw, plh = chunk:match(PATTERN) -- Get FX window position & size
        local _, _, scrw, scrh = reaper.my_getViewport(0, 0, plw, plh, 0, 0, plw, plh, 1) -- Get screen size
        local x, y = math.floor((scrw - plw)/2), math.floor((scrh - plh)/2) -- Centered FX on screen coordinates
        if tonumber(plx) ~= x and tonumber(ply) ~= y then -- Center FX if not centered
            local chunk = chunk:gsub(PATTERN, "FLOAT "..x..", "..y..", "..plw..", "..plh)
            reaper.SetTrackStateChunk(track, chunk)
        end
    end
    reaper.UpdateArrange()
end

reaper.PreventUIRefresh(1)
Main()
reaper.PreventUIRefresh(-1)
reaper.defer(NoUndoPoint)
Why an undo point is created? What should I do to avoid it?

I went into the trouble of making this action in order to avoid the undo point created with the SWS action "Float next FX (and close others) for selected tracks", and no luck..
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)

Last edited by amagalma; 06-21-2017 at 10:33 AM.
amagalma is offline   Reply With Quote
Old 06-21-2017, 10:53 AM   #6
me2beats
Human being with feelings
 
me2beats's Avatar
 
Join Date: Jul 2015
Location: Yekaterinburg, Russia
Posts: 400
Default

Quote:
Originally Posted by amagalma View Post
Why an undo point is created? What should I do to avoid it?
Hi, I did not really go into it, but it's probably because of
Code:
 reaper.Main_OnCommand(40291,0) -- add FX
__________________
My Reapack Repo
Donation
me2beats is offline   Reply With Quote
Old 06-21-2017, 11:08 AM   #7
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,107
Default

Yes, as me2beats said, creating undo point seems unavoidable currently when using native actions.

http://forum.cockos.com/showthread.php?t=192670
nofish is offline   Reply With Quote
Old 06-21-2017, 11:16 AM   #8
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 3,458
Default

I want an undo point to be added if I add an FX.. There is no problem with that part. The problem is when cycling between existing FX.
The action that creates the problem is reaper.TrackFX_Show(track, v, 2)

Just try the code and see
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)
amagalma is offline   Reply With Quote
Old 06-21-2017, 11:28 AM   #9
me2beats
Human being with feelings
 
me2beats's Avatar
 
Join Date: Jul 2015
Location: Yekaterinburg, Russia
Posts: 400
Default

Quote:
Originally Posted by amagalma View Post
I want an undo point to be added if I add an FX.. There is no problem with that part. The problem is when cycling between existing FX.
The action that creates the problem is reaper.TrackFX_Show(track, v, 2)

Just try the code and see
try this:
Code:
local r = reaper
local tr = r.GetSelectedTrack(0,0)
r.Undo_BeginBlock()
r.TrackFX_Show(tr, 0, 2) -- hide 1st fx
r.Undo_EndBlock('script name', 1)
Undo point is not created here, so I think the problem isn't in TrackFX_Show()
__________________
My Reapack Repo
Donation
me2beats is offline   Reply With Quote
Old 06-21-2017, 12:31 PM   #10
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 3,458
Default

It is created as it should.. 'script name'

If I do this:
Code:
function Main()
    reaper.Undo_BeginBlock()
    local track = reaper.GetSelectedTrack(0, 0)
    local fxcount = reaper.TrackFX_GetCount(track)
    if fxcount == 0 then
        reaper.Main_OnCommand(40291,0) -- add FX
        reaper.TrackFX_Show(track, 0, 0) -- Hide FX Chain
    else
        local t = {}
        local nextfx = 0
        for i = 0, fxcount-1 do -- Find the first open FX, if any
            local open = reaper.TrackFX_GetOpen(track, i)
            if open then t[#t+1] = i end
        end
        if #t ~= 0 then nextfx = t[1]+1 end
        if nextfx == fxcount then nextfx = 0 end
        reaper.TrackFX_Show(track, nextfx, 3) -- Show "next" FX
        for i,v in ipairs(t) do -- Close other open FX
            if tonumber(v) ~= tonumber(nextfx) then reaper.TrackFX_Show(track, v, 2) end
        end
        reaper.TrackFX_Show(track, 0, 0) -- Hide FX Chain
        local PATTERN = "FLOAT" .. (" (%d+)"):rep(4)
        local ok, chunk = reaper.GetTrackStateChunk(track, "")
        if not ok then return end
        local plx, ply, plw, plh = chunk:match(PATTERN) -- Get FX window position & size
        local _, _, scrw, scrh = reaper.my_getViewport(0, 0, plw, plh, 0, 0, plw, plh, 1) -- Get screen size
        local x, y = math.floor((scrw - plw)/2), math.floor((scrh - plh)/2) -- Centered FX on screen coordinates
        if tonumber(plx) ~= x and tonumber(ply) ~= y then -- Center FX if not centered
            local chunk = chunk:gsub(PATTERN, "FLOAT "..x..", "..y..", "..plw..", "..plh)
            reaper.SetTrackStateChunk(track, chunk)
        end
    end
    reaper.UpdateArrange()
    reaper.Undo_EndBlock("next fx", 1)
end

reaper.PreventUIRefresh(1)
Main()
reaper.PreventUIRefresh(-1)
then an undo point is created as expected.. But I do not want to do that.. I want no undo point at all when cycling between FX
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)
amagalma is offline   Reply With Quote
Old 06-21-2017, 01:05 PM   #11
me2beats
Human being with feelings
 
me2beats's Avatar
 
Join Date: Jul 2015
Location: Yekaterinburg, Russia
Posts: 400
Default

Quote:
Originally Posted by amagalma View Post
It is created as it should.. 'script name'
http://imgur.com/a/vkmak
__________________
My Reapack Repo
Donation
me2beats is offline   Reply With Quote
Old 06-21-2017, 01:33 PM   #12
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 3,458
Default

Thanks for getting into the trouble!

You have only one FX loaded in the chain.. Try with more and see
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)
amagalma is offline   Reply With Quote
Old 06-21-2017, 02:27 PM   #13
me2beats
Human being with feelings
 
me2beats's Avatar
 
Join Date: Jul 2015
Location: Yekaterinburg, Russia
Posts: 400
Default

Quote:
Originally Posted by amagalma View Post
Thanks for getting into the trouble!

You have only one FX loaded in the chain.. Try with more and see
Code:
reaper.TrackFX_Show(track, 0, 2)
if you cooment it the script will not create undo points.

there are several API functions with same issue (they clutter Undo history)
for example CSurf_OnPlayRateChange() and TrackFX_SetParam().

As far as I know, there is no alternative - it's impossible to close FX even via track chunk
__________________
My Reapack Repo
Donation
me2beats is offline   Reply With Quote
Old 06-21-2017, 04:42 PM   #14
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 3,458
Default

Yes...

I tried to do it with track chunk, but it was not possible. Setting SHOW 0 and FLOAT to FLOATPOS in the chunk, didn't do anything..
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)
amagalma is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 09:01 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.