-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixelbox_lite.lua
185 lines (158 loc) · 4.53 KB
/
pixelbox_lite.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
--[[
* api for easy interaction with drawing characters
* single file implementation of GuiH pixelbox api
* this version is very fast and ment for implementing into other apis
* OUTDATED VERSION, this only exists for the sake of not breaking stuff
GO GET LATEST AT https://github.com/9551-Dev/pixelbox_lite
]]
local PIXELBOX = {}
local OBJECT = {}
local t_sort,t_cat,s_char = table.sort,table.concat,string.char
local function sort(a,b) return a[2] > b[2] end
local distances = {
{5,256,16,8,64,32},
{4,16,16384,256,128},
[4] ={4,64,1024,256,128},
[8] ={4,512,2048,256,1},
[16] ={4,2,16384,256,1},
[32] ={4,8192,4096,256,1},
[64] ={4,4,1024,256,1},
[128] ={6,32768,256,1024,2048,4096,16384},
[256] ={6,1,128,2,512,4,8192},
[512] ={4,8,2048,256,128},
[1024] ={4,4,64,128,32768},
[2048] ={4,512,8,128,32768},
[4096] ={4,8192,32,128,32768},
[8192] ={3,32,4096,256128},
[16384]={4,2,16,128,32768},
[32768]={5,128,1024,2048,4096,16384}
}
local to_blit = {}
for i = 0, 15 do
to_blit[2^i] = ("%x"):format(i)
end
function PIXELBOX.RESTORE(BOX,color)
local bc = {}
for y=1,BOX.height*3 do
for x=1,BOX.width*2 do
if not bc[y] then bc[y] = {} end
bc[y][x] = color
end
end
BOX.CANVAS = bc
end
local function build_drawing_char(a,b,c,d,e,f)
local arr = {a,b,c,d,e,f}
local c_types = {}
local sortable = {}
local ind = 0
for i=1,6 do
local c = arr[i]
if not c_types[c] then
ind = ind + 1
c_types[c] = {0,ind}
end
local t = c_types[c]
local t1 = t[1] + 1
t[1] = t1
sortable[t[2]] = {c,t1}
end
local n = #sortable
while n > 2 do
t_sort(sortable,sort)
local bit6 = distances[sortable[n][1]]
local index,run = 1,false
local nm1 = n - 1
for i=2,bit6[1] do
if run then break end
local tab = bit6[i]
for j=1,nm1 do
if sortable[j][1] == tab then
index = j
run = true
break
end
end
end
local from,to = sortable[n][1],sortable[index][1]
for i=1,6 do
if arr[i] == from then
arr[i] = to
local sindex = sortable[index]
sindex[2] = sindex[2] + 1
end
end
sortable[n] = nil
n = n - 1
end
local n = 128
local a6 = arr[6]
if arr[1] ~= a6 then n = n + 1 end
if arr[2] ~= a6 then n = n + 2 end
if arr[3] ~= a6 then n = n + 4 end
if arr[4] ~= a6 then n = n + 8 end
if arr[5] ~= a6 then n = n + 16 end
if sortable[1][1] == arr[6] then
return s_char(n),sortable[2][1],arr[6]
else
return s_char(n),sortable[1][1],arr[6]
end
end
function OBJECT:render()
local t = self.term
local blit_line,set_cursor = t.blit,t.setCursorPos
local w_double = self.width*2
local canv = self.CANVAS
local sy = 0
for y=1,self.height*3,3 do
sy = sy + 1
local layer_1 = canv[y]
local layer_2 = canv[y+1]
local layer_3 = canv[y+2]
local char_line,fg_line,bg_line = {},{},{}
local n = 0
for x=1,w_double,2 do
local xp1 = x+1
local b11,b21,b12,b22,b13,b23 =
layer_1[x],layer_1[xp1],
layer_2[x],layer_2[xp1],
layer_3[x],layer_3[xp1]
local char,fg,bg = " ",1,b11
if not (b21 == b11
and b12 == b11
and b22 == b11
and b13 == b11
and b23 == b11) then
char,fg,bg = build_drawing_char(b11,b21,b12,b22,b13,b23)
end
n = n + 1
char_line[n] = char
fg_line [n] = to_blit[fg]
bg_line [n] = to_blit[bg]
end
set_cursor(1,sy)
blit_line(
t_cat(char_line,""),
t_cat(fg_line,""),
t_cat(bg_line,"")
)
end
end
function OBJECT:clear(color)
PIXELBOX.RESTORE(self,color)
end
function OBJECT:set_pixel(x,y,color)
self.CANVAS[y][x] = color
end
function PIXELBOX.new(terminal,bg)
local bg = bg or terminal.getBackgroundColor() or colors.black
local BOX = {}
local w,h = terminal.getSize()
BOX.term = terminal
setmetatable(BOX,{__index = OBJECT})
BOX.width = w
BOX.height = h
PIXELBOX.RESTORE(BOX,bg)
return BOX
end
return PIXELBOX