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
/
APLib.lua
3500 lines (2982 loc) · 124 KB
/
APLib.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/APLib
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 = {
ver = '1.29.0',
author = 'Marco4413',
website = 'https://github.com/Marco4413/APLib'
}
--MONITOR
globalMonitor = term
globalMonitorName = 'term'
globalMonitorGroup = {
enabled = false,
list = {}
}
globalMonitorWidth, globalMonitorHeight = globalMonitor.getSize()
globalMonitorBuffer = { -- EXPERIMENTAL RENDERER
enabled = false,
frames = {
lastFrame = {},
newFrame = {}
},
clear = function () -- CLEAR NEWFRAME TABLE
globalMonitorBuffer.frames.newFrame = {}
end,
write = function (x, y, text, fg, bg, transparentBG)
assert(type(x) == 'number', 'globalMonitorBuffer.write: x must be a number, got '..type(x))
assert(type(y) == 'number', 'globalMonitorBuffer.write: y must be a number, got '..type(y))
assert(type(fg) == 'number', 'globalMonitorBuffer.write: fg must be a number, got '..type(fg))
assert(type(bg) == 'number' or transparentBG, 'globalMonitorBuffer.write: bg must be a number, got '..type(bg))
y = tostring(y)
-- If row doesn't exist then create it
if not globalMonitorBuffer.frames.newFrame[y] then
globalMonitorBuffer.frames.newFrame[y] = {}
end
text = tostring(text)
-- For every character in text do
for char in text:gmatch('.') do
-- Store current pixel to a variable
local pixel = globalMonitorBuffer.frames.newFrame[y][tostring(x)]
local thisBG = bg
-- If it needs to be transparent then make thisBG the same color of the pixel that the char is on
if transparentBG then
if pixel then
thisBG = pixel.bg
else
thisBG = globalMonitor.getBackgroundColor()
end
end
-- Put new pixel in table
globalMonitorBuffer.frames.newFrame[y][tostring(x)] = {
char = char,
fg = fg,
bg = thisBG
}
x = x + 1
end
end,
draw = function ()
if not globalMonitorBuffer.enabled then return; end
-- Save current globalMonitor settings
local oldCursorPosX, oldCursorPosY = globalMonitor.getCursorPos()
local oldTextColor = globalMonitor.getTextColor()
local oldBackgroundColor = globalMonitor.getBackgroundColor()
-- For each row of the monitor do
for y=1, globalMonitorHeight do
-- Store row table
local row = globalMonitorBuffer.frames.newFrame[tostring(y)]
-- For each pixel in the row do
for x=1, globalMonitorWidth do
-- Set monitor's cursor to current pixel
globalMonitor.setCursorPos(x, y)
-- If the row has at least one custom pixel then
if row then
-- Store pixel into a variable
local pixel = row[tostring(x)]
-- If the pixel is a custom one then
if pixel then
-- Print the custom pixel with its settings
globalMonitor.setTextColor(pixel.fg)
globalMonitor.setBackgroundColor(pixel.bg)
globalMonitor.write(pixel.char)
else
-- Print background
globalMonitor.setBackgroundColor(oldBackgroundColor)
globalMonitor.write(' ')
end
else
-- Print the row with monitor's background color
globalMonitor.setBackgroundColor(oldBackgroundColor)
globalMonitor.write(string.rep(' ', globalMonitorWidth))
break
end
end
end
-- Restore old globalMonitor settings
globalMonitor.setCursorPos(oldCursorPosX, oldCursorPosY)
globalMonitor.setTextColor(oldTextColor)
globalMonitor.setBackgroundColor(oldBackgroundColor)
globalMonitorBuffer.frames.lastFrame = tableCopy(globalMonitorBuffer.frames.newFrame)
end
}
function setRenderer(_type) -- Sets the renderer for the library
assert(type(_type) == 'number', 'setRenderer: type must be a number, got '..type(_type))
if _type == 1 then -- CLASSIC RENDERER
globalMonitorBuffer.enabled = false
elseif _type == 2 then -- EXPERIMENTAL RENDERER
globalMonitorBuffer.enabled = true
end
end
--DRAWING
globalColor = colors.white
globalTextColor = colors.white
globalBackgroundTextColor = colors.black
globalRectangleType = 1
--LOOPS
globalLoop = {
enabled = false,
autoClear = true,
drawOnClock = true,
clockSpeed = 0.5,
timerSpeed = 0.1,
clock = 0,
APLWDBroadcastOnClock = false,
APLWDClearCacheOnDraw = true,
stats = {
automaticPos = true,
automaticPosOffset = {
x = 0,
y = 0
}
-- FPS (LABEL) These are created near the end
-- EPS (LABEL)
},
callbacks = {
onInit = function() end,
onStop = function() end,
onClock = function() end,
onEvent = function() end,
onTimer = function() end,
onMonitorChange = function() end
},
events = {
draw = {},
touch = {},
tick = {},
key = {},
char = {},
mouse_drag = {}
},
wasGroupChanged = false,
selectedGroup = 'none',
group = {
none = {
callbacks = {
onClock = function () end,
onEvent = function () end,
onTimer = function () end,
onMonitorChange = function () end,
onSet = function () end,
onUnset = function () end
},
objs = {}
},
LIBPrivate = {
callbacks = {
onClock = function () end,
onEvent = function () end,
onTimer = function () end,
onMonitorChange = function () end,
onSet = function () end,
onUnset = function () end
},
objs = {}
}
}
}
--GLOBALCALLBACKS
globalCallbacks = {
onBClear = function() end,
onSetMonitor = function() end
}
--HELPERS
renderEngine = {
classic = 1,
experimental = 2
}
rectangleTypes = {filled = 1, hollow = 2, checker = 3}
event = {
global = {
onBClear = 1,
onSetMonitor = 2
},
clock = {
onClock = 1
},
point = {
onDraw = 1,
onPress = 2,
onFailedPress = 3
},
rectangle = {
onDraw = 1,
onPress = 2,
onFailedPress = 3
},
header = {
onDraw = 1,
onPress = 2,
onFailedPress = 3
},
label = {
onDraw = 1,
onPress = 2,
onFailedPress = 3
},
button = {
onDraw = 1,
onPress = 2,
onFailedPress = 3
},
menu = {
onDraw = 1,
onPress = 2,
onFailedPress = 3,
onButtonPress = 4,
onFailedButtonPress = 5
},
percentagebar = {
onDraw = 1,
onPress = 2,
onFailedPress = 3
},
memo = {
onDraw = 1,
onPress = 2,
onFailedPress = 3,
onEdit = 4,
onCursorBlink = 5,
onActivated = 6,
onDeactivated = 7
},
window = {
onDraw = 1,
onPress = 2,
onFailedPress = 3,
onOBJPress = 4,
onFailedOBJPress = 5,
onEvent = 6
},
objGroup = {
onDraw = 1,
onOBJPress = 2,
onFailedOBJPress = 3
},
loop = {
group = {
onClock = 1,
onEvent = 2,
onTimer = 3,
onMonitorChange = 4,
onSet = 5,
onUnset = 6
},
onInit = 1,
onStop = 2,
onClock = 3,
onEvent = 4,
onTimer = 5,
onMonitorChange = 6
}
}
function stringSplit(_string, _separator)
_string = tostring(_string)
_separator = tostring(_separator)
local _words = {} -- CREATE THE RETURN TABLE
while true do -- LOOP
local pos = _string:find(_separator)
if pos then -- IF SEPARATOR IS IN STRING THEN
table.insert(_words, _string:sub(1, pos - 1)) -- PUT THE SEPARATED STRING INTO WORDS
_string = _string:sub(pos + 1) -- REMOVE THE STRING THAT WAS PUT INTO WORDS FROM STRING
else
table.insert(_words, _string)
break
end
end
return _words
end
function tablesAreEqual(t1, t2)
assert(type(t1) == 'table', 'tablesAreEqual: t1 must be a table, got '..type(t1))
assert(type(t2) == 'table', 'tablesAreEqual: t2 must be a table, got '..type(t2))
local function internalCheck(t1, t2, check2)
-- Check if they have the same size
if #t1 ~= #t2 then return false; end
-- Loop through every value and check if they're the same
for key, value in pairs(t1) do
-- If both values are a table then check them
if type(value) == 'table' and type(t2[key]) == 'table' then
internalCheck(value, t2[key])
-- If one value isn't the same then return false
elseif value ~= t2[key] then
return false
end
end
-- If nothing happened in the loop then they're equal, so return true
return check2 or internalCheck(t2, t1, true)
end
return internalCheck(t1, t2)
end
function tableCopy(_table)
assert(type(_table) == 'table', 'tableCopy: table must be a table, got '..type(_table))
-- Create table copy
local newT = {}
-- Loop through every key in table
for key, value in pairs(_table) do
-- If value is a table then copy it and assign the copy to the new table's key
if type(value) == 'table' then
newT[key] = tableCopy(value)
-- If value isn't a table then put value into new table's key
else
newT[key] = value
end
end
-- Return copy of the table
return newT
end
function tableHasKey(_table, _key)
assert(type(_table) == 'table', 'tableHasKey: table must be a table, got '..type(_table))
assert((type(_key) == 'string') or (type(_key) == 'number'), 'tableHasKey: key must be a string or a number, got '..type(_key))
for key, value in pairs(_table) do
if key == _key then
return true, value
end
end
return false
end
function tableHasValue(_table, _value)
assert(type(_table) == 'table', 'tableHasValue: table must be a table, got '..type(_table))
for key, value in pairs(_table) do
if value == _value then
return true, key
end
end
return false
end
OSSettings = {
settingsPath = '/.settings', -- PUT SETTINGS PATH IN OSSETTINGS
set = function (_entry, _value)
assert(type(_value) ~= 'nil', "OSSettings.set: value can't be nil, got "..type(_value))
settings.set(tostring(_entry), _value) -- SET SETTINGS VALUE
return settings.save(OSSettings.settingsPath) -- SAVE SETTINGS
end,
get = function (_entry) -- COPY SETTING'S get FUNCTION
return settings.get(tostring(_entry))
end,
getNames = function () -- COPY SETTING'S getNames FUNCTION
return settings.getNames()
end,
unset = function (_entry)
settings.unset(tostring(_entry)) -- UNSET SETTINGS VALUE
return settings.save(OSSettings.settingsPath) -- SAVE SETTINGS
end
}
function setGlobalCallback(_event, _callback)
assert(type(_callback) == 'function', 'setGlobalCallback: callback must be a function, got '..type(_callback))
if _event == 1 then
globalCallbacks.onBClear = _callback
elseif _event == 2 then
globalCallbacks.onSetMonitor = _callback
end
end
function bClear()
if globalMonitorBuffer.enabled then
globalMonitorBuffer.clear()
else
globalMonitor.clear()
globalMonitor.setCursorPos(1, 1)
end
globalCallbacks.onBClear()
if not APLWD.isReceiver and APLWD.cacheWritable then
table.insert(
APLWD.cache,
{
type = 'bClear'
}
)
end
end
function bClearMonitorGroup()
if globalMonitorGroup.enabled then -- CHECKS IF MONITORGROUP IS ENABLED
globalLoop.group[globalLoop.selectedGroup].callbacks.onMonitorChange(monitorName)
bClear()
local wasCacheWritable = APLWD.cacheWritable
if APLWD.enabled and wasCacheWritable then APLWD.cacheWritable = false; end -- DISABLE APLWD CACHE WRITE
local oldMonitor = globalMonitorName -- SAVES ORIGINAL MONITOR
for _, monitorName in pairs(globalMonitorGroup.list) do -- LOOPS THROUGH ALL MONITORS
if monitorName ~= oldMonitor then -- DRAW ONLY ON MONITOR THAT WASN'T THE GLOBAL ONE
setMonitor(monitorName)
globalLoop.group[globalLoop.selectedGroup].callbacks.onMonitorChange(monitorName)
bClear()
end
end
setMonitor(oldMonitor) -- RESETS TO ORIGINAL MONITOR
if APLWD.enabled and wasCacheWritable then APLWD.cacheWritable = true; end -- ENABLE APLWD CACHE WRITE
else
bClear()
end
end
function setMonitor(_monitorName)
_monitorName = tostring(_monitorName)
globalCallbacks.onSetMonitor(globalMonitor, globalMonitorName, globalMonitorWidth, globalMonitorHeight)
if _monitorName == 'term' then
globalMonitor = term --SET GLOBALMONITOR TO MONITOR
globalMonitorName = 'term'
globalMonitorWidth, globalMonitorHeight = globalMonitor.getSize()
else
assert(tostring(peripheral.getType(_monitorName)) == 'monitor', 'setMonitor: '.._monitorName..' must be a monitor, got '..tostring(peripheral.getType(_monitorName)))
local _monitor = peripheral.wrap(_monitorName)
globalMonitor = _monitor --SET GLOBALMONITOR TO MONITOR
globalMonitorName = _monitorName
globalMonitorWidth, globalMonitorHeight = globalMonitor.getSize()
end
end
function getMonitorSize()
return globalMonitorWidth, globalMonitorHeight
end
function setMonitorGroup(_monitorNameList)
assert(type(_monitorNameList) == 'table', 'setMonitorGroup: monitorNameList must be a table, got '..type(_monitorNameList))
for key, value in pairs(_monitorNameList) do
value = tostring(value)
if value ~= 'term' then
assert(tostring(peripheral.getType(value)) == 'monitor', 'setMonitorGroup: '..value..' must be a monitor, got '..tostring(peripheral.getType(value)))
end
end
globalMonitorGroup.list = _monitorNameList
end
function setMonitorGroupEnabled(_bool)
assert(type(_bool) == 'boolean', 'setMonitorGroupEnabled: bool must be a boolean, got '..type(_bool))
globalMonitorGroup.enabled = _bool
end
function resetMonitorGroup()
globalMonitorGroup.list = {}
end
APLWD = {
enabled = false,
cacheWritable = true,
clearOnDraw = false,
protocol = 'APLWD-'..info.ver,
senderName = 'SendeR',
receiverName = 'ReceiveR',
isReceiver = true,
myName = '',
senderID = '',
modemName = '',
cache = {}
}
APLWD.enable = function (_bool)
assert(type(_bool) == 'boolean', 'APLWD.enable: bool must be a boolean, got '..type(_bool))
APLWD.enabled = _bool
end
APLWD.broadcastOnLoopClock = function ()
globalLoop.APLWDBroadcastOnClock = true
end
APLWD.dontBroadcastOnLoopClock = function ()
globalLoop.APLWDBroadcastOnClock = false
end
APLWD.enableClearCacheOnLoopDraw = function (_bool)
assert(type(_bool) == 'boolean', 'APLWD.enableClearCacheOnLoopDraw: bool must be a boolean, got '..type(_bool))
globalLoop.APLWDClearCacheOnDraw = _bool
end
APLWD.host = function (_modemName, _hostname)
if APLWD.enabled then
_modemName = tostring(_modemName)
if _hostname then -- IF HOSTNAME ISN'T SPECIFIED SET IT TO COMPUTER'S ID
_hostname = tostring(_hostname)
else
_hostname = tostring(os.getComputerID())
end
-- MAKE SURE THAT MODEMNAME IS A MODEM
assert(tostring(peripheral.getType(_modemName)) == 'modem', 'APLWD.host: modemName must be a modem, got '..tostring(peripheral.getType(_modemName)))
rednet.open(_modemName) -- OPEN REDNET CONNECTION
if rednet.lookup(APLWD.protocol, APLWD.senderName.._hostname) then -- IF HOSTNAME WAS ALREADY TAKEN THEN ERROR
rednet.close(_modemName)
error("APLWD.host: There's already someone connected with hostname: ".._hostname)
end
rednet.host(APLWD.protocol, APLWD.senderName.._hostname) -- HOST APLWD ON HOSTNAME
APLWD.isReceiver = false
APLWD.myName = APLWD.senderName.._hostname
APLWD.modemName = _modemName
end
end
APLWD.connect = function (_modemName, _senderName, _hostname)
if APLWD.enabled then
_modemName = tostring(_modemName)
if _hostname then -- IF HOSTNAME ISN'T SPECIFIED SET IT TO COMPUTER'S ID
_hostname = tostring(_hostname)
else
_hostname = tostring(os.getComputerID())
end
_senderName = tostring(_senderName)
-- MAKE SURE THAT MODEMNAME IS A MODEM
assert(tostring(peripheral.getType(_modemName)) == 'modem', 'APLWD.connect: modemName must be a modem, got '..tostring(peripheral.getType(_modemName)))
rednet.open(_modemName) -- OPEN REDNET CONNECTION
if rednet.lookup(APLWD.protocol, APLWD.receiverName.._hostname) then -- IF HOSTNAME WAS ALREADY TAKEN THEN ERROR
rednet.close(_modemName)
error("APLWD.connect: There's already someone connected with hostname: ".._hostname)
end
-- GET ID OF THE SENDER
local _senderID = rednet.lookup(APLWD.protocol, APLWD.senderName.._senderName)
if not _senderID then -- IF NO SENDER WAS FOUND THEN ERROR
rednet.close(_modemName)
error("APLWD.connect: Didn't find any sender with name: ".._senderName)
end
rednet.host(APLWD.protocol, APLWD.receiverName.._hostname) -- HOST APLWD ON HOSTNAME
APLWD.isReceiver = true
APLWD.myName = APLWD.receiverName.._hostname
APLWD.senderID = _senderID
APLWD.modemName = _modemName
end
end
APLWD.close = function ()
if APLWD.modemName ~= '' then
if not APLWD.isReceiver then -- IF IS A SENDER THAN BROADCAST TO RECEIVERS THAT SENDER DISCONNECTED
rednet.broadcast('disconnected', APLWD.protocol)
end
rednet.unhost(APLWD.protocol, APLWD.myName) -- UNHOST COMPUTER
rednet.close(APLWD.modemName) -- CLOSE REDNET CONNECTION
APLWD.enable(false)
APLWD.clearCache() -- CLEAR CACHE AND RESET EVERY SETTING
APLWD.isReceiver = true
APLWD.myName = ''
APLWD.senderID = ''
APLWD.modemName = ''
end
end
APLWD.broadcastCache = function ()
if APLWD.enabled then
local _rednetConnection = rednet.isOpen()
assert(_rednetConnection, 'APLWD.broadcastCache: rednet connection must be opened first, connection '..tostring(_rednetConnection))
rednet.broadcast(APLWD.cache, APLWD.protocol)
APLWD.clearCache()
end
end
APLWD.receiveCache = function (_timeout)
if APLWD.enabled then
local _rednetConnection = rednet.isOpen()
assert(_rednetConnection, 'APLWD.receiveCache: rednet connection must be opened first, connection '..tostring(_rednetConnection))
local _senderID, _message, _protocol = rednet.receive(APLWD.protocol, tonumber(_timeout))
if type(_message) == 'table' then
if _senderID == APLWD.senderID then
APLWD.cache = _message
return true
end
elseif _message == 'disconnected' then
APLWD.close()
return false, 'disconnected'
end
return false
end
end
APLWD.drawCache = function ()
if APLWD.enabled then
local function drawCache()
for key, value in pairs(APLWD.cache) do
if value.type == 'bClear' then
bClearMonitorGroup()
elseif value.type == 'background' then
setBackgroundMonitorGroup(value.color)
elseif value.type == 'text' then
local oldTextColor = globalTextColor
local oldBackgroundTextColor = globalBackgroundTextColor
setTextColor(value.colors.textColor)
setBackgroundTextColor(value.colors.backgroundTextColor)
text(value.pos.x, value.pos.y, value.text, value.colors.transparentBG)
setTextColor(oldTextColor)
setBackgroundTextColor(oldBackgroundTextColor)
elseif value.type == 'point' then
local oldColor = globalColor
setColor(value.color)
point(value.pos.x, value.pos.y)
setColor(oldColor)
elseif value.type == 'rectangle' then
local oldColor = globalColor
setColor(value.color)
rectangle(value.pos.x1, value.pos.y1, value.pos.x2, value.pos.y2)
setColor(oldColor)
end
end
end
if globalMonitorGroup.enabled then -- CHECKS IF MONITORGROUP IS ENABLED
if APLWD.clearOnDraw then bClear(); end
drawCache()
if globalMonitorBuffer.enabled then globalMonitorBuffer.draw(); end
local oldMonitor = globalMonitorName -- SAVES ORIGINAL MONITOR
for _, monitorName in pairs(globalMonitorGroup.list) do -- LOOPS THROUGH ALL MONITORS
if monitorName ~= oldMonitor then -- DRAW ONLY ON MONITOR THAT WASN'T THE GLOBAL ONE
setMonitor(monitorName)
globalLoop.group[globalLoop.selectedGroup].callbacks.onMonitorChange(monitorName)
if globalMonitorBuffer.enabled then
globalMonitorBuffer.draw()
else
drawCache()
end
end
end
setMonitor(oldMonitor) -- RESETS TO ORIGINAL MONITOR
else
if APLWD.clearOnDraw then bClear(); end
drawCache()
if globalMonitorBuffer.enabled then globalMonitorBuffer.draw(); end
end
APLWD.clearCache()
end
end
APLWD.clearCache = function ()
APLWD.cache = {}
end
function setColor(_color)
assert(type(_color) == 'number', 'setColor: color must be a number, got '..type(_color))
globalColor = _color --SET GLOBALCOLOR TO COLOR
end
function setTextColor(_color)
assert(type(_color) == 'number', 'setTextColor: color must be a number, got '..type(_color))
globalTextColor = _color --SET GLOBALTEXTCOLOR TO COLOR
end
function setBackgroundTextColor(_color)
assert(type(_color) == 'number', 'setBackgroundTextColor: color must be a number, got '..type(_color))
globalBackgroundTextColor = _color --SET GLOBALTEXTBG TO COLOR
end
function setBackground(_color)
assert(type(_color) == 'number', 'setBackgroundColor: color must be a number, got '..type(_color))
if globalMonitorBuffer.enabled then
globalMonitor.setBackgroundColor(_color) --SET BG COLOR TO COLOR
else
globalMonitor.setBackgroundColor(_color) --SET BG COLOR TO COLOR
local wasCacheWritable = APLWD.cacheWritable
if APLWD.enabled and wasCacheWritable then APLWD.cacheWritable = false; end -- DISABLE APLWD CACHE WRITE
bClear() --CLEARS MONITOR
if APLWD.enabled and wasCacheWritable then APLWD.cacheWritable = true; end -- ENABLE APLWD CACHE WRITE
end
if not APLWD.isReceiver and APLWD.cacheWritable then
table.insert(
APLWD.cache,
{
type = 'background',
color = _color
}
)
end
end
function setBackgroundMonitorGroup(_color)
assert(type(_color) == 'number', 'setBackgroundMonitorGroup: color must be a number, got '..type(_color))
if globalMonitorGroup.enabled then -- CHECKS IF MONITORGROUP IS ENABLED
globalLoop.group[globalLoop.selectedGroup].callbacks.onMonitorChange(monitorName)
setBackground(_color)
local wasCacheWritable = APLWD.cacheWritable
if APLWD.enabled and wasCacheWritable then APLWD.cacheWritable = false; end -- DISABLE APLWD CACHE WRITE
local oldMonitor = globalMonitorName -- SAVES ORIGINAL MONITOR
for _, monitorName in pairs(globalMonitorGroup.list) do -- LOOPS THROUGH ALL MONITORS
if monitorName ~= oldMonitor then -- DRAW ONLY ON MONITOR THAT WASN'T THE GLOBAL ONE
setMonitor(monitorName)
globalLoop.group[globalLoop.selectedGroup].callbacks.onMonitorChange(monitorName)
setBackground(_color)
end
end
setMonitor(oldMonitor) -- RESETS TO ORIGINAL MONITOR
if APLWD.enabled and wasCacheWritable then APLWD.cacheWritable = true; end -- ENABLE APLWD CACHE WRITE
else
setBackground(_color)
end
end
function setRectangleType(_type)
assert(type(_type) == 'number', 'setRectangleType: type must be a number, got '..type(_type))
globalRectangleType = _type
end
function text(_x, _y, _text, _transparentBG)
assert(type(_x) == 'number', 'text: x must be a number, got '..type(_x))
assert(type(_y) == 'number', 'text: y must be a number, got '..type(_y))
_text = tostring(_text)
local oldCursorPosX, oldCursorPosY = globalMonitor.getCursorPos()
local oldTextColor = globalMonitor.getTextColor()
local oldBackgroundColor = globalMonitor.getBackgroundColor()
globalMonitor.setTextColor(globalTextColor)
globalMonitor.setBackgroundColor(globalBackgroundTextColor)
local lines = stringSplit(_text, '\n')
for key, value in pairs(lines) do
if globalMonitorBuffer.enabled then
globalMonitorBuffer.write(_x, _y + key - 1, value, globalTextColor, globalBackgroundTextColor, _transparentBG)
else
globalMonitor.setCursorPos(_x, _y + key - 1)
globalMonitor.write(value)
end
end
globalMonitor.setCursorPos(oldCursorPosX, oldCursorPosY)
globalMonitor.setTextColor(oldTextColor)
globalMonitor.setBackgroundColor(oldBackgroundColor)
if not APLWD.isReceiver and APLWD.cacheWritable then
table.insert(
APLWD.cache,
{
type = 'text',
text = _text,
pos = {
x = _x,
y = _y
},
colors = {
textColor = globalTextColor,
backgroundTextColor = globalBackgroundTextColor,
transparentBG = _transparentBG
}
}
)
end
end
function point(_x, _y)
assert(type(_x) == 'number', 'point: x must be a number, got '..type(_x))
assert(type(_y) == 'number', 'point: y must be a number, got '..type(_y))
local oldCursorPosX, oldCursorPosY = globalMonitor.getCursorPos() --PUTS CURSOR ON X, Y
local oldBackgroundColor = globalMonitor.getBackgroundColor() --CHANGES THE COLOR OF THE POINT
globalMonitor.setBackgroundColor(globalColor)
if globalMonitorBuffer.enabled then
globalMonitorBuffer.write(_x, _y, ' ', globalColor, globalColor)
else
globalMonitor.setCursorPos(_x, _y)
globalMonitor.write(' ') --DRAWS THE POINT
end
globalMonitor.setCursorPos(oldCursorPosX, oldCursorPosY) --RESTORES OLD CURSOR POS
globalMonitor.setBackgroundColor(oldBackgroundColor) --RESTORES OLD COLOR
if not APLWD.isReceiver and APLWD.cacheWritable then
table.insert(
APLWD.cache,
{
type = 'point',
pos = {
x = _x,
y = _y
},
color = globalColor
}
)
end
end
function line(x1, y1, x2, y2) -- SOURCE: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
assert(type(x1) == 'number', 'line: x1 must be a number, got '..type(x1))
assert(type(y1) == 'number', 'line: y1 must be a number, got '..type(y1))
assert(type(x2) == 'number', 'line: x2 must be a number, got '..type(x2))
assert(type(y2) == 'number', 'line: y2 must be a number, got '..type(y2))
local function lineLow(x1, y1, x2, y2)
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
point(x, y)
if D > 0 then
y = y + yi
D = D - 2 * dx
end
D = D + 2 * dy
end
end
local function lineHigh(x1, y1, x2, y2)
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
point(x, y)
if D > 0 then
x = x + xi
D = D - 2 * dy
end
D = D + 2 * dx
end
end
if math.abs(y2 - y1) < math.abs(x2 - x1) then
if x1 > x2 then
lineLow(x2, y2, x1, y1)
else
lineLow(x1, y1, x2, y2)
end
else
if y1 > y2 then
lineHigh(x2, y2, x1, y1)
else
lineHigh(x1, y1, x2, y2)
end
end
end
function rectangle(_x1, _y1, _x2, _y2)
assert(type(_x1) == 'number', 'rectangle: x1 must be a number, got '..type(_x1))
assert(type(_y1) == 'number', 'rectangle: y1 must be a number, got '..type(_y1))
assert(type(_x2) == 'number', 'rectangle: x2 must be a number, got '..type(_x2))
assert(type(_y2) == 'number', 'rectangle: y2 must be a number, got '..type(_y2))
local _incrementX = 1 --FINDS THE INCREMENT FOR THE LOOPS
local _incrementY = 1
if _x1 > _x2 then _incrementX = -1; end
if _y1 > _y2 then _incrementY = -1; end
local wasCacheWritable = APLWD.cacheWritable
if APLWD.enabled and wasCacheWritable then APLWD.cacheWritable = false; end -- DISABLE APLWD CACHE WRITE
if globalRectangleType == 1 then --CHECKS GLOBALRECTANGLETYPE
for x = _x1, _x2, _incrementX do --DRAWS FILLED RECTANGLE
for y = _y1, _y2, _incrementY do
point(x, y)
end
end
elseif globalRectangleType == 2 then
for x = _x1, _x2, _incrementX do --DRAWS HOLLOW RECTANGLE
point(x, _y1)
point(x, _y2)
end
for y = _y1, _y2, _incrementY do
point(_x1, y)
point(_x2, y)
end
elseif globalRectangleType == 3 then -- DRAWS CHECKER RECTANGLE
local draw = true
for x = _x1, _x2, _incrementX do
for y = _y1, _y2, _incrementY do
if draw then
point(x, y)
end
draw = not draw
end
if math.abs(_x1 - _x2) % 2 ~= 0 then draw = not draw; end
end
end
if APLWD.enabled and wasCacheWritable then APLWD.cacheWritable = true; end -- DISABLE APLWD CACHE WRITE
if not APLWD.isReceiver and APLWD.cacheWritable then
table.insert(
APLWD.cache,
{
type = 'rectangle',
pos = {
x1 = _x1,
y1 = _y1,