Advertisement
9551

charH

Oct 8th, 2021 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.38 KB | None | 0 0
  1. local expect = require("cc.expect").expect
  2. local createCanvas = function(termObj)
  3.     expect(1,termObj,"table")
  4.     local index = {}
  5.     local function toBlit(t)
  6.         if t[6] == 1 then for i = 1, 5 do t[i] = 1-t[i] end end
  7.         local n = 128
  8.         for i = 0, 4 do n = n + t[i+1]*2^i end
  9.         return n, t[6] == 1
  10.     end
  11.     local drawInternal = function(char)
  12.         char.draw = {toBlit(char)}
  13.     end
  14.     function index:print(x,y)
  15.         expect(0,self,"table")
  16.         expect(1,x,"number")
  17.         expect(2,y,"number")
  18.         local term = self.disp
  19.         local bc = term.getBackgroundColor()
  20.         local tc = term.getTextColor()
  21.         local temp = x%2==0
  22.         local charX = 1
  23.         local charY = (y - 1) % 3+1
  24.         local ox = math.ceil(x/2)
  25.         local oy = math.ceil(y/3)
  26.         if temp then charX = 2 end
  27.         if not self.map[ox] then self.map[ox] = {} end
  28.         if not self.map[ox][oy] then
  29.             self.map[ox][oy] = {0,0,0,0,0,0}
  30.             local exchange = self.map[ox][oy]
  31.             local i = ((charY-1) * 2) + charX
  32.             exchange[i] = 1
  33.             drawInternal(exchange)
  34.         else
  35.             local exchange = self.map[ox][oy]
  36.             local i = ((charY-1) * 2) + charX
  37.             exchange[i] = 1
  38.             self.map[ox][oy] = exchange
  39.             drawInternal(self.map[ox][oy])
  40.         end
  41.         if self.map[ox][oy].draw[2] then
  42.             term.setTextColor(bc)
  43.             term.setBackgroundColor(tc)
  44.         end
  45.         term.setCursorPos(ox,oy)
  46.         term.write(string.char(self.map[ox][oy].draw[1]))
  47.         term.setBackgroundColor(bc)
  48.         term.setTextColor(tc)
  49.     end
  50.     function index:create(x,y)
  51.         expect(0,self,"table")
  52.         expect(1,x,"number")
  53.         expect(2,y,"number")
  54.         local temp = x%2==0
  55.         local charX = 1
  56.         local charY = (y - 1) % 3+1
  57.         local ox = math.ceil(x/2)
  58.         local oy = math.ceil(y/3)
  59.         if temp then charX = 2 end
  60.         if not self.map[ox] then self.map[ox] = {} end
  61.         if not self.map[ox][oy] then
  62.             self.map[ox][oy] = {0,0,0,0,0,0}
  63.             local exchange = self.map[ox][oy]
  64.             local i = ((charY-1) * 2) + charX
  65.             exchange[i] = 1
  66.             drawInternal(exchange)
  67.         else
  68.             local exchange = self.map[ox][oy]
  69.             local i = ((charY-1) * 2) + charX
  70.             exchange[i] = 1
  71.             self.map[ox][oy] = exchange
  72.             drawInternal(self.map[ox][oy])
  73.         end
  74.     end
  75.     function index:remove(x,y)
  76.         expect(0,self,"table")
  77.         expect(1,x,"number")
  78.         expect(2,y,"number")
  79.         local temp = x%2==0
  80.         local charX = 1
  81.         local charY = (y - 1) % 3+1
  82.         if temp then charX = 2 end
  83.         local ox = math.ceil(x/2)
  84.         local oy = math.ceil(y/3)
  85.         if not self[ox] then self[ox] = {} end
  86.         if not self.map[ox][oy] then
  87.             self.map[ox] = {}
  88.             self.map[ox][oy] = {0,0,0,0,0,0}
  89.         else
  90.             local exchange = self.map[ox][oy]
  91.             local i = ((charY-1) * 2) + charX
  92.             exchange[i] = 0
  93.             self.map[ox][oy] = exchange
  94.             drawInternal(self.map[ox][oy])
  95.         end
  96.     end
  97.     function index:clear()
  98.         expect(0,self,"table")
  99.         self.map = {}
  100.     end
  101.     function index:getSize()
  102.         expect(0,self,"table")
  103.         local x,y = self.disp.getSize()
  104.         return x*2,y*3
  105.     end
  106.     function index:draw()
  107.         expect(0,self,"table")
  108.         local term = self.disp
  109.         local bc = term.getBackgroundColor()
  110.         local tc = term.getTextColor()
  111.         for k,v in pairs(self.map) do
  112.             for k1,v1 in pairs(v) do
  113.                 for k2,v2 in pairs(v1) do
  114.                     if k2 == "draw" then
  115.                         if v2[2] then
  116.                             term.setBackgroundColor(tc)
  117.                             term.setTextColor(bc)
  118.                         end
  119.                         term.setCursorPos(k,k1)
  120.                         term.write(string.char(v2[1]))
  121.                         term.setBackgroundColor(bc)
  122.                         term.setTextColor(tc)
  123.                     end
  124.                 end
  125.             end
  126.         end
  127.     end
  128.     local pixelMap = {disp=termObj,map={},funcs=index}
  129.     return setmetatable(pixelMap,{__index=index})
  130. end
  131. return {
  132.     createCanvas = createCanvas,
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement