LDDestroier

NFT API

May 14th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.14 KB | None | 0 0
  1. local tchar, bchar = string.char(31), string.char(30)
  2.  
  3. loadImageData = function(image, background) --string image
  4.     image = image:lower()
  5.     local output = {{},{},{}} --char, text, back
  6.     local y = 1
  7.     local text, back = " ", background or " "
  8.     local doSkip, c1, c2 = false
  9.     for i = 1, #image do
  10.         if doSkip then
  11.             doSkip = false
  12.         else
  13.             output[1][y] = output[1][y] or ""
  14.             output[2][y] = output[2][y] or ""
  15.             output[3][y] = output[3][y] or ""
  16.             c1, c2 = image:sub(i,i), image:sub(i+1,i+1)
  17.             if c1 == tchar then
  18.                 text = c2
  19.                 doSkip = true
  20.             elseif c1 == bchar then
  21.                 back = c2
  22.                 doSkip = true
  23.             elseif c1 == "\n" then
  24.                 y = y + 1
  25.                 text, back = " ", background or " "
  26.             else
  27.                 output[1][y] = output[1][y]..c1
  28.                 output[2][y] = output[2][y]..text
  29.                 output[3][y] = output[3][y]..back
  30.             end
  31.         end
  32.     end
  33.     return output
  34. end
  35.  
  36. loadImage = function(path, background)
  37.     local file = fs.open(path,"r")
  38.     local output = loadImageData(file.readAll(), background)
  39.     file.close()
  40.     return output
  41. end
  42.  
  43. unloadImage = function(image)
  44.     local output = ""
  45.     local text, back = " ", " "
  46.     local c, t, b
  47.     for y = 1, #image[1] do
  48.         for x = 1, #image[1][y] do
  49.             c, t, b = image[1][y]:sub(x,x), image[2][y]:sub(x,x), image[3][y]:sub(x,x)
  50.             if (t ~= text) or (x + y == 2) then output = output..tchar..t end
  51.             if (b ~= back) or (x + y == 2) then output = output..bchar..b end
  52.             output = output..c
  53.         end
  54.         if y ~= #image[1] then
  55.             output = output.."\n"
  56.             text, back = " ", " "
  57.         end
  58.     end
  59.     return output
  60. end
  61.  
  62. drawImage = function(image, x, y)
  63.     local cx, cy = term.getCursorPos()
  64.     for iy = 1, #image[1] do
  65.         term.setCursorPos(x,y+(iy-1))
  66.         term.blit(image[1][iy], image[2][iy], image[3][iy])
  67.     end
  68.     term.setCursorPos(cx,cy)
  69. end
  70.  
  71. drawImageTransparent = function(image, x, y)
  72.     local cx, cy = term.getCursorPos()
  73.     local c, t, b
  74.     for iy = 1, #image[1] do
  75.         for ix = 1, #image[1][iy] do
  76.             c, t, b = image[1][iy]:sub(ix,ix), image[2][iy]:sub(ix,ix), image[3][iy]:sub(ix,ix)
  77.             if (b ~= " ") or (c ~= " ") then
  78.                 term.setCursorPos(x+(ix-1),y+(iy-1))
  79.                 term.blit(c, t, b)
  80.             end
  81.         end
  82.     end
  83.     term.setCursorPos(cx,cy)
  84. end
Add Comment
Please, Sign In to add comment