forked from IgorTimofeev/GUI
-
Notifications
You must be signed in to change notification settings - Fork 3
/
GUI.lua
executable file
·4734 lines (3860 loc) · 148 KB
/
GUI.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
require("advancedLua")
local component = require("component")
local computer = require("computer")
local keyboard = require("keyboard")
local filesystem = require("filesystem")
local unicode = require("unicode")
local event = require("event")
local color = require("color")
local image = require("image")
local buffer = require("doubleBuffering")
-----------------------------------------------------------------------------------------
local GUI = {
ALIGNMENT_HORIZONTAL_LEFT = 1,
ALIGNMENT_HORIZONTAL_CENTER = 2,
ALIGNMENT_HORIZONTAL_RIGHT = 3,
ALIGNMENT_VERTICAL_TOP = 4,
ALIGNMENT_VERTICAL_CENTER = 5,
ALIGNMENT_VERTICAL_BOTTOM = 6,
DIRECTION_HORIZONTAL = 7,
DIRECTION_VERTICAL = 8,
SIZE_POLICY_ABSOLUTE = 9,
SIZE_POLICY_RELATIVE = 10,
IO_MODE_FILE = 11,
IO_MODE_DIRECTORY = 12,
IO_MODE_BOTH = 13,
IO_MODE_OPEN = 14,
IO_MODE_SAVE = 15,
BUTTON_PRESS_DURATION = 0.2,
BUTTON_ANIMATION_DURATION = 0.2,
SWITCH_ANIMATION_DURATION = 0.3,
FILESYSTEM_DIALOG_ANIMATION_DURATION = 0.5,
CONTEXT_MENU_SEPARATOR_COLOR = 0xA5A5A5,
CONTEXT_MENU_DEFAULT_TEXT_COLOR = 0x2D2D2D,
CONTEXT_MENU_DEFAULT_BACKGROUND_COLOR = 0xFFFFFF,
CONTEXT_MENU_PRESSED_BACKGROUND_COLOR = 0x3366CC,
CONTEXT_MENU_PRESSED_TEXT_COLOR = 0xFFFFFF,
CONTEXT_MENU_DISABLED_COLOR = 0x878787,
CONTEXT_MENU_BACKGROUND_TRANSPARENCY = 0.18,
CONTEXT_MENU_SHADOW_TRANSPARENCY = 0.4,
BACKGROUND_CONTAINER_PANEL_COLOR = 0x0,
BACKGROUND_CONTAINER_TITLE_COLOR = 0xE1E1E1,
BACKGROUND_CONTAINER_PANEL_TRANSPARENCY = 0.3,
WINDOW_BACKGROUND_PANEL_COLOR = 0xF0F0F0,
WINDOW_SHADOW_TRANSPARENCY = 0.6,
WINDOW_TITLE_BACKGROUND_COLOR = 0xE1E1E1,
WINDOW_TITLE_TEXT_COLOR = 0x2D2D2D,
WINDOW_TAB_BAR_DEFAULT_BACKGROUND_COLOR = 0x2D2D2D,
WINDOW_TAB_BAR_DEFAULT_TEXT_COLOR = 0xF0F0F0,
WINDOW_TAB_BAR_SELECTED_BACKGROUND_COLOR = 0xF0F0F0,
WINDOW_TAB_BAR_SELECTED_TEXT_COLOR = 0x2D2D2D,
PALETTE_CONFIG_PATH = filesystem.path(getCurrentScript()) .. ".palette.cfg",
LUA_SYNTAX_COLOR_SCHEME = {
background = 0x1E1E1E,
text = 0xE1E1E1,
strings = 0x99FF80,
loops = 0xFFFF98,
comments = 0x898989,
boolean = 0xFFDB40,
logic = 0xFFCC66,
numbers = 0x66DBFF,
functions = 0xFFCC66,
compares = 0xFFCC66,
lineNumbersBackground = 0x2D2D2D,
lineNumbersText = 0xC3C3C3,
scrollBarBackground = 0x2D2D2D,
scrollBarForeground = 0x5A5A5A,
selection = 0x4B4B4B,
indentation = 0x2D2D2D
},
LUA_SYNTAX_PATTERNS = {
"[%.%,%>%<%=%~%+%-%*%/%^%#%%%&]", "compares", 0, 0,
"[^%a%d][%.%d]+[^%a%d]", "numbers", 1, 1,
"[^%a%d][%.%d]+$", "numbers", 1, 0,
"0x%w+", "numbers", 0, 0,
" not ", "logic", 0, 1,
" or ", "logic", 0, 1,
" and ", "logic", 0, 1,
"function%(", "functions", 0, 1,
"function%s[^%s%(%)%{%}%[%]]+%(", "functions", 9, 1,
"nil", "boolean", 0, 0,
"false", "boolean", 0, 0,
"true", "boolean", 0, 0,
" break$", "loops", 0, 0,
"elseif ", "loops", 0, 1,
"else[%s%;]", "loops", 0, 1,
"else$", "loops", 0, 0,
"function ", "loops", 0, 1,
"local ", "loops", 0, 1,
"return", "loops", 0, 0,
"until ", "loops", 0, 1,
"then", "loops", 0, 0,
"if ", "loops", 0, 1,
"repeat$", "loops", 0, 0,
" in ", "loops", 0, 1,
"for ", "loops", 0, 1,
"end[%s%;]", "loops", 0, 1,
"end$", "loops", 0, 0,
"do ", "loops", 0, 1,
"do$", "loops", 0, 0,
"while ", "loops", 0, 1,
"\'[^\']+\'", "strings", 0, 0,
"\"[^\"]+\"", "strings", 0, 0,
"%-%-.+", "comments", 0, 0,
},
}
--------------------------------------------------------------------------------
function GUI.setAlignment(object, horizontalAlignment, verticalAlignment)
object.horizontalAlignment, object.verticalAlignment = horizontalAlignment, verticalAlignment
return object
end
function GUI.getAlignmentCoordinates(x, y, width1, height1, horizontalAlignment, verticalAlignment, width2, height2)
if horizontalAlignment == GUI.ALIGNMENT_HORIZONTAL_CENTER then
x = x + width1 / 2 - width2 / 2
elseif horizontalAlignment == GUI.ALIGNMENT_HORIZONTAL_RIGHT then
x = x + width1 - width2
elseif horizontalAlignment ~= GUI.ALIGNMENT_HORIZONTAL_LEFT then
error("Unknown horizontal alignment: " .. tostring(horizontalAlignment))
end
if verticalAlignment == GUI.ALIGNMENT_VERTICAL_CENTER then
y = y + height1 / 2 - height2 / 2
elseif verticalAlignment == GUI.ALIGNMENT_VERTICAL_BOTTOM then
y = y + height1 - height2
elseif verticalAlignment ~= GUI.ALIGNMENT_VERTICAL_TOP then
error("Unknown vertical alignment: " .. tostring(verticalAlignment))
end
return x, y
end
function GUI.getMarginCoordinates(x, y, horizontalAlignment, verticalAlignment, horizontalMargin, verticalMargin)
if horizontalAlignment == GUI.ALIGNMENT_HORIZONTAL_RIGHT then
x = x - horizontalMargin
else
x = x + horizontalMargin
end
if verticalAlignment == GUI.ALIGNMENT_VERTICAL_BOTTOM then
y = y - verticalMargin
else
y = y + verticalMargin
end
return x, y
end
--------------------------------------------------------------------------------
local function objectIsPointInside(object, x, y)
return
x >= object.x and
x < object.x + object.width and
y >= object.y and
y < object.y + object.height
end
local function objectDraw(object)
return object
end
function GUI.object(x, y, width, height)
return {
x = x,
y = y,
width = width,
height = height,
isPointInside = objectIsPointInside,
draw = objectDraw
}
end
--------------------------------------------------------------------------------
local function containerObjectIndexOf(object)
if not object.parent then error("Object doesn't have a parent container") end
for objectIndex = 1, #object.parent.children do
if object.parent.children[objectIndex] == object then
return objectIndex
end
end
end
local function containerObjectMoveForward(object)
local objectIndex = containerObjectIndexOf(object)
if objectIndex < #object.parent.children then
object.parent.children[index], object.parent.children[index + 1] = object.parent.children[index + 1], object.parent.children[index]
end
return object
end
local function containerObjectMoveBackward(object)
local objectIndex = containerObjectIndexOf(object)
if objectIndex > 1 then
object.parent.children[objectIndex], object.parent.children[objectIndex - 1] = object.parent.children[objectIndex - 1], object.parent.children[objectIndex]
end
return object
end
local function containerObjectMoveToFront(object)
table.remove(object.parent.children, containerObjectIndexOf(object))
table.insert(object.parent.children, object)
return object
end
local function containerObjectMoveToBack(object)
table.remove(object.parent.children, containerObjectIndexOf(object))
table.insert(object.parent.children, 1, object)
return object
end
local function containerObjectRemove(object)
table.remove(object.parent.children, containerObjectIndexOf(object))
end
local function containerObjectAnimationStart(animation, duration)
animation.position = 0
animation.duration = duration
animation.started = true
animation.startUptime = computer.uptime()
computer.pushSignal("GUI", "animationStarted")
end
local function containerObjectAnimationStop(animation)
animation.position = 0
animation.started = false
end
local function containerObjectAnimationRemove(animation)
animation.removeLater = true
end
local function containerObjectAddAnimation(object, frameHandler, onFinish)
local animation = {
object = object,
position = 0,
start = containerObjectAnimationStart,
stop = containerObjectAnimationStop,
remove = containerObjectAnimationRemove,
frameHandler = frameHandler,
onFinish = onFinish,
}
object.firstParent.animations = object.firstParent.animations or {}
table.insert(object.firstParent.animations, animation)
return animation
end
local function containerAddChild(container, object, atIndex)
object.localX = object.x
object.localY = object.y
object.indexOf = containerObjectIndexOf
object.moveToFront = containerObjectMoveToFront
object.moveToBack = containerObjectMoveToBack
object.moveForward = containerObjectMoveForward
object.moveBackward = containerObjectMoveBackward
object.remove = containerObjectRemove
object.addAnimation = containerObjectAddAnimation
local function updateFirstParent(object, firstParent)
object.firstParent = firstParent
if object.children then
for i = 1, #object.children do
updateFirstParent(object.children[i], firstParent)
end
end
end
object.parent = container
updateFirstParent(object, container.firstParent or container)
if atIndex then
table.insert(container.children, atIndex, object)
else
table.insert(container.children, object)
end
return object
end
local function containerRemoveChildren(container, from, to)
from = from or 1
for objectIndex = from, to or #container.children do
table.remove(container.children, from)
end
end
local function getRectangleIntersection(R1X1, R1Y1, R1X2, R1Y2, R2X1, R2Y1, R2X2, R2Y2)
if R2X1 <= R1X2 and R2Y1 <= R1Y2 and R2X2 >= R1X1 and R2Y2 >= R1Y1 then
return
math.max(R2X1, R1X1),
math.max(R2Y1, R1Y1),
math.min(R2X2, R1X2),
math.min(R2Y2, R1Y2)
else
return
end
end
local function containerDraw(container)
local R1X1, R1Y1, R1X2, R1Y2, child = buffer.getDrawLimit()
local intersectionX1, intersectionY1, intersectionX2, intersectionY2 = getRectangleIntersection(
R1X1,
R1Y1,
R1X2,
R1Y2,
container.x,
container.y,
container.x + container.width - 1,
container.y + container.height - 1
)
if intersectionX1 then
buffer.setDrawLimit(intersectionX1, intersectionY1, intersectionX2, intersectionY2)
for i = 1, #container.children do
child = container.children[i]
if not child.hidden then
child.x, child.y = container.x + child.localX - 1, container.y + child.localY - 1
child:draw()
end
end
buffer.setDrawLimit(R1X1, R1Y1, R1X2, R1Y2)
end
return container
end
function GUI.container(x, y, width, height)
local container = GUI.object(x, y, width, height)
container.children = {}
container.passScreenEvents = true
container.draw = containerDraw
container.removeChildren = containerRemoveChildren
container.addChild = containerAddChild
return container
end
function GUI.fullScreenContainer()
return GUI.container(1, 1, buffer.getResolution())
end
--------------------------------------------------------------------------------
local function workspaceStart(workspace, eventPullTimeout)
local animation, animationIndex, animationOnFinishMethodsIndex, animationOnFinishMethods, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, e32
local function handle(isScreenEvent, currentContainer, intersectionX1, intersectionY1, intersectionX2, intersectionY2)
if
not isScreenEvent or
intersectionX1 and
e3 >= intersectionX1 and
e3 <= intersectionX2 and
e4 >= intersectionY1 and
e4 <= intersectionY2
then
local currentContainerPassed, child, newIntersectionX1, newIntersectionY1, newIntersectionX2, newIntersectionY2
if isScreenEvent then
if currentContainer.eventHandler and not currentContainer.disabled then
currentContainer.eventHandler(workspace, currentContainer, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, e32)
end
currentContainerPassed = not currentContainer.passScreenEvents
elseif currentContainer.eventHandler then
currentContainer.eventHandler(workspace, currentContainer, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, e32)
end
for i = #currentContainer.children, 1, -1 do
child = currentContainer.children[i]
if not child.hidden then
if child.children then
newIntersectionX1, newIntersectionY1, newIntersectionX2, newIntersectionY2 = getRectangleIntersection(
intersectionX1,
intersectionY1,
intersectionX2,
intersectionY2,
child.x,
child.y,
child.x + child.width - 1,
child.y + child.height - 1
)
if
newIntersectionX1 and
handle(
isScreenEvent,
child,
newIntersectionX1,
newIntersectionY1,
newIntersectionX2,
newIntersectionY2,
e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, e32
)
then
return true
end
else
if workspace.needConsume then
workspace.needConsume = nil
return true
end
if isScreenEvent then
if child:isPointInside(e3, e4) then
if child.eventHandler and not child.disabled then
child.eventHandler(workspace, child, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, e32)
end
if not child.passScreenEvents then
return true
end
end
elseif child.eventHandler then
child.eventHandler(workspace, child, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, e32)
end
end
end
end
if currentContainerPassed then
return true
end
end
end
workspace.eventPullTimeout = eventPullTimeout
repeat
e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, e32 = event.pull(workspace.animations and 0 or workspace.eventPullTimeout)
handle(
e1 == "touch" or
e1 == "drag" or
e1 == "drop" or
e1 == "scroll" or
e1 == "double_touch",
workspace,
workspace.x,
workspace.y,
workspace.x + workspace.width - 1,
workspace.y + workspace.height - 1
)
if workspace.animations then
animationIndex, animationOnFinishMethodsIndex, animationOnFinishMethods = 1, 1, {}
-- Продрачиваем анимации и вызываем обработчики кадров
while animationIndex <= #workspace.animations do
animation = workspace.animations[animationIndex]
if animation.removeLater then
table.remove(workspace.animations, animationIndex)
if #workspace.animations == 0 then
workspace.animations = nil
break
end
else
if animation.started then
animation.position = (computer.uptime() - animation.startUptime) / animation.duration
if animation.position < 1 then
animation.frameHandler(animation)
else
animation.position, animation.started = 1, false
animation.frameHandler(animation)
if animation.onFinish then
animationOnFinishMethods[animationOnFinishMethodsIndex] = animation
animationOnFinishMethodsIndex = animationOnFinishMethodsIndex + 1
end
end
end
animationIndex = animationIndex + 1
end
end
-- По завершению продрочки отрисовываем изменения на экране
workspace:draw()
-- Вызываем поочередно все методы .onFinish
for i = 1, #animationOnFinishMethods do
animationOnFinishMethods[i].onFinish(animationOnFinishMethods[i])
end
end
until workspace.needClose
workspace.needClose = nil
end
local function workspaceStop(workspace)
workspace.needClose = true
end
local function workspaceConsumeEvent(workspace)
workspace.needConsume = true
end
local function workspaceDraw(object, ...)
containerDraw(object)
buffer.drawChanges(...)
end
function GUI.workspace(x, y, width, height)
local workspace = GUI.container(x or 1, y or 1, width or buffer.getWidth(), height or buffer.getHeight())
workspace.draw = workspaceDraw
workspace.start = workspaceStart
workspace.stop = workspaceStop
workspace.consumeEvent = workspaceConsumeEvent
return workspace
end
--------------------------------------------------------------------------------
local function pressableDraw(pressable)
local background = pressable.pressed and pressable.colors.pressed.background or pressable.disabled and pressable.colors.disabled.background or pressable.colors.default.background
local text = pressable.pressed and pressable.colors.pressed.text or pressable.disabled and pressable.colors.disabled.text or pressable.colors.default.text
if background then
buffer.drawRectangle(pressable.x, pressable.y, pressable.width, pressable.height, background, text, " ")
end
buffer.drawText(math.floor(pressable.x + pressable.width / 2 - unicode.len(pressable.text) / 2), math.floor(pressable.y + pressable.height / 2), text, pressable.text)
end
local function pressableHandlePress(workspace, pressable, ...)
pressable.pressed = not pressable.pressed
workspace:draw()
if not pressable.switchMode then
pressable.pressed = not pressable.pressed
event.sleep(GUI.BUTTON_PRESS_DURATION)
workspace:draw()
end
if pressable.onTouch then
pressable.onTouch(workspace, pressable, ...)
end
end
local function pressableEventHandler(workspace, pressable, e1, ...)
if e1 == "touch" then
pressableHandlePress(workspace, pressable, e1, ...)
end
end
local function pressable(x, y, width, height, backgroundColor, textColor, backgroundPressedColor, textPressedColor, backgroundDisabledColor, textDisabledColor, text)
local pressable = GUI.object(x, y, width, height)
pressable.colors = {
default = {
background = backgroundColor,
text = textColor
},
pressed = {
background = backgroundPressedColor,
text = textPressedColor
},
disabled = {
background = backgroundDisabledColor,
text = textDisabledColor
}
}
pressable.pressed = false
pressable.text = text
pressable.draw = pressableDraw
pressable.eventHandler = pressableEventHandler
return pressable
end
--------------------------------------------------------------------------------
local function buttonPlayAnimation(button, onFinish)
button.animationStarted = true
button:addAnimation(
function(animation)
if button.pressed then
if button.colors.default.background and button.colors.pressed.background then
button.animationCurrentBackground = color.transition(button.colors.pressed.background, button.colors.default.background, animation.position)
end
button.animationCurrentText = color.transition(button.colors.pressed.text, button.colors.default.text, animation.position)
else
if button.colors.default.background and button.colors.pressed.background then
button.animationCurrentBackground = color.transition(button.colors.default.background, button.colors.pressed.background, animation.position)
end
button.animationCurrentText = color.transition(button.colors.default.text, button.colors.pressed.text, animation.position)
end
end,
function(animation)
button.animationStarted = false
button.pressed = not button.pressed
onFinish(animation)
end
):start(button.animationDuration)
end
local function buttonPress(button, workspace, object, ...)
if button.animated then
local eventData = {...}
buttonPlayAnimation(button, function(animation)
if button.onTouch then
button.onTouch(workspace, button, table.unpack(eventData))
end
animation:remove()
if not button.switchMode then
buttonPlayAnimation(button, function(animation)
animation:remove()
end)
end
end)
else
pressableHandlePress(workspace, button, ...)
end
end
local function buttonEventHandler(workspace, button, e1, ...)
if e1 == "touch" and (not button.animated or not button.animationStarted) then
button:press(workspace, button, e1, ...)
end
end
local function buttonGetColors(button)
if button.disabled then
return button.colors.disabled.background, button.colors.disabled.text
else
if button.animated and button.animationStarted then
return button.animationCurrentBackground, button.animationCurrentText
else
if button.pressed then
return button.colors.pressed.background, button.colors.pressed.text
else
return button.colors.default.background, button.colors.default.text
end
end
end
end
local function buttonDrawText(button, textColor)
buffer.drawText(math.floor(button.x + button.width / 2 - unicode.len(button.text) / 2), math.floor(button.y + button.height / 2), textColor, button.text)
end
local function buttonDraw(button)
local backgroundColor, textColor = buttonGetColors(button)
if backgroundColor then
buffer.drawRectangle(button.x, button.y, button.width, button.height, backgroundColor, textColor, " ", button.colors.transparency)
end
buttonDrawText(button, textColor)
end
local function framedButtonDraw(button)
local backgroundColor, textColor = buttonGetColors(button)
if backgroundColor then
buffer.drawFrame(button.x, button.y, button.width, button.height, backgroundColor)
end
buttonDrawText(button, textColor)
end
local function roundedButtonDraw(button)
local backgroundColor, textColor = buttonGetColors(button)
if backgroundColor then
local x2, y2 = button.x + button.width - 1, button.y + button.height - 1
if button.height > 1 then
buffer.drawText(button.x + 1, button.y, backgroundColor, string.rep("▄", button.width - 2))
buffer.drawText(button.x, button.y, backgroundColor, "⣠")
buffer.drawText(x2, button.y, backgroundColor, "⣄")
buffer.drawRectangle(button.x, button.y + 1, button.width, button.height - 2, backgroundColor, textColor, " ")
buffer.drawText(button.x + 1, y2, backgroundColor, string.rep("▀", button.width - 2))
buffer.drawText(button.x, y2, backgroundColor, "⠙")
buffer.drawText(x2, y2, backgroundColor, "⠋")
else
buffer.drawRectangle(button.x, button.y, button.width, button.height, backgroundColor, textColor, " ")
GUI.roundedCorners(button.x, button.y, button.width, button.height, backgroundColor)
end
end
buttonDrawText(button, textColor)
end
local function tagButtonDraw(button)
local backgroundColor, textColor = buttonGetColors(button)
buffer.drawRectangle(button.x, button.y, button.width, button.height, backgroundColor, textColor, " ")
buffer.drawText(button.x - 1, button.y, backgroundColor, "◀")
buttonDrawText(button, textColor)
end
local function buttonCreate(x, y, width, height, backgroundColor, textColor, backgroundPressedColor, textPressedColor, text)
local button = pressable(x, y, width, height, backgroundColor, textColor, backgroundPressedColor, textPressedColor, 0x878787, 0xA5A5A5, text)
button.animationDuration = GUI.BUTTON_ANIMATION_DURATION
button.animated = true
button.animationCurrentBackground = backgroundColor
button.animationCurrentText = textColor
button.press = buttonPress
button.eventHandler = buttonEventHandler
return button
end
local function adaptiveButtonCreate(x, y, xOffset, yOffset, backgroundColor, textColor, backgroundPressedColor, textPressedColor, text)
return buttonCreate(x, y, unicode.len(text) + xOffset * 2, yOffset * 2 + 1, backgroundColor, textColor, backgroundPressedColor, textPressedColor, text)
end
function GUI.button(...)
local button = buttonCreate(...)
button.draw = buttonDraw
return button
end
function GUI.adaptiveButton(...)
local button = adaptiveButtonCreate(...)
button.draw = buttonDraw
return button
end
function GUI.framedButton(...)
local button = buttonCreate(...)
button.draw = framedButtonDraw
return button
end
function GUI.adaptiveFramedButton(...)
local button = adaptiveButtonCreate(...)
button.draw = framedButtonDraw
return button
end
function GUI.roundedButton(...)
local button = buttonCreate(...)
button.draw = roundedButtonDraw
return button
end
function GUI.adaptiveRoundedButton(...)
local button = adaptiveButtonCreate(...)
button.draw = roundedButtonDraw
return button
end
function GUI.tagButton(...)
local button = buttonCreate(...)
button.draw = tagButtonDraw
return button
end
function GUI.adaptiveTagButton(...)
local button = adaptiveButtonCreate(...)
button.draw = tagButtonDraw
return button
end
--------------------------------------------------------------------------------
local function drawPanel(object)
buffer.drawRectangle(object.x, object.y, object.width, object.height, object.colors.background, 0x0, " ", object.colors.transparency)
return object
end
function GUI.panel(x, y, width, height, color, transparency)
local object = GUI.object(x, y, width, height)
object.colors = {
background = color,
transparency = transparency
}
object.draw = drawPanel
return object
end
--------------------------------------------------------------------------------
local function drawLabel(object)
local xText, yText = GUI.getAlignmentCoordinates(
object.x,
object.y,
object.width,
object.height,
object.horizontalAlignment,
object.verticalAlignment,
unicode.len(object.text),
1
)
buffer.drawText(math.floor(xText), math.floor(yText), object.colors.text, object.text)
return object
end
function GUI.label(x, y, width, height, textColor, text)
local object = GUI.object(x, y, width, height)
object.setAlignment = GUI.setAlignment
object:setAlignment(GUI.ALIGNMENT_HORIZONTAL_LEFT, GUI.ALIGNMENT_VERTICAL_TOP)
object.colors = {text = textColor}
object.text = text
object.draw = drawLabel
return object
end
--------------------------------------------------------------------------------
local function drawImage(object)
buffer.drawImage(object.x, object.y, object.image)
return object
end
function GUI.image(x, y, image)
local object = GUI.object(x, y, image[1], image[2])
object.image = image
object.draw = drawImage
return object
end
--------------------------------------------------------------------------------
function GUI.actionButtons(x, y, fatSymbol)
local symbol = fatSymbol and "⬤" or "●"
local container = GUI.container(x, y, 5, 1)
container.close = container:addChild(GUI.button(1, 1, 1, 1, nil, 0xFF4940, nil, 0x992400, symbol))
container.minimize = container:addChild(GUI.button(3, 1, 1, 1, nil, 0xFFB640, nil, 0x996D00, symbol))
container.maximize = container:addChild(GUI.button(5, 1, 1, 1, nil, 0x00B640, nil, 0x006D40, symbol))
return container
end
--------------------------------------------------------------------------------
local function drawProgressBar(object)
local activeWidth = math.floor(math.min(object.value, 100) / 100 * object.width)
if object.thin then
buffer.drawText(object.x, object.y, object.colors.passive, string.rep("━", object.width))
buffer.drawText(object.x, object.y, object.colors.active, string.rep("━", activeWidth))
else
buffer.drawRectangle(object.x, object.y, object.width, object.height, object.colors.passive, 0x0, " ")
buffer.drawRectangle(object.x, object.y, activeWidth, object.height, object.colors.active, 0x0, " ")
end
if object.showValue then
local stringValue = (object.valuePrefix or "") .. object.value .. (object.valuePostfix or "")
buffer.drawText(math.floor(object.x + object.width / 2 - unicode.len(stringValue) / 2), object.y + 1, object.colors.value, stringValue)
end
return object
end
function GUI.progressBar(x, y, width, activeColor, passiveColor, valueColor, value, thin, showValue, valuePrefix, valuePostfix)
local object = GUI.object(x, y, width, 1)
object.value = value
object.colors = {active = activeColor, passive = passiveColor, value = valueColor}
object.thin = thin
object.draw = drawProgressBar
object.showValue = showValue
object.valuePrefix = valuePrefix
object.valuePostfix = valuePostfix
return object
end
--------------------------------------------------------------------------------
function GUI.drawShadow(x, y, width, height, transparency, thin)
if thin then
buffer.drawRectangle(x + width, y + 1, 1, height - 1, 0x0, 0x0, " ", transparency)
buffer.drawText(x + 1, y + height, 0x0, string.rep("▀", width), transparency)
buffer.drawText(x + width, y, 0x0, "▄", transparency)
else
buffer.drawRectangle(x + width, y + 1, 2, height, 0x0, 0x0, " ", transparency)
buffer.drawRectangle(x + 2, y + height, width - 2, 1, 0x0, 0x0, " ", transparency)
end
end
function GUI.roundedCorners(x, y, width, height, color, transparency)
buffer.drawText(x - 1, y, color, "⠰", transparency)
buffer.drawText(x + width, y, color, "⠆", transparency)
end
--------------------------------------------------------------------------------
function GUI.alert(...)
local args = {...}
for i = 1, #args do
if type(args[i]) == "table" then
args[i] = table.toString(args[i], true)
else
args[i] = tostring(args[i])
end
end
if #args == 0 then args[1] = "nil" end
local sign = image.fromString([[06030000FF 0000FF 00F7FF▟00F7FF▙0000FF 0000FF 0000FF 00F7FF▟F7FF00 F7FF00 00F7FF▙0000FF 00F7FF▟F7FF00NF7FF00oF7FF00tF7FF00e00F7FF▙]])
local offset = 2
local lines = #args > 1 and "\"" .. table.concat(args, "\", \"") .. "\"" or args[1]
local bufferWidth, bufferHeight = buffer.getResolution()
local width = math.floor(bufferWidth * 0.5)
local textWidth = width - image.getWidth(sign) - 2
lines = string.wrap(lines, textWidth)
local height = image.getHeight(sign)
if #lines + 2 > height then
height = #lines + 2
end
local workspace = GUI.workspace(1, math.floor(bufferHeight / 2 - height / 2), bufferWidth, height + offset * 2)
local oldPixels = buffer.copy(workspace.x, workspace.y, workspace.width, workspace.height)
local x, y = math.floor(bufferWidth / 2 - width / 2), offset + 1
workspace:addChild(GUI.panel(1, 1, workspace.width, workspace.height, 0x1D1D1D))
workspace:addChild(GUI.image(x, y, sign))
workspace:addChild(GUI.textBox(x + image.getWidth(sign) + 2, y, textWidth, #lines, 0x1D1D1D, 0xE1E1E1, lines, 1, 0, 0)).eventHandler = nil
local buttonWidth = 10
local button = workspace:addChild(GUI.roundedButton(x + image.getWidth(sign) + textWidth - buttonWidth + 2, workspace.height - offset, buttonWidth, 1, 0x3366CC, 0xE1E1E1, 0xE1E1E1, 0x3366CC, "OK"))
button.onTouch = function()
workspace:stop()
buffer.paste(workspace.x, workspace.y, oldPixels)
buffer.drawChanges()
end
workspace.eventHandler = function(workspace, object, e1, e2, e3, e4, ...)
if e1 == "key_down" and e4 == 28 then
button.animated = false
button:press(workspace, object, e1, e2, e3, e4, ...)
end
end
workspace:draw(true)
workspace:start()
end
--------------------------------------------------------------------------------
local function codeViewDraw(codeView)
local y, toLine, colorScheme, patterns = codeView.y, codeView.fromLine + codeView.height - 1, codeView.syntaxColorScheme, codeView.syntaxPatterns
-- Line numbers bar and code area
codeView.lineNumbersWidth = unicode.len(tostring(toLine)) + 2
codeView.codeAreaPosition = codeView.x + codeView.lineNumbersWidth
codeView.codeAreaWidth = codeView.width - codeView.lineNumbersWidth
-- Line numbers
buffer.drawRectangle(codeView.x, y, codeView.lineNumbersWidth, codeView.height, colorScheme.lineNumbersBackground, colorScheme.lineNumbersText, " ")
-- Background
buffer.drawRectangle(codeView.codeAreaPosition, y, codeView.codeAreaWidth, codeView.height, colorScheme.background, colorScheme.text, " ")