mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-10-02 17:00:47 +00:00
Merge pull request #1 from BombBloke/patch-1
Better update paintutils.lua
This commit is contained in:
commit
157bc01be5
@ -1,17 +1,9 @@
|
|||||||
|
local setPos, write, setCol, blit = term.setCursorPos, term.write, term.setBackgroundColour, term.blit
|
||||||
local function drawPixelInternal( xPos, yPos )
|
|
||||||
term.setCursorPos( xPos, yPos )
|
|
||||||
term.write(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
local function drawLineHorizontal( xPos, yPos , nLen )
|
|
||||||
term.setCursorPos( xPos, yPos )
|
|
||||||
term.write(string.rep( " ", nLen ))
|
|
||||||
end
|
|
||||||
|
|
||||||
local tColourLookup = {}
|
local tColourLookup = {}
|
||||||
for n=1,16 do
|
for n=1,16 do
|
||||||
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
|
tColourLookup[ string.sub( "0123456789abcdef",n,n ) ] = 2^(n-1)
|
||||||
|
tColourLookup[ 2^(n-1) ] = string.sub( "0123456789abcdef",n,n )
|
||||||
end
|
end
|
||||||
|
|
||||||
function loadImage( sPath )
|
function loadImage( sPath )
|
||||||
@ -21,37 +13,53 @@ function loadImage( sPath )
|
|||||||
|
|
||||||
local tImage = {}
|
local tImage = {}
|
||||||
if fs.exists( sPath ) then
|
if fs.exists( sPath ) then
|
||||||
local file = io.open(sPath, "r" )
|
for sLine in io.lines(sPath) do
|
||||||
local sLine = file:read()
|
|
||||||
while sLine do
|
|
||||||
local tLine = {}
|
local tLine = {}
|
||||||
for x=1,sLine:len() do
|
for x=1,#sLine do
|
||||||
tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
|
tLine[x] = tColourLookup[ string.sub(sLine,x,x) ] or 0
|
||||||
end
|
end
|
||||||
table.insert( tImage, tLine )
|
table.insert( tImage, tLine )
|
||||||
sLine = file:read()
|
|
||||||
end
|
end
|
||||||
file:close()
|
|
||||||
return tImage
|
return tImage
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function saveImage( tImage, sPath )
|
||||||
|
if type( tImage ) ~= "table" then
|
||||||
|
error( "Expected paintutils image", 2 )
|
||||||
|
elseif type( sPath ) ~= "string" then
|
||||||
|
error( "Expected path", 2 )
|
||||||
|
end
|
||||||
|
|
||||||
|
local file = fs.open(sPath, "w" )
|
||||||
|
|
||||||
|
for y=1,#tImage do
|
||||||
|
local tOld, tNew = tImage[y], {}
|
||||||
|
for x=1,#tOld do
|
||||||
|
tNew[x] = tColourLookup[ tOld[x] ] or " "
|
||||||
|
end
|
||||||
|
file.writeLine( table.concat( tNew ) )
|
||||||
|
end
|
||||||
|
file.close()
|
||||||
|
end
|
||||||
|
|
||||||
function drawPixel( xPos, yPos, nColour )
|
function drawPixel( xPos, yPos, nColour )
|
||||||
if type( xPos ) ~= "number" or type( yPos ) ~= "number" or (nColour ~= nil and type( nColour ) ~= "number") then
|
if type( xPos ) ~= "number" or type( yPos ) ~= "number" or (nColour ~= nil and type( nColour ) ~= "number") then
|
||||||
error( "Expected x, y, colour", 2 )
|
error( "Expected x, y [, colour]", 2 )
|
||||||
end
|
end
|
||||||
if nColour then
|
if nColour then
|
||||||
term.setBackgroundColor( nColour )
|
setCol( nColour )
|
||||||
end
|
end
|
||||||
drawPixelInternal( xPos, yPos )
|
setPos( xPos, yPos )
|
||||||
|
write( " " )
|
||||||
end
|
end
|
||||||
|
|
||||||
function drawLine( startX, startY, endX, endY, nColour )
|
function drawLine( startX, startY, endX, endY, nColour )
|
||||||
if type( startX ) ~= "number" or type( startX ) ~= "number" or
|
if type( startX ) ~= "number" or type( startX ) ~= "number" or
|
||||||
type( endX ) ~= "number" or type( endY ) ~= "number" or
|
type( endX ) ~= "number" or type( endY ) ~= "number" or
|
||||||
(nColour ~= nil and type( nColour ) ~= "number") then
|
(nColour ~= nil and type( nColour ) ~= "number") then
|
||||||
error( "Expected startX, startY, endX, endY, colour", 2 )
|
error( "Expected startX, startY, endX, endY [, colour]", 2 )
|
||||||
end
|
end
|
||||||
|
|
||||||
startX = math.floor(startX)
|
startX = math.floor(startX)
|
||||||
@ -60,14 +68,15 @@ function drawLine( startX, startY, endX, endY, nColour )
|
|||||||
endY = math.floor(endY)
|
endY = math.floor(endY)
|
||||||
|
|
||||||
if nColour then
|
if nColour then
|
||||||
term.setBackgroundColor( nColour )
|
setCol( nColour )
|
||||||
end
|
end
|
||||||
if startX == endX and startY == endY then
|
if startX == endX and startY == endY then
|
||||||
drawPixelInternal( startX, startY )
|
setPos( startX, startY )
|
||||||
|
write(" ")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local minX = math.min( startX, endX )
|
local minX, minY, maxX, maxY = math.min( startX, endX )
|
||||||
if minX == startX then
|
if minX == startX then
|
||||||
minY = startY
|
minY = startY
|
||||||
maxX = endX
|
maxX = endX
|
||||||
@ -84,7 +93,9 @@ function drawLine( startX, startY, endX, endY, nColour )
|
|||||||
local yDiff = maxY - minY
|
local yDiff = maxY - minY
|
||||||
|
|
||||||
if minY == maxY then
|
if minY == maxY then
|
||||||
drawLineHorizontal( minX, minY , xDiff + 1 )
|
local sStr = string.rep( " ", xDiff + 1 )
|
||||||
|
setPos( minX, minY )
|
||||||
|
write( sStr )
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -92,23 +103,18 @@ function drawLine( startX, startY, endX, endY, nColour )
|
|||||||
local y = minY
|
local y = minY
|
||||||
local dy = yDiff / xDiff
|
local dy = yDiff / xDiff
|
||||||
for x=minX,maxX do
|
for x=minX,maxX do
|
||||||
drawPixelInternal( x, math.floor( y + 0.5 ) )
|
setPos( x, math.floor( y + 0.5 ) )
|
||||||
|
write( " " )
|
||||||
y = y + dy
|
y = y + dy
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
local x = minX
|
local x, mul = minX, maxY >= minY and 1 or -1
|
||||||
local dx = xDiff / yDiff
|
local dx = xDiff / yDiff * mul
|
||||||
if maxY >= minY then
|
for y=minY,maxY,mul do
|
||||||
for y=minY,maxY do
|
setPos( math.floor( x + 0.5 ), y )
|
||||||
drawPixelInternal( math.floor( x + 0.5 ), y )
|
write( " " )
|
||||||
x = x + dx
|
x = x + dx
|
||||||
end
|
end
|
||||||
else
|
|
||||||
for y=minY,maxY,-1 do
|
|
||||||
drawPixelInternal( math.floor( x + 0.5 ), y )
|
|
||||||
x = x - dx
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -116,7 +122,7 @@ function drawBox( startX, startY, endX, endY, nColour )
|
|||||||
if type( startX ) ~= "number" or type( startX ) ~= "number" or
|
if type( startX ) ~= "number" or type( startX ) ~= "number" or
|
||||||
type( endX ) ~= "number" or type( endY ) ~= "number" or
|
type( endX ) ~= "number" or type( endY ) ~= "number" or
|
||||||
(nColour ~= nil and type( nColour ) ~= "number") then
|
(nColour ~= nil and type( nColour ) ~= "number") then
|
||||||
error( "Expected startX, startY, endX, endY, colour", 2 )
|
error( "Expected startX, startY, endX, endY [, colour]", 2 )
|
||||||
end
|
end
|
||||||
|
|
||||||
startX = math.floor(startX)
|
startX = math.floor(startX)
|
||||||
@ -125,32 +131,29 @@ function drawBox( startX, startY, endX, endY, nColour )
|
|||||||
endY = math.floor(endY)
|
endY = math.floor(endY)
|
||||||
|
|
||||||
if nColour then
|
if nColour then
|
||||||
term.setBackgroundColor( nColour )
|
setCol( nColour )
|
||||||
end
|
end
|
||||||
if startX == endX and startY == endY then
|
if startX == endX and startY == endY then
|
||||||
drawPixelInternal( startX, startY )
|
setPos( startX, startY )
|
||||||
|
write( " " )
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local minX = math.min( startX, endX )
|
local minX, minY, maxX, maxY
|
||||||
if minX == startX then
|
if startX < endX then minX, maxX = startX, endX else minX, maxX = endX, startX end
|
||||||
minY = startY
|
if startY < endY then minY, maxY = startY, endY else minY, maxY = endY, startY end
|
||||||
maxX = endX
|
|
||||||
maxY = endY
|
|
||||||
else
|
|
||||||
minY = endY
|
|
||||||
maxX = startX
|
|
||||||
maxY = startY
|
|
||||||
end
|
|
||||||
|
|
||||||
drawLineHorizontal( minX, minY , maxX - minX + 1 )
|
local sStr = string.rep( " ", maxX - minX + 1 )
|
||||||
drawLineHorizontal( minX, maxY , maxX - minX + 1 )
|
setPos( minX, minY )
|
||||||
|
write( sStr )
|
||||||
|
setPos( minX, maxY )
|
||||||
|
write( sStr )
|
||||||
|
|
||||||
if (maxY - minY) >= 2 then
|
|
||||||
for y=(minY+1),(maxY-1) do
|
for y=(minY+1),(maxY-1) do
|
||||||
drawPixelInternal( minX, y )
|
setPos( minX, y )
|
||||||
drawPixelInternal( maxX, y )
|
write( " " )
|
||||||
end
|
setPos( maxX, y )
|
||||||
|
write( " " )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -158,7 +161,7 @@ function drawFilledBox( startX, startY, endX, endY, nColour )
|
|||||||
if type( startX ) ~= "number" or type( startX ) ~= "number" or
|
if type( startX ) ~= "number" or type( startX ) ~= "number" or
|
||||||
type( endX ) ~= "number" or type( endY ) ~= "number" or
|
type( endX ) ~= "number" or type( endY ) ~= "number" or
|
||||||
(nColour ~= nil and type( nColour ) ~= "number") then
|
(nColour ~= nil and type( nColour ) ~= "number") then
|
||||||
error( "Expected startX, startY, endX, endY, colour", 2 )
|
error( "Expected startX, startY, endX, endY [, colour]", 2 )
|
||||||
end
|
end
|
||||||
|
|
||||||
startX = math.floor(startX)
|
startX = math.floor(startX)
|
||||||
@ -167,26 +170,22 @@ function drawFilledBox( startX, startY, endX, endY, nColour )
|
|||||||
endY = math.floor(endY)
|
endY = math.floor(endY)
|
||||||
|
|
||||||
if nColour then
|
if nColour then
|
||||||
term.setBackgroundColor( nColour )
|
setCol( nColour )
|
||||||
end
|
end
|
||||||
if startX == endX and startY == endY then
|
if startX == endX and startY == endY then
|
||||||
drawPixelInternal( startX, startY )
|
setPos( startX, startY )
|
||||||
|
write( " " )
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local minX = math.min( startX, endX )
|
local minX, minY, maxX, maxY
|
||||||
if minX == startX then
|
if startX < endX then minX, maxX = startX, endX else minX, maxX = endX, startX end
|
||||||
minY = startY
|
if startY < endY then minY, maxY = startY, endY else minY, maxY = endY, startY end
|
||||||
maxX = endX
|
|
||||||
maxY = endY
|
|
||||||
else
|
|
||||||
minY = endY
|
|
||||||
maxX = startX
|
|
||||||
maxY = startY
|
|
||||||
end
|
|
||||||
|
|
||||||
|
local sStr = string.rep( " ", maxX - minX + 1 )
|
||||||
for y=minY,maxY do
|
for y=minY,maxY do
|
||||||
drawLineHorizontal( minX, y , maxX - minX + 1 )
|
setPos( minX, y )
|
||||||
|
write( sStr )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -195,11 +194,17 @@ function drawImage( tImage, xPos, yPos )
|
|||||||
error( "Expected image, x, y", 2 )
|
error( "Expected image, x, y", 2 )
|
||||||
end
|
end
|
||||||
for y=1,#tImage do
|
for y=1,#tImage do
|
||||||
local tLine = tImage[y]
|
local tLine, sBG, counter = tImage[y], {}, 0
|
||||||
for x=1,#tLine do
|
for x=1,#tLine+1 do
|
||||||
if tLine[x] > 0 then
|
local px = tLine[x] or 0
|
||||||
term.setBackgroundColor( tLine[x] )
|
if px > 0 then
|
||||||
drawPixelInternal( x + xPos - 1, y + yPos - 1 )
|
counter = counter + 1
|
||||||
|
sBG[counter] = tColourLookup[ px ]
|
||||||
|
elseif counter > 0 then
|
||||||
|
setPos( x + xPos - 1 - counter, y + yPos - 1 )
|
||||||
|
local sT = string.rep( " ", counter )
|
||||||
|
blit( sT, sT, table.concat( sBG ) )
|
||||||
|
sBG, counter = {}, 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user