/
Lokasenna_MIDI Randomization Tool.lua
3117 lines (2027 loc) · 71.6 KB
/
Lokasenna_MIDI Randomization Tool.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: MIDI Randomization Tool
Version: 1.0.0
Author: Lokasenna
Donation: https://paypal.me/Lokasenna
Changelog:
Initial release
Links:
Lokasenna's Website http://forum.cockos.com/member.php?u=10417
About:
Provides several functions for randomizing/humanizing a MIDI item.
Requires an open MIDI editor, and will only apply to selected notes.
--]]
-- Licensed under the GNU GPL v3
local dm, _ = debug_mode
local function Msg(str)
reaper.ShowConsoleMsg(tostring(str).."\n")
end
local info = debug.getinfo(1,'S');
script_path = info.source:match[[^@?(.*[\/])[^\/]-$]]
---- Libraries added with Lokasenna's Script Compiler ----
---- Beginning of file: Lokasenna_GUI revised\Lokasenna_GUI preview\Core.lua ----
----------------------------------------------------------------
------------------Copy everything from here---------------------
----------------------------------------------------------------
local function GUI_table ()
local GUI = {}
GUI.version = "beta 10"
-- Print stuff to the Reaper console. For debugging purposes.
GUI.Msg = function (str)
reaper.ShowConsoleMsg(tostring(str).."\n")
end
--[[ Use when working with file paths if you need to add your own /s
(Borrowed from X-Raym)
]]--
GUI.file_sep = string.match(reaper.GetOS(), "Win") and "\\" or "/"
--Also might need to know this
GUI.SWS_exists = reaper.APIExists("BR_Win32_GetPrivateProfileString")
---- 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}, -- Tabs BG
elm_bg = {48, 48, 48, 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}, -- Element Outline
txt = {192, 192, 192, 255}, -- Text
shadow = {0, 0, 0, 48}, -- Element Shadows
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},
}
--[[ Apply a font preset
fnt Font preset number
or
A preset table -> GUI.font({"Arial", 10, "i"})
]]--
GUI.font = function (fnt)
local font, size, str = table.unpack( type(fnt) == "table" and fnt or GUI.fonts[fnt])
-- Different OSes use different font sizes, for some reason
-- This should give a roughly equal size on Mac
if string.find(reaper.GetOS(), "OSX") then
size = math.floor(size * 0.8)
end
-- Cheers to Justin and Schwa for this
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
--[[ Apply a color preset
col Color preset string -> "elm_fill"
or
Color table -> {1, 0.5, 0.5[, 1]}
R G B [ A]
]]--
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
--[[ Draw a background rectangle for the given string
A solid background is necessary for blitting z layers
on their own; antialiased text with a transparent background
looks like complete shit. This function draws a rectangle 2px
larger than your text on all sides.
Call with your position, font, and color already set:
gfx.x, gfx.y = self.x, self.y
GUI.font(self.font)
GUI.color(self.col)
GUI.text_bg(self.text)
gfx.drawstr(self.text)
Also accepts an optional background color:
GUI.text_bg(self.text, "elm_bg")
]]--
GUI.text_bg = function (str, col)
local x, y = gfx.x, gfx.y
local r, g, b, a = gfx.r, gfx.g, gfx.b, gfx.a
col = col or "wnd_bg"
GUI.color(col)
local w, h = gfx.measurestr(str)
w, h = w + 4, h + 4
gfx.rect(gfx.x - 2, gfx.y - 2, w, h, true)
gfx.x, gfx.y = x, y
gfx.set(r, g, b, a)
end
---- General functions ----
--[[
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 adapted from: http://lua-users.org/wiki/SortedIteration
GUI.full_sort = function (op1, op2)
-- Sort strings that begin with a number as if they were numbers,
-- i.e. so that 12 > "6 apples"
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, or
use f to provide your own sorting function as per pairs() and ipairs()
]]--
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 ----
--[[ Prepares a table of character widths
Iterates through all of the GUI.fonts[] 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
probably 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
--[[ Returns 'str' wrapped to fit a given pixel 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.init_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
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 ----
-- Convert a hex color RRGGBB 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
-- Convert rgb[a] to hsv[a]; useful for gradients
-- Arguments/returns are given 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 color presets
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
returns r, g, b, a
]]--
GUI.gradient = function (col_a, col_b, pos)
local col_a = {GUI.rgb2hsv( table.unpack( type(col_a) == "table" and col_a or GUI.colors(col_a) )) }
local col_b = {GUI.rgb2hsv( table.unpack( type(col_b) == "table" and col_b or GUI.colors(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)
if not places then
return num > 0 and math.floor(num + 0.5) or math.ceil(num - 0.5)
else
places = 10^places
return num > 0 and math.floor(num * places + 0.5) or math.ceil(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
-- Odds are you don't need much precision
-- If you do, just specify GUI.pi = math.pi() in your code
GUI.pi = 3.14159
-- 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)
else
if 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
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.
table.insert(coords, coords[1])
table.insert(coords, 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,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
--[[
We'll use this to let elements have their own graphics buffers
to do whatever they want in.
num = How many buffers you want, or 1 if not specified.
Returns a table of buffers, or just a buffer number if num = 1
i.e.
-- Assign this element's buffer
function GUI.my_element:new(.......)
...new stuff...
my_element.buffers = GUI.GetBuffer(4)
-- or
my_element.buffer = GUI.GetBuffer()
end
-- Draw to the buffer
function GUI.my_element:init()
gfx.dest = self.buffers[1]
-- or
gfx.dest = self.buffer
...draw stuff...
end
-- Copy from the buffer
function GUI.my_element:draw()
gfx.blit(self.buffers[1], 1, 0)
-- or
gfx.blit(self.buffer, 1, 0)
end
]]--
-- Any used buffers will be marked as True here
GUI.buffers = {}
-- When deleting elements, their buffer numbers
-- will be added here for easy access.
GUI.freed_buffers = {}
GUI.GetBuffer = function (num)
local ret = {}
local prev
for i = 1, (num or 1) do
if #GUI.freed_buffers > 0 then
ret[i] = table.remove(GUI.freed_buffers)
else
for j = (not prev and 1023 or prev - 1), 0, -1 do
if not GUI.buffers[j] then
ret[i] = j
GUI.buffers[j] = true
break
end
end
end
end
return (#ret == 1) and ret[1] or ret
end
-- Elements should pass their buffer (or buffer table) to this
-- when being deleted
GUI.FreeBuffer = function (num)
if type(num) == "number" then
table.insert(GUI.freed_buffers, num)
else
for k, v in pairs(num) do
table.insert(GUI.freed_buffers, v)
end
end
end
-- Are these coordinates inside the given element?
-- If no coords are given, will use the mouse cursor
GUI.IsInside = function (elm, x, y)
if not elm then return false end
local x, y = x or GUI.mouse.x, y or GUI.mouse.y
return ( x >= (elm.x or 0) and x < ((elm.x or 0) + (elm.w or 0)) and
y >= (elm.y or 0) and y < ((elm.y or 0) + (elm.h or 0)) )
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.