-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgb.lua
1361 lines (1198 loc) · 36.8 KB
/
tgb.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
--[[
-- Terminal Glasses Bridge API
--
-- Developped by icewindow
--
-- Tested using the following versions
-- ComputerCraft 1.73
-- OpenPeripheral Core 1.1.1
-- OpenPeripheral Integration 0.2.2
-- OpenPeripheral Addons 0.3.1
--]]
local types = {
diagram = "diagram",
bargraph = "bargraph",
terminal = "terminal",
bitmap = "bitmap"
}
-- Keeping track of elements
local tEnhancedElements = {}
local nLastId = 1
local vanillaColors = {
[colors.white] = 0xF0F0F0,
[colors.orange] = 0xF2B233,
[colors.magenta] = 0xE57FD8,
[colors.lightBlue] = 0x99B2F2,
[colors.yellow] = 0xDEDE6C,
[colors.lime] = 0x7FCC19,
[colors.pink] = 0xF2B2CC,
[colors.gray] = 0xC4C4C4,
[colors.lightGray] = 0x999999,
[colors.cyan] = 0x4C99B2,
[colors.purple] = 0xB266E5,
[colors.blue] = 0x3366CC,
[colors.brown] = 0x7F664C,
[colors.green] = 0x57A64E,
[colors.red] = 0xCC4C4C,
[colors.black] = 0x000000,
}
--[[
Crates a diagram on the surface
param nPosx
X position of the upper-left corner
param nPosy
Y position of the upper-left corner
param nWidth
Diagram width
param nHeight
Diagram height
param nDataColor
(Optional) Color to draw the data in. Defaults to blue (0x0000FF)
param nDataOpacity
(Optional) Opacity for the data points. Defaults to 1 (100% opaque)
param bBar
(Optional) If true, diagram data will be plotted as bars. If false, only points will be drawn. Defaults to true.
param bReverse
(Optional) If true, the Y-axis will be rendered on the right side of the diagram and data will be filled right-to-left. Defaults to false
param nMarkerSpaceX
(Optional) Spacing between the markers on X axis. Must be 0 or greater if provided. 0 means no marker.
If omitted or set to false the diagram will be drawn without diagram surface or axis (dummy diagram) and any following parameters will be ignored.
param nMarkerSpaceY
Spacing between markers on Y axis. Must be 0 or greater. 0 means no markers.
param nDiagramColor
(Optional) Color of the diagram axis. Defaults to black (0x000000)
param nAxisOpacity
(Optional) Axis opacity. Defaults to 1 (100% opaque)
param nSurfaceOpacity
(Optional) Opacity of the diagram surface. Defaults to half of nAxisOpacity
--]]
local function addDiagram( oSurface, nPosx, nPosy, nWidth, nHeight, nDataColor, nDataOpacity, bBar, bReverse, nMarkerSpaceX, nMarkerSpaceY, nDiagramColor, nAxisOpacity, nSurfaceOpacity )
if type(oSurface) ~= "table" or
type(nPosx) ~= "number" or
type(nPosy) ~= "number" or
type(nWidth) ~= "number" or
type(nHeight) ~= "number" or
(nDataColor ~= nil and type(nDataColor) ~= "number") or
(nDataOpacity ~= nil and type(nDataOpacity) ~= "number") or
(bBar ~= nil and type(bBar) ~= "boolean") or
(bReverse ~= nil and type(bReverse) ~= "boolean") or
(nMarkerSpaceX ~= nil and type(nMarkerSpaceX) ~= "number") or
(nMarkerSpaceX ~= nil and type(nMarkerSpaceY) ~= "number") or
(nDiagramColor ~= nil and type(nDiagramColor) ~= "number") or
(nAxisOpacity ~= nil and type(nAxisOpacity) ~= "number") or
(nSurfaceOpacity ~= nil and type(nSurfaceOpacity) ~= "number") then
error("Expected object, number, number, number, number, [number], [number], [boolean], [boolean], [[number, number], [number], [number], [number]]")
end
-- Setup defaults
nDataColor = nDataColor or 0xFF
nDataOpacity = nDataOpacity or 1
if bBar == nil then bBar = true end
if bReverse == nil then bReverse = false end
nDiagramColor = nDiagramColor or 0
nAxisOpacity = nAxisOpacity or 1
nSurfaceOpacity = nSurfaceOpacity or nAxisOpacity * 0.5
-- Setup locals
local elements = {
markerX = {},
markerY = {},
bars = {}
}
local bVisible = true
local tData = {}
local bNormalize = true
local nNormalizeScale = nHeight
local userdata
local nTgbid = nLastId
nLastId = nLastId + 1
-- Setup
if nMarkerSpaceX then
-- Draw diagram body
elements[1] = oSurface.addBox(nPosx + (not bReverse and 2 or 0), nPosy, nWidth, nHeight, nDiagramColor, nSurfaceOpacity)
elements[2] = oSurface.addBox(nPosx + (not bReverse and 2 or 0), nPosy + nHeight, nWidth, 1, nDiagramColor, nAxisOpacity)
elements[3] = oSurface.addBox(nPosx + (bReverse and nWidth or 1), nPosy, 1, nHeight + 1, nDiagramColor, nAxisOpacity)
for i=1, 3 do
elements[i].setUserdata({_tgbid = nTgbid})
end
if nMarkerSpaceX > 0 then
for i=0, nWidth, nMarkerSpaceX do
local box = oSurface.addBox(nPosx + (bReverse and 0 or 1) + i, nPosy + nHeight + 1, 1, 1, nDiagramColor, nAxisOpacity)
box.setUserdata({_tgbid = nTgbid});
table.insert(elements.markerX, box)
end
end
if nMarkerSpaceY > 0 then
for i=nHeight, 0, -nMarkerSpaceY do
local box = oSurface.addBox(nPosx + (bReverse and nWidth + 1 or 0), nPosy + i, 1, 1, nDiagramColor, nAxisOpacity)
box.setUserdata({_tgbid = nTgbid});
table.insert(elements.markerY, box)
end
end
for i=1, #elements do
elements[i].setUserdata({_tgbid = nTgbid, rel = "body"})
end
end
for i=1,nWidth do
elements.bars[i] = oSurface.addBox(nPosx, nPosy, 1, 0, nDataColor, nDataOpacity)
elements.bars[i].setUserdata({_tgbid = nTgbid, rel = "bar", offset = i})
end
-- Helper functions
local function updateDiagram()
local start = bReverse and nWidth or 1
local finish = bReverse and 1 or nWidth
local step = bReverse and -1 or 1
local i = 1
for c=start,finish,step do
local value = tData[i]
if value then
if bNormalize then
value = (value / nNormalizeScale) * nHeight
end
value = math.floor(value + 0.5)
local barStart = nPosy + nHeight - value
elements.bars[i].setX(nPosx + (bReverse and -1 or 1) + c)
elements.bars[i].setY(barStart)
if bBar then
elements.bars[i].setHeight(value)
else
elements.bars[i].setHeight(value == 0 and 0 or 1)
end
end
i = i + 1
end
end
-- Diagram implementation
local diagram = {}
function diagram.delete()
for i=1, #elements do
elements[i].delete()
end
for i=1, #elements.markerX do
elements.markerX[i].delete()
end
for i=1, #elements.markerY do
elements.markerY[i].delete()
end
for i=1, #elements.bars do
elements.bars[i].delete()
end
tEnhancedElements[nTgbid] = nil
end
function diagram.setVisible( bVis )
for i=1, #elements do
elements[i].setVisible(bVis)
end
for i=1, #elements.markerX do
elements.markerX[i].setVisible(bVis)
end
for i=1, #elements.markerY do
elements.markerY[i].setVisible(bVis)
end
for i=1, #elements.bars do
elements.bars[i].setVisible(bVis)
end
bVisible = bVis
end
function diagram.setNormalize( bNewNormalize )
bNormalize = bNewNormalize
updateDiagram()
end
function diagram.setNormalizeScale( nNewNormalizeScale )
nNormalizeScale = nNewNormalizeScale
updateDiagram()
end
function diagram.setX( nNewx )
if nMarkerSpaceX then
elements[1].setX(nNewx + (not bReverse and 2 or 0))
elements[2].setX(nNewx + (not bReverse and 2 or 0))
elements[3].setX(nNewx + (bReverse and nWidth or 1))
end
for i=1,#elements.markerX do
elements.markerX[i].setX(nNewx + (bReverse and 0 or 1) + (i - 1) * nMarkerSpaceX)
end
for i=1,#elements.markerY do
elements.markerY[i].setX(nNewx + (bReverse and nWidth + 1 or 0))
end
nPosx = nNewx
updateDiagram()
end
function diagram.setY( nNewy )
if nMarkerSpaceX then
elements[1].setY(nNewy)
elements[2].setY(nNewy + nHeight)
elements[3].setY(nNewy)
end
for i=1,#elements.markerX do
elements.markerX[i].setY(nNewy + nHeight + 1)
end
for i=1,#elements.markerY do
elements.markerY[i].setY(nNewy + (i - 1) * nMarkerSpaceY)
end
nPosy = nNewy
updateDiagram()
end
function diagram.setZ( nPosz )
for i=1, #elements do
elements[i].setZ(nPosz)
end
for i=1, #elements.markerX do
elements.markerX[i].setZ(nPosz)
end
for i=1, #elements.markerY do
elements.markerY[i].setZ(nPosz)
end
for i=1, #elements.bars do
elements.bars[i].setZ(nPosz)
end
end
function diagram.getX()
return nPosx
end
function diagram.getY()
return nPosy
end
function diagram.getVisible()
return bVisible
end
function diagram.setUserdata( data )
userdata = data
end
function diagram.getUserdata()
return userdata
end
function diagram.getId()
return nTgbid
end
function diagram.setData( tNewData )
tData = tNewData
updateDiagram()
end
function diagram.getData()
return tData
end
function diagram.getType()
return types.diagram
end
function diagram.insertAtStart( nValue )
table.remove(tData, nWidth)
table.insert(tData, 1, nValue)
updateDiagram()
end
function diagram.insertAtEnd( nValue )
table.remove(tData, 1)
table.insert(tData, nValue)
updateDiagram()
end
return diagram
end
--[[
Add a bargraph to the surface
param oSurface
The surface to create the bargraph on
param nPosx
X position of the bargraph
param nPosy
Y position of the bargraph
param nWidth
Thickness of the bargraph. Must be 1 or greater
param nMaxValue
The maximal value the graph can display. Must be 1 or greater
param bVertical
If true the bar will be rendered vertical instead of horizontal
param bReverse
If true the graph will start at the highest extreme and go towards the lowest extreme
param nBorderColor
Color of the border. Defaults to black (0x000000)
param nBorderOpacity
Opacity of the border. Defaults to 1 (100% opaque)
param nFillColor
Color of the bar. Defaults to white (0xFFFFFF)
param nFillOpacity
Opacity of the bar. Defaults to 1 (100% opaque)
param nFillBackgroundColor
Color of the "negative" bar. Defaults to black (0x000000)
param nFillBackgroundOpacity
Opacity of the "negative" bar. Defaults to 0.5 (50% transparent)
--]]
function addBargraph( oSurface, nPosx, nPosy, nWidth, nMaxValue, bVertical, bReverse, nBorderColor, nBorderOpacity, nFillColor, nFillOpacity, nFillBackgroundColor, nFillBackgroundOpacity )
if type(oSurface) ~= "table" or
type(nPosx) ~= "number" or
type(nPosy) ~= "number" or
type(nWidth) ~= "number" or
type(nMaxValue) ~= "number" or
type(bVertical) ~= "boolean" or
type(bReverse) ~= "boolean" or
(nColor ~= nil and type(nColor) ~= "number") or
(nOpacity ~= nil and type(nOpacity) ~= "number") or
(nFillColor ~= nil and type(nFillColor) ~= "number") or
(nFillOpacity ~= nil and type(nFillOpacity) ~= "number") then
error("Expected object, number, number, number, number, boolean, boolean, [number], [number], [number], [number], [number], [number]")
end
-- Setup defaults
nBorderColor = nBorderColor or 0
nBorderOpacity = nBorderOpacity or 1
nFillColor = nFillColor or 0xFFFFFF
nFillOpacity = nFillOpacity or 1
nFillBackgroundColor = nFillBackgroundColor or 0
nFillBackgroundOpacity = nFillBackgroundOpacity or 0.5
nWidth = nWidth < 1 and 1 or nWidth
nMaxValue = nMaxValue < 1 and 1 or nMaxValue
-- Setup locals
local nValue = 0
local tBorder = {}
local oFill
local oNegativeFill
local bVisible = true
local userdata
local nTgbid = nLastId
nLastId = nLastId + 1
-- Helper function
local function adjustFill()
if bVertical and bReverse then
oFill.setX(nPosx + 1)
oFill.setY(nPosy + 1)
oFill.setRotation(0)
oNegativeFill.setX(nPosx + nWidth + 1)
oNegativeFill.setY(nPosy + nMaxValue + 1)
oNegativeFill.setRotation(180)
elseif bVertical and not bReverse then
oFill.setX(nPosx + nWidth + 1)
oFill.setY(nPosy + nMaxValue + 1)
oFill.setRotation(180)
oNegativeFill.setX(nPosx + 1)
oNegativeFill.setY(nPosy + 1)
oNegativeFill.setRotation(0)
elseif not bVertical and bReverse then
oFill.setX(nPosx + nMaxValue + 1)
oFill.setY(nPosy + 1)
oFill.setRotation(90)
oNegativeFill.setX(nPosx + 1)
oNegativeFill.setY(nPosy + nWidth + 1)
oNegativeFill.setRotation(270)
else
oFill.setX(nPosx + 1)
oFill.setY(nPosy + nWidth + 1)
oFill.setRotation(270)
oNegativeFill.setX(nPosx + nMaxValue + 1)
oNegativeFill.setY(nPosy + 1)
oNegativeFill.setRotation(90)
end
end
local function adjustBorder()
tBorder[1].setWidth((bVertical and nWidth or nMaxValue) + 2)
tBorder[2].setHeight(bVertical and nMaxValue or nWidth)
tBorder[3].setY(nPosy + (bVertical and nMaxValue or nWidth) + 1)
tBorder[3].setWidth((bVertical and nWidth or nMaxValue) + 2)
tBorder[4].setX(nPosx + (bVertical and nWidth or nMaxValue) + 1)
tBorder[4].setHeight(bVertical and nMaxValue or nWidth)
end
local function repositionBorder()
tBorder[1].setX(nPosx)
tBorder[1].setY(nPosy)
tBorder[2].setX(nPosx)
tBorder[2].setY(nPosy + 1)
tBorder[3].setX(nPosx)
tBorder[3].setY(nPosy + (bVertical and nMaxValue or nWidth) + 1)
tBorder[4].setX(nPosx + (bVertical and nWidth or nMaxValue) + 1)
tBorder[4].setY(nPosy + 1)
end
-- Element implementation
local graph = {}
function graph.delete()
for i=1,4 do
tBorder[i].delete()
end
oFill.delete()
oNegativeFill.delete()
tEnhancedElements[nTgbid] = nil
end
function graph.setVisible( bVis )
for i=1,4 do
tBorder[i].setVisible(bVis)
end
oFill.setVisible(bVis)
oNegativeFill.setVisible(bVis)
bVisible = bVis
end
function graph.getVisible()
return bVisible
end
function graph.getType()
return types.bargraph
end
function graph.setValue( nNewValue )
nNewValue = nNewValue <= nMaxValue and nNewValue or nMaxValue
nNewValue = nNewValue < 0 and 0 or nNewValue
nValue = nNewValue
oFill.setHeight(nNewValue)
oNegativeFill.setHeight(nMaxValue - nNewValue)
end
function graph.setVertical( bNewVertical )
if bVertical ~= bNewVertical then
bVertical = bNewVertical
adjustBorder()
adjustFill()
end
end
function graph.setReverse( bNewReverse )
if bReverse ~= bNewReverse then
bReverse = bNewReverse
adjustFill()
end
end
function graph.setX( nNewX )
nPosx = nNewX
repositionBorder()
adjustFill()
end
function graph.setY( nNewY )
nPosy = nNewY
repositionBorder()
adjustFill()
end
function graph.setWidth( nNewWidth )
nWidth = nNewWidth < 1 and 1 or nNewWidth
oFill.setWidth(nWidth)
oNegativeFill.setWidth(nWidth)
adjustBorder()
adjustFill()
end
function graph.setMaxValue( nNewMaxValue )
nMaxValue = nNewMaxValue < 1 and 1 or nNewMaxValue
adjustBorder()
adjustFill()
graph.setValue(nValue)
end
function graph.setBorderColor( nNewColor )
for i=1,4 do
tBorder.setColor(nNewColor)
end
nBorderColor = nNewColor
end
function graph.setBorderOpacity( nNewOpacity )
for i=1,4 do
tBorder.setOpacity(nNewOpacity)
end
nBorderOpacity = nNewOpacity
end
function graph.setFillColor( nNewColor )
oFill.setColor(nNewColor)
nFillColor = nNewColor
end
function graph.setFillOpacity( nNewOpacity )
oFill.setOpacity(nNewOpacity)
nFillOpacity = nNewOpacity
end
function graph.setFillBackgroundColor( nNewColor )
oNegativeFill.setColor(nNewColor)
nFillBackgroundColor = nNewColor
end
function graph.setFillBackgroundOpacity( nNewOpacity )
oNegativeFill.setOpacity(nNewOpacity)
nFillBackgroundOpacity = nNewOpacity
end
function graph.getX()
return nPosx
end
function graph.getY()
return nPosy
end
function graph.setUserdata( data )
userdata = data
end
function graph.getUserdata()
return userdata
end
function graph.getId()
return nTgbid
end
function graph.getValue()
return nValue
end
function graph.getMaxValue()
return nMaxValue
end
function graph.getWidth()
return nWidth
end
function graph.isReverse()
return bReverse
end
function graph.isVertical()
return bVertical
end
function graph.getBorderColor()
return nBorderColor
end
function graph.getBorderOpacity()
return nBorderOpacity
end
function graph.getFillColor()
return nFillColor
end
function graph.getFillOpacity()
return nFillOpacity
end
function graph.getFillBackgroundColor()
return nFillBackgroundColor
end
function graph.getFillBackgroundOpacity()
return nFillBackgroundOpacity
end
-- Build graph
tBorder[1] = oSurface.addBox(nPosx, nPosy, (bVertical and nWidth or nMaxValue) + 2, 1, nBorderColor, nBorderOpacity) -- top horizontal
tBorder[2] = oSurface.addBox(nPosx, nPosy + 1, 1, bVertical and nMaxValue or nWidth, nBorderColor, nBorderOpacity) -- left vertical
tBorder[3] = oSurface.addBox(nPosx, nPosy + (bVertical and nMaxValue or nWidth) + 1, (bVertical and nWidth or nMaxValue) + 2, 1, nBorderColor, nBorderOpacity) -- bottom horizontal
tBorder[4] = oSurface.addBox(nPosx + (bVertical and nWidth or nMaxValue) + 1, nPosy + 1, 1, bVertical and nMaxValue or nWidth, nBorderColor, nBorderOpacity) -- right vertical
-- Add proto boxes for the fill
oFill = oSurface.addBox(0, 0, nWidth, 0, nFillColor, nFillOpacity)
oNegativeFill = oSurface.addBox(0, 0, nWidth, nMaxValue, nFillBackgroundColor, nFillBackgroundOpacity)
adjustFill()
for i=1, 4 do
tBorder[i].setUserdata({_tgbid = nTgbid, rel = "border"})
end
oFill.setUserdata({_tgbid = nTgbid, rel = "fill"})
oNegativeFill.setUserdata({_tgbid = nTgbid, rel = "fill"})
return graph
end
--[[
Creates a terminal display object
param oSurface
The surface to create the terminal on
param nPosx
X coordinate of the terminal
param nPosy
Y coordinate of the terminal
param nChars
Number of characters in one row
param nRows
Number ow rows on the terminal
param nTextColor
(Optional) Text color. Defaults to vanilla white (0xF0F0F0)
param nBackgroundColor
(Optional) Background color. Defaults to black (0x000000)
param nTextOpacity
(Optional) Opacity of the terminal text. Defaults to 1 (100% opaque)
param nBackgroundOpacity
(Optional) Opacity of the terminal background. Defaults to 0.5 (50% opaque)
--]]
local function addTerminal( oSurface, nPosx, nPosy, nChars, nRows, nTextColor, nBackgroundColor, nTextOpacity, nBackgroundOpacity )
if type(oSurface) ~= "table" or
type(nPosx) ~= "number" or
type(nPosy) ~= "number" or
type(nChars) ~= "number" or
type(nRows) ~= "number" or
(nTextColor ~= nil and type(nTextColor) ~= "number") or
(nBackgroundColor ~= nil and type(nBackgroundColor) ~= "number") or
(nTextOpacity ~= nil and type(nTextOpacity) ~= "number") or
(nBackgroundOpacity ~= nil and type(nBackgroundOpacity) ~= "number") then
error("Expected object, number, number, number, number, [number], [number], [number], [number]")
end
local charWidth = 6
local charHeight = 9
local width = nChars * charWidth + 1
local height = nRows * charHeight
nTextColor = nTextColor or 0xF0F0F0
nBackgroundColor = nBackgroundColor or 0
nTextOpacity = nTextOpacity or 1
nBackgroundOpacity = nBackgroundOpacity or 0.5
local elements = {}
local lineBuffer = {}
local nCursorX = 1
local nCursorY = 1
local cursor = oSurface.addBox(nPosx, nPosy + charHeight, charWidth + 1, 1, nTextColor, nTextOpacity)
local cursorBlink = true
local visible = true
local textProcessing = false
local doScreenUpdates = false
local userdata
local nTgbid = nLastId
nLastId = nLastId + 1
-- Setup
for i=1,nRows do
local lineElements = {}
local lineChars = {}
for j=1,nChars do
lineElements[j] = {
oSurface.addBox(nPosx + (j - 1) * charWidth, nPosy + (i - 1) * charHeight , charWidth, charHeight, nBackgroundColor, nBackgroundOpacity),
oSurface.addText(nPosx + (j - 1) * charWidth + 1, nPosy + (i - 1) * charHeight + 1, " ", nTextColor)
}
lineElements[j][1].setZ(0)
lineElements[j][2].setZ(1)
lineElements[j][2].setAlpha(nTextOpacity)
lineChars[j] = {" ", nBackgroundColor, nTextColor}
end
elements[i] = lineElements
lineBuffer[i] = lineChars
end
cursor.setZ(2)
-- helper functions
local function numberToRGB( nColor )
local red = bits.rshift(nColor, 16)
local green = bits.rshift(bits.band(nColor, 0xFF00), 8)
local blue = bits.band(nColor(nColor, 0xFF))
return red, green, blue
end
local function postScreenUpdate()
if doScreenUpdates then
if oSurface.sync and type(oSurface.sync) == "function" then
oSurface.sync()
else
os.queueEvent("tgb_update")
end
end
end
local function updateCursorPos()
cursor.setX( nPosx + (nCursorX - 1) * charWidth )
cursor.setY( nPosy + nCursorY * charHeight )
end
local function updateCursorColor()
if nCursorX >= 1 and nCursorX <= nChars and nCursorY >= 1 and nCursorY <= nRows then
cursor.setColor(lineBuffer[nCursorY][nCursorX][3])
postScreenUpdate()
end
end
local function updateScreen()
for i=1,#lineBuffer do
local row = lineBuffer[i]
for j=1,#row do
local column = row[j]
local pixel = elements[i][j]
pixel[1].setColor(column[2])
pixel[2].setColor(column[3])
pixel[2].setText(column[1])
end
end
end
-- Terminal implementation
local term = {}
function term.delete()
for i=1,#elements do
local elementsRow = elements[i]
for j=1,#elementsRow do
elementsRow[j][1].delete()
elementsRow[j][2].delete()
end
end
cursor.delete()
tEnhancedElements[nTgbid] = nil
end
function term.clear()
for i=1,nRows do
for j=1,nChars do
lineBuffer[i][j] = {" ", nBackgroundColor, nTextColor}
end
end
if visible then
updateScreen()
updateCursorColor()
postScreenUpdate()
end
end
function term.clearLine()
if nCursorY >= 1 and nCursorY <= nRows then
for i=1,nChars do
lineBuffer[nCursorY][i] = {" ", nBackgroundColor, nTextColor}
end
if visible then
updateScreen()
updateCursorColor()
updateCursorPos()
end
end
end
function term.getCursorPos()
return nCursorX, nCursorY
end
function term.setCursorPos( x, y )
nCursorX = math.floor( x )
nCursorY = math.floor( y )
if visible then
updateCursorPos()
updateCursorColor()
postScreenUpdate()
end
end
function term.setCursorBlink( blink )
cursorBlink = blink
cursor.setVisible(blink)
end
function term.isColor()
return true
end
function term.isColour()
return true
end
local function setTextColor( color, tgbcolor )
if tgbcolor then
nTextColor = color
else
if vanillaColors[color] == nil then
error("Illegal color! Set second parameter to true to set custom color!")
end
nTextColor = vanillaColors[color]
end
cursor.setColor(nTextColor)
end
function term.setTextColor( color, tgbcolor )
setTextColor( color, tgbcolor )
end
function term.setTextColour( color, tgbcolor )
setTextColor( color, tgbcolor )
end
local function setBackgroundColor( color, tgbcolor )
if tgbcolor then
nBackgroundColor = color
else
if vanillaColors[color] == nil then
error("Illegal color! Set second parameter to true to set custom color!")
end
nBackgroundColor = vanillaColors[color]
end
end
function term.setBackgroundColor( color, tgbcolor )
setBackgroundColor( color, tgbcolor )
end
function term.setBackgroundColour( color, tgbcolor )
setBackgroundColor( color, tgbcolor )
end
function term.getSize()
return nChars, nRows
end
function term.getVisible()
return visible
end
function term.scroll( n )
for i=1,n do
table.remove(lineBuffer, 1)
local newLine = {}
for j=1,nChars do
newLine[j] = {" ", nBackgroundColor, nTextColor}
end
table.insert(lineBuffer, newLine)
end
if visible then
updateScreen()
updateCursorColor()
updateCursorPos()
postScreenUpdate()
end
end
function term.setVisible( bVis )
visible = bVis
for i=1,#elements do
for j=1,#elements[i] do
elements[i][j][1].setVisible(bVis)
elements[i][j][2].setVisible(bVis)
end
end
if visible then
updateScreen()
updateCursorPos()
updateCursorColor()
postScreenUpdate()
end
end
function term.getType()
return types.terminal
end
function term.setTextProcessing( bProcessing )
textProcessing = bProcessing
end
function term.isTextProcessing()
return textProcessing
end
function term.setDoScreenUpdate( bUpdate )
doScreenUpdates = bUpdate
end
function term.isDoingScreenUpdates()
return doScreenUpdates
end
function term.write( sLine )
local w,h = term.getSize()
local function newLine()
if nCursorY >= h then
scroll(1)
nCursorY = nCursorY - 1
end
term.setCursorPos(1, nCursorY + 1)
end
local function write( data )
local row = lineBuffer[nCursorY]
local cur = 1
while data:len() > 0 do
local column = row[nCursorX]
column[1] = data:sub(1,1)
column[2] = nBackgroundColor
column[3] = nTextColor
nCursorX = nCursorX + 1
data = data:sub(2)
if nCursorX > w then
newLine()
row = lineBuffer[nCursorY]
end
end
end
local data = tostring(sLine)
while data:len() > 0 do
if textProcessing then
-- Don't try to process 1 character long strings
if data:len() == 1 then
write(data)
data = data:sub(2)
end
-- Minecraft text color code &[0-9A-F]
local fcolor = data:match("^&.")
if fcolor then
if data:match("^&[%x&]") then
fcolor = fcolor:sub(2)
if fcolor == "&" then
write(fcolor)
else
term.setTextColor(math.pow(2, tonumber(fcolor, 16)))
end
data = data:sub(3)
else
write(fcolor)
data = data:sub(2)
end
end
-- Minecraft background color code $[0-9A-F]
local bcolor = data:match("^%$.")
if bcolor then
if data:match("^%$[%x%$]") then
bcolor = bcolor:sub(2)
if bcolor == "$" then
write(bcolor)
else
term.setBackgroundColor(math.pow(2, tonumber(bcolor, 16)))
end
else
write(bcolor)
end
data = data:sub(3)
end
local acolor = data:match("^%%.......")
if acolor then
if data:match("^%%%x%x%x%x%x%x[%$&]") then
local target = acolor:sub(8,8)
acolor = acolor:sub(2,7)
if target == "&" then
term.setTextColor(tonumber(acolor, 16), true)
else
term.setBackgroundColor(tonumber(acolor, 16), true)
end
else
write(acolor)