lego11

Nuovo Paintutils

Jan 30th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.32 KB | None | 0 0
  1.  
  2. local function drawPixelInternal( xPos, yPos )
  3.     term.setCursorPos( xPos, yPos )
  4.     term.write(" ")
  5. end
  6.  
  7. local tColourLookup = {}
  8. for n=1,16 do
  9.     tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
  10. end
  11.  
  12. function loadImage( sPath )
  13.     if type( sPath ) ~= "string" then
  14.         error( "Expected path", 2 )
  15.     end
  16.  
  17.     local tImage = {}
  18.     if fs.exists( sPath ) then
  19.         local file = io.open(sPath, "r" )
  20.         local sLine = file:read()
  21.         while sLine do
  22.             local tLine = {}
  23.             for x=1,sLine:len() do
  24.                 tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
  25.             end
  26.             table.insert( tImage, tLine )
  27.             sLine = file:read()
  28.         end
  29.         file:close()
  30.         return tImage
  31.     end
  32.     return nil
  33. end
  34.  
  35. function drawPixel( xPos, yPos, nColour )
  36.     if type( xPos ) ~= "number" or type( yPos ) ~= "number" or (nColour ~= nil and type( nColour ) ~= "number") then
  37.         error( "Expected x, y, colour", 2 )
  38.     end
  39.     if nColour then
  40.         term.setBackgroundColor( nColour )
  41.     end
  42.     drawPixelInternal( xPos, yPos )
  43. end
  44.  
  45. function drawLine( startX, startY, endX, endY, nColour )
  46.     if type( startX ) ~= "number" or type( startX ) ~= "number" or
  47.        type( endX ) ~= "number" or type( endY ) ~= "number" or
  48.        (nColour ~= nil and type( nColour ) ~= "number") then
  49.         error( "Expected startX, startY, endX, endY, colour", 2 )
  50.     end
  51.    
  52.     startX = math.floor(startX)
  53.     startY = math.floor(startY)
  54.     endX = math.floor(endX)
  55.     endY = math.floor(endY)
  56.  
  57.     if nColour then
  58.         term.setBackgroundColor( nColour )
  59.     end
  60.     if startX == endX and startY == endY then
  61.         drawPixelInternal( startX, startY )
  62.         return
  63.     end
  64.    
  65.     local minX = math.min( startX, endX )
  66.     if minX == startX then
  67.         minY = startY
  68.         maxX = endX
  69.         maxY = endY
  70.     else
  71.         minY = endY
  72.         maxX = startX
  73.         maxY = startY
  74.     end
  75.  
  76.     -- TODO: clip to screen rectangle?
  77.        
  78.     local xDiff = maxX - minX
  79.     local yDiff = maxY - minY
  80.            
  81.     if xDiff > math.abs(yDiff) then
  82.         local y = minY
  83.         local dy = yDiff / xDiff
  84.         for x=minX,maxX do
  85.             drawPixelInternal( x, math.floor( y + 0.5 ) )
  86.             y = y + dy
  87.         end
  88.     else
  89.         local x = minX
  90.         local dx = xDiff / yDiff
  91.         if maxY >= minY then
  92.             for y=minY,maxY do
  93.                 drawPixelInternal( math.floor( x + 0.5 ), y )
  94.                 x = x + dx
  95.             end
  96.         else
  97.             for y=minY,maxY,-1 do
  98.                 drawPixelInternal( math.floor( x + 0.5 ), y )
  99.                 x = x - dx
  100.             end
  101.         end
  102.     end
  103. end
  104.  
  105. function drawBox( startX, startY, endX, endY, nColour )
  106.     if type( startX ) ~= "number" or type( startX ) ~= "number" or
  107.        type( endX ) ~= "number" or type( endY ) ~= "number" or
  108.        (nColour ~= nil and type( nColour ) ~= "number") then
  109.         error( "Expected startX, startY, endX, endY, colour", 2 )
  110.     end
  111.  
  112.     startX = math.floor(startX)
  113.     startY = math.floor(startY)
  114.     endX = math.floor(endX)
  115.     endY = math.floor(endY)
  116.  
  117.     if nColour then
  118.         term.setBackgroundColor( nColour )
  119.     end
  120.     if startX == endX and startY == endY then
  121.         drawPixelInternal( startX, startY )
  122.         return
  123.     end
  124.  
  125.     local minX = math.min( startX, endX )
  126.     if minX == startX then
  127.         minY = startY
  128.         maxX = endX
  129.         maxY = endY
  130.     else
  131.         minY = endY
  132.         maxX = startX
  133.         maxY = startY
  134.     end
  135.  
  136.     for x=minX,maxX do
  137.         drawPixelInternal( x, minY )
  138.         drawPixelInternal( x, maxY )
  139.     end
  140.  
  141.     if (maxY - minY) >= 2 then
  142.         for y=(minY+1),(maxY-1) do
  143.             drawPixelInternal( minX, y )
  144.             drawPixelInternal( maxX, y )
  145.         end
  146.     end
  147. end
  148.  
  149. function drawFilledBox( startX, startY, endX, endY, nColour )
  150.     if type( startX ) ~= "number" or type( startX ) ~= "number" or
  151.        type( endX ) ~= "number" or type( endY ) ~= "number" or
  152.        (nColour ~= nil and type( nColour ) ~= "number") then
  153.         error( "Expected startX, startY, endX, endY, colour", 2 )
  154.     end
  155.  
  156.     startX = math.floor(startX)
  157.     startY = math.floor(startY)
  158.     endX = math.floor(endX)
  159.     endY = math.floor(endY)
  160.  
  161.     if nColour then
  162.         term.setBackgroundColor( nColour )
  163.     end
  164.     if startX == endX and startY == endY then
  165.         drawPixelInternal( startX, startY )
  166.         return
  167.     end
  168.  
  169.     local minX = math.min( startX, endX )
  170.     if minX == startX then
  171.         minY = startY
  172.         maxX = endX
  173.         maxY = endY
  174.     else
  175.         minY = endY
  176.         maxX = startX
  177.         maxY = startY
  178.     end
  179.  
  180.     for x=minX,maxX do
  181.         for y=minY,maxY do
  182.             drawPixelInternal( x, y )
  183.         end
  184.     end
  185. end
  186.  
  187. function drawImage( tImage, xPos, yPos )
  188.     if type( tImage ) ~= "table" or type( xPos ) ~= "number" or type( yPos ) ~= "number" then
  189.         error( "Expected image, x, y", 2 )
  190.     end
  191.     for y=1,#tImage do
  192.         local tLine = tImage[y]
  193.         for x=1,#tLine do
  194.             if tLine[x] > 0 then
  195.                 term.setBackgroundColor( tLine[x] )
  196.                 drawPixelInternal( x + xPos - 1, y + yPos - 1 )
  197.             end
  198.         end
  199.     end
  200. end
Add Comment
Please, Sign In to add comment