/
Fabian_MFXlist.lua
1658 lines (1332 loc) · 59.1 KB
/
Fabian_MFXlist.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- @description MFXlist
-- @author M Fabian
-- @version 0.9.9beta
-- @changelog Fix auto-float quick added FX (issue #33)
-- @screenshot MFXlist.gif https://github.com/martinfabian/MFXlist/raw/main/MFXlist.gif
-- @about
-- # MFXlist
-- A Lua ReaScript that implements an FX strip meant to be docked to the left
-- of the TCP in the arrange view. MFXlist packages existing functionality into
-- a better (in my opinion) user interface compared to the existing native
-- implementation; the used screen estate is simply smaller.
--
-- MFXlist is mainly developed and tested on Windows and Reaper 6.20, and it
-- uses the js_ReaScriptAPI extension (https://forum.cockos.com/showthread.php?t=212174).
--
-- For detailed info see the MFXlist Github repo https://github.com/martinfabian/MFXlist.
-- For bugs and questions, see the Reaper forum thread https://forum.cockos.com/showthread.php?p=2398556
local string, table, math = string, table, math
local rpr, gfx = reaper, gfx
-------------------------------------------
-- Variables with underscore are global
-- All caps denotes constants, do not assign to these in the code!
-- Non-constants are used to communicate between different parts of the code
MFXlist =
{
-- user settable stuff
COLOR_EMPTYSLOT = {40/255, 40/255, 40/255},
COLOR_FXHOVERED = {1, 1, 0},
COLOR_DROPMOVE = {0, 0, 1},
COLOR_DROPCOPY = {0, 1, 0},
COLOR_SELECTEDTRACK = {1, 1, 1},
-- Determines whether to show FX type (JS, VST, etc) in the name
SHOW_FXTYPE = false,
--[[ not used for now
COLOR_BLACK = {012/255, 012/255, 012/255},
COLOR_VST = {},
COLOR_JSFX = {},
COLOR_HIGHLIGHT = {},
COLOR_FAINT = {60/255, 60/255, 60/255},
--]]
FX_DISABLEDA = 0.3, -- fade of name for disabled FX
FX_OFFLINEDA = 0.1, -- even fainter for offlined FX
-- Delay for return of focus during mouse wheel
FOCUS_DELAY = 10,
FONT_NAME1 = "Arial",
FONT_NAME2 = "Courier New",
FONT_SIZE1 = 14,
FONT_SIZE2 = 16,
FONT_FXNAME = 14,
FONT_FXBOLD = 15,
FONT_HEADER = 16,
FONT_BOLDFLAG = 0x42000000, -- bold
FONT_ITFLAG = 0x49000000, -- italics
FONT_OUTFLAG = 0x4F000000, -- outline
FONT_BLURFLAG = 0x52000000, -- blurred
FONT_SHARPFLAG = 0x53000000, -- sharpen
FONT_UNDERFLAG = 0x55000000, -- underline
FONT_INVFLAG = 0x56000000, -- invert
-- Script specific constants, from here below change only if you really know what you are doing
SCRIPT_VERSION = "v0.9.9",
SCRIPT_NAME = "MFX-list",
SCRIPT_AUTHORS = {"M Fabian"},
SCRIPT_YEAR = "2020-2021",
-- Mouse button and modifier key constants
MB_LEFT = 1,
MB_RIGHT = 2,
MOD_CTRL = 4,
MOD_SHIFT = 8,
MOD_ALT = 16,
MOD_WIN = 32,
MOD_KEYS = 4+8+16+32,
-- determines how far nmouse can be moved between down and up to still be considered a left click
-- this then also decides how much the mosue has to move with left MB down to be considered as dragging
CLICK_RESOX = 30, -- maybe should not really care about horizontal moves?
CLICK_RESOY = 10,
-- Right click menu
MENU_STR = "Info|Quit",
MENU_SHOWINFO = 1,
MENU_QUIT = 2,
MENU_QUICKFX = {"ReaEQ", "ReaComp", "ReaFIR", "ReaDelay"}, -- To appear on right-click menu
-- Flag constants for TrackFX_Show(track, index, showFlag)
FXCHAIN_HIDE = 0,
FXCHAIN_SHOW = 1,
FXFLOAT_HIDE = 2,
FXFLOAT_SHOW = 3,
-- flag constants for GetTrackState(track) return
TRACK_FXENABLED = 4,
TRACK_MUTED = 8, -- seemed liek a good idea, but don't know how to really use it
-- Height for FX slots, FX names are drawn centered (and clipped) inside this high rectangles
SLOT_HEIGHT = 13, -- pixels high
-- Gap between the first track and teh master track (when visible)
MASTER_GAP = 5,
-- For matching and shrinking FX names
MATCH_UPTOCOLON = "(.-:)",
MATCH_UPTOSLASH = "(.-/)",
-- Nondocked window size, and docker address (overridden from EXTSTATE if such exists)
WIN_X = 1000,
WIN_Y = 200,
WIN_W = 200,
WIN_H = 200,
DOCKER_NUM = 512+1, -- 512 = left of arrange view, +1 == docked (not universally true)
-- "Win" for Windows, "Mac" for Mac, "Linux" for Linux, determined when initializing
WHAT_OS = nil, -- On Mac the y-coords go the other direction
-- Window class names to look for, I have no idea how or if this works on Mac/Linux
-- CLASS_TRACKLISTWIN = "REAPERTrackListWindow", -- this is the arrange view where the media items live
CLASS_TCPDISPLAY = "REAPERTCPDisplay", -- this is the TCP where the track panes live
TCP_HWND = nil, -- filled in when script initializes (so strictly speaking not constant, but yeah...)
TCP_top = nil, -- Set on every defer before calling any other function
TCP_bot = nil, -- Set on every defer before calling any other function
MFX_HWND = nil, -- this is our own window, need this to make mousewheel work
ACT_SCROLLVERT = 989, -- View: Scroll vertically (MIDI CC relative/mousewheel)
ACT_ZOOMVERT = 991, -- View: Zoom vertically (MIDI CC relative/mousewheel)
ACT_SCROLLVIEWDOWN = 40139, -- View: Scroll view down
ACT_SCROLLVIEWUP = 40138, -- View: Scroll view up
ACT_ZOOMINVERT = 40111, -- View: Zoom in vertical
ACT_ZOOMOUTVERT = 40112, -- View: Zoom out vertical
ACT_FXBROWSERWINDOW = 40271, -- View: Show FX browser window
ALT_FXBROWSER = nil, -- Alternative FX browswer, put command ID here
-- "_RS490460a16d7e7bb0285ccb1891b67f8f59593a61", -- Quick Adder
-- "_RS36fe8a223d7ec08e45d4e8569c9bc15b9e417dfa", -- Fast FX finder
ALT_FXBROWSERTITLE = nil,
CMD_FOCUSARRANGE = 0, -- SWS/BR: Focus arrange (_BR_FOCUS_ARRANGE_WND)
CMD_FOCUSTRACKS = 0, -- SWS/BR: Focus tracks (_BR_FOCUS_TRACKS)
CMD_SCROLLTCPDOWN = 0,-- Xenakios/SWS: Scroll track view down (page)
CMD_SCROLLTCPUP = 0, -- Xenakios/SWS: Scroll track view up (page)
-- off-screen draw buffer for the header (coudl not get thsi to work, see drawHeader() below)
BLITBUF_HEAD = 16,
BLITBUF_HEADW = 300,
BLITBUF_HEADH = 300,
-- Globally accessible variables used to communicate between different parts of the code
mouse_y = nil, -- is set to mouse_y when mouse inside MFXlist, else nil
track_hovered = nil, -- is set to the track (ptr) currently under mouse cursor, nil if mouse outside of client are
fx_hovered = nil, -- is set to the index (1-based!) of FX under the mouse cursor, nil if mouse is outside of current track FX
mbl_downx = nil, -- stores left mouse button down coords, used for left MB drag actions
mbl_downy = nil,
down_object = nil, -- {track, fx} stores left mouse button down object if any
drag_object = nil, -- {track, fx} that is dragged, given by track_hovered, fx_hovered
drag_endx = nil,
drag_endy = nil,
openwin_list = nil, -- list of currently open windows to help the external win-close focus issue
count_down = 0,
footer_text = "MFX-list", -- changes after initializing, shows name of currently hovered track
header_text = "MFX-list", -- this doesn't really change after initialzing, but could if useful
}
local MFXlist = MFXlist -- MFXlist has to be global for preswet to work, here it becmoes local
local CURR_PROJ = 0
------------------------------------------ Stolen from https://stackoverflow.com/questions/41942289/display-contents-of-tables-in-lua
-- Recursive print of a table, returns a string
local function tprint (tbl, indent)
if not indent then indent = 0 end
local toprint = string.rep(" ", indent) .. "{\r\n"
indent = indent + 2
for k, v in pairs(tbl) do
toprint = toprint .. string.rep(" ", indent)
if (type(k) == "number") then
toprint = toprint .. "[" .. k .. "] = "
elseif (type(k) == "string") then
toprint = toprint .. k .. "= "
end
if (type(v) == "number") then
toprint = toprint .. v .. ",\r\n"
elseif (type(v) == "string") then
toprint = toprint .. "\"" .. v .. "\",\r\n"
elseif (type(v) == "table") then
toprint = toprint .. tprint(v, indent + 2) .. ",\r\n"
else
toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
end
end
toprint = toprint .. string.rep(" ", indent-2) .. "}"
return toprint
end
-------------------------- Windows specific stuff here --------------------------------------------------------------
-------------------------------------------------------- Stolen from https://forum.cockos.com/showthread.php?t=230919
-- Requires js_ReaScriptAPI extension,
-- https://forum.cockos.com/showthread.php?t=212174
local function getClientBounds(hwnd)
local ret, left, top, right, bottom = rpr.JS_Window_GetClientRect(hwnd)
local height = bottom - top
if MFXlist.WHAT_OS == "Mac" then height = top - bottom end
return left, top, right-left, height
end --GetClientBounds
-----------------------------------------
local function getAllChildWindows(hwnd)
local arr = rpr.new_array({}, 255)
rpr.JS_Window_ArrayAllChild(hwnd, arr)
return arr.table()
end -- getAllChildWIndows
------------------------------------------
local function getTitleMatchWindows(title, exact)
local reaperarray = rpr.new_array({}, 255)
rpr.JS_Window_ArrayFind(title, exact, reaperarray)
return reaperarray.table()
end -- getTitleMatchWindows
-----------------------------------------------------------------
-- Find the occurrance-th instance of a window named by classname
local function FindChildByClass(hwnd, classname, occurrence)
local adr = getAllChildWindows(hwnd)
local count = #adr
for j = 1, count do
local hwnd = rpr.JS_Window_HandleFromAddress(adr[j])
if rpr.JS_Window_GetClassName(hwnd) == classname then
occurrence = occurrence - 1
if occurrence == 0 then
return hwnd
end
end
end
end --FindChildByClass
---------------------------------------------------------
-- Returns the HWND and the screen coordinates of the TCP
local function getTCPProperties()
-- get first reaper child window with classname "REAPERTCPDisplay".
local tcp_hwnd = FindChildByClass(rpr.GetMainHwnd(), MFXlist.CLASS_TCPDISPLAY, 1)
if tcp_hwnd then
local x,y,w,h = getClientBounds(tcp_hwnd)
--msg(w) -- show width
return tcp_hwnd, x, y, w, h
end
return nil, -1, -1, -1, -1
end
------------------------------------------------------------------------------------
-- This works, except for when no modifier key is used, then it scrolls the arrange!
local function sendTCPWheelMessage(mbkeys, wheel, screenx, screeny)
local retval = rpr.JS_WindowMessage_Send(MFXlist.TCP_HWND,
"WM_MOUSEWHEEL",
mbkeys, -- wParam, mouse buttons and modifier keys
wheel, -- wParamHighWWord, wheel distance
screenx, screeny) -- lParam, lParamHighWord, need to fake it is over TCP?
return retval
end -- sendTCPWheelMessage
--------------------------------------------------------------------
-- None of these seem to work, disregard
local function sendTCPScrollMessage(mbkeys, wheel, screenx, screeny)
-- From swell-types.h:
local SB_LINEUP, SB_LINEDOWN = 0, 1
local updown = wheel < 0 and SB_LINEDOWN or SB_LINEUP
local retval = rpr.JS_WindowMessage_Send(MFXlist.TCP_HWND, "WM_VSCROLL", updown, 0, 0, 0)
end
---------------------------------------------- SWS specifc stuff go here
local function initSWSCommands()
MFXlist.CMD_FOCUSARRANGE = rpr.NamedCommandLookup("_BR_FOCUS_ARRANGE_WND")
MFXlist.CMD_FOCUSTRACKS = rpr.NamedCommandLookup("_BR_FOCUS_TRACKS")
MFXlist.CMD_SCROLLTCPDOWN = rpr.NamedCommandLookup("_XENAKIOS_TVPAGEDOWN") -- scrolls TCP, but too much
MFXlist.CMD_SCROLLTCPUP = rpr.NamedCommandLookup("_XENAKIOS_TVPAGEUP")
end
------------------------------------------------------
-- If given the command ID for alternative FX browser
-- replace that command ID by what Reaper returns for it
-- Open the browser to get its hwnd and title
-- The title is needed to toggle it open/close,
-- or can we use the hwnd for that?
local function initAltFXBrowser()
if MFXlist.ALT_FXBROWSER then
local cmd = rpr.NamedCommandLookup(MFXlist.ALT_FXBROWSER)
if cmd <= 0 then
MFXlist.ALT_FXBROWSER = nil
return
end
MFXlist.ALT_FXBROWSER = cmd
rpr.PreventUIRefresh(1)
rpr.Main_OnCommand(MFXlist.ALT_FXBROWSER, 0) -- open the window
local hwnd = rpr.JS_Window_GetFocus()
if not hwnd then
if DO_DEBUG then Msg("Could not get handle to Alt FX Browser window") end
MFXlist.ALT_FXBROWSER = nil
else -- So we have the hwnd now, will it remain the same? Probably not
MFXlist.ALT_FXBROWSERTITLE = rpr.JS_Window_GetTitle(hwnd)
-- rpr.JS_Window_Destroy(hwnd)
rpr.JS_WindowMessage_Post(hwnd, "WM_CLOSE", 0,0,0,0) -- if I close like this I guess I can reuse the hwnd
if DO_DEBUG then Msg("Title of Alt FX Browser: "..MFXlist.ALT_FXBROWSERTITLE) end -- this works!
end
rpr.PreventUIRefresh(-1)
end
end -- initAltFXBrowser
------------------------------------------------
-- Fetches the action IDs for various commands
local function initCommands()
initAltFXBrowser()
end -- initCommands
-------------------------------------------------
-- These scroll whole pages, I don't want that
local function scrollTCPUp()
rpr.Main_OnCommand(MFXlist.CMD_SCROLLTCPUP, 0)
end -- scrollTCPUp
-------------------------------
local function scrollTCPDown()
rpr.Main_OnCommand(MFXlist.CMD_SCROLLTCPDOWN, 0)
end -- scrollTCPDown
-----------------------------------------------
-- Set the focus to TCP so keystrokes go there
-- Called after (almost) every mouse click
local function focusTCP()
rpr.JS_Window_SetFocus(MFXlist.TCP_HWND)
end -- focusTCP
---------------
local function focusMFX()
rpr.JS_Window_SetFocus(MFXlist.MFX_HWND)
end -- focusMFX
---------------
local function hasFocusMFX()
return rpr.JS_Window_GetFocus() == MFXlist.MFX_HWND
end -- hasFocusMFX
----------------------------------------------------------
-- Simple linked list implementation for the openwin_list
linkedList = -- has to be global here, made local below
{
head = nil,
length = 0,
new = function()
local self = {}
setmetatable(self, {__index = linkedList})
return self
end, -- new
insert = function(self, element)
self.head = {next = self.head, elem = element}
self.length = self.length + 1
return
end, -- insert
print = function(self, outputter)
if not outputter then
outputter = print
end
if not self.head then
outputter("<empty list>")
return
end
local ptr = self.head
while ptr do
outputter(ptr.elem)
ptr = ptr.next
end
end, -- print
find = function(self, element, compare)
if not compare then
compare = function(p1, p2) return p1 == p2 end
end
local ptr = self.head
while ptr do
if compare(ptr.elem, element) then
return ptr
end
ptr = ptr.next
end
return ptr -- nil
end, -- find
-- Have to first find, then remove
remove = function(self, ptr)
if not ptr then return false end
if ptr == self.head then
self.head = self.head.next
self.length = self.length - 1
return true
end
local pptr = self.head
while pptr do
if pptr.next == ptr then
pptr.next = ptr.next -- adjust links
self.length = self.length - 1
return true
end
pptr = pptr.next
end
return false -- not found
end, -- remove (ptr)
}
local linkedList = linkedList
----------------------------------------------------------------
-- This doesn't work, I find no way to make sense of the docker
local function findLeftDock()
local mhwnd = rpr.GetMainHwnd()
local _, mleft, mtop, mright, mbottom = rpr.JS_Window_GetClientRect(mhwnd)
Msg("MainHwnd, left: "..mleft..", top: "..mtop..", right: "..mright..", bottom: "..mbottom)
local adr = getTitleMatchWindows("REAPER_dock", true) -- this does get all 16 dockers
local count = #adr
local docknumber = 0 -- the order of how the dockers are returned does not make sense to me
for i = 1, count do
local hwnd = rpr.JS_Window_HandleFromAddress(adr[i])
--local classname = rpr.JS_Window_GetClassName(hwnd)
--Msg("i: "..i..", adr: "..adr[i]..", class: "..classname)
--if classname == "REAPER_dock" then
local _, left, top, right, bottom = rpr.JS_Window_GetRect(hwnd)
Msg("REAPER_dock #"..docknumber..", left: "..left..", top: "..top..", right: "..right..", bottom: "..bottom)
if left == mleft then -- this should be the left docker?
-- then what? how to get its number? do they come in order?
--MFXlist.DOCKER_NUM = 2^docknumber + 1
end
docknumber = docknumber + 1
--end
end
end -- findLeftDock
---------------------------------------------
-- This is the next best thing we can do, add
-- menu option to switch to the next docker
local function nextDocker(bits)
local masked = bits & 0xFFFF
local shifted = masked >> 1
if shifted == 0 then
shifted = 0x8001
else
shifted = shifted | 0x01
end
return shifted
end -- nextDocker
------------------------------
local function switchDocker()
MFXlist.DOCKER_NUM = nextDocker(MFXlist.DOCKER_NUM)
gfx.quit()
gfx.clear = MFXlist.COLOR_EMPTYSLOT[1] * 255 + MFXlist.COLOR_EMPTYSLOT[2] * 255 * 256 + MFXlist.COLOR_EMPTYSLOT[3] * 255 * 65536
gfx.init(MFXlist.SCRIPT_NAME, MFXlist.WIN_W, MFXlist.WIN_H, MFXlist.DOCKER_NUM, MFXlist.WIN_X, MFXlist.WIN_Y)
end -- switchDocker
-----------------------------------------
-- Seems that the only way to affect last
-- touched track by scripting is to do:
local function setLastTouchedTrack(track)
-- Save current track selection
rpr.SetOnlyTrackSelected(track)
-- set back current track selection
end -- setLastTouchedTrack
--------------------------------------------------------
local function split(str, sep)
local fields = {}
local pattern = str.format("([^%s]+)", sep)
str:gsub(
pattern,
function(c)
fields[#fields + 1] = c
end
)
return fields
end
--------------------------------------------------------
local function formatFXNameAndType(fxname, fxtype, fxprefix)
local trimmed_fx_name = fxname
if fxtype == 2 then -- JS
-- local segments = split(fxname, "/")
-- trimmed_fx_name = segments[#segments]
local segment = fxname:match(MFXlist.MATCH_UPTOSLASH)
if segment and segment:len() >= 5 then
trimmed_fx_name = fxname:gsub(MFXlist.MATCH_UPTOSLASH, "")
end
end
-- Strip parenthesized text
trimmed_fx_name = trimmed_fx_name:gsub("%([^()]*%)", "")
-- For video processor we remove trailing " -- video processor"
if fxtype == 6 then
trimmed_fx_name = trimmed_fx_name:gsub(" -- video processor", "")
end
if MFXlist.SHOW_FXTYPE then
trimmed_fx_name = fxprefix..trimmed_fx_name
end
return trimmed_fx_name
end
---------------------------------------------------
-- If this fx has a prefix in its name, return that
-- else, generate a prefix given the fxtype number
local function getFXPrefix(fxname, fxtype)
local prefix = fxname:match(MFXlist.MATCH_UPTOCOLON)
if prefix and prefix:len() <= 5 then -- 5, because "VSTi:" is 5 long
return prefix, fxname:gsub(MFXlist.MATCH_UPTOCOLON.."%s", "") -- remove the prefix
end
if fxtype == 2 then
return "JS:", fxname
elseif fxtype == 3 then
return "VST:", fxname
elseif fxtype == 6 then
return "VID:", fxname
end
return "", fxname
end -- getFXPrefix
--------------------------------------------------------
local function collectFX(track)
assert(track, "collectFX: invalid parameter - track")
local fxtab = {}
local numfx = rpr.TrackFX_GetCount(track)
for i = 1, numfx do
local _, fxname = rpr.TrackFX_GetFXName(track, i-1, "")
local fxtype = rpr.TrackFX_GetIOSize(track, i-1) -- This gets the type as a number
--fxname:match(MFXlist.MATCH_UPTOCOLON) or "VID:" -- Video processor FX don't have prefixes
local fxprefix
fxprefix, fxname = getFXPrefix(fxname, fxtype)
--if fxtype == 6 then Msg("fxprefix: "..fxprefix..", fxname: "..fxname) end
fxname = formatFXNameAndType(fxname, fxtype, fxprefix)
local enabled = rpr.TrackFX_GetEnabled(track, i-1)
local offlined = rpr.TrackFX_GetOffline(track, i-1)
table.insert(fxtab, {fxname = fxname, fxtype = fxtype, fxprefix = fxprefix, enabled = enabled, offlined = offlined}) -- confusing <key, value> pairs here, but it works
end
return fxtab
end
------------------------------------------
local function getTrackPosAndHeight(track)
assert(track, "getTrackPosAndHeight: invalid parameter - track")
local height = rpr.GetMediaTrackInfo_Value(track, "I_WNDH") -- current TCP window height in pixels including envelopes
local posy = rpr.GetMediaTrackInfo_Value(track, "I_TCPY") -- current TCP window Y-position in pixels relative to top of arrange view
return posy, height
end -- getTrackPosAndHeight()
---------------------------------------------------------------------------
local function getTrackInfo(track)
assert(track, "getTrackInfo: invalid parameter - track")
local _, name = rpr.GetTrackName(track)
local visible = rpr.IsTrackVisible(track, false) -- false for TCP (true for MCP)
local enabled = rpr.GetMediaTrackInfo_Value(track, "I_FXEN") ~= 0 -- fx enabled, 0=bypassed, !0=fx active
local selected = rpr.IsTrackSelected(track) -- true if selected, false if not
local posy, height = getTrackPosAndHeight(track)
local fx = collectFX(track)
return {track = track, name = name, selected = selected, visible = visible, enabled = enabled, height = height, posy = posy, fx = fx}
end
------------------------------
local function collectTracks()
local tracks = {}
if rpr.GetMasterTrackVisibility() & 0x1 == 1 then -- Master track visible in TCP
local master = rpr.GetMasterTrack(CURR_PROJ)
local info = getTrackInfo(master)
table.insert(tracks, info)
end
local numtracks = rpr.CountTracks(CURR_PROJ) -- excludes the master track, taken care of above
for i = 1, numtracks do
local track = rpr.GetTrack(CURR_PROJ, i-1)
local info = getTrackInfo(track)
table.insert(tracks, info)
end
return tracks
end
--------------------------------------------------------
local function showTracks(tracks) -- In console output
for i = 1, #tracks do
local trinfo = tracks[i]
-- local fxtable = collectFX(trinfo.track)
local fxtable = trinfo.fx
for j = 1, #fxtable do
local fx = fxtable[j]
end
end
end
--------------------------------------------------------
-- Find the index of the first track visible in the TCP
-- A track can be invisible from the TCP for two reasons:
-- 1. It has its TCP visbility property set to false, and then its height seems to be 0 (this is used)
-- 2. It is outside of the TCP view rectangle, posy either negative or larger than TCP height
local function getFirstTCPTrackLinear()
-- This version does a linear search from index 0 looking for the first track for which posy+height > 0
-- Note that the returned track index is 1-based, just as track numbering, so MASTER is index 0
if rpr.GetMasterTrackVisibility() & 0x1 == 1 then -- Master track visible in TCP
local master = rpr.GetMasterTrack(CURR_PROJ)
local posy, height = getTrackPosAndHeight(master)
if height + posy > 0 then return master, 0 end
end
local numtracks = rpr.CountTracks(CURR_PROJ) -- excludes the master track, taken care of above
if numtracks == 0 then return nil, -1 end
for i = 1, numtracks do
local track = rpr.GetTrack(CURR_PROJ, i-1)
local posy, height = getTrackPosAndHeight(track)
if height + posy > 0 then return track, i end -- rules out invisible track (height == 0) at the top (posy = 0)
end
assert(nil, "getFirstTrackLinear: Should never get here!")
end -- getFirstTCPTrackLinear
---------------------------------------
local function getFirstTCPTrackBinary()
local fixForMasterTCPgap = false
if rpr.GetMasterTrackVisibility() & 0x1 == 1 then -- Master track visible in TCP
local master = rpr.GetMasterTrack(CURR_PROJ)
local posy, height = getTrackPosAndHeight(master)
if height + posy > 0 then return master, 0 end
if height + posy + MFXlist.MASTER_GAP >= 0 then fixForMasterTCPgap = true end
end
local numtracks = rpr.CountTracks(CURR_PROJ)
if numtracks == 0 then return nil, -1 end
-- When the MASTER posy + height == 0, then the 0th track is at posy == 5
-- And this is then the first visible track. Special case
if fixForMasterTCPgap then
local track = rpr.GetTrack(CURR_PROJ, 0)
return track, 1
end
local left, right = 0, numtracks - 1
while left <= right do
local index = math.floor((left + right) / 2)
local track = rpr.GetTrack(CURR_PROJ, index)
local posy, height = getTrackPosAndHeight(track)
if posy < 0 then
if posy + height > 0 then return track, index + 1 end -- Rules out invisible tracks, height == 0, at the top
left = index + 1
elseif posy > 0 then
right = index - 1
else -- posy == 0, then this is the one
return track, index + 1
end
end
-- Are all tracks hidden (height = 0) and first track has negative y-coord? (issue #18)
-- Then there are no visible tracks, what do we do? Pretend that there are no tracks!
return nil, -1
end -- getFirstTCPTrackBinary
---------------------------------------------------------------------------
-- Does a binary search, halving and halving until it finds the right track
-- If no tracks, or only the Master track is visible, this returns nil, 0
-- But in that case getFirstTCPTrack has already returned either the master or -1
local function getLastTCPTrackBinary(tcpheight)
assert(tcpheight, "getLastTCPTrack: invalid parameter - tcpheight")
local numtracks = rpr.CountTracks(CURR_PROJ)
if numtracks == 0 then return nil, 0 end
-- is the last track visible?, If so we are done
local track = rpr.GetTrack(CURR_PROJ, numtracks-1)
local posy, _ = getTrackPosAndHeight(track)
if posy < tcpheight then return track, numtracks end
-- else, do a binary search
local left, right = 0, numtracks - 1
while left <= right do
local index = math.floor((left + right) / 2)
local track = rpr.GetTrack(CURR_PROJ, index)
local posy, height = getTrackPosAndHeight(track)
if posy < tcpheight then
if posy + height >= tcpheight then return track, index + 1 end
left = index + 1
elseif posy > tcpheight then
right = index - 1
else -- posy == tcpheight, the previous track is the last visible
local track = rpr.GetTrack(CURR_PROJ, index - 1)
return track, index
end
end
return nil, 0 -- we should never really get here
end -- getLastTCPTrackBinary
--------------------------------------------------------------------
-- Since the TCP has a limited number of visible tracks, a linear search
-- starting from the first visible track may be faster than a binary
-- That was the idea, but measuring there seems to be no significant improvement
-- Instead binary search is slightly better with many visible tracks
local function getLastTCPTrackLinear(tcpheight, firsttrack)
assert(tcpheight and firsttrack, "getLastTrackLinear: invalid parameter - tcpheight or firsttrack")
-- Same as in binary search, first take care of some obvious easy cases
local numtracks = rpr.CountTracks(CURR_PROJ)
if numtracks == 0 then return nil, 0 end
-- is the last track visible?, If so we are done
local track = rpr.GetTrack(CURR_PROJ, numtracks-1)
local posy, _ = getTrackPosAndHeight(track)
if posy < tcpheight then return track, numtracks end
-- else, look from the first towards the last linearily
for i = firsttrack, numtracks do -- firsttrack is 1-based
local track = rpr.GetTrack(CURR_PROJ, i-1)
local posy, height = getTrackPosAndHeight(track)
if posy + height > tcpheight then return track, i end
end
assert(nil, "getLastTCPTrackLinear: should not really get here!")
end -- getLastTCPTrackLinear
--------------------------------------------
-- Tracks can be invisible for two reasons:
-- 1. outside the TCP bounding box
-- 2. have visibility property turned off
local function collectVisibleTracks()
local _, _, _, h = getClientBounds(MFXlist.TCP_HWND)
local _, findex = getFirstTCPTrackBinary()
local _, lindex = getLastTCPTrackBinary(h)
local vistracks = {}
if findex < 0 then return vistracks end -- No visible tracks
if findex == 0 then -- master track is visible
local master = rpr.GetMasterTrack(CURR_PROJ)
local minfo = getTrackInfo(master)
table.insert(vistracks, minfo)
findex = 1
end
for i = findex, lindex do
local track = rpr.GetTrack(CURR_PROJ, i-1)
local trinfo = getTrackInfo(track)
if trinfo.visible then table.insert(vistracks, trinfo) end
end
return vistracks
end -- collectVisibleTracks
-----------------------------
local function drawHeader()
-- Draw over everything above the FX list drawing area
gfx.set(MFXlist.COLOR_EMPTYSLOT[1], MFXlist.COLOR_EMPTYSLOT[2], MFXlist.COLOR_EMPTYSLOT[3])
gfx.rect(0, 0, gfx.w, MFXlist.TCP_top)
--gfx.set(MFXlist.COLOR_FAINT[1], MFXlist.COLOR_FAINT[1], MFXlist.COLOR_FAINT[1])
gfx.set(1, 1, 1, 0.7)
gfx.x, gfx.y = 0, 0
gfx.setfont(MFXlist.FONT_HEADER)
gfx.drawstr(MFXlist.header_text, 5, gfx.w, MFXlist.TCP_top)
gfx.a = MFXlist.FX_DISABLEDA
gfx.line(0, MFXlist.TCP_top, gfx.w, MFXlist.TCP_top)
end -- drawHeader
------------------------------
local function drawFooter()
-- Draw bottom line of FX list area (should not draw FX below this, it will be erased)
gfx.line(0, MFXlist.TCP_bot, gfx.w, MFXlist.TCP_bot)
gfx.set(MFXlist.COLOR_EMPTYSLOT[1], MFXlist.COLOR_EMPTYSLOT[2], MFXlist.COLOR_EMPTYSLOT[3])
gfx.rect(0, MFXlist.TCP_bot + 1, gfx.w, gfx.h - MFXlist.TCP_bot - 1)
local text = MFXlist.footer_text
if text and text ~= "" then
gfx.set(1, 1, 1, 0.7)
gfx.setfont(MFXlist.FONT_FXNAME) --, MFXlist.FONT_NAME1, MFXlist.FONT_SIZE1)
gfx.x, gfx.y = 0, MFXlist.TCP_bot
gfx.drawstr(text, 5, gfx.w, gfx.h) -- Note, the last two parameters are the right/bottom COORDS of the box to draw within, not width/height
end
end -- drawFooter
----------------------------------------------------
local function drawSelectedIndicator(ycoord, height)
-- Msg("drawSelectedIndicator("..ycoord..", "..height..")")
gfx.set(MFXlist.COLOR_SELECTEDTRACK[1], MFXlist.COLOR_SELECTEDTRACK[2], MFXlist.COLOR_SELECTEDTRACK[3], 1)
gfx.line(gfx.w-2, ycoord + 1, gfx.w-2, ycoord + height - 2)
end -- drawSelectedIndicator
----------------------------------
local function drawDropIndicator()
if gfx.mouse_cap & MFXlist.MOD_CTRL == MFXlist.MOD_CTRL then
gfx.set(MFXlist.COLOR_DROPCOPY[1], MFXlist.COLOR_DROPCOPY[2], MFXlist.COLOR_DROPCOPY[3])
else
gfx.set(MFXlist.COLOR_DROPMOVE[1], MFXlist.COLOR_DROPMOVE[2], MFXlist.COLOR_DROPMOVE[3])
end
gfx.line(10, gfx.y, gfx.w-10, gfx.y)
end -- drawDropIndicator
-----------------------
local function drawTracks()
-- gfx.set(1, 1, 1)
gfx.setfont(MFXlist.FONT_FXNAME)--, MFXlist.FONT_NAME1, MFXlist.FONT_SIZE1)
MFXlist.fx_hovered = nil
MFXlist.track_hovered = nil
local drawy = MFXlist.TCP_top
local vistracks = collectVisibleTracks()
local numtracks = #vistracks
for i = 1, numtracks do
gfx.set(1, 1, 1, gfx.a)
local insidetrack = false -- used to send message from track to FX on track
local trinfo = vistracks[i]
local posy = trinfo.posy
local height = trinfo.height
local selected = trinfo.selected
local chainon = trinfo.enabled -- track FX chain enabled
-- if the mouse is currently inside this track rect
if MFXlist.mouse_y and posy <= MFXlist.mouse_y-drawy and MFXlist.mouse_y-drawy <= posy + height then
MFXlist.footer_text = trinfo.name
insidetrack = true -- send message to FX part of code, see below
MFXlist.track_hovered = trinfo.track
end
-- Draw bounding box for track FX
gfx.a = MFXlist.FX_OFFLINEDA -- bounding box is always drawn faint
gfx.rect(0, drawy + posy, gfx.w, height, (chainon and 0 or 1)) -- disabled chain is not filled with faint color
-- Calc the number of FX slots to draw, and draw them
local fxlist = trinfo.fx
local numfxs = math.ceil(height / MFXlist.SLOT_HEIGHT) -- max num FX to show
local count = math.min(#fxlist, numfxs)
local cropy = drawy+posy+height-1 -- crop FX name slot to this
gfx.x, gfx.y = 0, drawy + posy -- drawing FX names start at this position
for i = 1, count do
local fx = fxlist[i]
gfx.a = (fx.enabled and not fx.offlined and chainon) and 1 or MFXlist.FX_DISABLEDA -- disabled FX are shown faint
-- if mouse hovers over this FX, draw it in different color
if insidetrack and gfx.y <= MFXlist.mouse_y and MFXlist.mouse_y < gfx.y + MFXlist.SLOT_HEIGHT then
gfx.set(MFXlist.COLOR_FXHOVERED[1], MFXlist.COLOR_FXHOVERED[2], MFXlist.COLOR_FXHOVERED[3], gfx.a)
gfx.setfont(MFXlist.FONT_FXBOLD)
MFXlist.fx_hovered = i -- store fx index (1-based!) for mouse click
else
gfx.setfont(MFXlist.FONT_FXNAME)
gfx.set(1, 1, 1, gfx.a)
end
gfx.x = 0
local corner = math.min(gfx.y + MFXlist.SLOT_HEIGHT, cropy) -- make sure to crop within the bounding track rect
gfx.drawstr(fx.fxname, 1, gfx.w, corner)
if fx.offlined then -- strikeout offlined FX
local w, h = gfx.measurestr(fx.name)
local y = gfx.y + MFXlist.SLOT_HEIGHT/2
gfx.line((gfx.w-w)/2, y, (gfx.w+w)/2, y, gfx.a)
end
-- if dragging and are on top of this FX, show drop indicator above it
if insidetrack and MFXlist.drag_object and MFXlist.drag_object[2] and MFXlist.fx_hovered == i then
drawDropIndicator()
end
gfx.y = gfx.y + MFXlist.SLOT_HEIGHT
end
-- if dragging and not hovering any FX, draw drop indicator at end of FX chain
if insidetrack and MFXlist.drag_object and MFXlist.drag_object[2] and not MFXlist.fx_hovered then
drawDropIndicator()
end
if selected then
drawSelectedIndicator(drawy + posy, height)
end
end
end -- drawTracks
-----------------------------------------------------------
-- Toggle open/close FX window (wtype == 2 for floating)
-- index is here 0-based!
-- wtype is 0 for FX chain window, 2 for floating FX window
local function handleToggleWindow(track, index, wtype)
local openclose = rpr.TrackFX_GetOpen(track, index) and wtype or wtype + 1 -- 0,2: to close, 1,3: to open
if DO_DEBUG then
local _, tname = rpr.GetTrackName(track)
local _, fxname = rpr.TrackFX_GetFXName(track, index, "")
Msg("handleToggleWindow: "..tname..", "..fxname.." (openclose: "..openclose..", wtype: "..wtype..")")
end -- DO_DEBUG
rpr.TrackFX_Show(track, index, openclose)
if openclose == wtype then -- just closed, remove from openwin_list, and focus TCP
local compare = function(p1, p2) return p1[1] == p2[1] and p1[2] == p2[2] end
local ptr = MFXlist.openwin_list:find({track, index}, compare)
if ptr then -- what if not found (we get nil here)?
MFXlist.openwin_list:remove(ptr)
end
if DO_DEBUG then
local str = (not ptr and "nil!" or "found")
Msg("Window "..str..", list size: "..MFXlist.openwin_list.length)
end -- DO_DEBUG
focusTCP()
else -- just opened, add to openwin_list
MFXlist.openwin_list:insert({track, index})
end
end -- handleToggleWindow
-----------------------------------------
-- Shows it in Reaper's console (for now)
-- Not using Msg here, since we want this
-- to show even if DO_DEBUG is false
local function showInfo()
local width = math.tointeger(gfx.w)
local height = math.tointeger(gfx.h)
local x, y, w, h = getClientBounds(MFXlist.TCP_HWND) -- TCP screen coords
rpr.ShowConsoleMsg("\n"..MFXlist.SCRIPT_NAME.." "..MFXlist.SCRIPT_VERSION..'\n')
local authors = table.concat(MFXlist.SCRIPT_AUTHORS, ", ")
rpr.ShowConsoleMsg(authors..", "..MFXlist.SCRIPT_YEAR..'\n')
rpr.ShowConsoleMsg("Dock: "..math.tointeger(gfx.dock(-1))..", gfx.w: "..width..", gfx.h: "..height)
rpr.ShowConsoleMsg("\nTCP area (screen coords): "..x..", "..y..", "..w..", "..h)
rpr.ShowConsoleMsg("\nMFXlist header: 0, 0, "..width..", "..math.tointeger(MFXlist.TCP_top))
rpr.ShowConsoleMsg("\nMFXlist track area: 0, "..math.tointeger(MFXlist.TCP_top)..", "..width..", "..math.tointeger(MFXlist.TCP_bot - MFXlist.TCP_top))
rpr.ShowConsoleMsg("\nMFXlist footer: 0, "..math.tointeger(MFXlist.TCP_bot)..", "..width..", "..math.tointeger(height - MFXlist.TCP_bot))
if DO_DEBUG then
Msg("\nWhat OS? "..MFXlist.WHAT_OS)
Msg("gfx.ext_retina: "..gfx.ext_retina)
end
end -- showInfo
---------------------------------------
local function setupMenu(quickfx)