forked from ReaTeam/ReaScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLokasenna_Script Compiler.lua
3777 lines (2490 loc) · 84.1 KB
/
Lokasenna_Script Compiler.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: Script compiler
Version: 2.0.1
Author: Lokasenna
Donation: https://paypal.me/Lokasenna
Changelog:
A few small bug fixes
Checking if the output folder was created is disabled, due to
the API function giving buggy return values
Refactored/reorganized the code for readability
Links:
Forum thread http://forum.cockos.com/showthread.php?t=185264
Lokasenna's Website http://forum.cockos.com/member.php?u=10417
About:
Simplifies the process of working with and distributing additional
library files for Lua scripters, by scanning your script for a few
specific commands and using them to copy the external libraries'
contents directly into the base script.
See the forum thread for a more detailed explanation.
Extensions: SWS/S&M 2.8.3
--]]
-- Licensed under the GNU GPL v3
local info = debug.getinfo(1,'S');
script_path = info.source:match[[^@?(.*[\/])[^\/]-$]]
local function req(file)
local ret = loadfile(script_path .. file) or loadfile(script_path .. "Libraries\\" .. file)
if not ret then
reaper.ShowMessageBox("Couldn't find "..file.."\n\nLokasenna probably forgot to compile the script again.", "Missing library", 0)
return 0
else
return ret
end
end
req("Lokasenna_GUI library beta 7.lua")()
------------------------------------
-------- GUI Setup -----------------
------------------------------------
GUI.name = "Lokasenna's Script Compiler"
GUI.x, GUI.y, GUI.w, GUI.h = 0, 0, 600, 600
GUI.anchor, GUI.corner = "mouse", "C"
GUI.fonts[1] = {"Calibri", 32}
GUI.fonts[2] = {"Calibri", 20}
GUI.fonts[3] = {"Calibri", 16}
GUI.fonts[4] = {"Calibri", 16}
GUI.fonts[6] = {"Calibri", 18, "b"}
------------------------------------
-------- Variables + Data ----------
------------------------------------
local help_str =
[=[This script aims to simplify the process of distributing Lua ReaScripts that make use of additional
libraries, i.e. by 'require', 'loadfile', or '@import'. It searches through your base script for a few
specifically-formatted comments to determine what libraries you need, then copies their contents
directly into the body of the script.
Example:
---- Libraries added with Lokasenna's Script Compiler ----
---- Beginning of file: Lokasenna_GUI library beta 7.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 7"
-- Might want to know this
GUI.OS = reaper.GetOS()
---- 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", 36, "b"}, -- 1. Title
{"Calibri", 28}, -- 2. Header
{"Calibri", 22}, -- 3. Label
{"Calibri", 18} -- 4. Value
}
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_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, 102}, -- Shadow
-- 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(GUI.fonts[fnt])
-- ASCII values:
-- Bold 98
-- Italics 105
-- Underline 117
--[[ old way
local flags = 0
if b == 1 then flags = flags + 98 end
if i == 1 then flags = flags + 105 end
if u == 1 then flags = flags + 117 end
]]--
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
GUI.color = function (col)
gfx.set(table.unpack(GUI.colors[col]))
end
-- Global shadow size, in pixels
GUI.shadow_dist = 2
-- 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
GUI.table_copy = function (source)
if type(source) ~= "table" then return source end
local meta = getmetatable(source)
local new = {}
for k, v in pairs(source) do
if type(v) == "table" then
new[k] = GUI.table_copy(v)
else
new[k] = v
end
end
setmetatable(new, meta)
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
-- Allows "for x, y in pairs(z) do" in alphabetical/numerical order
-- Copied from Programming In Lua, 19.3
GUI.kpairs = function (t, f)
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
-- 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 number to hexadecimal
GUI.num2hex = function (num)
local hexstr = '0123456789abcdef'
local s = ''
while num > 0 do
local mod = math.fmod(num, 16)
s = string.sub(hexstr, mod+1, mod+1) .. s
num = math.floor(num / 16)
end
if s == '' then s = '0' end
return s
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 blue = string.sub(num, 3, 4)
local green = string.sub(num, 5, 6)
red = tonumber(red, 16)
blue = tonumber(blue, 16)
green = tonumber(green, 16)
return red, green, blue
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)
local inside =
x >= elm.x and x < (elm.x + elm.w) and
y >= elm.y and y < (elm.y + elm.h)
return inside
end
-- Make sure the window position is on-screen
GUI.check_window_pos = function ()
local x, y, w, h = GUI.x, GUI.y, GUI.w, GUI.h
local l, t, r, b = x, y, x + w, y + h
local __, __, screen_w, screen_h = reaper.my_getViewport(l, t, r, b, l, t, r, b, 1)
if l < 0 then GUI.x = 0 end
if r > screen_w then GUI.x = (screen_w - w - 16) end
if t < 0 then GUI.y = 0 end
if b > screen_h then GUI.y = (screen_h - h - 40) end
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
-- 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
---- Our main functions ----
-- Maintain a list of all GUI elements, sorted by their z order
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
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}
end
end
if init then GUI.z_max = math.max(z, GUI.z_max) end
end
for i = 0, GUI.z_max do
if not z_table[i] then z_table[i] = {} end
end
GUI.elms_list = z_table
end
GUI.Init = function ()
-- Create the window
gfx.clear = GUI.rgb2num(table.unpack(GUI.colors.wnd_bg))
if not GUI.x then GUI.x = 0 end
if not GUI.y then GUI.y = 0 end
if not GUI.w then GUI.w = 640 end
if not GUI.h then GUI.h = 480 end
GUI.x, GUI.y = GUI.get_window_pos(GUI.x, GUI.y, GUI.w, GUI.h, GUI.anchor, GUI.corner)
gfx.init(GUI.name, GUI.w, GUI.h, 0, GUI.x, GUI.y)
GUI.cur_w, GUI.cur_h = gfx.w, gfx.h
-- Measure the window's title bar, in case we need it
local __, __, wnd_y, __, __ = gfx.dock(-1, 0, 0, 0, 0)
local __, gui_y = gfx.clienttoscreen(0, 0)
GUI.title_height = gui_y - wnd_y
-- Initialize a few values
GUI.last_time = 0
GUI.mouse = {
x = 0,
y = 0,
cap = 0,
down = false,
r_down = false,
wheel = 0,
lwheel = 0
}
-- Convert color presets from 0..255 to 0..1
for i, col in pairs(GUI.colors) do
col[1], col[2], col[3], col[4] = col[1] / 255, col[2] / 255, col[3] / 255, col[4] / 255
end
-- Initialize the tables for our z-order functions
GUI.update_elms_list(true)
if not GUI.elms_hide then
GUI.elms_hide = {}
end
if not GUI.elms_freeze then GUI.elms_freeze = {} end
if GUI.Exit then reaper.atexit(GUI.Exit) end
end
GUI.Main = function ()
-- Update mouse and keyboard state, window dimensions
GUI.mouse.x, GUI.mouse.y = gfx.mouse_x, gfx.mouse_y
GUI.mouse.wheel = gfx.mouse_wheel
GUI.mouse.cap = gfx.mouse_cap
GUI.char = gfx.getchar()
if GUI.cur_w ~= gfx.w or GUI.cur_h ~= gfx.h then
GUI.cur_w, GUI.cur_h = gfx.w, gfx.h
GUI.resized = true
else
GUI.resized = false
end
-- (Escape key) (Window closed) (User function says to close)
if GUI.char == 27 or GUI.char == -1 or GUI.quit == true then
return 0
else
reaper.defer(GUI.Main)
end
-- Update each element's state, starting from the top down.
-- This is very important, so that lower elements don't
-- "steal" the mouse.
GUI.update_elms_list()
-- We'll use this to break the update loop early if the user did something
-- Slightly more efficient, and averts any bugs from false positives
GUI.elm_updated = false
for i = 0, GUI.z_max do
if #GUI.elms_list[i] > 0 and not (GUI.elms_hide[i] or GUI.elms_freeze[i]) then
for __, elm in pairs(GUI.elms_list[i]) do
GUI.cur_elm = elm
GUI.Update(GUI.elms[elm])
if GUI.elm_updated then break end
end
end
if GUI.elm_updated then break end
end
GUI.mouse.last_down = GUI.mouse.down
GUI.mouse.last_r_down = GUI.mouse.r_down
-- If the user gave us a function to run, check to see if it needs to be run again, and do so.
--
if GUI.func then
GUI.freq = GUI.freq or 1
local new_time = os.time()
if new_time - GUI.last_time >= GUI.freq then
GUI.func()
GUI.last_time = new_time
end
end
-- Redraw all of the elements, starting from the bottom up.
GUI.update_elms_list()
for i = GUI.z_max, 0, -1 do
if #GUI.elms_list[i] > 0 and not GUI.elms_hide[i] then
for __, elm in pairs(GUI.elms_list[i]) do
if not GUI.elms[elm] then GUI.Msg(elm.." doesn't exist?") end
GUI.elms[elm]:draw()
end
end
end
GUI.Draw_Version()
gfx.update()
end
GUI.Update = function (elm)
local x, y = GUI.mouse.x, GUI.mouse.y
local wheel = GUI.mouse.wheel
local inside = GUI.IsInside(elm, x, y)
-- Left button click
if GUI.mouse.cap&1==1 then
-- If it wasn't down already...
if not GUI.mouse.last_down then
-- Was a different element clicked?
if not inside then
elm.focus = false
else
GUI.mouse.down = true
GUI.mouse.ox, GUI.mouse.oy = x, y
GUI.mouse.lx, GUI.mouse.ly = x, y
elm.focus = true
elm:onmousedown()
GUI.elm_updated = true
-- Double clicked?
if GUI.mouse.uptime and os.clock() - GUI.mouse.uptime < 0.20 then
elm:ondoubleclick()
GUI.elm_updated = true
end
end
-- Dragging? Did the mouse start out in this element?
elseif (x ~= GUI.mouse.lx or y ~= GUI.mouse.ly) and GUI.IsInside(elm, GUI.mouse.ox, GUI.mouse.oy) then
if elm.focus ~= false then
elm:ondrag()
GUI.elm_updated = true
end
GUI.mouse.lx, GUI.mouse.ly = x, y
end
-- If it was originally clicked in this element and has now been released
elseif GUI.mouse.down and GUI.IsInside(elm, GUI.mouse.ox, GUI.mouse.oy) then
elm:onmouseup()
GUI.elm_updated = true
GUI.mouse.down = false
GUI.mouse.ox, GUI.mouse.oy = -1, -1
GUI.mouse.lx, GUI.mouse.ly = -1, -1
GUI.mouse.uptime = os.clock()
end
-- Right button click
if GUI.mouse.cap&2==2 then
-- If it wasn't down already...
if not GUI.mouse.last_r_down then
-- Was a different element clicked?
if not inside then
--elm.focus = false
else
GUI.mouse.r_down = true
GUI.mouse.r_ox, GUI.mouse.r_oy = x, y
GUI.mouse.r_lx, GUI.mouse.r_ly = x, y
--elm.focus = true
elm:onmouser_down()
GUI.elm_updated = true
end
-- Double clicked?
if GUI.mouse.r_uptime and os.clock() - GUI.mouse.r_uptime < 0.20 then
elm:onr_doubleclick()
GUI.elm_updated = true
end
-- Dragging? Did the mouse start out in this element?
elseif (x ~= GUI.mouse.r_lx or y ~= GUI.mouse.r_ly) and GUI.IsInside(elm, GUI.mouse.r_ox, GUI.mouse.r_oy) then
if elm.focus ~= false then
elm:onr_drag()
GUI.elm_updated = true
end
GUI.mouse.r_lx, GUI.mouse.r_ly = x, y
end
-- If it was originally clicked in this element and has now been released
elseif GUI.mouse.r_down and GUI.IsInside(elm, GUI.mouse.r_ox, GUI.mouse.r_oy) then
elm:onmouser_up()
GUI.elm_updated = true
GUI.mouse.r_down = false
GUI.mouse.r_ox, GUI.mouse.r_oy = -1, -1
GUI.mouse.r_lx, GUI.mouse.r_ly = -1, -1
GUI.mouse.r_uptime = os.clock()
end
-- If the mouse is hovering over the element
if not GUI.mouse.down and not GUI.mouse.r_down and inside then
elm:onmouseover()
GUI.elm_updated = true
elm.mouseover = true
else
elm.mouseover = false
--elm.hovering = false
end