-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowderface.lua
More file actions
1350 lines (1329 loc) · 46 KB
/
Powderface.lua
File metadata and controls
1350 lines (1329 loc) · 46 KB
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
--------------------------------
-- Powderface --
-- TPT User Interface Library --
-- v1.0 --
-- made by Geniusz1 --
--------------------------------
require('socket')
local ui = {}
ui.contains = function(x, y, a1, b1, a2, b2)
return not (x < a1 or x > a2 or y < b1 or y > b2)
end
gfx.drawPixel = function(x, y, r, g, b, a)
gfx.drawLine(x, y, x, y, r, g, b, a)
end
ui.container = function()
local c = {
children = {},
grabbers = {}
}
function c:draw()
for _, child in ipairs(self.children) do
child:draw()
end
for _, grabber in ipairs(self.grabbers) do
grabber:draw()
end
end
function c:handle_event(evt, ...)
for _, child in ipairs(self.children) do
if child[evt] then
child[evt](child, ...)
end
end
for _, grabber in ipairs(self.grabbers) do
if grabber[evt] then
grabber[evt](grabber, self, ...)
end
end
end
function c:append(...)
for _, child in ipairs({...}) do
table.insert(self.children, child)
end
end
function c:append_at(child, pos)
table.insert(self.children, pos, child)
end
function c:append_grabber(...)
for _, grabber in ipairs({...}) do
table.insert(self.grabbers, grabber)
end
end
function c:append_grabber_at(grabber, pos)
table.insert(self.grabbers, pos, grabber)
end
return c
end
ui.grabber = function(x1, y1, x2, y2, draw_scope, do_not_allow_grabber_to_get_lost_outside_the_screen)
local g = {
scope = {
x1 = x1,
y1 = y1,
x2 = x2,
y2 = y2
},
hover = false,
held = false,
draw_scope = draw_scope or false,
do_not_allow_grabber_to_get_lost_outside_the_screen = do_not_allow_grabber_to_get_lost_outside_the_screen or false
}
function g:draw()
if draw_scope then
gfx.drawRect(self.scope.x1, self.scope.y1, self.scope.x2 - self.scope.x1 + 1, self.scope.y2 - self.scope.y1 + 1)
end
end
function g:set_scope(x1, y1, x2, y2)
self.scope = {
x1 = x1,
y1 = y1,
x2 = x2,
y2 = y2
}
end
function g:set_do_not_allow_grabber_to_get_lost_outside_the_screen(do_not_allow_grabber_to_get_lost_outside_the_screen)
self.do_not_allow_grabber_to_get_lost_outside_the_screen = do_not_allow_grabber_to_get_lost_outside_the_screen
end
function g:mouseup(parent_container, x, y, button, reason)
self.held = false
end
function g:mousemove(parent_container, x, y, dx, dy)
self.hover = ui.contains(x, y, self.scope.x1, self.scope.y1, self.scope.x2, self.scope.y2)
local indexes_of_values_to_bring_hover_back = {}
if self.do_not_allow_grabber_to_get_lost_outside_the_screen then
-- TODO: fix it
if self.scope.x1 + dx < 1 then
dx = 0
end
if self.scope.x2 + dx > gfx.WIDTH - 1 then
dx = 0
end
if self.scope.y1 + dx < 1 then
dy = 0
end
if self.scope.y2 + dx > gfx.HEIGHT - 1 then
dy = 0
end
end
if self.held then
for _, v in ipairs(parent_container.children) do
if v['set_position'] then
v:set_position((v.x or x) + dx, (v.y or y) + dy)
end
if v.held then
v.held = false
end
if v['update_items_position'] then
v:update_items_position()
end
if v['shift_position'] then
v:shift_position(dx, dy)
end
end
self:set_scope(self.scope.x1 + dx, self.scope.y1 + dy, self.scope.x2 + dx, self.scope.y2 + dy)
end
end
function g:mousedown(parent_container, x, y, button)
self.held = self.hover
end
return g
end
ui.box = function(x, y, w, h, r, g, b, a, draw_background, draw_border)
local box = {
x = x,
y = y,
w = w,
h = h,
x2 = x + w - 1, -- x2, y2 adjustment
y2 = y + h - 1, -- x2, y2 adjustment
visible = true,
draw_background = draw_background or false,
draw_border = draw_border or true,
border = {r = r or 255, g = g or 255, b = b or 255},
background = {r = r or 0, g = g or 0, b = b or 0, a = a or 255},
drawlist = {}
}
function box:draw()
if self.visible then
for _, f in ipairs(self.drawlist) do
f(self)
end
end
end
function box:drawadd(f, pos)
if pos then
table.insert(self.drawlist, pos, f)
else
table.insert(self.drawlist, f)
end
end
box:drawadd(function(self)
if self.draw_background then
gfx.fillRect(self.x, self.y, self.w, self.h, self.background.r, self.background.g, self.background.b, self.background.a)
end
if self.draw_border then
gfx.drawRect(self.x, self.y, self.w, self.h, self.border.r, self.border.g, self.border.b, self.border.a)
end
end)
function box:set_backgroud(r, g, b, a)
self.background.r = r
self.background.g = g
self.background.b = b
self.background.a = a
end
function box:set_border(r, g, b, a)
self.border.r = r
self.border.g = g
self.border.b = b
self.border.a = a
end
function box:set_position(x, y)
self.x = x
self.y = y
self.x2 = x + self.w - 1
self.y2 = y + self.h - 1
end
function box:set_size(w, h)
self.w = w
self.h = h
self.x2 = self.x + w - 1
self.y2 = self.y + h - 1
end
return box
end
ui.text = function(x, y, text, r, g, b, a)
local txt = {
x = x,
y = y,
text = text,
visible = true,
color = {r = r or 255, g = g or 255, b = b or 255, a = a or 255},
drawlist = {},
hover = false,
held = false
}
txt.w, txt.h = gfx.textSize(text)
txt.x2 = x + txt.w
txt.y2 = y + txt.h
function txt:draw()
if self.visible then
for _, f in ipairs(self.drawlist) do
f(self)
end
end
end
function txt:drawadd(f, pos)
if pos then
table.insert(self.drawlist, pos, f)
else
table.insert(self.drawlist, f)
end
end
txt:drawadd(function(self)
gfx.drawText(self.x, self.y, self.text, self.color.r, self.color.g, self.color.b, self.color.a)
end)
function txt:mousemove(x, y, dx, dy)
self.hover = ui.contains(x, y, self.x, self.y, self.x2, self.y2)
end
function txt:mousedown(x, y, button)
self.held = self.hover
end
function txt:set_color(r, g, b, a)
self.color = {
r = r,
g = g,
b = b,
a = a
}
end
function txt:set_text(text)
self.text = text
self.w, self.h = gfx.textSize(text)
end
function txt:set_position(x, y)
self.x = x
self.y = y
self.x2 = x + self.w - 1
self.y2 = y + self.h - 1
end
return txt
end
ui.scroll_text = function(x, y, max_w, text, direction, r, g, b, a)
local txt = {
x = x,
y = y,
text = text,
visible_text = '',
visible = true,
direction = direction or 'left',
color = {r = r or 255, g = g or 255, b = b or 255, a = a or 255},
scroll_pos = 1,
drawlist = {},
hover = false,
held = false
}
txt.w, txt.h = gfx.textSize(text)
txt.adjusted_x = x
txt.max_w = max_w
txt.x2 = txt.x + max_w - 1
txt.y2 = txt.y + txt.h - 1 -- x2, y2 adjustment
max_visible_chars = 1
function txt:draw()
if self.visible then
for _, f in ipairs(self.drawlist) do
f(self)
end
end
end
function txt:drawadd(f, pos)
if pos then
table.insert(self.drawlist, pos, f)
else
table.insert(self.drawlist, f)
end
end
txt:drawadd(function(self)
self.adjusted_x = self.x
if self.direction == 'right' then
self.adjusted_x = self.x2 - tpt.textwidth(self.visible_text)
end
gfx.drawText(self.adjusted_x, self.y, self.visible_text, self.color.r, self.color.g, self.color.b, self.color.a)
end)
function txt:set_color(r, g, b, a)
self.color = {
r = r,
g = g,
b = b,
a = a
}
end
function txt:get_max_visible_chars()
return max_visible_chars
end
function txt:update_text()
local temp_max_visible_chars = 1
for i = 1, #self.text do
if tpt.textwidth(self.text:sub(self.scroll_pos, self.scroll_pos + i)) < self.max_w then
temp_max_visible_chars = temp_max_visible_chars + 1
else
break
end
end
max_visible_chars = temp_max_visible_chars
self.visible_text = self.text:sub(self.scroll_pos, self.scroll_pos + max_visible_chars - 1)
end
function txt:set_scroll_pos(pos)
if pos > #self.text - #self.visible_text + 1 then
self.scroll_pos = #self.text - #self.visible_text + 1
elseif pos < 1 then
self.scroll_pos = 1
else
self.scroll_pos = pos
end
self:update_text()
end
function txt:mousemove(x, y, dx, dy)
self.hover = ui.contains(x, y, self.x, self.y, self.x2, self.y2)
end
function txt:mousedown(x, y, button)
self.held = self.hover
end
function txt:set_text(text)
self.text = text
self:update_text()
end
txt:set_text(text)
function txt:set_direction(direction)
self.direction = direction == 'right' and 'right' or 'left'
end
function txt:set_position(x, y)
self.x = x
self.y = y
self.x2 = x + self.max_w - 1
self.y2 = y + self.h - 1
end
return txt
end
ui.inputbox = function(x, y, w, h, placeholder, r, g, b)
local pw, ph = gfx.textSize(placeholder)
ph = ph - 4 -- for some reason it's 4 too many
if w == 0 then w = pw + 7 end
if h == 0 then h = ph + 9 end
local ib = ui.box(x, y, w, h)
ib.placeholder = ui.text(x + 4, y + h/2 - ph/2, placeholder, r, g, b, 100)
ib.text = ui.scroll_text(x + 4, y + h/2 - ph/2, w - 8, '', 'left', r, g, b)
ib.hover = false
ib.held = false
ib.focus = false
ib.cursor = 0
ib.cursor_moving = false
ib.visible_cursor = 0
ib.color = {r = r or 255, g = g or 255, b = b or 255},
ib:set_backgroud(r, g, b)
ib:drawadd(function(self)
if tpt.textwidth(self.text.text) > self.text.max_w then
self.text:set_direction('right')
else
self.text:set_direction('left')
end
local cursorx = tpt.textwidth(self.text.visible_text:sub(1, self.cursor - self.text.scroll_pos + 1))
if self.hover and not self.focus then
gfx.fillRect(self.x + 1, self.y + 1, self.w-2, self.h-2, self.color.r, self.color.g, self.color.b, 30)
end
if self.focus then
gfx.fillRect(self.x + 1, self.y + 1, self.w-2, self.h-2, self.color.r, self.color.g, self.color.b, 30)
if math.floor(socket.gettime()*2) % 2 == 0 and not self.cursor_moving then
gfx.drawLine(self.text.adjusted_x + cursorx, self.y + 2, self.text.adjusted_x + cursorx, self.y2-2, self.color.r, self.color.g, self.color.b)
elseif self.cursor_moving then
gfx.drawLine(self.text.adjusted_x + cursorx, self.y + 2, self.text.adjusted_x + cursorx, self.y2-2, self.color.r, self.color.g, self.color.b)
end
end
self.text:draw()
if not self.focus and #self.text.text == 0 then
self.placeholder:draw()
end
self.cursor_moving = false
end)
function ib:set_color(r, g, b)
self.placeholder:set_color(r, g, b, 100)
self.text:set_color(r, g, b)
self.color = {
r = r,
g = g,
b = b
}
self:set_backgroud(r, g, b)
self:set_border(r, g, b)
end
function ib:set_position(x, y)
self.x = x
self.y = y
self.x2 = x + self.w - 1
self.y2 = y + self.h - 1
self.placeholder:set_position(x + 4, y + self.h/2 - ph/2)
self.text:set_position(x + 4, y + self.h/2 - ph/2)
end
function ib:mousemove(x, y, dx, dy)
self.hover = ui.contains(x, y, self.x, self.y, self.x2, self.y2)
end
function ib:mousedown(x, y, button)
self.held = self.hover
if self.hover then
self.focus = true
end
end
function ib:mouseup(x, y, button, reason)
if not self.hover then
self.focus = false
end
end
function ib:move_cursor(amt)
local temp_cursor = self.cursor + amt
if temp_cursor > #self.text.text then temp_cursor = #self.text.text end
if temp_cursor < 0 then temp_cursor = 0 return end
if amt > 0 and temp_cursor == self.text.scroll_pos + self.text:get_max_visible_chars() then
self.text:set_scroll_pos(self.text.scroll_pos + 1)
elseif amt < 0 and temp_cursor == self.text.scroll_pos - 2 then
self.text:set_scroll_pos(self.text.scroll_pos - 1)
end
self.cursor = temp_cursor
end
function ib:keypress(key, scan, rep, shift, ctrl, alt)
-- Esc
if scan == 41 then
self.focus = false
-- Enter
elseif scan == 40 and not rep then
self.focus = false
-- Right
elseif scan == 79 then
self.cursor_moving = true
self:move_cursor(1)
-- Left
elseif scan == 80 then
self.cursor_moving = true
self:move_cursor(-1)
-- Backspace
elseif scan == 42 then
if self.cursor > 0 then
self.text:set_text(self.text.text:sub(1,self.cursor-1)..self.text.text:sub(self.cursor + 1))
self.text:set_scroll_pos(self.text.scroll_pos - 1)
self:move_cursor(-1)
end
-- Delete
elseif scan == 76 then
self.text:set_text(self.text.text:sub(1,self.cursor)..self.text.text:sub(self.cursor + 2))
self.text:set_scroll_pos(self.text.scroll_pos - 1)
-- CTRL + C
elseif scan == 6 and ctrl then
platform.clipboardPaste(self.text.text)
-- CTRL + V
elseif scan == 25 and ctrl then
local paste = platform.clipboardCopy()
self.text:set_text(self.text.text:sub(1, self.cursor)..paste..self.text.text:sub(self.cursor + 1))
self.text:set_scroll_pos(self.text.scroll_pos + #paste)
self:move_cursor(#paste)
-- CTRL + X
elseif scan == 27 and ctrl then
platform.clipboardPaste(self.text.text)
self.text:set_text('')
self:move_cursor(-self.cursor)
end
return
end
function ib:textinput(text)
if not self.focus then
return
end
if #text > 1 or string.byte(text) < 20 or string.byte(text) > 126 then return end
newstr = self.text.text:sub(1, self.cursor)..text..self.text.text:sub(self.cursor + 1)
self.text:set_text(newstr)
self:move_cursor(1)
if self.cursor >= #self.text.text then
self.text:set_scroll_pos(#self.text.text)
end
return
end
return ib
end
ui.button = function(x, y, w, h, text, f, text_align, r, g, b)
local tw, th = gfx.textSize(text)
th = th - 4 -- for some reason it's 4 too many
if w == 0 then w = tw + 7 end
if h == 0 then h = th + 9 end
x, y = x - 1, y - 1
local button = ui.box(x, y, w, h, r, g, b)
button.enabled = true
button.hover = false
button.held = false
button.text_align = text_align or 'center'
button.f = f or function() end
button:set_border(r, g, b)
button.label = ui.text(x + w/2 - tw/2, y + h/2 - th/2, text, r, g, b, a)
button.color = {r = r or 255, g = g or 255, b = b or 255}
local last_clicked = {x = 0, y = 0}
button:drawadd(function(self)
local r, g, b = self.color.r, self.color.g, self.color.b
if not self.enabled then
r, g, b = 100, 100, 100
end
if self.enabled and self.hover then
gfx.fillRect(self.x + 1, self.y + 1, self.w-2, self.h-2, self.color.r, self.color.g, self.color.b, 70)
end
self.label:draw()
if not self.held then
gfx.drawLine(self.x, self.y2 + 1, self.x2-1, self.y2 + 1, r, g, b)
gfx.drawLine(self.x-1, self.y + 1, self.x-1, self.y2 + 1, r, g, b)
end
end)
function button:set_color(r, g, b)
self.label:set_color(r, g, b)
self.color = {
r = r,
g = g,
b = b,
}
self:set_border(r, g, b)
end
function button:set_enabled(enabled)
self.enabled = enabled
if enabled then
self.label:set_color(self.color.r, self.color.g, self.color.b)
self:set_border(self.color.r, self.color.g, self.color.b)
else
self.label:set_color(100, 100, 100)
self:set_border(100, 100, 100)
end
end
function button:set_function(f)
self.f = f
end
function button:set_text_align(text_align)
self.text_align = text_align
local tx = self.x + self.w/2 - self.label.w/2
if text_align == 'left' then
tx = self.x + 4
elseif text_align == 'right' then
tx = self.x2 - tpt.textwidth(self.label.text) - 4
end
self.label:set_position(tx, self.label.y)
end
button:set_text_align(text_align)
function button:set_position(x, y)
self.x = x
self.y = y
self.x2 = x + self.w - 1
self.y2 = y + self.h - 1
local tx = self.x + self.w/2 - self.label.w/2
if self.text_align == 'left' then
tx = self.x + 4
elseif self.text_align == 'right' then
tx = self.x2 - tpt.textwidth(self.label.text) - 4
end
self.label:set_position(tx, y + self.h/2 - th/2)
end
function button:mousemove(x, y, dx, dy)
self.hover = ui.contains(x, y, self.x, self.y, self.x2, self.y2) and self.enabled
end
function button:mousedown(x, y, button)
if self.hover then
self.held = true
self:set_position(self.x - 1, self.y + 1)
end
last_clicked.x = x
last_clicked.y = y
end
function button:mouseup(x, y, button, reason)
if self.held then
self.held = false
self:set_position(self.x + 1, self.y - 1)
end
if self.hover and ui.contains(last_clicked.x, last_clicked.y, self.x, self.y, self.x2, self.y2) then
self.f()
end
end
return button
end
ui.flat_button = function(x, y, w, h, text, f, text_align, r, g, b)
local tw, th = gfx.textSize(text)
th = th - 4 -- for some reason it's 4 too many
if w == 0 then w = tw + 7 end
if h == 0 then h = th + 9 end
local button = ui.box(x, y, w, h, r, g, b)
button.enabled = true
button.hover = false
button.held = false
button.text_align = text_align or 'center'
button.f = f or function() end
button.label = ui.text(x + w/2 - tw/2, y + h/2 - th/2, text, r, g, b, a)
button.color = {r = r or 255, g = g or 255, b = b or 255}
local last_clicked = {x = 0, y = 0}
button:drawadd(function(self)
if self.enabled and self.hover then
gfx.fillRect(self.x, self.y, self.w, self.h, 255, 255, 255, 50)
end
self.label:draw()
if self.held then
gfx.fillRect(self.x, self.y, self.w, self.h, self.color.r, self.color.g, self.color.b, 80)
end
end)
function button:set_color(r, g, b)
self.label:set_color(r, g, b)
self.color = {
r = r,
g = g,
b = b,
}
self:set_border(r, g, b)
end
function button:set_enabled(enabled)
self.enabled = enabled
if enabled then
self.label:set_color(self.color.r, self.color.g, self.color.b)
else
self.label:set_color(100, 100, 100)
end
end
function button:set_function(f)
self.f = f
end
function button:set_text_align(text_align)
self.text_align = text_align
local tx = self.x + self.w/2 - self.label.w/2
if text_align == 'left' then
tx = self.x + 4
elseif text_align == 'right' then
tx = self.x2 - tpt.textwidth(self.label.text) - 4
end
self.label:set_position(tx, self.label.y)
end
button:set_text_align(text_align)
function button:set_position(x, y)
self.x = x
self.y = y
self.x2 = x + self.w - 1
self.y2 = y + self.h - 1
local tx = self.x + self.w/2 - self.label.w/2
if self.text_align == 'left' then
tx = self.x + 4
elseif self.text_align == 'right' then
tx = self.x2 - tpt.textwidth(self.label.text) - 4
end
self.label:set_position(tx, y + self.h/2 - th/2)
end
function button:mousemove(x, y, dx, dy)
self.hover = ui.contains(x, y, self.x, self.y, self.x2, self.y2) and self.enabled
end
function button:mousedown(x, y, button)
if self.hover then
self.held = true
end
last_clicked.x = x
last_clicked.y = y
end
function button:mouseup(x, y, button, reason)
if self.held then
self.held = false
end
if self.hover and ui.contains(last_clicked.x, last_clicked.y, self.x, self.y, self.x2, self.y2) then
self.f()
end
end
return button
end
ui.checkbox = function(x, y, text, r, g, b)
local cb = ui.box(x, y, 9, 9)
cb.checked = false
cb.label = ui.text(x + 14, y + 1, text, r, g, b)
cb.enabled = true
cb.hover = false
cb.held = false
cb.color = {r = r or 255, g = g or 255, b = b or 255},
cb:set_backgroud(r, g, b)
local last_clicked = {x = 0, y = 0}
cb:drawadd(function (self)
local r, g, b = self.color.r, self.color.g, self.color.b
if not self.enabled then
r, g, b = 100, 100, 100
end
if self.hover then
gfx.fillRect(self.x2 + 4, self.y - 1, self.label.w + 3, 11, self.color.r, self.color.g, self.color.b, 50)
end
if self.draw_background then
gfx.drawLine(self.x + 1, self.y + 3, self.x + 3, self.y + 5, 0, 0, 0)
gfx.drawLine(self.x + 1, self.y + 4, self.x + 3, self.y + 6, 0, 0, 0)
gfx.drawLine(self.x + 1, self.y + 5, self.x + 3, self.y + 7, 0, 0, 0)
gfx.drawLine(self.x + 4, self.y + 4, self.x2, self.y, 0, 0, 0)
gfx.drawLine(self.x + 4, self.y + 5, self.x2, self.y + 1, 0, 0, 0)
gfx.drawLine(self.x + 4, self.y + 6, self.x2, self.y + 2, 0, 0, 0)
end
if self.held then
gfx.fillRect(self.x + 1, self.y + 1, 7, 7, self.color.r, self.color.g, self.color.b)
end
gfx.drawRect(self.x, self.y, 9, 9, r, g, b)
self.label:draw()
end)
function cb:set_color(r, g, b)
self.label:set_color(r, g, b)
self.color = {
r = r,
g = g,
b = b
}
self:set_backgroud(r, g, b)
end
function cb:set_enabled(enabled)
self.enabled = enabled
if enabled then
self.label:set_color(self.color.r, self.color.g, self.color.b)
self:set_backgroud(self.color.r, self.color.g, self.color.b)
else
self.label:set_color(100, 100, 100)
self:set_backgroud(100, 100, 100)
end
end
function cb:set_checked(checked)
self.checked = checked
self.draw_background = checked
end
function cb:set_position(x, y)
self.x = x
self.y = y
self.x2 = x + self.w - 1
self.y2 = y + self.h - 1
self.label:set_position(x + 14, y + 1)
end
function cb:mousemove(x, y, dx, dy)
self.hover = ui.contains(x, y, self.x, self.y, self.x2 + 6 + self.label.w, self.y2) and self.enabled
end
function cb:mousedown(x, y, button)
self.held = self.hover and self.enabled
last_clicked.x = x
last_clicked.y = y
end
function cb:mouseup(x, y, button, reason)
if self.held then
self.held = false
end
if self.hover and ui.contains(last_clicked.x, last_clicked.y, self.x, self.y, self.x2 + 6 + self.label.w, self.y2) then
self.checked = not self.checked
self.draw_background = not self.draw_background
end
end
return cb
end
ui.radio_button = function(x, y, text, r, g, b, a)
local rb = {
x = x,
y = y,
w = 9,
h = 9,
x2 = x + 8, -- x2, y2 adjustment
y2 = y + 8, -- x2, y2 adjustment
visible = true,
color = {r = r or 255, g = g or 255, b = b or 255},
selected = false,
label = ui.text(x + 14, y + 1, text, r, g, b, a),
enabled = true,
hover = false,
held = false,
drawlist = {}
}
local last_clicked = {x = 0, y = 0}
function rb:draw()
if self.visible then
for _, f in ipairs(self.drawlist) do
f(self)
end
end
end
function rb:drawadd(f, pos)
if pos then
table.insert(self.drawlist, pos, f)
else
table.insert(self.drawlist, f)
end
end
rb:drawadd(function(self)
local r, g, b = self.color.r, self.color.g, self.color.b
if not self.enabled then
r, g, b = 100, 100, 100
end
if self.hover then
gfx.fillRect(self.x2 + 4, self.y - 1, self.label.w + 3, 11, self.color.r, self.color.g, self.color.b, 50)
end
if self.selected then
gfx.fillRect(self.x + 2, self.y + 2, 5, 5, r, g, b)
gfx.drawPixel(self.x + 2, self.y + 2, 0, 0, 0) -- top left
gfx.drawPixel(self.x + 2, self.y2 - 2, 0, 0, 0) -- bottom left
gfx.drawPixel(self.x2 - 2, self.y + 2, 0, 0, 0) -- top right
gfx.drawPixel(self.x2 - 2, self.y2 - 2, 0, 0, 0) -- bottom right
end
if self.held then
gfx.fillRect(self.x + 1, self.y + 1, 7, 7, self.color.r, self.color.g, self.color.b)
end
-- the 'circle'
gfx.drawLine(self.x, self.y + 2, self.x, self.y2 - 2, r, g, b)
gfx.drawLine(self.x2, self.y + 2, self.x2, self.y2 - 2, r, g, b)
gfx.drawLine(self.x + 2, self.y, self.x2 - 2, self.y, r, g, b)
gfx.drawLine(self.x + 2, self.y2, self.x2 - 2, self.y2, r, g, b)
gfx.drawPixel(self.x + 1, self.y + 1, r, g, b)
gfx.drawPixel(self.x + 1, self.y2 - 1, r, g, b)
gfx.drawPixel(self.x2 - 1, self.y + 1, r, g, b)
gfx.drawPixel(self.x2 - 1, self.y2 - 1, r, g, b)
-- that was the 'circle'
self.label:draw()
end)
function rb:set_color(r, g, b)
self.label:set_color(r, g, b)
self.color = {
r = r,
g = g,
b = b
}
end
function rb:set_enabled(enabled)
self.enabled = enabled
if enabled then
self.label:set_color(self.color.r, self.color.g, self.color.b)
else
self.label:set_color(100, 100, 100)
end
end
function rb:set_selected(selected)
self.selected = selected
end
function rb:set_position(x, y)
self.x = x
self.y = y
self.x2 = x + 8
self.y2 = y + 8
self.label:set_position(x + 14, y + 1)
end
function rb:mousemove(x, y, dx, dy)
self.hover = ui.contains(x, y, self.x, self.y, self.x2 + 6 + self.label.w, self.y2) and self.enabled
end
function rb:mousedown(x, y, button)
self.held = self.hover and self.enabled
last_clicked.x = x
last_clicked.y = y
end
function rb:mouseup(x, y, button, reason)
if self.held then
self.held = false
end
if self.hover and ui.contains(last_clicked.x, last_clicked.y, self.x, self.y, self.x2 + 6 + self.label.w, self.y2) then
self.selected = true
end
end
return rb
end
ui.radio_group = function(...)
local rg = {
buttons = {...},
selected = 0,
enabled = true,
visible = true
}
local last_clicked = {x = 0, y = 0}
function rg:append(butt)
table.insert(self.buttons, butt)
end
function rg:set_selected(n)
self.selected = n
for _, butt in ipairs(self.buttons) do
butt:set_selected(false)
end
self.buttons[n]:set_selected(true)
end
function rg:draw()
if self.visible then
for _, butt in ipairs(self.buttons) do
butt:draw(self)
end
end
end
function rg:set_enabled(enabled)
self.enabled = enabled
for _, butt in ipairs(self.buttons) do
butt:set_enabled(enabled)
end
end
function rg:shift_position(dx, dy) -- TODO: do it more how it should be done
for _, butt in ipairs(self.buttons) do
butt:set_position(butt.x + dx, butt.y + dy)
end
end
function rg:mousemove(x, y, dx, dy)
for _, butt in ipairs(self.buttons) do
butt:mousemove(x, y, dx, dy)
end
end
function rg:mousedown(x, y, button)
for _, butt in ipairs(self.buttons) do
butt:mousedown(x, y, button)
end
last_clicked.x = x
last_clicked.y = y
end
function rg:mouseup(x, y, button, reason)
for i, butt in ipairs(self.buttons) do
butt:mouseup(x, y, button, reason)
if butt.hover then
self.selected = i
for _, butt2 in ipairs(self.buttons) do
if butt2 ~= butt and ui.contains(last_clicked.x, last_clicked.y, butt.x, butt.y, butt.x2 + 6 + butt.label.w, butt.y2) then
butt2:set_selected(false)
end
end
end
end
end
return rg
end
ui.switch = function(x, y, text, r, g, b, colorful)
local sw = {
x = x,
y = y,
w = 15,
h = 9,
x2 = x + 14, -- x2, y2 adjustment
y2 = y + 8, -- x2, y2 adjustment
visible = true,
color = {r = r or 255, g = g or 255, b = b or 255},
switched_on = false,
colorful = colorful or false,
label = ui.text(x + 20, y + 1, text, r, g, b, a),
enabled = true,
hover = false,
held = false,
drawlist = {}
}
local last_clicked = {x = 0, y = 0}
function sw:draw()
if self.visible then
for _, f in ipairs(self.drawlist) do
f(self)
end
end
end
function sw:drawadd(f, pos)
if pos then
table.insert(self.drawlist, pos, f)
else
table.insert(self.drawlist, f)
end
end
sw:drawadd(function(self)
local r, g, b = self.color.r, self.color.g, self.color.b
if self.switched_on then
if self.colorful then
r, g, b = 0, 255, 0
end
else
r, g, b = self.color.r - 100, self.color.g - 100, self.color.b - 100
if self.colorful then
r, g, b = 255, 0, 0
end
end
if not self.enabled then
r, g, b = 100, 100, 100
end
if self.hover then
gfx.fillRect(self.x2 + 4, self.y - 1, self.label.w + 3, 11, self.color.r, self.color.g, self.color.b, 50)
end
if self.switched_on then
gfx.fillRect(self.x + 8, self.y + 2, 5, 5, r, g, b)
gfx.drawPixel(self.x + 8, self.y + 2, 0, 0, 0) -- top left
gfx.drawPixel(self.x + 8, self.y2 - 2, 0, 0, 0) -- bottom left
gfx.drawPixel(self.x + 12, self.y + 2, 0, 0, 0) -- top right
gfx.drawPixel(self.x + 12, self.y2 - 2, 0, 0, 0) -- bottom right
else
gfx.fillRect(self.x + 2, self.y + 2, 5, 5, r, g, b)
gfx.drawPixel(self.x + 2, self.y + 2, 0, 0, 0) -- top left