Files
2026-04-30 15:09:56 +00:00

1 line
5.0 KiB
Lua

local a={["units"]={},["defaultBackgroundColor"]=0,["defaultForegroundColor"]=16777215,["displayWidth"]=0,["displayHeight"]=0,["charWidth"]=0,["charHeight"]=0,["backgroundColor"]=16777215}local b=import("component")local b=b.gpu;local c={}local d={}local e=nil;function a.units.charToBraille(a,b)return a*2,b*4 end;function a.units.brailleToChar(a,b)return math.ceil(a/2),math.ceil(b/4)end;function a.init(d,f,g)if d==nil and f==nil then d,f=b.getResolution()end;a.charWidth=d;a.charHeight=f;d,f=a.units.charToBraille(d,f)g=g or a.defaultBackgroundColor;if g~=0 then for a=1,d*f do c[a]=g end end;a.displayWidth=d;a.displayHeight=f;a.backgroundColor=g;pcall(function()e=b.allocateBuffer()end)a.clear()end;function a.set(b,e,f)if b<1 or b>a.displayWidth or e<1 or e>a.displayHeight then return false end;f=f or a.defaultForegroundColor;local g=b+e*a.displayWidth;c[g]=f;local a=math.floor((b-1)/2)+math.floor((e-1)/4)*a.charWidth+1;d[a]=true;return true end;function a.get(b,d)local b=b+d*a.displayWidth;return c[b]or a.backgroundColor end;local function f(a)local b={}for c=1,#a do local a=a[c]if b[a]==nil then b[a]=1 else b[a]=b[a]+1 end end;return b end;local function g(a)local b={}for a,c in pairs(a)do table.insert(b,{a,c})end;table.sort(b,function(a,b)return a[2]>b[2]end)for a=1,#b do b[a]=b[a][1]end;return b end;local function h(a,b)return((a>>16)&255)-((b>>16)&255)+((a>>8)&255)-((b>>8)&255)+(a&255)-(b&255)end;local function i(a)local b=g(f(a))for c=1,#a do local d=a[c]if d==b[1]then a[c]=0;goto continue elseif d==b[2]then a[c]=1;goto continue else if h(d,b[1])<h(d,b[2])then a[c]=0 else a[c]=1 end end::continue::end;return a,b[1]or 0,b[2]or 0 end;local function f(a)local b=10240;for c=1,8 do b=b|a[c]<<(c-1)end;if b==10240 then return" "end;return utf8.char(b)end;function a.update()if e~=nil then b.setActiveBuffer(e)end;for c=1,a.displayHeight,4 do for e=1,a.displayWidth,2 do local g=math.floor(e/2)+math.floor(c/4)*a.charWidth+1;if d[g]then local h={a.get(e,c),a.get(e,c+1),a.get(e,c+2),a.get(e+1,c),a.get(e+1,c+1),a.get(e+1,c+2),a.get(e,c+3),a.get(e+1,c+3)}local j=nil;local k=nil;h,j,k=i(h)cx,cy=a.units.brailleToChar(e,c)b.setBackground(j)b.setForeground(k)b.set(cx,cy,f(h))d[g]=false end end end;if e~=nil then b.bitblt()b.setActiveBuffer(0)end end;function a.clear()if e~=nil then b.setActiveBuffer(e)end;local d=a.backgroundColor;b.setBackground(d)b.fill(1,1,a.displayWidth,a.displayHeight," ")c={}end;function a.free()if e==nil then return true else return b.freeBuffer(e)end end;function a.drawLine(b,c,d,e,f)b,c,d,e=math.floor(b),math.floor(c),math.floor(d),math.floor(e)local g=math.abs(d-b)local h=math.abs(e-c)local i=b<d and 1 or-1;local j=c<e and 1 or-1;local k=g-h;while true do a.set(b,c,f)if b==d and c==e then break end;local a=2*k;if a>-h then k=k-h;b=b+i end;if a<g then k=k+g;c=c+j end end end;function a.drawRect(b,c,d,e,f)b,c,d,e=math.floor(b),math.floor(c),math.floor(d),math.floor(e)if b>d then b,d=d,b end;if c>e then c,e=e,c end;for b=b,d do a.set(b,c,f)a.set(b,e,f)end;for c=c+1,e-1 do a.set(b,c,f)a.set(d,c,f)end end;function a.fillRect(b,c,d,e,f)b,c,d,e=math.floor(b),math.floor(c),math.floor(d),math.floor(e)if b>d then b,d=d,b end;if c>e then c,e=e,c end;for b=b,d do for c=c,e do a.set(b,c,f)end end end;function a.drawCircle(b,c,d,e)b=math.floor(b)c=math.floor(c)d=math.floor(d)local f=0;local g=d;local d=3-2*d;while g>=f do a.set(b+f,c+g,e)a.set(b-f,c+g,e)a.set(b+f,c-g,e)a.set(b-f,c-g,e)a.set(b+g,c+f,e)a.set(b-g,c+f,e)a.set(b+g,c-f,e)a.set(b-g,c-f,e)if d<0 then d=d+4*f+6 else d=d+4*(f-g)+10;g=g-1 end;f=f+1 end end;function a.drawEllipse(b,c,d,e,f)if b>d then b,d=d,b end;if c>e then c,e=e,c end;local g=math.floor((b+d)/2)local h=math.floor((c+e)/2)local b=math.floor((d-b)/2)local c=math.floor((e-c)/2)if b<=0 or c<=0 then return end;if b==c then a.drawCircle(g,h,b,f)return end;if b<=1 and c<=1 then a.set(g,h,f)return elseif b<=1 then for b=h-c,h+c do a.set(g,b,f)end;return elseif c<=1 then for b=g-b,g+b do a.set(b,h,f)end;return end;local d=0;local e=c;local b=b*b;local i=c*c;local c=i-(b*c)+(0.25*b)local j=2*i*d;local k=2*b*e;while j<k do a.set(g+d,h+e,f)a.set(g-d,h+e,f)a.set(g+d,h-e,f)if c<0 then d=d+1;j=j+(2*i)c=c+j+i else d=d+1;e=e-1;j=j+(2*i)k=k-(2*b)c=c+j-k+i end end;local c=i*(d+0.5)*(d+0.5)+b*(e-1)*(e-1)-b*i;while e>=0 do a.set(g+d,h+e,f)a.set(g-d,h+e,f)a.set(g+d,h-e,f)a.set(g-d,h-e,f)if c>0 then e=e-1;k=k-(2*b)c=c-k+b else e=e-1;d=d+1;j=j+(2*i)k=k-(2*b)c=c+j-k+b end end end;function a.fillCircle(b,c,d,e)b,c=math.floor(b+0.5),math.floor(c+0.5)d=math.floor(d+0.5)if d<=0 then return end;local f,g=b-d,b+d;local h,i=c-d,c+d;for h=h,i do for f=f,g do local b,c=f-b,h-c;local b=b*b+c*c;if b<=d*d then a.set(f,h,e)end end end end;function a.fillEllipse(b,c,d,e,f)local g=(b+d)/2;local h=(c+e)/2;local b=math.abs(d-b)/2;local c=math.abs(e-c)/2;g=math.floor(g+0.5)h=math.floor(h+0.5)b=math.floor(b+0.5)c=math.floor(c+0.5)if b<=0 or c<=0 then return end;if b==c then a.fillCircle(g,h,b,f)return end;local d=g-b;local e=g+b;local i=h-c;local j=h+c;for i=i,j do for d=d,e do local e=d-g;local g=i-h;local b=(e*e)/(b*b)+(g*g)/(c*c)if b<=1 then a.set(d,i,f)end end end end;return a