-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPitchBendTargets.lua
5207 lines (3867 loc) · 150 KB
/
PitchBendTargets.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
--[[
ReaScript name: PitchBend Targets
Website:
Version: 0.5.1
Changelog: Initial release
About: PitchBend Targets allow you to precisely control the pitchbend of any notes
REAPER: at least v6
Author: Sid
License: GNU GPL v3
Made with the Lokasenna_GUI library beta 8
https://github.com/jalovatt/Lokasenna_GUI/wiki/
https://forum.cockos.com/showthread.php?t=177772
Requires The SWS / S&M extension
https://www.sws-extension.org/
Requires js_ReaScriptAPI: API functions for ReaScripts
https://github.com/ReaTeam/Extensions/raw/master/index.xml
_________________________________________________________________________
THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
__________________________________________________________________________
Changelog:
Initial release
]] -- Start Gui
---- Beginning of file: Lokasenna_GUI library beta 8.lua ----
--[=[
Lokasenna_GUI Library
by Lokasenna
Provides functions and classes for adding a GUI to a LUA script with minimal effort
To use:
1. Copy/paste the code below into the beginning of your script.
2. To create the window:
GUI.name = "My window's title"
GUI.x, GUI.y = __, __
GUI.w, GUI.h = __, __
GUI.anchor, GUI.corner = __, __
x,y offset coordinates from the anchor position
w,h window dimensions
anchor "screen" or "mouse"
corner "TL"
"T"
"TR"
"R"
"BR"
"B"
"BL"
"L"
"C"
If no anchor and corner are specified, it will default to the top-left corner of the screen.
3. To add GUI elements, declare a table called GUI.elms and populate it like so:
GUI.elms = {
item1 = type:New(parameters),
item2 = type:New(parameters),
item3 = type:New(parameters),
...etc...
}
(Don't forget those commas at the end)
See the :New method for each element below for a list of parameters
4. Additional documentation:
Lokasenna_GUI script example.lua A working example of the various GUI elements
Lokasenna_GUI example functions.lua Common things you might want the GUI to do for you
]=] --
----------------------------------------------------------------
------------------Copy everything from here---------------------
----------------------------------------------------------------
local function GUI_table()
local GUI = {}
GUI.version = "beta 8"
-- Might want to know this
GUI.OS = reaper.GetOS()
--[[ Use when working with file paths if you need to add your own /s
(Borrowed from X-Raym)
]] --
GUI.file_sep = string.match(GUI.OS, "Win") and "\\" or "/"
-- Also might need to know this
GUI.SWS_exists = function()
local exists = reaper.APIExists("BR_Win32_GetPrivateProfileString")
if not exists then
reaper.ShowMessageBox("This script requires the SWS extension.", "SWS not found", 0)
end
return exists
end
GUI.SaveExt = function()
_, GUI.x, GUI.y, GUI.w, GUI.h = gfx.dock(-1, 0, 0, 0, 0)
reaper.SetExtState(GUI.name, "CoordX", GUI.x, 1)
reaper.SetExtState(GUI.name, "CoordY", GUI.y, 1)
reaper.SetExtState(GUI.name, "CoordW", GUI.w, 1)
reaper.SetExtState(GUI.name, "CoordH", GUI.h, 1)
end
GUI.LoadExt = function()
GUI.x = math.floor(tonumber(reaper.GetExtState(GUI.name, "CoordX")) or GUI.x)
GUI.y = math.floor(tonumber(reaper.GetExtState(GUI.name, "CoordY")) or GUI.y)
GUI.w = math.floor(tonumber(reaper.GetExtState(GUI.name, "CoordW")) or GUI.w)
GUI.h = math.floor(tonumber(reaper.GetExtState(GUI.name, "CoordH")) or GUI.h)
end
GUI.Exit = function()
GUI.SaveExt()
end
---- Keyboard constants ----
GUI.chars = {
ESCAPE = 27,
SPACE = 32,
BACKSPACE = 8,
HOME = 1752132965,
END = 6647396,
INSERT = 6909555,
DELETE = 6579564,
RETURN = 13,
UP = 30064,
DOWN = 1685026670,
LEFT = 1818584692,
RIGHT = 1919379572,
F1 = 26161,
F2 = 26162,
F3 = 26163,
F4 = 26164,
F5 = 26165,
F6 = 26166,
F7 = 26167,
F8 = 26168,
F9 = 26169,
F10 = 6697264,
F11 = 6697265,
F12 = 6697266,
}
--[[ Font and color presets
Can be set using the accompanying functions GUI.font
and GUI.color. i.e.
GUI.font(2) applies the Header preset
GUI.color("elm_fill") applies the Element Fill color preset
Colors are converted from 0-255 to 0-1 when GUI.Init() runs,
so if you need to access the values directly at any point be
aware of which format you're getting in return.
]] --
GUI.fonts = {
-- Font, size, bold/italics/underline
-- ^ One string: "b", "iu", etc.
{"Calibri", 32}, -- 1. Title
{"Calibri", 20}, -- 2. Header
{"Calibri", 16}, -- 3. Label
{"Calibri", 16}, -- 4. Value
version = {"Calibri", 12, "i"},
}
GUI.colors = {
-- Element colors
wnd_bg = {64, 64, 64, 255}, -- Window BG
tab_bg = {56, 56, 56, 255},
elm_bg = {48, 48, 48, 255}, -- Element BG
elm_p_bg = {24, 24, 24, 255}, -- Element BG
elm_frame = {96, 96, 96, 255}, -- Element Frame
elm_fill = {64, 192, 64, 255}, -- Element Fill
elm_outline = {32, 32, 32, 255},
txt = {192, 192, 192, 255}, -- Text
shadow = {0, 0, 0, 80}, -- Shadow
faded = {0, 0, 0, 64},
-- Standard 16 colors
black = {0, 0, 0, 255},
white = {255, 255, 255, 255},
red = {255, 0, 0, 255},
lime = {0, 255, 0, 255},
blue = {0, 0, 255, 255},
yellow = {255, 255, 0, 255},
cyan = {0, 255, 255, 255},
magenta = {255, 0, 255, 255},
silver = {192, 192, 192, 255},
gray = {128, 128, 128, 255},
maroon = {128, 0, 0, 255},
olive = {128, 128, 0, 255},
green = {0, 128, 0, 255},
purple = {128, 0, 128, 255},
teal = {0, 128, 128, 255},
navy = {0, 0, 128, 255},
none = {0, 0, 0, 0},
}
GUI.font = function(fnt)
local font, size, str = table.unpack(type(fnt) == "table" and fnt or GUI.fonts[fnt])
if string.find(GUI.OS, "OSX") then
size = math.floor(size * 0.8)
end
local flags = 0
if str then
for i = 1, str:len() do
flags = flags * 256 + string.byte(str, i)
end
end
gfx.setfont(1, font, size, flags)
end
-- Start drawing with one of the colors from the table
GUI.color = function(col)
-- If we're given a table of color values, just pass it right along
if type(col) == "table" then
gfx.set(col[1], col[2], col[3], col[4] or 1)
else
gfx.set(table.unpack(GUI.colors[col]))
end
end
-- Global shadow size, in pixels
GUI.shadow_dist = 2
--[[
How fast the caret in textboxes should blink, measured in GUI update loops.
'16' looks like a fairly typical textbox caret.
Because each On and Off redraws the textbox's Z layer, this can cause CPU issues
in scripts with lots of drawing to do. In that case, raising it to 24 or 32
will still look alright but require less redrawing.
]] --
GUI.txt_blink_rate = 16
-- Draw the given string of the first color with a shadow
-- of the second color (at 45' to the bottom-right)
GUI.shadow = function(str, col1, col2)
local x, y = gfx.x, gfx.y
GUI.color(col2)
for i = 1, GUI.shadow_dist do
gfx.x, gfx.y = x + i, y + i
gfx.drawstr(str)
end
GUI.color(col1)
gfx.x, gfx.y = x, y
gfx.drawstr(str)
end
-- Draws a string using the given text and outline color presets
GUI.outline = function(str, col1, col2)
local x, y = gfx.x, gfx.y
GUI.color(col2)
gfx.x, gfx.y = x + 1, y + 1
gfx.drawstr(str)
gfx.x, gfx.y = x - 1, y + 1
gfx.drawstr(str)
gfx.x, gfx.y = x - 1, y - 1
gfx.drawstr(str)
gfx.x, gfx.y = x + 1, y - 1
gfx.drawstr(str)
GUI.color(col1)
gfx.x, gfx.y = x, y
gfx.drawstr(str)
end
---- General functions ----
-- Print stuff to the Reaper console. For debugging purposes.
GUI.Msg = function(message)
reaper.ShowConsoleMsg(tostring(message) .. "\n")
end
--[[
Copy the contents of one table to another, since Lua can't do it natively
Provide a second table as 'base' to use it as the basis for copying, only
bringing over keys from the source table that don't exist in the base
'depth' only exists to provide indenting for my debug messages, it can
be left out when calling the function.
]] --
GUI.table_copy = function(source, base, depth)
-- 'Depth' is only for indenting debug messages
depth = ((not not depth) and (depth + 1)) or 0
if type(source) ~= "table" then
return source
end
local meta = getmetatable(source)
local new = base or {}
for k, v in pairs(source) do
if type(v) == "table" then
-- _=dm and GUI.Msg(string.rep("\t", depth + 1)..tostring(k).." is a table; recursing...")
if base then
_ = dm and GUI.Msg(string.rep("\t", depth) .. "base:\t" .. tostring(k))
new[k] = GUI.table_copy(v, base[k], depth)
else
new[k] = GUI.table_copy(v, nil, depth)
end
else
if not base or (base and new[k] == nil) then
_ = dm and GUI.Msg(string.rep("\t", depth) .. "added:\t" .. tostring(k) .. " = " .. tostring(v))
new[k] = v
end
end
-- _=dm and GUI.Msg(string.rep("\t", depth).."done with "..tostring(k))
end
setmetatable(new, meta)
-- _=dm and GUI.Msg(string.rep("\t", depth).."finished copying")
return new
end
-- Compare the contents of one table to another, since Lua can't do it natively
GUI.table_compare = function(t_a, t_b)
if type(t_a) ~= "table" or type(t_b) ~= "table" then
return false
end
local key_exists = {}
for k1, v1 in pairs(t_a) do
local v2 = t_b[k1]
if v2 == nil or not GUI.table_compare(v1, v2) then
return false
end
key_exists[k1] = true
end
for k2, v2 in pairs(t_b) do
if not key_exists[k2] then
return false
end
end
end
-- To open files in their default app, or URLs in a browser
-- Copied from Heda; cheers!
GUI.open_file = function(path)
local OS = reaper.GetOS()
if OS == "OSX32" or OS == "OSX64" then
os.execute('open "" "' .. path .. '"')
else
os.execute('start "" "' .. path .. '"')
end
end
-- Sorting function taken from: http://lua-users.org/wiki/SortedIteration
GUI.full_sort = function(op1, op2)
if type(op1) == "string" and string.match(op1, "^(%-?%d+)") then
op1 = tonumber(string.match(op1, "^(%-?%d+)"))
end
if type(op2) == "string" and string.match(op2, "^(%-?%d+)") then
op2 = tonumber(string.match(op2, "^(%-?%d+)"))
end
-- if op1 == "0" then op1 = 0 end
-- if op2 == "0" then op2 = 0 end
local type1, type2 = type(op1), type(op2)
if type1 ~= type2 then -- cmp by type
return type1 < type2
elseif type1 == "number" and type2 == "number" or type1 == "string" and type2 == "string" then
return op1 < op2 -- comp by default
elseif type1 == "boolean" and type2 == "boolean" then
return op1 == true
else
return tostring(op1) < tostring(op2) -- cmp by address
end
end
--[[
Allows "for x, y in pairs(z) do" in alphabetical/numerical order
Copied from Programming In Lua, 19.3
Call with f = "full" to use the full sorting function above
]] --
GUI.kpairs = function(t, f)
if f == "full" then
f = GUI.full_sort
end
local a = {}
for n in pairs(t) do
table.insert(a, n)
end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function() -- iterator function
i = i + 1
if a[i] == nil then
return nil
else
return a[i], t[a[i]]
end
end
return iter
end
---- Text functions ----
--[[ Preapres a table of character widths
Iterates through all of the font presets, storing the widths
of every printable ASCII character in a table.
Accessable via: GUI.txt_width[font_num][char_num]
- Requires a window to have been opened in Reaper
- 'get_txt_width' and 'word_wrap' will automatically run this
if it hasn't been run already; it may be rather clunky to use
on demand depending on what your script is doing, so it's
typically better to run this immediately after initiliazing
the window and then have the width table ready to use.
]] --
GUI.init_txt_width = function()
_ = dm and GUI.Msg("init_txt_width")
GUI.txt_width = {}
local arr
for k in pairs(GUI.fonts) do
GUI.font(k)
GUI.txt_width[k] = {}
arr = {}
for i = 1, 255 do
arr[i] = gfx.measurechar(i)
end
GUI.txt_width[k] = arr
end
end
-- Returns the total width (in pixels) for a given string and font
GUI.get_txt_width = function(str, font)
if not GUI.txt_width then
GUI.ini_txt_width()
end
local widths = GUI.txt_width[font]
local w = 0
for i = 1, string.len(str) do
w = w + widths[string.byte(string.sub(str, i, i))]
end
return w
end
--[[ Wraps a string to fit a given width
str String. Can include line breaks/paragraphs; they should be preserved.
font Font preset number
w Pixel width
indent Number of spaces to indent the first line of each paragraph
(The algorithm skips tab characters and leading spaces, so
use this parameter instead)
i.e. Blah blah blah blah -> indent = 2 -> Blah blah blah blah
blah blah blah blah blah blah blah blah
pad Indent wrapped lines by the first __ characters of the paragraph
(For use with bullet points, etc)
i.e. - Blah blah blah blah -> pad = 2 -> - Blah blah blah blah
blah blah blah blah blah blah blah blah
This function expands on the "greedy" algorithm found here:
https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap#Algorithm
]] --
GUI.word_wrap = function(str, font, w, indent, pad)
_ = dm and GUI.Msg("word wrap:\n\tfont " .. tostring(font) .. "\n\twidth " .. tostring(w) .. "\n\tindent " .. tostring(indent))
if not GUI.txt_width then
GUI.ini_txt_width()
end
local ret_str = {}
local w_left, w_word
local space = GUI.txt_width[font][string.byte(" ")]
local new_para = indent and string.rep(" ", indent) or 0
local w_pad = pad and GUI.get_txt_width(string.sub(str, 1, pad), font) or 0
local new_line = "\n" .. string.rep(" ", math.floor(w_pad / space))
for line in string.gmatch(str, "([^\n\r]*)[\n\r]*") do
table.insert(ret_str, new_para)
-- Check for leading spaces and tabs
local leading, line = string.match(line, "^([%s\t]*)(.*)$")
if leading then
table.insert(ret_str, leading)
end
w_left = w
for word in string.gmatch(line, "([^%s]+)") do
-- Check for tab stops, since this function won't parse them properly
w_word = GUI.get_txt_width(word, font)
if (w_word + space) > w_left then
table.insert(ret_str, new_line)
w_left = w - w_word
else
w_left = w_left - (w_word + space)
end
table.insert(ret_str, word)
table.insert(ret_str, " ")
end
table.insert(ret_str, "\n")
end
table.remove(ret_str, #ret_str)
ret_str = table.concat(ret_str)
return ret_str
end
-- Returns an ordinal string (i.e. 30 --> 30th)
GUI.ordinal = function(num)
rem = num % 10
num = GUI.round(num)
if num == 1 then
str = num .. "st"
elseif rem == 2 then
str = num .. "nd"
elseif num == 13 then
str = num .. "th"
elseif rem == 3 then
str = num .. "rd"
else
str = num .. "th"
end
return str
end
---- Color and drawing functions ----
-- Take 8-bit RGB values and return the combined integer
-- (equal to hex colors of the form 0xRRGGBB)
GUI.rgb2num = function(red, green, blue)
green = green * 256
blue = blue * 256 * 256
return red + green + blue
end
-- Convert a hex color to 8-bit values r,g,b
GUI.hex2rgb = function(num)
if string.sub(num, 1, 2) == "0x" then
num = string.sub(num, 3)
end
local red = string.sub(num, 1, 2)
local green = string.sub(num, 3, 4)
local blue = string.sub(num, 5, 6)
red = tonumber(red, 16) or 0
green = tonumber(green, 16) or 0
blue = tonumber(blue, 16) or 0
return red, green, blue
end
-- Thanks to Heda for this one
GUI.num2rgb = function(num)
r = num & 255
g = (num >> 8) & 255
b = (num >> 16) & 255
return r, g, b
end
-- Convert rgb[a] to hsv[a]; useful for gradients
-- Provide the arguments as 0-1
GUI.rgb2hsv = function(r, g, b, a)
local max = math.max(r, g, b)
local min = math.min(r, g, b)
local chroma = max - min
-- Dividing by zero is never a good idea
if chroma == 0 then
return 0, 0, max, (a or 1)
end
local hue
if max == r then
hue = ((g - b) / chroma) % 6
elseif max == g then
hue = ((b - r) / chroma) + 2
elseif max == b then
hue = ((r - g) / chroma) + 4
else
hue = -1
end
if hue ~= -1 then
hue = hue / 6
end
local sat = (max ~= 0) and ((max - min) / max) or 0
return hue, sat, max, (a or 1)
end
-- ...and back the other way
GUI.hsv2rgb = function(h, s, v, a)
local chroma = v * s
local hp = h * 6
local x = chroma * (1 - math.abs(hp % 2 - 1))
local r, g, b
if hp <= 1 then
r, g, b = chroma, x, 0
elseif hp <= 2 then
r, g, b = x, chroma, 0
elseif hp <= 3 then
r, g, b = 0, chroma, x
elseif hp <= 4 then
r, g, b = 0, x, chroma
elseif hp <= 5 then
r, g, b = x, 0, chroma
elseif hp <= 6 then
r, g, b = chroma, 0, x
else
r, g, b = 0, 0, 0
end
local min = v - chroma
return r + min, g + min, b + min, (a or 1)
end
--[[
Returns the color for a given position on an HSV gradient between two colors
col_a Tables of {R, G, B[, A]}, values from 0-1
col_b
pos Position along the gradient, 0 = col_a, 1 = col_b
]] --
GUI.gradient = function(col_a, col_b, pos)
local col_a = {GUI.rgb2hsv(table.unpack(col_a))}
local col_b = {GUI.rgb2hsv(table.unpack(col_b))}
local h = math.abs(col_a[1] + (pos * (col_b[1] - col_a[1])))
local s = math.abs(col_a[2] + (pos * (col_b[2] - col_a[2])))
local v = math.abs(col_a[3] + (pos * (col_b[3] - col_a[3])))
local a = (#col_a == 4) and (math.abs(col_a[4] + (pos * (col_b[4] - col_a[4])))) or 1
return GUI.hsv2rgb(h, s, v, a)
end
-- Round a number to the nearest integer (or optional decimal places)
GUI.round = function(num, places)
-- return num % 1 >= 0.5 and math.ceil(num) or math.floor(num)
if not places then
return math.floor(num + 0.5)
else
places = 10 ^ places
return math.floor(num * places + 0.5) / places
end
end
-- Make sure val is between min and max
GUI.clamp = function(num, min, max)
if min > max then
min, max = max, min
end
return math.min(math.max(num, min), max)
end
GUI.pi = 3.1415927
-- Improved roundrect() function with fill, adapted from mwe's EEL example.
GUI.roundrect = function(x, y, w, h, r, antialias, fill)
local aa = antialias or 1
fill = fill or 0
if fill == 0 or false then
gfx.roundrect(x, y, w, h, r, aa)
elseif h >= 2 * r then
-- Corners
gfx.circle(x + r, y + r, r, 1, aa) -- top-left
gfx.circle(x + w - r, y + r, r, 1, aa) -- top-right
gfx.circle(x + w - r, y + h - r, r, 1, aa) -- bottom-right
gfx.circle(x + r, y + h - r, r, 1, aa) -- bottom-left
-- Ends
gfx.rect(x, y + r, r, h - r * 2)
gfx.rect(x + w - r, y + r, r + 1, h - r * 2)
-- Body + sides
gfx.rect(x + r, y, w - r * 2, h + 1)
else
r = (h / 2 - 1)
-- Ends
gfx.circle(x + r, y + r, r, 1, aa)
gfx.circle(x + w - r, y + r, r, 1, aa)
-- Body
gfx.rect(x + r, y, w - (r * 2), h)
end
end
-- Improved triangle() function with optional non-fill
GUI.triangle = function(fill, ...)
-- Pass any calls for a filled triangle on to the original function
if fill == 1 then
gfx.triangle(...)
else
-- Store all of the provided coordinates into an array
local coords = {...}
-- Duplicate the first pair at the end, so the last line will
-- be drawn back to the starting point.
coords[#coords + 1] = coords[1]
coords[#coords + 1] = coords[2]
-- Draw a line from each pair of coords to the next pair.
for i = 1, #coords - 2, 2 do
gfx.line(coords[i], coords[i + 1], coords[i + 2], coords[i + 3])
end
end
end
--[[
Takes an angle in radians (omit Pi) and a radius, returns x, y
Will return coordinates relative to an origin of 0, or absolute
coordinates if an origin point is specified
]] --
GUI.polar2cart = function(angle, radius, ox, oy)
local angle = angle * GUI.pi
local x = radius * math.cos(angle)
local y = radius * math.sin(angle)
if ox and oy then
x, y = x + ox, y + oy
end
return x, y
end
--[[
Takes cartesian coords, with optional origin coords, and returns
an angle (in radians) and radius. The angle is given without reference
to Pi; that is, pi/4 rads would return as simply 0.25
]] --
GUI.cart2polar = function(x, y, ox, oy)
local dx, dy = x - (ox or 0), y - (oy or 0)
local angle = math.atan(dy, dx) / GUI.pi
local r = math.sqrt(dx * dx + dy * dy)
return angle, r
end
-- Are these coordinates inside the given element?
GUI.IsInside = function(elm, x, y)
if not elm then
return false
end
x, y = x or GUI.mouse.x, y or GUI.mouse.y
local inside = x >= elm.x and x < (elm.x + elm.w) and y >= elm.y and y < (elm.y + elm.h)
return inside
end
--[[
Returns x,y coordinates for a window with the specified anchor position
If no anchor is specified, it will default to the top-left corner of the screen.
x,y offset coordinates from the anchor position
w,h window dimensions
anchor "screen" or "mouse"
corner "TL"
"T"
"TR"
"R"
"BR"
"B"
"BL"
"L"
"C"
]] --
GUI.get_window_pos = function(x, y, w, h, anchor, corner)
local ax, ay, aw, ah = 0, 0, 0, 0
local __, __, scr_w, scr_h = reaper.my_getViewport(x, y, x + w, y + h, x, y, x + w, y + h, 1)
if anchor == "screen" then
aw, ah = scr_w, scr_h
elseif anchor == "mouse" then
ax, ay = reaper.GetMousePosition()
end
local cx, cy = 0, 0
if corner then
local corners = {
TL = {0, 0},
T = {(aw - w) / 2, 0},
TR = {(aw - w) - 16, 0},
R = {(aw - w) - 16, (ah - h) / 2},
BR = {(aw - w) - 16, (ah - h) - 40},
B = {(aw - w) / 2, (ah - h) - 40},
BL = {0, (ah - h) - 40},
L = {0, (ah - h) / 2},
C = {(aw - w) / 2, (ah - h) / 2},
}
cx, cy = table.unpack(corners[corner])
end
x = x + ax + cx
y = y + ay + cy
--[[
Disabled until I can figure out the multi-monitor issue
-- Make sure the window is entirely on-screen
local l, t, r, b = x, y, x + w, y + h
if l < 0 then x = 0 end
if r > scr_w then x = (scr_w - w - 16) end
if t < 0 then y = 0 end
if b > scr_h then y = (scr_h - h - 40) end
]] --
return x, y
end
---- Z-layer blitting ----
-- On each draw loop, only layers that are set to true in this table
-- will be redrawn; if false, it will just copy them from the buffer
-- Set [0] = true to redraw everything.
GUI.redraw_z = {}
---- Our main functions ----
-- Maintain a list of all GUI elements, sorted by their z order
-- Also removes any elements with z = -1, for automatically
-- cleaning things up.
GUI.update_elms_list = function(init)
local z_table = {}
if init then
GUI.elms_list = {}
GUI.z_max = 0
end
for key, __ in pairs(GUI.elms) do
local z = GUI.elms[key].z or 5
-- Delete elements if the script asked to
if z == -1 then
GUI.elms[key] = nil
else
if z_table[z] then
table.insert(z_table[z], key)
else
z_table[z] = {key}