-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeed_trainer.lua
2491 lines (1633 loc) · 53.2 KB
/
speed_trainer.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
--[=[
Lokasenna_GUI Script example
by Lokasenna
Code by Bert Hu http://github.com/berthu
]=]--
-- All of the GUI code
local function GUI_table ()
local GUI = {}
GUI.version = "beta 2"
---- 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
-- ^ (Only one at a time) ^
{"Calibri", 36, 1, 0, 0}, -- 1. Title
{"Calibri", 28, 0, 0, 0}, -- 2. Header
{"Calibri", 22, 0, 0, 0}, -- 3. Label
{"Calibri", 18, 0, 0, 0} -- 4. Value
}
GUI.colors = {
wnd_bg = {64, 64, 64, 1}, -- Window BG
elm_bg = {48, 48, 48, 1}, -- Element BG
elm_frame = {96, 96, 96, 1}, -- Element Frame
elm_fill = {64, 192, 64, 1}, -- Element Fill
elm_outline = {32, 32, 32, 1},
txt = {192, 192, 192, 1}, -- Text
shadow = {0, 0, 0, 0.4} -- Shadow
}
GUI.font = function (fnt)
local font, size, b, i, u = table.unpack(GUI.fonts[fnt])
-- ASCII values:
-- Bold 98
-- Italics 105
-- Underline 117
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
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 with a shadow
GUI.shadow = function (str)
local x, y = gfx.x, gfx.y
GUI.color("shadow")
for i = 1, GUI.shadow_dist do
gfx.x, gfx.y = x + i, y + i
gfx.drawstr(str)
end
GUI.color("txt")
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
-- 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
GUI.round = function (num)
return num % 1 >= 0.5 and math.ceil(num) or math.floor(num)
end
-- 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 * math.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
-- 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 ----
GUI.Init = function ()
-- Create the window
gfx.clear = GUI.rgb2num(table.unpack(GUI.colors.wnd_bg))
-- local x, y = reaper.GetMousePosition()
-- if GUI.x == "c" then GUI.x = x - (GUI.w / 2) end
-- if GUI.y == "c" then GUI.y = y - (GUI.h / 2) end
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)
-- Initialize a few values
GUI.last_time = 0
GUI.mouse = {
x = 0,
y = 0,
cap = 0,
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[1] / 255, col[2] / 255, col[3] / 255
end
end
GUI.Main = function ()
-- Update mouse and keyboard state
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()
-- (Escape key) (Window closed) (User function says to close)
if GUI.char == 27 or GUI.char == 32 or GUI.char == -1 or GUI.quit == true then
reaper.CSurf_OnStop()
userfunc("")
-- See if an exit function was specified
if GUI.exit then
GUI.exit()
end
return 0
else
reaper.defer(GUI.Main)
end
-- Update each element's state
for key, elm in pairs(GUI.elms) do
GUI.Update(elm)
end
-- 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()
local new_time = os.clock()
if new_time - GUI.last_time >= GUI.freq then
GUI.func()
GUI.last_time = new_time
end
end
-- Redraw each element
for key, elm in pairs(GUI.elms) do
elm:draw()
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 char = GUI.char
-- Left button down
if GUI.mouse.cap&1==1 then
-- If it wasn't down already...
if not GUI.mouse.down then
-- Was a different element clicked?
if not GUI.IsInside(elm, x, y) 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()
end
-- Double clicked?
if GUI.mouse.uptime and os.clock() - GUI.mouse.uptime < 0.15 then
elm:ondoubleclick()
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 ~= nil then elm:ondrag() 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.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
-- If the mousewheel's state has changed
if GUI.mouse.wheel ~= GUI.mouse.lwheel and GUI.IsInside(elm, GUI.mouse.x, GUI.mouse.y) then
local inc = (GUI.mouse.wheel - GUI.mouse.lwheel) / 120
elm:onwheel(inc)
GUI.mouse.lwheel = GUI.mouse.wheel
end
-- If the element is in focus and the user typed something
if elm.focus and char ~= 0 then elm:ontype(char) end
end
-- For use with external user functions. Returns the given element's current value or, if specified, sets a new one.
GUI.Val = function (elm, newval)
if newval then
GUI.elms[elm]:val(newval)
else
return GUI.elms[elm]:val()
end
end
-- Display the version number
GUI.Draw_Version = function ()
local str = "Running Lokasenna_GUI "..GUI.version
gfx.setfont(1, "Arial", 12, 105)
GUI.color("txt")
local str_w, str_h = gfx.measurestr(str)
gfx.x = GUI.w - str_w
gfx.y = GUI.h - str_h
gfx.drawstr(str)
end
---- Elements ----
--[[ Label class.
---- User parameters ----
x, y Coordinates of top-left corner
caption Label text
shadow (1) Draw a shadow, (2) No shadow
]]--
-- Label - New
GUI.Label = {}
function GUI.Label:new(x, y, caption, shadow)
local label = {}
label.type = "Label"
label.x, label.y = x, y
-- Placeholders for these values, since we don't need them but some functions will throw a fit if they aren't there
label.w, label.h = 0, 0
label.retval = caption
label.shadow = shadow or 0
setmetatable(label, self)
self.__index = self
return label
end
-- Label - Draw
function GUI.Label:draw()
local x, y = self.x, self.y
GUI.font(1)
gfx.x, gfx.y = x, y
local output = self.output or self.retval
if self.shadow == 1 then
GUI.shadow(output)
else
gfx.drawstr(output)
end
end
-- Label - Get/set value
function GUI.Label:val(newval)
if newval then
self.retval = newval
else
return self.retval
end
end
-- Label - Unused methods.
function GUI.Label:onmousedown() end
function GUI.Label:onmouseup() end
function GUI.Label:ondoubleclick() end
function GUI.Label:ondrag() end
function GUI.Label:onwheel() end
function GUI.Label:ontype() end
--[[ Slider class.
---- User parameters ----
x, y, w Coordinates of top-left corner, width. Height is fixed.
caption Label / question
min, max Minimum and maximum slider values
steps How many steps between min and max
default Where the slider should start
(also sets the start of the fill bar)
ticks Display ticks or not. **Currently does nothing**
]]--
-- Slider - New
local Slider = {}
function Slider:new(x, y, w, caption, min, max, steps, default, ticks)
local Slider = {}
Slider.type = "Slider"
Slider.x, Slider.y, Slider.w, Slider.h = x, y, w, 8
Slider.caption = caption
Slider.min, Slider.max = min, max
Slider.steps = steps
Slider.default, Slider.curstep = default, default
Slider.ticks = ticks
Slider.curval = Slider.curstep / steps
Slider.retval = GUI.round((((max - min) / steps) * Slider.curstep) + min)
setmetatable(Slider, self)
self.__index = self
return Slider
end
-- Slider - Draw
function Slider:draw()
local x, y, w, h = self.x, self.y, self.w, self.h
local steps = self.steps
local curstep = self.curstep
local min, max = self.min, self.max
-- Size of the handle
local radius = 8
-- Draw track
GUI.color("elm_bg")
GUI.roundrect(x, y, w, h, 4, 1, 1)
GUI.color("elm_outline")
GUI.roundrect(x, y, w, h, 4, 1, 0)
-- Limit everything to be drawn within the square part of the track
x, w = x + 4, w - 8
-- Handle
local inc = w / steps
local ox, oy = x + inc * curstep, y + (h / 2)
-- Fill in between the two handles
local fill_x = GUI.round( x + ((self.default / steps) * w) )
local fill_w = GUI.round( ox - fill_x )
if fill_w < 0 then
fill_w = math.abs(fill_w)
fill_x = fill_x - fill_w
end
GUI.color("elm_fill")
gfx.rect(fill_x, y + 1, fill_w, h - 2, 1)
-- Handle shadows
GUI.color("shadow")
for i = 1, GUI.shadow_dist do
gfx.circle(ox + i, oy + i, radius - 1, 1, 1)
end
-- Handle bodies
GUI.color("elm_frame")
gfx.circle(ox, oy, radius - 1, 1, 1)
GUI.color("elm_outline")
gfx.circle(ox, oy, radius, 0, 1)
-- Draw caption
GUI.font(3)
local str_w, str_h = gfx.measurestr(self.caption)
gfx.x = x + (w - str_w) / 2
gfx.y = y - h - str_h
GUI.shadow(self.caption)
-- Draw ticks + highlighted labels if specified
-- Draw slider value otherwise
GUI.font(4)
local output = self.output or self.retval
local str_w, str_h = gfx.measurestr(output)
gfx.x = x + (w - str_w) / 2
gfx.y = y + h + h
gfx.drawstr(output)
end
-- Slider - Get/set value
function Slider:val(newval)
if newval then
self.retval = newval
else
return self.retval
end
end
-- Slider - Mouse down
function Slider:onmousedown()
-- Snap to the nearest value
self.curval = (GUI.mouse.x - self.x) / self.w
if self.curval > 1 then self.curval = 1 end
if self.curval < 0 then self.curval = 0 end
self.curstep = GUI.round(self.curval * self.steps)
self.retval = GUI.round(((self.max - self.min) / self.steps) * self.curstep + self.min)
end
-- Slider - Dragging
function Slider:ondrag()
local x = GUI.mouse.x
local lx = GUI.mouse.lx
-- Ctrl?
local ctrl = GUI.mouse.cap&4==4
-- A multiplier for how fast the slider should move. Higher values = slower
-- Ctrl Normal
local adj = ctrl and 1200 or 150
-- Make sliders behave consistently at different sizes
local adj_scale = self.w / 150
adj = adj * adj_scale
self.curval = self.curval + ((x - lx) / adj)
if self.curval > 1 then self.curval = 1 end
if self.curval < 0 then self.curval = 0 end
self.curstep = GUI.round(self.curval * self.steps)
self.retval = GUI.round(((self.max - self.min) / self.steps) * self.curstep + self.min)
end
-- Slider - Mousewheel
function Slider:onwheel(inc)
local ctrl = GUI.mouse.cap&4==4
-- How many steps per wheel-step.
local fine = 1
local coarse = math.max( GUI.round(self.steps / 30), 1)
local adj = ctrl and fine or coarse
self.curval = self.curval + inc * adj / self.steps
if self.curval < 0 then self.curval = 0 end
if self.curval > 1 then self.curval = 1 end
self.curstep = GUI.round(self.curval * self.steps)
self.retval = GUI.round(((self.max - self.min) / self.steps) * self.curstep + self.min)
end
-- Slider - Unused methods
function Slider:onmouseup() end
function Slider:ondoubleclick() end
function Slider:ontype() end
GUI.Slider = Slider
--[[ Range class
---- User parameters ----
x, y, w Coordinates of top-left corner, width. Height is fixed.
caption Label / question
min, max Minimum and maximum slider values
steps How many steps between min and max
default_a Where the sliders should start
default_b
ticks Display ticks or not. **Currently does nothing**
]]--
local Range = {}
function Range:new(x, y, w, caption, min, max, steps, default_a, default_b)
local Range = {}
Range.type = "Range"
Range.x, Range.y, Range.w, Range.h = x, y, w, 8
Range.caption = caption
Range.min, Range.max = min, max
Range.steps = steps
Range.default_a, Range.default_b = default_a, default_b
Range.curstep_a, Range.curstep_b = default_a, default_b
Range.curval_a = Range.curstep_a / steps
Range.curval_b = Range.curstep_b / steps
Range.retval_a = GUI.round(((max - min) / steps) * Range.curstep_a + min)
Range.retval_b = GUI.round(((max - min) / steps) * Range.curstep_b + min)
setmetatable(Range, self)
self.__index = self
return Range
end
-- Range - Draw
function Range:draw()
local x, y, w, h = self.x, self.y, self.w, self.h
local steps = self.steps
local curstep_a = self.curstep_a
local curstep_b = self.curstep_b
local min, max = self.min, self.max
-- Size of the handle
local radius = 8
-- Draw track
GUI.color("elm_bg")
GUI.roundrect(x, y, w, h, 4, 1, 1)
GUI.color("elm_outline")
GUI.roundrect(x, y, w, h, 4, 1, 0)
-- Limit everything to be drawn within the square part of the track
x, w = x + 4, w - 8
-- Handles
local inc = w / steps
local ox_a, oy_a = x + inc * curstep_a, y + (h / 2)
local ox_b, oy_b = x + inc * curstep_b, oy_a
local ox_fill = ox_a < ox_b and ox_a or ox_b
local fill_w = math.abs(ox_a - ox_b)
-- Fill in between the two handles
GUI.color("elm_fill")
gfx.rect(ox_fill, y + 1, fill_w, h - 2, 1)
-- Handle shadows
GUI.color("shadow")
for i = 1, GUI.shadow_dist do
gfx.circle(ox_a + i, oy_a + i, radius - 1, 1, 1)
gfx.circle(ox_b + i, oy_b + i, radius - 1, 1, 1)
end
-- Handle bodies
GUI.color("elm_frame")
gfx.circle(ox_a, oy_a, radius - 1, 1, 1)
gfx.circle(ox_b, oy_b, radius - 1, 1, 1)
GUI.color("elm_outline")
gfx.circle(ox_a, oy_a, radius, 0, 1)
gfx.circle(ox_b, oy_b, radius, 0, 1)
-- Draw caption
GUI.font(3)
local str_w, str_h = gfx.measurestr(self.caption)
gfx.x = x + (w - str_w) / 2
gfx.y = y - h - str_h
GUI.shadow(self.caption)
GUI.font(4)
-- Handle A's value
local output_a = self.output_a or self.retval_a
local str_w, str_h = gfx.measurestr(output)
gfx.x = x
gfx.y = y + h + h
gfx.drawstr(output_a)
-- Handle B's value
local output_b = self.output_b or self.retval_b
local str_w, str_h = gfx.measurestr(output_b)
gfx.x = x + w - str_w
--gfx.y = y + h + h
gfx.drawstr(output_b)
end