-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframebuffer.lua
More file actions
927 lines (838 loc) · 38.1 KB
/
framebuffer.lua
File metadata and controls
927 lines (838 loc) · 38.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
--- The framebuffer library provides functions to make "window" and "framebuffer"
-- objects. These objects imitate a Terminal object (as returned by
-- @{system.terminal.openterm}) or GFXTerminal object (as returned by
-- @{system.terminal.opengfx}) that may or may not draw to a parent object.
-- Windows and framebuffers may be used as parents to other windows and
-- framebuffers, in addition to the root terminal object.
--
-- A framebuffer object holds its own state, can be redrawn onto the parent
-- terminal even if the parent is changed, can be removed from the parent and
-- used independently, and its contents can be accessed from code. A window
-- object simply changes the coordinates of writing methods, and is entirely
-- dependent on the parent.
--
-- The type of object returned by each function is dependent on the parent
-- passed in. If a Terminal object is passed, a Terminal object is created; if a
-- GFXTerminal object is passed, a GFXTerminal object is created. When creating
-- a framebuffer with no parent, the @{empty} fields are used to specify the type.
--
-- @module system.framebuffer
local expect = require "expect"
local util = require "util"
local framebuffer = {}
--- Empty objects for use when creating framebuffers with no parents.
-- @field text Used to create a text mode Terminal framebuffer
-- @field graphics Used to create a graphics mode GFXTerminal framebuffer
framebuffer.empty = {
text = {}, -- Used to create a text mode Terminal framebuffer
graphics = {}, -- Used to create a graphics mode GFXTerminal framebuffer
}
--- Creates a new window object.
-- @tparam Terminal|GFXTerminal parent The parent object to render to
-- @tparam number x The X coordinate in the parent to start at
-- @tparam number y The Y coordinate in the parent to start at
-- @tparam number width The width of the window
-- @tparam number height The height of the window
-- @treturn Terminal|GFXTerminal The new window object
function framebuffer.window(parent, x, y, width, height)
expect(1, parent, "Terminal", "GFXTerminal")
expect(2, x, "number")
expect(3, y, "number")
expect(4, width, "number")
expect(5, height, "number")
local typ = util.type(parent)
local win = {}
function win.close() end
function win.getSize() return width, height end
function win.getPosition() return x, y end
function win.getParent() return parent end
function win.getPaletteColor(color) return parent.getPaletteColor(color) end
function win.setPaletteColor(color, r, g, b) return parent.setPaletteColor(color, r, g, b) end
win.getPaletteColour = win.getPaletteColor
win.setPaletteColour = win.setPaletteColor
function win.reposition(_x, _y, w, h, p)
expect(1, _x, "number", "nil")
expect(2, _y, "number", "nil")
expect(3, w, "number", "nil")
expect(4, h, "number", "nil")
expect(5, p, "nil", typ)
x = _x or x
y = _y or y
width = w or width
height = h or height
parent = p or parent
end
function win.resize(w, h)
expect(1, w, "number", "nil")
expect(2, h, "number", "nil")
width = w or width
height = h or height
end
function win.reparent(p)
expect(1, p, "nil", typ)
parent = p or parent
end
if typ == "Terminal" then
setmetatable(win, {__name = "Terminal"})
local cx, cy, cblink = 1, 1, parent.getCursorBlink()
local fg, bg = parent.getTextColor(), parent.getBackgroundColor()
function win.write(text)
expect(1, text, "string")
if cy < 1 or cy > height or cx > width or #text == 0 then return end
if cx < 1 then
local d = math.min(1 - cx, #text)
cx = cx + d
if d == #text then return end
text = text:sub(d)
end
local d = math.min(width - cx + 1, #text)
parent.setCursorPos(x+cx-1, y+cy-1)
parent.setTextColor(fg)
parent.setBackgroundColor(bg)
parent.write(text:sub(1, d))
cx = cx + d
end
function win.blit(text, fgs, bgs)
expect(1, text, "string")
expect(2, fgs, "string")
expect(3, bgs, "string")
if #text ~= #fgs or #fgs ~= #bgs then error("Arguments must be the same length", 2) end
if cy < 1 or cy > height or cx > width or #text == 0 then return end
if cx < 1 then
local d = math.min(1 - cx, #text)
cx = cx + d
if d == #text then return end
text, fgs, bgs = text:sub(d), fgs:sub(d), bgs:sub(d)
end
local d = math.min(width - cx + 1, #text)
parent.setCursorPos(x+cx-1, y+cy-1)
parent.blit(text:sub(1, d), fgs:sub(1, d), bgs:sub(1, d))
fg, bg = parent.getTextColor(), parent.getBackgroundColor()
cx = cx + d
end
function win.clear()
parent.setTextColor(fg)
parent.setBackgroundColor(bg)
for yy = 1, height do
parent.setCursorPos(x, y+yy-1)
parent.write((" "):rep(width))
end
end
function win.clearLine()
parent.setTextColor(fg)
parent.setBackgroundColor(bg)
parent.setCursorPos(x, y+cy-1)
parent.write((" "):rep(width))
end
function win.getCursorPos()
return cx, cy
end
function win.setCursorPos(_x, _y)
expect(1, _x, "number")
expect(2, _y, "number")
cx, cy = _x, _y
parent.setCursorPos(x+cx-1, y+cy-1)
end
function win.getCursorBlink()
return cblink
end
function win.setCursorBlink(blink)
expect(1, blink, "boolean")
cblink = blink
parent.setCursorBlink(blink)
end
function win.isColor()
return parent.isColor()
end
function win.scroll(lines)
expect(1, lines, "number")
if math.abs(lines) >= width then
return win.clear()
elseif lines > 0 then
for i = lines + 1, height do
parent.setCursorPos(x, y+i-lines-1)
parent.blit(win.getLine(i))
end
for i = height - lines + 1, height do
parent.setCursorPos(x, y+i-1)
parent.setTextColor(fg)
parent.setBackgroundColor(bg)
parent.write((' '):rep(width))
end
elseif lines < 0 then
for i = 1, height + lines do
parent.setCursorPos(x, y+i-lines-1)
parent.blit(win.getLine(i))
end
for i = 1, -lines do
parent.setCursorPos(x, y+i-1)
parent.setTextColor(fg)
parent.setBackgroundColor(bg)
parent.write((' '):rep(width))
end
else return end
end
function win.getTextColor()
return fg
end
function win.setTextColor(color)
expect(1, color, "number")
fg = color
parent.setTextColor(color)
end
function win.getBackgroundColor()
return bg
end
function win.setBackgroundColor(color)
expect(1, color, "number")
bg = color
parent.setBackgroundColor(color)
end
function win.getLine(_y)
expect(1, _y, "number")
local l1, l2, l3 = parent.getLine(y+_y-1)
if not l1 then return nil end
return l1:sub(x, x+width-1), l2:sub(x, x+width-1), l3:sub(x, x+width-1)
end
function win.restoreCursor()
parent.setCursorPos(x+cx-1, y+cy-1)
parent.setCursorBlink(cblink)
parent.setTextColor(fg)
end
win.isColour = win.isColor
win.getTextColour = win.getTextColor
win.setTextColour = win.setTextColor
win.getBackgroundColour = win.getBackgroundColor
win.setBackgroundColour = win.setBackgroundColor
else
setmetatable(win, {__name = "GFXTerminal"})
function win.clear()
parent.drawPixels(x, y, 15, width, height)
end
function win.getPixel(_x, _y)
return parent.getPixel(x+_x, y+_y)
end
function win.setPixel(_x, _y, color)
return parent.setPixel(x+_x, y+_y, color)
end
function win.getPixels(_x, _y, _width, _height, asStr)
return parent.getPixels(x+_x, y+_y, _width, _height, asStr)
end
function win.drawPixels(_x, _y, data, _width, _height)
return parent.drawPixels(x+_x, y+_y, data, _width, _height)
end
function win.getFrozen()
return parent.getFrozen()
end
function win.setFrozen(frozen)
return parent.setFrozen(frozen)
end
end
return win
end
--- Creates a new framebuffer object.
-- @tparam Terminal|GFXTerminal parent The parent object to render to, or a member of @{empty} to not use a parent
-- @tparam number|nil wx The X coordinate in the parent to start at (`nil` if there's no parent)
-- @tparam number|nil wy The Y coordinate in the parent to start at (`nil` if there's no parent)
-- @tparam number w The width of the framebuffer
-- @tparam number h The height of the framebuffer
-- @tparam[opt] boolean visible Whether the window should be visible upon creation
-- @tparam[opt] boolean transparency Whether the window should support transparency (i.e. replaces space colors with the color behind it)
-- @treturn Terminal|GFXTerminal The new framebuffer object
function framebuffer.framebuffer(parent, wx, wy, w, h, visible, transparency)
local isGFX
if parent == framebuffer.empty.text or parent == framebuffer.empty.graphics then
wx = expect(2, wx, "number", "nil") or 1
wy = expect(3, wy, "number", "nil") or 1
isGFX = parent == framebuffer.empty.graphics
parent = nil
else
expect(1, parent, "Terminal", "GFXTerminal")
expect(2, wx, "number")
expect(3, wy, "number")
isGFX = util.type(parent) == "GFXTerminal"
end
expect(4, w, "number")
expect(5, h, "number")
expect(6, visible, "boolean", "nil")
expect(6, transparency, "boolean", "nil")
if visible == nil then visible = true end
local size = {width = w, height = h}
if isGFX then
local buffer = {
palette = {},
dirtyRects = {},
dirtyPalette = {},
frozen = false,
}
for y = 1, size.height * 9 do buffer[y] = ('\15'):rep(size.width * 6) end
if parent then
for i = 0, 15 do
buffer.palette[i] = {parent.getPaletteColor(i)}
buffer.dirtyPalette[i] = true
end
else
buffer.palette = {
[0] = {0.94117647058824, 0.94117647058824, 0.94117647058824},
{0.94901960784314, 0.69803921568627, 0.2},
{0.89803921568627, 0.49803921568627, 0.84705882352941},
{0.6, 0.69803921568627, 0.94901960784314},
{0.87058823529412, 0.87058823529412, 0.42352941176471},
{0.49803921568627, 0.8, 0.098039215686275},
{0.94901960784314, 0.69803921568627, 0.8},
{0.29803921568627, 0.29803921568627, 0.29803921568627},
{0.6, 0.6, 0.6},
{0.29803921568627, 0.6, 0.69803921568627},
{0.69803921568627, 0.4, 0.89803921568627},
{0.2, 0.4, 0.8},
{0.49803921568627, 0.4, 0.29803921568627},
{0.34117647058824, 0.65098039215686, 0.30588235294118},
{0.8, 0.29803921568627, 0.29803921568627},
{0.066666666666667, 0.066666666666667, 0.066666666666667}
}
for i = 0, 15 do buffer.dirtyPalette[i] = true end
end
for i = 16, 255 do
buffer.palette[i] = {0, 0, 0}
buffer.dirtyPalette[i] = true
end
local win = setmetatable({}, {__name = "GFXTerminal"})
function win.close() end
function win.getSize()
return size.width * 6, size.height * 9
end
function win.clear()
for y = 1, size.height * 9 do buffer[y] = ('\15'):rep(size.width * 6) end
win.redraw(true)
end
function win.getPixel(x, y)
expect(1, x, "number")
expect(2, y, "number")
expect.range(x, 0, size.width * 6 - 1)
expect.range(y, 0, size.height * 9 - 1)
x, y = math.floor(x), math.floor(y)
return buffer[y+1]:byte(x+1)
end
function win.setPixel(x, y, color)
expect(1, x, "number")
expect(2, y, "number")
expect(3, color, "number")
expect.range(x, 0, size.width * 6 - 1)
expect.range(y, 0, size.height * 9 - 1)
expect.range(color, 0, 255)
x, y = math.floor(x), math.floor(y)
buffer[y+1] = buffer[y+1]:sub(1, x) .. string.char(color) .. buffer[y+1]:sub(x + 2)
buffer.dirtyRects[#buffer.dirtyRects+1] = {x = x, y = y, color = color}
win.redraw()
end
function win.getPixels(x, y, width, height, asStr)
expect(1, x, "number")
expect(2, y, "number")
expect(3, width, "number")
expect(4, height, "number")
expect(5, asStr, "boolean", "nil")
expect.range(width, 0)
expect.range(height, 0)
x, y = math.floor(x), math.floor(y)
local t = {}
for py = 1, height do
if asStr then t[py] = buffer[y+py]:sub(x + 1, x + width)
else t[py] = {buffer[y+py]:sub(x + 1, x + width):byte(1, -1)} end
end
return t
end
function win.drawPixels(x, y, data, width, height)
expect(1, x, "number")
expect(2, y, "number")
expect(3, data, "table", "number")
local isn = type(data) == "number"
expect(4, width, "number", not isn and "nil" or nil)
expect(5, height, "number", not isn and "nil" or nil)
expect.range(x, 0, size.width * 6 - 1)
expect.range(y, 0, size.height * 9 - 1)
if width then expect.range(width, 0) end
if height then expect.range(height, 0) end
if isn then expect.range(data, 0, 255) end
if width == 0 or height == 0 then return end
x, y = math.floor(x), math.floor(y)
if width and x + width >= size.width * 6 then width = size.width * 6 - x end
height = height or #data
local rect = {x = x, y = y, width = width, height = height}
for py = 1, height do
if y + py > size.height * 9 then break end
if isn then
local s = string.char(data):rep(width)
buffer[y+py] = buffer[y+py]:sub(1, x) .. s .. buffer[y+py]:sub(x + width + 1)
rect[py] = s
elseif data[py] ~= nil then
if type(data[py]) ~= "table" and type(data[py]) ~= "string" then
error("bad argument #3 to 'drawPixels' (invalid row " .. py .. ")", 2)
end
local width = width or #data[py]
if x + width >= size.width * 6 then width = size.width * 6 - x end
local s
if type(data[py]) == "string" then
s = data[py]
if #s < width then s = s .. ('\15'):rep(width - #s)
elseif #s > width then s = s:sub(1, width) end
else
s = ""
for px = 1, width do s = s .. string.char(data[py][px] or buffer[y+py]:byte(x+px)) end
end
buffer[y+py] = buffer[y+py]:sub(1, x) .. s .. buffer[y+py]:sub(x + width + 1)
rect[py] = s
end
end
buffer.dirtyRects[#buffer.dirtyRects+1] = rect
win.redraw()
end
function win.getFrozen()
return buffer.frozen
end
function win.setFrozen(f)
expect(1, f, "boolean")
buffer.frozen = f
win.redraw()
end
function win.getPaletteColor(color)
expect(1, color, "number")
expect.range(color, 0, 255)
return table.unpack(buffer.palette[color])
end
function win.setPaletteColor(color, r, g, b)
expect(1, color, "number")
expect(2, r, "number")
if g == nil and b == nil then r, g, b = bit32.band(bit32.rshift(r, 16), 0xFF) / 255, bit32.band(bit32.rshift(r, 8), 0xFF) / 255, bit32.band(r, 0xFF) / 255 end
expect(3, g, "number")
expect(4, b, "number")
expect.range(r, 0, 1)
expect.range(g, 0, 1)
expect.range(b, 0, 1)
expect.range(color, 0, 255)
buffer.palette[color] = {r, g, b}
buffer.dirtyPalette[color] = true
win.redraw()
end
function win.getPosition()
return wx, wy
end
function win.getParent()
return parent
end
function win.reposition(x, y, w, h, p)
expect(1, x, "number", "nil")
expect(2, y, "number", "nil")
wx = x or wx
wy = y or wy
if p then win.reparent(p) end
if w or h then return win.resize(w, h) end
end
function win.resize(width, height)
expect(1, width, "number", "nil")
expect(2, height, "number", "nil")
if width > size.width then
for y = 1, size.height * 9 do
buffer[y] = buffer[y] .. ('\15'):rep((width - size.width) * 6)
end
buffer.dirtyRects[#buffer.dirtyRects+1] = {
x = size.width * 6 + 1, y = 1,
width = (width - size.width) * 6, height = size.height * 9
}
elseif width < size.width then
for y = 1, size.height * 9 do
buffer[y] = buffer[y]:sub(1, width * 6)
end
end
size.width = width
if height > size.height then
for y = size.height * 9 + 1, height * 9 do
buffer[y] = ('\15'):rep(width * 6)
end
buffer.dirtyRects[#buffer.dirtyRects+1] = {
x = 1, y = size.height * 9 + 1,
width = size.width * 6, height = (height - size.height) * 9
}
elseif height < size.height then
for y = height * 9 + 1, size.height * 9 do
buffer[y] = nil
end
end
size.height = height
end
function win.reparent(p)
expect(1, p, "GFXTerminal", "nil")
parent = p
win.redraw()
end
function win.redraw(full)
if not parent or not visible then return end
if parent.setFrozen then parent.setFrozen(true) end
if full then
parent.clear()
parent.drawPixels(0, 0, buffer)
for i = 0, 255 do parent.setPaletteColor(i, buffer.palette[i][1], buffer.palette[i][2], buffer.palette[i][3]) end
else
if buffer.frozen then
if parent.setFrozen then parent.setFrozen(false) end
return
end
for _, v in ipairs(buffer.dirtyRects) do
if v.color then parent.setPixel(v.x, v.y, v.color, v.width, v.height)
else parent.drawPixels(v.x, v.y, v) end
end
for i in pairs(buffer.dirtyPalette) do parent.setPaletteColor(i, buffer.palette[i][1], buffer.palette[i][2],buffer.palette[i][3]) end
end
if parent.setFrozen then parent.setFrozen(false) end
buffer.dirtyRects, buffer.dirtyPalette = {}, {}
end
function win.isVisible()
return visible
end
function win.setVisible(v)
expect(1, v, "boolean")
visible = v
win.redraw()
end
win.getPaletteColour = win.getPaletteColor
win.setPaletteColour = win.setPaletteColor
win.redraw()
return win
else
local buffer = {
cursor = {x = 1, y = 1},
cursorBlink = false,
colors = {fg = '0', bg = 'f'},
palette = {},
dirtyLines = {},
dirtyPalette = {},
}
for y = 1, size.height do
buffer[y] = {(' '):rep(size.width), ('0'):rep(size.width), ('f'):rep(size.width)}
buffer.dirtyLines[y] = true
end
if parent then
for i = 0, 15 do
buffer.palette[i] = {parent.getPaletteColor(i)}
buffer.dirtyPalette[i] = true
end
else
buffer.palette = {
[0] = {0.94117647058824, 0.94117647058824, 0.94117647058824},
{0.94901960784314, 0.69803921568627, 0.2},
{0.89803921568627, 0.49803921568627, 0.84705882352941},
{0.6, 0.69803921568627, 0.94901960784314},
{0.87058823529412, 0.87058823529412, 0.42352941176471},
{0.49803921568627, 0.8, 0.098039215686275},
{0.94901960784314, 0.69803921568627, 0.8},
{0.29803921568627, 0.29803921568627, 0.29803921568627},
{0.6, 0.6, 0.6},
{0.29803921568627, 0.6, 0.69803921568627},
{0.69803921568627, 0.4, 0.89803921568627},
{0.2, 0.4, 0.8},
{0.49803921568627, 0.4, 0.29803921568627},
{0.34117647058824, 0.65098039215686, 0.30588235294118},
{0.8, 0.29803921568627, 0.29803921568627},
{0.066666666666667, 0.066666666666667, 0.066666666666667}
}
for i = 0, 15 do buffer.dirtyPalette[i] = true end
end
local win = setmetatable({}, {__name = "Terminal"})
function win.close() end
function win.write(text)
text = tostring(text)
expect(1, text, "string")
if buffer.cursor.y < 1 or buffer.cursor.y > size.height then return
elseif buffer.cursor.x > size.width or buffer.cursor.x + #text < 1 then
buffer.cursor.x = buffer.cursor.x + #text
return
elseif buffer.cursor.x < 1 then
text = text:sub(-buffer.cursor.x + 2)
buffer.cursor.x = 1
end
local ntext = #text
if buffer.cursor.x + #text > size.width then text = text:sub(1, size.width - buffer.cursor.x + 1) end
buffer[buffer.cursor.y][1] = buffer[buffer.cursor.y][1]:sub(1, buffer.cursor.x - 1) .. text .. buffer[buffer.cursor.y][1]:sub(buffer.cursor.x + #text)
buffer[buffer.cursor.y][2] = buffer[buffer.cursor.y][2]:sub(1, buffer.cursor.x - 1) .. buffer.colors.fg:rep(#text) .. buffer[buffer.cursor.y][2]:sub(buffer.cursor.x + #text)
buffer[buffer.cursor.y][3] = buffer[buffer.cursor.y][3]:sub(1, buffer.cursor.x - 1) .. buffer.colors.bg:rep(#text) .. buffer[buffer.cursor.y][3]:sub(buffer.cursor.x + #text)
buffer.cursor.x = buffer.cursor.x + ntext
buffer.dirtyLines[buffer.cursor.y] = true
win.redraw()
end
function win.blit(text, fg, bg)
text = tostring(text)
expect(1, text, "string")
expect(2, fg, "string")
expect(3, bg, "string")
if #text ~= #fg or #fg ~= #bg then error("Arguments must be the same length", 2) end
if buffer.cursor.y < 1 or buffer.cursor.y > size.height then return
elseif buffer.cursor.x > size.width or buffer.cursor.x < 1 - #text then
buffer.cursor.x = buffer.cursor.x + #text
win.redraw()
return
elseif buffer.cursor.x < 1 then
text, fg, bg = text:sub(-buffer.cursor.x + 2), fg:sub(-buffer.cursor.x + 2), bg:sub(-buffer.cursor.x + 2)
buffer.cursor.x = 1
end
local ntext = #text
if buffer.cursor.x + #text > size.width then text, fg, bg = text:sub(1, size.width - buffer.cursor.x + 1), fg:sub(1, size.width - buffer.cursor.x + 1), bg:sub(1, size.width - buffer.cursor.x + 1) end
--[[if transparency and (fg:find("[! ]") or bg:find("[! ]")) then
local prevtext, prevfg, prevbg = parent.getLine(wy + buffer.cursor.y - 1)
if prevtext then
prevtext, prevfg, prevbg =
prevtext:sub(math.max(wx + buffer.cursor.x - 1, 1), math.max(wx + buffer.cursor.x + size.width - 2, 1)),
prevfg:sub(math.max(wx + buffer.cursor.x - 1, 1), math.max(wx + buffer.cursor.x + size.width - 2, 1)),
prevbg:sub(math.max(wx + buffer.cursor.x - 1, 1), math.max(wx + buffer.cursor.x + size.width - 2, 1))
if wx + buffer.cursor.x - 1 < 1 then
local add = (" "):rep(1 - (wx + buffer.cursor.x - 1))
prevtext, prevfg, prevbg = add .. prevtext, add .. prevfg, add .. prevbg
end
if #prevtext < #text then
local add = (" "):rep(#text - #prevtext)
prevtext, prevfg, prevbg = prevtext .. add, prevfg .. add, prevbg .. add
end
text = text:gsub("()\0", function(n) return prevtext:sub(n, n) end)
fg = fg:gsub("() ", function(n) return prevfg:sub(n, n) end):gsub("()!", function(n) return prevbg:sub(n, n) end)
bg = bg:gsub("() ", function(n) return prevbg:sub(n, n) end):gsub("()!", function(n) return prevfg:sub(n, n) end)
end
end]]
buffer[buffer.cursor.y][1] = buffer[buffer.cursor.y][1]:sub(1, buffer.cursor.x - 1) .. text .. buffer[buffer.cursor.y][1]:sub(buffer.cursor.x + #text)
buffer[buffer.cursor.y][2] = buffer[buffer.cursor.y][2]:sub(1, buffer.cursor.x - 1) .. fg .. buffer[buffer.cursor.y][2]:sub(buffer.cursor.x + #fg)
buffer[buffer.cursor.y][3] = buffer[buffer.cursor.y][3]:sub(1, buffer.cursor.x - 1) .. bg .. buffer[buffer.cursor.y][3]:sub(buffer.cursor.x + #bg)
buffer.cursor.x = buffer.cursor.x + ntext
buffer.dirtyLines[buffer.cursor.y] = true
win.redraw()
end
function win.clear()
for y = 1, size.height do
buffer[y] = {(' '):rep(size.width), buffer.colors.fg:rep(size.width), buffer.colors.bg:rep(size.width)}
buffer.dirtyLines[y] = true
end
win.redraw()
end
function win.clearLine()
if buffer.cursor.y >= 1 and buffer.cursor.y <= size.height then
buffer[buffer.cursor.y] = {(' '):rep(size.width), buffer.colors.fg:rep(size.width), buffer.colors.bg:rep(size.width)}
buffer.dirtyLines[buffer.cursor.y] = true
win.redraw()
end
end
function win.getCursorPos()
return buffer.cursor.x, buffer.cursor.y
end
function win.setCursorPos(cx, cy)
expect(1, cx, "number")
expect(2, cy, "number")
if cx == buffer.cursor.x and cy == buffer.cursor.y then return end
buffer.cursor.x, buffer.cursor.y = math.floor(cx), math.floor(cy)
win.redraw()
end
function win.getCursorBlink()
return buffer.cursorBlink
end
function win.setCursorBlink(b)
expect(1, b, "boolean")
buffer.cursorBlink = b
win.redraw()
end
function win.isColor()
return true
end
function win.getSize()
return size.width, size.height
end
function win.scroll(lines)
expect(1, lines, "number")
if math.abs(lines) >= size.width then
for y = 1, size.height do buffer[y] = {(' '):rep(size.width), buffer.colors.fg:rep(size.width), buffer.colors.bg:rep(size.width)} end
elseif lines > 0 then
for i = lines + 1, size.height do buffer[i - lines] = buffer[i] end
for i = size.height - lines + 1, size.height do buffer[i] = {(' '):rep(size.width), buffer.colors.fg:rep(size.width), buffer.colors.bg:rep(size.width)} end
elseif lines < 0 then
for i = 1, size.height + lines do buffer[i - lines] = buffer[i] end
for i = 1, -lines do buffer[i] = {(' '):rep(size.width), buffer.colors.fg:rep(size.width), buffer.colors.bg:rep(size.width)} end
else return end
for i = 1, size.height do buffer.dirtyLines[i] = true end
win.redraw()
end
function win.getTextColor()
return tonumber(buffer.colors.fg, 16)
end
function win.setTextColor(color)
expect(1, color, "number")
expect.range(color, 0, 15)
buffer.colors.fg = ("%x"):format(math.floor(color))
end
function win.getBackgroundColor()
return tonumber(buffer.colors.bg, 16)
end
function win.setBackgroundColor(color)
expect(1, color, "number")
expect.range(color, 0, 15)
buffer.colors.bg = ("%x"):format(math.floor(color))
end
function win.getPaletteColor(color)
expect(1, color, "number")
expect.range(color, 0, 15)
return table.unpack(buffer.palette[math.floor(color)])
end
function win.setPaletteColor(color, r, g, b)
expect(1, color, "number")
expect(2, r, "number")
if g == nil and b == nil then r, g, b = bit32.band(bit32.rshift(r, 16), 0xFF) / 255, bit32.band(bit32.rshift(r, 8), 0xFF) / 255, bit32.band(r, 0xFF) / 255 end
expect(3, g, "number")
expect(4, b, "number")
expect.range(color, 0, 15)
if r < 0 or r > 1 then error("bad argument #2 (value out of range)", 2) end
if g < 0 or g > 1 then error("bad argument #3 (value out of range)", 2) end
if b < 0 or b > 1 then error("bad argument #4 (value out of range)", 2) end
buffer.palette[math.floor(color)] = {r, g, b}
buffer.dirtyPalette[math.floor(color)] = true
win.redraw()
end
function win.getLine(y)
expect(1, y, "number")
local l = buffer[y]
if l then return table.unpack(l, 1, 3) end
end
function win.getPosition()
return wx, wy
end
function win.getParent()
return parent
end
function win.reposition(x, y, w, h, p)
expect(1, x, "number", "nil")
expect(2, y, "number", "nil")
wx = x or wx
wy = y or wy
if p then win.reparent(p) end
if x or y then win.redraw(true) end
if w or h then return win.resize(w, h) end
end
function win.resize(width, height)
expect(1, width, "number", "nil")
expect(2, height, "number", "nil")
if width > size.width then
for y = 1, size.height do
buffer[y][1] = buffer[y][1] .. (' '):rep(width - size.width)
buffer[y][2] = buffer[y][2] .. buffer.colors.fg:rep(width - size.width)
buffer[y][3] = buffer[y][3] .. buffer.colors.bg:rep(width - size.width)
buffer.dirtyLines[y] = true
end
elseif width < size.width then
for y = 1, size.height do
buffer[y][1] = buffer[y][1]:sub(1, width)
buffer[y][2] = buffer[y][2]:sub(1, width)
buffer[y][3] = buffer[y][3]:sub(1, width)
end
end
size.width = width
if height > size.height then
for y = size.height + 1, height do
buffer[y] = {(' '):rep(width), buffer.colors.fg:rep(width), buffer.colors.bg:rep(width)}
buffer.dirtyLines[y] = true
end
elseif height < size.height then
for y = height + 1, size.height do
buffer[y] = nil
end
end
size.height = height
end
function win.reparent(p)
expect(1, p, "Terminal", "nil")
parent = p
win.redraw()
end
function win.redraw(full)
if not parent or not visible then return end
parent.setCursorBlink(false)
if full then
for y = 1, size.height do
parent.setCursorPos(wx, wy+y-1)
if transparency and (buffer[y][2]:find("[! ]") or buffer[y][3]:find("[! ]")) then
local prevtext, prevfg, prevbg = parent.getLine(wy+y-1)
if prevtext then
prevtext, prevfg, prevbg =
prevtext:sub(math.max(wx, 1), math.max(wx + size.width - 1, 1)),
prevfg:sub(math.max(wx, 1), math.max(wx + size.width - 1, 1)),
prevbg:sub(math.max(wx, 1), math.max(wx + size.width - 1, 1))
if wx < 1 then
local add = (" "):rep(1 - wx)
prevtext, prevfg, prevbg = add .. prevtext, add .. prevfg, add .. prevbg
end
if #prevtext < #buffer[y][1] then
local add = (" "):rep(#buffer[y][1] - #prevtext)
prevtext, prevfg, prevbg = prevtext .. add, prevfg .. add, prevbg .. add
end
local text = buffer[y][1]:gsub("()\0", function(n) return prevtext:sub(n, n) end)
local fg = buffer[y][2]:gsub("() ", function(n) return prevfg:sub(n, n) end):gsub("()!", function(n) return prevbg:sub(n, n) end)
local bg = buffer[y][3]:gsub("() ", function(n) return prevbg:sub(n, n) end):gsub("()!", function(n) return prevfg:sub(n, n) end)
parent.blit(text, fg, bg)
end
else
parent.blit(buffer[y][1], buffer[y][2], buffer[y][3])
end
end
for i = 0, 15 do parent.setPaletteColor(i, buffer.palette[i][1], buffer.palette[i][2], buffer.palette[i][3]) end
else
for y in pairs(buffer.dirtyLines) do
parent.setCursorPos(wx, wy+y-1)
if #buffer[y][1] ~= #buffer[y][2] or #buffer[y][2] ~= #buffer[y][3] then error(debug.traceback("Internal error: Invalid lengths"), 2) end
if transparency and (buffer[y][2]:find("[! ]") or buffer[y][3]:find("[! ]")) then
local prevtext, prevfg, prevbg = parent.getLine(wy+y-1)
if prevtext then
prevtext, prevfg, prevbg =
prevtext:sub(math.max(wx, 1), math.max(wx + size.width - 1, 1)),
prevfg:sub(math.max(wx, 1), math.max(wx + size.width - 1, 1)),
prevbg:sub(math.max(wx, 1), math.max(wx + size.width - 1, 1))
if wx < 1 then
local add = (" "):rep(1 - wx)
prevtext, prevfg, prevbg = add .. prevtext, add .. prevfg, add .. prevbg
end
if #prevtext < #buffer[y][1] then
local add = (" "):rep(#buffer[y][1] - #prevtext)
prevtext, prevfg, prevbg = prevtext .. add, prevfg .. add, prevbg .. add
end
local text = buffer[y][1]:gsub("()\0", function(n) return prevtext:sub(n, n) or "\0" end)
local fg = buffer[y][2]:gsub("() ", function(n) return prevfg:sub(n, n) end):gsub("()!", function(n) return prevbg:sub(n, n) end)
local bg = buffer[y][3]:gsub("() ", function(n) return prevbg:sub(n, n) end):gsub("()!", function(n) return prevfg:sub(n, n) end)
parent.blit(text, fg, bg)
end
else
parent.blit(buffer[y][1], buffer[y][2], buffer[y][3])
end
end
for i in pairs(buffer.dirtyPalette) do parent.setPaletteColor(i, buffer.palette[i][1], buffer.palette[i][2], buffer.palette[i][3]) end
end
parent.setCursorPos(wx+buffer.cursor.x-1, wy+buffer.cursor.y-1)
parent.setCursorBlink(buffer.cursorBlink)
parent.setTextColor(win.getTextColor())
buffer.dirtyLines, buffer.dirtyPalette = {}, {}
end
function win.restoreCursor()
if not parent or not visible then return end
parent.setCursorPos(wx+buffer.cursor.x-1, wy+buffer.cursor.y-1)
parent.setCursorBlink(buffer.cursorBlink)
parent.setTextColor(win.getTextColor())
end
function win.isVisible()
return visible
end
function win.setVisible(v, r)
expect(1, v, "boolean")
visible = v
if v and r ~= false then win.redraw() end
end
win.isColour = win.isColor
win.getTextColour = win.getTextColor
win.setTextColour = win.setTextColor
win.getBackgroundColour = win.getBackgroundColor
win.setBackgroundColour = win.setBackgroundColor
win.getPaletteColour = win.getPaletteColor
win.setPaletteColour = win.setPaletteColor
win.redraw()
return win
end
end
return framebuffer