This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYAGUI.lua
3356 lines (3116 loc) · 138 KB
/
YAGUI.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
--[[
Copyright (c) 2022, Marco4413 : https://github.com/Marco4413/YAGUI
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
--]]
-- INFO MODULE
local info = {
ver = "1.43",
author = "Marco4413",
website = "https://github.com/Marco4413/YAGUI/",
documentation = "https://Marco4413.github.io/YAGUI/",
copyright = "Copyright (c) 2022, Marco4413 : https://github.com/Marco4413/YAGUI\n\nPermission to use, copy, modify, and/or distribute this software for any\npurpose with or without fee is hereby granted, provided that the above\ncopyright notice and this permission notice appear in all copies.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\nWITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\nMERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\nANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\nWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\nACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\nOR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.",
nfp = "1.0.0",
nft = "1.0.0",
yai = "1.0.0"
}
-- CONSTANTS MODULE
-- THESE WILL BE TRANSFORMED IN GLOBAL VARIABLES WHEN LIBRARY IS RETURNED
local const = {
TIMER = "timer",
TOUCH = "screen_touch",
MOUSEUP = "mouse_up",
MOUSEDRAG = "mouse_drag",
MOUSEMOVE = "mouse_move",
MOUSESCROLL = "mouse_scroll",
CHAR = "char",
KEY = "key",
KEYUP = "key_up",
PASTE = "paste",
REDNET = "rednet_message",
MODEM = "modem_message",
TERMINATE = "terminate",
TERMRESIZE = "term_resize",
COROUTINE = "coroutine",
DELETED = "DELETED",
NONE = "NONE",
ALL = "ALL",
SEND = "SEND",
RECEIVE = "RECEIVE",
HOST = "HOST",
USER = "USER",
DISCONNECTED = "DISCONNECTED",
CONNECTION_REQUEST = "CONNECTION_REQUEST",
OK = "OK",
NO = "NO",
ERROR = "ERROR",
LOW_PRIORITY = 1,
HIGH_PRIORITY = 2,
ONSTART = 1,
ONSTOP = 2,
ONDRAW = 3,
ONPRESS = 4,
ONFAILEDPRESS = 5,
ONTIMEOUT = 6,
ONCLOCK = 7,
ONEVENT = 8,
ONFOCUS = 9,
ONKEY = 10,
ONCHAR = 11,
ONMOUSESCROLL = 12,
ONCURSORCHANGE = 13,
ONWRITE = 14,
ONCONNECT = 15,
ONDISCONNECT = 16,
ONSEND = 17,
ONRECEIVE = 18,
ONDRAG = 19,
ONRESIZE = 20,
ONPASTE = 21,
ONHOVER = 22,
ONRELEASE = 23,
MOUSE_LEFT = 1,
MOUSE_RIGHT = 2,
MOUSE_MIDDLE = 3,
SCROLL_UP = -1,
SCROLL_DOWN = 1,
COMPUTER = "computer",
TURTLE = "turtle",
POCKET = "pocket",
ALIGN_LEFT = 1,
ALIGN_CENTER = 2,
ALIGN_RIGHT = 3
}
for key_name, key in next, keys do
if type(key) == "number" then
const["KEY_"..key_name:upper()] = key
end
end
-- This is used by the library to draw rectangles with borders
local drawing_characters = {
TOP = string.char(131),
BOTTOM = string.char(143),
LEFT = string.char(149),
RIGHT = string.char(149),
MIDDLE = string.char(140),
TOPLEFT = string.char(151),
TOPRIGHT = string.char(148),
BOTTOMLEFT = string.char(138),
BOTTOMRIGHT = string.char(133),
MIDDLELEFT = string.char(132),
MIDDLERIGHT = string.char(136)
}
-- DEFINING ALL UTILITIES HERE TO BE ABLE TO ACCESS THEM EVERYWHERE
local generic_utils = {}
local string_utils = {}
local math_utils = {}
local table_utils = {}
local color_utils = {}
local event_utils = {}
local setting_utils = {}
local monitor_utils = {}
-- GENERIC UTILS MODULE
generic_utils = {
-- SETS A CALLBACK TO THE SPECIFIED OBJECT
-- Note: It was deprecated in v1.29 You should now be using element.set_callback instead
set_callback = function (gui_element, event, callback)
gui_element:set_callback(event, callback)
end,
-- RETURNS THE TYPE OF COMPUTER (computer, turtle, pocket) THAT IS BEING USED
get_computer_type = function ()
local comp_type = const.COMPUTER
if turtle then
comp_type = const.TURTLE
elseif pocket then
comp_type = const.POCKET
end
return comp_type, term.isColor()
end,
-- ERRORS IF EVEN ... ARGUMENTS AREN'T OF SPECIFIED TYPE IN PREVIOUS STRING ARGUMENT
expect = function (context, ...)
local t = {...}
local type_separators = "[/%.,]"
context = type(context) == "string" and table.concat({" to '", context, "'"}) or ""
local should_error = false
local error_msg
for i=1, #t, 2 do
local types_string = tostring(t[i])
local this_should_error = true
local bad_type
for k, required_type in next, string_utils.split(types_string, type_separators) do
local this_value = t[i + 1]
local this_value_type = type(this_value)
if this_value_type ~= required_type then
bad_type = this_value_type
else
this_should_error = false
break
end
end
if this_should_error then
local required_types = string_utils.split(types_string, type_separators)
local last_type = table.remove(required_types, #required_types)
local expected_types = #required_types == 0 and last_type or table.concat({table.concat(required_types, ", "), " or ", last_type})
error_msg = string.format(
"bad argument #%d%s (%s expected, got %s)",
(i + 1) / 2, context, expected_types, bad_type
)
should_error = true
break
end
end
if should_error then
error(error_msg, 3)
return false
end
return true
end,
get_type = function (value) local value_type = type(value); return value_type == "table" and type(value._type) == "string" and value._type or value_type; end
}
-- STRING UTILS MODULE
string_utils = {
magic_characters = {"(", ")", ".", "%", "+", "-", "*", "?", "[", "]", "^", "$"},
-- CONCATENATES STRINGS IN A TABLE
join = table.concat,
-- SPLITS STRING EVERY TIME SEPARATOR (PATTERN) IS FOUND
split = function (str, sep)
if not string.find(str, sep) then
return {str}
end
local return_table = {}
local pattern = table.concat({"(.-)", sep, "()"})
local last_pos
for this_match, pos in string.gfind(str, pattern) do
return_table[#return_table + 1] = this_match
last_pos = pos
end
return_table[#return_table + 1] = string.sub(str, last_pos)
return return_table
end,
-- SPLITS STRING EVERY TIME CHAR IS FOUND
split_by_char = function (str, char)
local lines = {}
local lp = 1
for i=1, #str do
if str:sub(i, i) == char then
lines[#lines + 1] = i == lp and "" or str:sub(lp, i - 1)
lp = i + 1
end
end
lines[#lines + 1] = str:sub(lp, #str)
return lines
end,
-- COMPARES V1 AND V2 AND IF V1 IS NEWER THAN V2 THEN IT RETURNS 1, IF THEY'RE THE SAME IT RETURNS 0 ELSE IT RETURNS -1
-- v1 = "0.2"; v2 = "0.1" -> returns 1
-- v1 = "0.1"; v2 = "0.1" -> returns 0
-- v1 = "0.1"; v2 = "0.2" -> returns -1
compare_versions = function (v1, v2)
local v1s = string_utils.split(v1, ".")
local v2s = string_utils.split(v2, ".")
local v1l = #v1s
local v2l = #v2s
for i=1, math.min(v1l, v2l) do
if tonumber(v1s[i]) > tonumber(v2s[i]) then
return 1
elseif tonumber(v1s[i]) < tonumber(v2s[i]) then
return -1
end
end
if v1l > v2l then
return 1
elseif v1l < v2l then
return -1
end
return 0
end,
-- ESCAPES ALL MAGIC CHARACTERS IN A STRING
escape_magic_characters = function (str)
for key, character in next, string_utils.magic_characters do
str = str:gsub(table.concat({"[%", character, "]"}), table.concat({"%%%", character}))
end
return str
end,
-- GETS THE EXTENSION OF PATH
get_extension = function (path)
return path:match("^.+%.(.+)$") or ""
end,
-- FORMATS A NUMBER SO THAT IT HAS precision DECIMAL DIGITS AND TRUNCATES IT AT THE LAST SIGNIFICANT DIGIT
format_number = function (number, precision)
number = tostring(number)
precision = precision or 0
local unit = number:gsub("(.*)%..*", "%1")
if precision <= 0 then
return unit
end
local on_dot = #unit + 1
local decimal_digits = number:sub(on_dot + 1, on_dot + precision)
decimal_digits = decimal_digits:reverse():gsub("0*(.*)", "%1"):reverse()
if #decimal_digits > 0 then
return table.concat({unit, ".", decimal_digits})
end
return unit
end,
-- REMOVES EXTRA SPACES BEFORE AND AFTER THE SPECIFIED STRING
trim = function (str)
return (str:gsub("^%s*(.*)%s*$", "%1"))
end
}
-- MATH UTILS MODULE
math_utils = {
-- A 2D VECTOR
Vector2 = {
-- RETURNS NEW VECTOR2
new = function (x, y)
local newVector2 = {
x = x or 0,
y = y or 0
}
setmetatable(newVector2, math_utils.Vector2)
return newVector2
end,
-- RETURNS DUPLICATE OF THE VECTOR
duplicate = function (self)
return math_utils.Vector2.new(self.x, self.y)
end,
-- RETURNS VECTOR LENGTH SQUARED
length_sq = function (self)
return self.x * self.x + self.y * self.y
end,
-- RETURNS VECTOR LENGTH
length = function (self)
return math.sqrt(self.x * self.x + self.y * self.y)
end,
-- RETURNS UNIT VECTOR
unit = function (self)
return self / self:length()
end,
-- RETURNS THE DOT PRODUCT BETWEEN TWO VECTORS
dot = function (self, other)
return self.x * other.x + self.y * other.y
end,
-- RETURNS THE CROSS PRODUCT BETWEEN TWO VECTORS
cross = function (self, other)
return self.x * other.y - self.y * other.x
end,
-- RETURNS A VECTOR THAT IS ROTATED BY angle
rotate = function (self, angle)
local angle_cos = math.cos(angle)
local angle_sin = math.sin(angle)
return math_utils.Vector2.new(
angle_cos * self.x - angle_sin * self.y,
angle_sin * self.x + angle_cos * self.y
)
end,
-- RETURNS tostring(Vector1) WITH precision DECIMAL NUMBERS
string = function (self, precision)
if precision then
return string.format(table.concat({"(%.", precision, "f; %.", precision, "f)"}), self.x, self.y)
else
return string.format("(%f; %f)", self.x, self.y)
end
end,
-- TOSTRING METAMETHOD, handles tostring(Vector1)
__tostring = function (self)
return self:string(0)
end,
-- LEN METAMETHOD, handles #Vector1
__len = function (self)
return self:length()
end,
-- ADD METAMETHOD, handles Vector1 + Vector2
__add = function (self, other)
return math_utils.Vector2.new(self.x + other.x, self.y + other.y)
end,
-- SUB METAMETHOD, handles Vector1 - Vector2
__sub = function (self, other)
return math_utils.Vector2.new(self.x - other.x, self.y - other.y)
end,
-- MUL METAMETHOD, handles Vector1 * number
__mul = function (self, number)
if type(self) == "number" then
return math_utils.Vector2.new(number.x * self, number.y * self)
else
return math_utils.Vector2.new(self.x * number, self.y * number)
end
end,
-- DIV METAMETHOD, handles Vector1 / number
__div = function (self, number)
return math_utils.Vector2.new(self.x / number, self.y / number)
end,
-- EQUAL METAMETHOD, handles Vector1 == Vector2
__eq = function (self, other)
return self.x == other.x and self.y == other.y
end,
-- LESS THAN METAMETHOD, handles Vector1 < Vector2; Vector1 > Vector2
__lt = function (self, other)
return self:length_sq() < other:length_sq()
end,
-- LESS OR EQUAL METAMETHOD, handles Vector1 <= Vector2; Vector1 >= Vector2
__le = function (self, other)
return self:length_sq() <= other:length_sq()
end
},
-- A 3D VECTOR
Vector3 = {
-- RETURNS NEW VECTOR3
new = function (x, y, z)
local newVector3 = {
x = x or 0,
y = y or 0,
z = z or 0
}
setmetatable(newVector3, math_utils.Vector3)
return newVector3
end,
-- RETURNS DUPLICATE OF THE VECTOR
duplicate = function (self)
return math_utils.Vector3.new(self.x, self.y, self.z)
end,
-- RETURNS VECTOR LENGTH SQUARED
length_sq = function (self)
return self.x * self.x + self.y * self.y + self.z * self.z
end,
-- RETURNS VECTOR LENGTH
length = function (self)
return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
end,
-- RETURNS UNIT VECTOR
unit = function (self)
return self / self:length()
end,
-- RETURNS THE DOT PRODUCT BETWEEN TWO VECTORS
dot = function (self, other)
return self.x * other.x + self.y * other.y + self.z * other.z
end,
-- RETURNS THE CROSS PRODUCT BETWEEN TWO VECTORS
cross = function (self, other)
return math_utils.Vector3.new(
self.y * other.z - self.z * other.y,
self.z * other.x - self.x * other.z,
self.x * other.y - self.y * other.x
)
end,
-- RETURNS A VECTOR THAT IS ROTATED ON axis BY angle
rotate = function (self, axis, angle)
local angle_cos = math.cos(angle)
return angle_cos * self + math.sin(angle) * axis:cross(self) + (1 - angle_cos) * axis:dot(self) * axis
end,
-- RETURNS tostring(Vector1) WITH precision DECIMAL NUMBERS
string = function (self, precision)
if precision then
return string.format(table.concat({"(%.", precision, "f; %.", precision, "f; %.", precision, "f)"}), self.x, self.y, self.z)
else
return string.format("(%f; %f; %f)", self.x, self.y, self.z)
end
end,
-- TOSTRING METAMETHOD, handles tostring(Vector1)
__tostring = function (self)
return self:string(0)
end,
-- LEN METAMETHOD, handles #Vector1
__len = function (self)
return self:length()
end,
-- ADD METAMETHOD, handles Vector1 + Vector2
__add = function (self, other)
return math_utils.Vector3.new(self.x + other.x, self.y + other.y, self.z + other.z)
end,
-- SUB METAMETHOD, handles Vector1 - Vector2
__sub = function (self, other)
return math_utils.Vector3.new(self.x - other.x, self.y - other.y, self.z - other.z)
end,
-- MUL METAMETHOD, handles Vector1 * number
__mul = function (self, number)
if type(self) == "number" then
return math_utils.Vector3.new(number.x * self, number.y * self, number.z * self)
else
return math_utils.Vector3.new(self.x * number, self.y * number, self.z * number)
end
end,
-- DIV METAMETHOD, handles Vector1 / number
__div = function (self, number)
return math_utils.Vector3.new(self.x / number, self.y / number, self.z / number)
end,
-- EQUAL METAMETHOD, handles Vector1 == Vector2
__eq = function (self, other)
return self.x == other.x and self.y == other.y and self.z == other.z
end,
-- LESS THAN METAMETHOD, handles Vector1 < Vector2; Vector1 > Vector2
__lt = function (self, other)
return self:length_sq() < other:length_sq()
end,
-- LESS OR EQUAL METAMETHOD, handles Vector1 <= Vector2; Vector1 >= Vector2
__le = function (self, other)
return self:length_sq() <= other:length_sq()
end
},
-- MAPS A NUMBER FROM A RANGE TO ANOTHER ONE
map = function (value, value_start, value_stop, return_start, return_stop, constrained)
local mapped_value = (value - value_start) / (value_stop - value_start) * (return_stop - return_start) + return_start
if constrained then return math_utils.constrain(mapped_value, return_start, return_stop); end
return mapped_value
end,
-- CONSTRAINS A NUMBER TO A RANGE
constrain = function (value, min_value, max_value)
return math.min(max_value, math.max(min_value, value))
end,
-- ROUNDS A NUMBER AND RETURNS IT
round = function (number)
return math.floor(number + 0.5)
end,
-- ROUNDS ALL SPECIFIED NUMBERS AND RETURNS THEM IN THE SAME ORDER
round_numbers = function (...)
local numbers = {...}
local rounded = {}
for key, number in next, numbers do
rounded[#rounded + 1] = math_utils.round(number)
end
return table.unpack(rounded)
end,
-- FLOORS ALL SPECIFIED NUMBERS AND RETURNS THEM IN THE SAME ORDER
floor_numbers = function (...)
local numbers = {...}
local floored = {}
for key, number in next, numbers do
floored[#floored + 1] = math.floor(number)
end
return table.unpack(floored)
end,
-- CEILS ALL SPECIFIED NUMBERS AND RETURNS THEM IN THE SAME ORDER
ceil_numbers = function (...)
local numbers = {...}
local ceiled = {}
for key, number in next, numbers do
ceiled[#ceiled + 1] = math.ceil(number)
end
return table.unpack(ceiled)
end
}
math_utils.Vector2.__index = math_utils.Vector2
math_utils.Vector3.__index = math_utils.Vector3
math_utils.Vector2.ONE = math_utils.Vector2.new( 1, 1)
math_utils.Vector2.UP = math_utils.Vector2.new( 0, -1)
math_utils.Vector2.DOWN = math_utils.Vector2.new( 0, 1)
math_utils.Vector2.LEFT = math_utils.Vector2.new(-1, 0)
math_utils.Vector2.RIGHT = math_utils.Vector2.new( 1, 0)
math_utils.Vector2.ZERO = math_utils.Vector2.new( 0, 0)
math_utils.Vector3.ONE = math_utils.Vector3.new( 1, 1, 1)
math_utils.Vector3.UP = math_utils.Vector3.new( 0, 1, 0)
math_utils.Vector3.DOWN = math_utils.Vector3.new( 0, -1, 0)
math_utils.Vector3.LEFT = math_utils.Vector3.new(-1, 0, 0)
math_utils.Vector3.RIGHT = math_utils.Vector3.new( 1, 0, 0)
math_utils.Vector3.FORWARD = math_utils.Vector3.new( 0, 0, 1)
math_utils.Vector3.BACK = math_utils.Vector3.new( 0, 0, -1)
math_utils.Vector3.ZERO = math_utils.Vector3.new( 0, 0, 0)
-- TABLE UTILS MODULE
table_utils = {
-- CHECKS IF THERE'S THE SPECIFIED VALUE IN THE TABLE, IF VALUE WAS FOUND
-- IT RETURNS TRUE AND THE KEY OF THE TABLE WHERE THE VALUE IS
has_value = function (tbl, value)
for tbl_key, tbl_value in next, tbl do
if tbl_value == value then return true, tbl_key; end
end
return false, nil
end,
-- CHECKS IF THERE'S THE SPECIFIED KEY IN THE TABLE, IF KEY WAS FOUND
-- IT RETURNS TRUE AND THE VALUE AT KEY IN THE TABLE
has_key = function (tbl, key)
if tbl[key] ~= nil then return true, tbl[key]; end
return false, nil
end,
-- A more advanced table serializer than textutils.serialise
-- - Doesn't error with functions, it just turns them into tostring(function) (they can't be unserialized correctly).
-- - Has a depth field which can be used to serialize recursive tables without getting an error.
serialise = function (tbl, depth, pretty, recursion, serialise_metatables, serialise_index, indent, new_line, space)
local depth = depth or 0
local indent = indent or " "
local current_depth = 0
local new_line = new_line or "\n"
local space = space or " "
if not pretty then indent, new_line, space = "", "", ""; end
local root = "root"
local found_tables = {
[tbl] = root
}
local function i_tbl_serialize(tbl, path)
local this_indent = indent:rep(current_depth + 1)
local str_tbl = table.concat({"{", new_line})
local function add_tbl(tbl)
local tbl_length = #tbl
local last_i = 0
for key, value in next, tbl do
local key_type = type(key)
local key_string
if (key_type == "number") or (key_type == "boolean") or (key_type == "nil") then
key_string = tostring(key)
else
key_string = string.format("%q", tostring(key))
end
if not serialise_index and (key == "__index") then value = {}; end
local value_type = type(value)
if key_type == "number" and key <= tbl_length and key == last_i + 1 then
last_i = key
str_tbl = table.concat({str_tbl, this_indent})
else
str_tbl = table.concat({str_tbl, string.format("%s[%s]%s=%s", this_indent, key_string, space, space)})
end
if value_type == "table" then
if not next(value) then
str_tbl = table.concat({str_tbl, "{}"})
elseif (depth <= -1) or (current_depth < depth) then
if found_tables[value] and not recursion then
str_tbl = table.concat({str_tbl, string.format("%q", found_tables[value])})
else
local this_path = table.concat({path, "[", key_string, "]"})
found_tables[value] = this_path
current_depth = current_depth + 1
str_tbl = table.concat({str_tbl, i_tbl_serialize(value, this_path)})
current_depth = current_depth - 1
end
else
str_tbl = table.concat({str_tbl, "{}"})
end
elseif (value_type == "number") or (value_type == "boolean") or (value_type == "nil") then
str_tbl = table.concat({str_tbl, string.format("%s", tostring(value))})
else
str_tbl = table.concat({str_tbl, string.format("%q", tostring(value))})
end
if next(tbl, key) then
str_tbl = table.concat({str_tbl, ",", new_line})
else
str_tbl = table.concat({str_tbl, new_line})
end
end
end
local metatable = getmetatable(tbl)
if serialise_metatables and metatable then
add_tbl(metatable)
if next(tbl) then
str_tbl = table.concat({str_tbl:sub(1, #str_tbl - #new_line), ",", str_tbl:sub(#str_tbl - #new_line + 1)})
end
end
add_tbl(tbl)
str_tbl = table.concat({str_tbl, indent:rep(current_depth), "}"})
return str_tbl
end
return i_tbl_serialize(tbl, root)
end,
-- Just copies of unserialise functions in textutils
unserialise = textutils.unserialise,
unserialize = textutils.unserialize,
get = function (tbl, ...)
local path = {...}
local current_value = tbl
for i=1, #path do
current_value = current_value[path[i]]
end
return current_value
end,
set = function (value, tbl, ...)
local path = {...}
local key_to_change = table.remove(path)
local table_with_key = table_utils.get(tbl, table.unpack(path))
local old_value = table_with_key[key_to_change]
table_with_key[key_to_change] = value
return old_value
end,
better_remove = function (tbl, ...)
local indices = {...}
local removed = #indices
for i=1, #indices do
tbl[indices[i]] = nil
end
local j, nils = 1, {}
for i=1, #tbl + removed do
if tbl[i] == nil then
nils[#nils + 1] = i
elseif #nils > 0 then
tbl[nils[j]], nils[#nils + 1], tbl[i] = tbl[i], i
j = j + 1
end
end
end
}
table_utils.serialize = table_utils.serialise
-- COLOR UTILS MODULE
color_utils = {
colors = {
[ 1 ] = "0",
[ 2 ] = "1",
[ 4 ] = "2",
[ 8 ] = "3",
[ 16 ] = "4",
[ 32 ] = "5",
[ 64 ] = "6",
[ 128 ] = "7",
[ 256 ] = "8",
[ 512 ] = "9",
[ 1024 ] = "a",
[ 2048 ] = "b",
[ 4096 ] = "c",
[ 8192 ] = "d",
[ 16384 ] = "e",
[ 32768 ] = "f"
},
paint = {},
-- TAKES A DECIMAL VALUE FROM COLORS API AND RETURNS ITS PAINT VALUE
color_to_paint = function (color)
return color_utils.colors[color]
end,
-- TAKES A PAINT AND RETURNS ITS DECIMAL VALUE FROM COLORS API
paint_to_color = function (paint)
return color_utils.paint[paint]
end
}
for key, value in next, color_utils.colors do
color_utils.paint[value] = key
end
-- EVENT UTILS MODULE
event_utils = {
-- USED TO CHECK IF AN AREA OF THE SCREEN WAS PRESSED
in_area = function (press_x, press_y, x, y, width, height)
if press_x >= x and press_x < x + width then
if press_y >= y and press_y < y + height then
return true
end
end
return false
end,
-- USED TO FORMAT "RAW_EVENTS"
-- RAW_EVENTS = {os.pullEvent()}
format_event = function (event_table)
local event = {}
event.name = event_table[1]
if event.name == "timer" then
event.name = const.TIMER
event.id = event[2]
elseif event.name == "mouse_click" then
event.name = const.TOUCH
event.from = "terminal"
event.button = event_table[2]
event.x = event_table[3]
event.y = event_table[4]
elseif event.name == "monitor_touch" then
event.name = const.TOUCH
event.from = event_table[2]
event.button = 1
event.x = event_table[3]
event.y = event_table[4]
elseif event.name == "mouse_drag" then
event.name = const.MOUSEDRAG
event.button = event_table[2]
event.x = event_table[3]
event.y = event_table[4]
elseif event.name == "mouse_move" then
event.name = const.MOUSEMOVE
event.button = event_table[2]
event.x = event_table[3]
event.y = event_table[4]
elseif event.name == "mouse_up" then
event.name = const.MOUSEUP
event.button = event_table[2]
event.x = event_table[3]
event.y = event_table[4]
elseif event.name == "mouse_scroll" then
event.name = const.MOUSESCROLL
event.direction = event_table[2]
event.x = event_table[3]
event.y = event_table[4]
elseif event.name == "char" then
event.name = const.CHAR
event.char = event_table[2]
elseif event.name == "key" then
event.name = const.KEY
event.key = event_table[2]
elseif event.name == "key_up" then
event.name = const.KEYUP
event.key = event_table[2]
elseif event.name == "paste" then
event.name = const.PASTE
event.paste = event_table[2]
elseif event.name == "rednet_message" then
event.name = const.REDNET
event.from = event_table[2]
event.message = event_table[3]
local dp = event_table[4]
if type(dp) == "number" then
event.distance = dp
else
event.protocol = tostring(dp)
end
elseif event.name == "modem_message" then
local msg = event_table[5]
event.name = const.MODEM
event.side = event_table[2]
event.from = event_table[4]
event.protocol = msg.sProtocol or ""
event.message = msg.message
elseif event.name == "terminate" then
event.name = const.TERMINATE
elseif event.name == "term_resize" then
event.name = const.TERMRESIZE
elseif event.name == "coroutine" then
event.name = const.COROUTINE
event.thread = event_table[2]
event.status = event_table[3]
event.ok = event_table[4]
local i = 5
if not event.ok then
event.error = event_table[5]
i = i + 1
end
event.returned = {}
for i=i, #event_table do
event.returned[#event.returned + 1] = event_table[i]
end
else
event.parameters = {}
for key=2, #event_table do
event.parameters[#event.parameters + 1] = event_table[key]
end
end
event.raw = event_table
return event
end,
delete_event = function (formatted_event)
if formatted_event.name ~= const.DELETED then
return {name = const.DELETED, deleted = formatted_event}
end
return formatted_event
end
}
-- SETTING UTILS MODULE
setting_utils = {
-- PATH WHERE SETTINGS WILL BE SAVED (shouldn't be changed)
_path = "/.settings",
-- SETS SETTING AND THEN SAVES ALL SETTINGS
set = function (name, value)
settings.set(name, value)
settings.save(setting_utils._path)
end,
-- UNSETS SETTING AND THEN SAVES ALL SETTINGS
unset = function (name)
settings.unset(name)
settings.save(setting_utils._path)
end,
-- GETS SETTING AND RETURNS IT
get = function (name)
return settings.get(name)
end
}
-- MONITOR UTILS MODULE
monitor_utils = {
-- RETURNS A TABLE WHICH CONTAINS ALL VALID MONITORS FROM monitor_names
get_monitors = function (monitor_names)
local monitors = {}
for key, peripheral_name in next, monitor_names do
if peripheral_name == "terminal" then
monitors[peripheral_name] = term
else
if peripheral.getType(peripheral_name) == "monitor" then
monitors[peripheral_name] = peripheral.wrap(peripheral_name)
end
end
end
return monitors
end,
-- PRINTS STRINGS ON SPECIFIED MONITOR WITH SPECIFIED FOREGROUND AND BACKGROUND
better_print = function (monitor, foreground, background, ...)
local strings = table.concat({...})
local old_foreground = monitor.getTextColor()
local old_background = monitor.getBackgroundColor()
if foreground then monitor.setTextColor(foreground); end
if background then monitor.setBackgroundColor(background); end
print(strings)
monitor.setTextColor(old_foreground)
monitor.setBackgroundColor(old_background)
end,
-- CLEARS SPECIFIED MONITOR AND SETS CURSOR TO 1, 1
better_clear = function (monitor)
monitor.clear()
monitor.setCursorPos(1, 1)
end
}
-- Used by screen_buffer functions to draw stuff on the screen
local internal_draw = {
lineLow = function (sb, x1, y1, x2, y2, color)
local dir = 1
if x1 > x2 then dir = -1; end
local dx = x2 - x1
local dy = y2 - y1
local yi = 1
if dy < 0 then
yi = -1
dy = -dy
end
local D = 2 * dy - dx
local y = y1
for x=x1, x2, dir do
sb.buffer:set_pixel(x, y, " ", color, color)
if D > 0 then
y = y + yi
D = D - 2 * dx
end
D = D + 2 * dy
end
end,
lineHigh = function (sb, x1, y1, x2, y2, color)
local dir = 1
if y1 > y2 then dir = -1; end
local dx = x2 - x1
local dy = y2 - y1
local xi = 1
if dx < 0 then
xi = -1
dx = -dx
end
local D = 2 * dx - dy
local x = x1
for y=y1, y2, dir do
sb.buffer:set_pixel(x, y, " ", color, color)
if D > 0 then
x = x + xi
D = D - 2 * dy
end
D = D + 2 * dx
end
end,
get_rect_char = function (x, y, w, h)
if h == 1 then
if w == 1 then
return drawing_characters.MIDDLE, true
else
if x == 1 then
return drawing_characters.MIDDLERIGHT, true
elseif x == w then
return drawing_characters.MIDDLELEFT, true
else
return drawing_characters.MIDDLE, true
end
end
elseif y == 1 then
if x == 1 then
return drawing_characters.TOPLEFT, false
elseif x == w then
return drawing_characters.TOPRIGHT, true
else
return drawing_characters.TOP, false
end
elseif y == h then
if x == 1 then
return drawing_characters.BOTTOMLEFT, true
elseif x == w then
return drawing_characters.BOTTOMRIGHT, true
else
return drawing_characters.BOTTOM, true
end
elseif x == 1 then
return drawing_characters.LEFT, false
elseif x == w then
return drawing_characters.RIGHT, true
else
return " ", false
end
end,
buffer = {
-- CHECKS IF SPECIFIED PIXEL WAS CREATED WITH "set_pixel" FUNCTION
is_pixel_custom = function (self, x, y)
return self.pixels[y] and self.pixels[y][x] and true or false
end,
-- RETURNS PIXEL AT X, Y IF CUSTOM ELSE IT WILL RETURN THE DEFAULT PIXEL
-- "DEFAULT PIXEL" IS THE BACKGROUND PIXEL
get_pixel = function (self, x, y)
return self.pixels[y] and self.pixels[y][x] or {
char = self.background.char,
foreground = self.background.foreground,
background = self.background.background,
inverted = self.background.inverted
}
end,
-- SETS PROPERTIES FOR A PIXEL SO IT ISN'T A "DEFAULT PIXEL" ANYMORE
set_pixel = function (self, x, y, char, foreground, background, inverted)
x, y = math.floor(x), math.floor(y)
local pixel = self:get_pixel(x, y)