Advertisement
incinirate

shh

Jun 29th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.14 KB | None | 0 0
  1. if not fs.exists(shell.resolve("blittle")) then shell.run("/rom/programs/http/pastebin get ujchRSnU blittle") end
  2. os.loadAPI(shell.resolve("blittle"))
  3.  
  4. local args = {...}
  5. if #args < 1 then
  6.   printError("Usage: "..shell.getRunningProgram().." <filename>")
  7.   return
  8. end
  9.  
  10. --[[
  11.  
  12. {
  13.         ["width"] = (number) image width
  14.         ["height"] = (number) image height
  15.  
  16.         [1] = {
  17.                 [1] = First text row.
  18.                 [2] = Second text row.
  19.                 -- etc
  20.         },
  21.  
  22.         [2] = {
  23.                 [1] = First textColour row.
  24.                 [2] = Second textColour row.
  25.                 -- etc
  26.         },
  27.  
  28.         [3] = {
  29.                 [1] = First backgroundColour row.
  30.                 [2] = Second backgroundColour row.
  31.                 -- etc
  32.         }
  33. }
  34.  
  35. ]]
  36. local lookup = "0123456789abcdef"
  37. local precomp2H = {}
  38. for i=1,#lookup do
  39.   precomp2H[2^(i-1)] = lookup:sub(i,i)
  40. end
  41. local function toHex(dec)
  42.   return precomp2H[dec]
  43. end
  44. local precomp2D = {}
  45. for i=1,#lookup do
  46.   precomp2D[lookup:sub(i,i)] = 2^(i-1)
  47. end
  48. local function toDec(hex)
  49.   return precomp2D[hex]
  50. end
  51.  
  52. local fn = args[1]
  53. local bimg = {}
  54. if fs.exists(fn) then
  55.   bimg = blittle.load(fn)
  56. else
  57.   bimg = {width=0,height=0,[1]={" "},[2]={"f"},[3]={"f"}}
  58. end
  59.  
  60. local transpChar = "\127"
  61.  
  62. local offx,offy = 1,1
  63. local scnw,scnh = term.getSize()
  64.  
  65. local showToolbar = true
  66. local pallette = {"f","f"}
  67. local dirtyInfo = true
  68. local dirtyLine = -1
  69. local dirtyPallette = true
  70. local blobj = {width=0,height=0,{},{},{}}
  71.  
  72. local function convertToBLittle()
  73.   local eobj = {}
  74.   for i=1,#bimg[3] do
  75.     eobj[i] = {}
  76.     for n in bimg[3][i]:gmatch(".") do
  77.       table.insert(eobj[i],toDec(n))
  78.     end
  79.   end
  80.   blobj = blittle.shrink(eobj)
  81. end
  82.  
  83. --sticky is 1 of the following: 0-topLeft 1-topRight 2-bottomLeft 3-bottomRight
  84. local function enlargeCanvas(dx,dy,sticky)
  85.   local newWidth  = bimg.width  + dx
  86.   local newHeight = bimg.height + dy
  87.   local newData = {width=newWidth,height=newHeight,{},{},{}}
  88.  
  89.   local stickyX = sticky % 2 == 0 and true or false
  90.   local stickyY = sticky < 2      and true or false
  91.  
  92.   for i=1,newHeight do
  93.     newData[1][i] = (" "):rep(newWidth)
  94.     newData[2][i] = ("0"):rep(newWidth)
  95.     newData[3][i] = ("f"):rep(newWidth)
  96.   end
  97.  
  98.   --Now copy old data into new canvas
  99.   local counter = 1
  100.   for i = (stickyY and 1 or 1+dy), (stickyY and bimg.height or newHeight) do
  101.     if stickyX then
  102.       newData[3][i] = bimg[3][counter]..("f"):rep(dx) --left aligned
  103.     else
  104.       newData[3][i] = ("f"):rep(dx)..bimg[3][counter] --right aligned
  105.     end
  106.     counter = counter + 1
  107.   end
  108.  
  109.   offx = offx - (stickyX and 0 or dx)
  110.   offy = offy - (stickyY and 0 or dy)
  111.   dirtyInfo = true
  112.  
  113.   bimg = newData
  114. end
  115.  
  116. local drawMode = false --false=normal true=blittle
  117.  
  118. local function draw()
  119.   if dirtyInfo then
  120.     for i=1,scnh do
  121.       term.setCursorPos(1,i)
  122.       term.setBackgroundColor(colors.black)
  123.       term.setTextColor(colors.gray)
  124.       term.write(transpChar:rep(scnw))
  125.     end
  126.   end
  127.   if drawMode then
  128.     --blittle.draw(table blittle image [, number xPos] [, number yPos] [, table terminal])
  129.     if dirtyInfo then
  130.       blittle.draw(blobj, offx, offy, term.current())
  131.     end
  132.   else
  133.     if dirtyLine < 1 and not dirtyInfo and not dirtyPallette then
  134.       return
  135.     end
  136.     if dirtyInfo then
  137.       for i=1,scnh do
  138.       --for i=offy,math.min(#bimg[1]) do
  139.         if i-offy < bimg.height and i-offy+1 > 0 then
  140.           local py = i-offy+1
  141.           term.setCursorPos(offx,i)
  142.           term.blit(bimg[1][py],bimg[2][py],bimg[3][py])
  143.         end
  144.       end
  145.     elseif dirtyLine > 0 then
  146.       term.setCursorPos(offx,offy+dirtyLine-1)
  147.       term.blit(bimg[1][dirtyLine],bimg[2][dirtyLine],bimg[3][dirtyLine])
  148.     end
  149.     if (dirtyInfo or dirtyPallette) and showToolbar then
  150.       for i=1,16 do
  151.         term.setBackgroundColor(2^(i-1))
  152.         term.setCursorPos(scnw-1,i)
  153.         term.write("  ")
  154.       end
  155.       for i=1,2 do
  156.         term.setBackgroundColor(toDec(pallette[i]))
  157.         term.setTextColor(colors.white)
  158.         term.setCursorPos(scnw-2+i,17)
  159.         term.write("\7\7")
  160.       end
  161.     end
  162.     dirtyInfo = false
  163.   end
  164. end
  165.  
  166. while true do
  167.   draw()
  168.   local e,p1,p2,p3,p4,p5 = os.pullEvent()
  169.   if e=="key" then
  170.     local k = p1
  171.     local ox,oy = offx, offy
  172.     if k == keys.left or k == keys.a then
  173.       offx = offx - 1
  174.     elseif k == keys.right or k == keys.d then
  175.       offx = offx + 1
  176.     elseif k == keys.up or k == keys.w then
  177.       offy = offy - 1
  178.     elseif k == keys.down or k == keys.d then
  179.       offy = offy + 1
  180.     elseif k == keys.grave then
  181.       drawMode = not drawMode
  182.       if drawMode then
  183.         convertToBLittle()
  184.       end
  185.       dirtyInfo = true
  186.     end
  187.     if ox~=offx or oy~=offy then
  188.       dirtyInfo = true
  189.     end
  190.   elseif e=="mouse_click" or e=="mouse_drag" then
  191.     local b,x,y = p1,p2,p3
  192.     if x>scnw-2 and y<17 then
  193.       --Picking a color
  194.       pallette[b] = toHex(2^(y-1))
  195.       dirtyPallette = true
  196.     elseif x>scnw-2 and y==17 then
  197.       --Copy color
  198.       pallette[b] = pallette[x-(scnw-2)]
  199.       dirtyPallette = true
  200.     end
  201.   end
  202. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement