2019-07-27 10:34:36 +00:00
|
|
|
local expect = dofile("rom/modules/main/cc/expect.lua").expect
|
2017-05-01 13:32:39 +00:00
|
|
|
|
|
|
|
local function drawPixelInternal( xPos, yPos )
|
|
|
|
term.setCursorPos( xPos, yPos )
|
|
|
|
term.write(" ")
|
|
|
|
end
|
|
|
|
|
|
|
|
local tColourLookup = {}
|
|
|
|
for n=1,16 do
|
|
|
|
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
|
|
|
|
end
|
|
|
|
|
2017-07-15 22:27:52 +00:00
|
|
|
local function parseLine( tImageArg, sLine )
|
|
|
|
local tLine = {}
|
|
|
|
for x=1,sLine:len() do
|
|
|
|
tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
|
|
|
|
end
|
|
|
|
table.insert( tImageArg, tLine )
|
|
|
|
end
|
|
|
|
|
|
|
|
function parseImage( sRawData )
|
2019-05-30 18:36:28 +00:00
|
|
|
expect(1, sRawData, "string")
|
2017-07-15 22:27:52 +00:00
|
|
|
local tImage = {}
|
2017-07-16 06:08:54 +00:00
|
|
|
for sLine in ( sRawData .. "\n" ):gmatch( "(.-)\n" ) do -- read each line like original file handling did
|
2017-07-17 05:49:14 +00:00
|
|
|
parseLine( tImage, sLine )
|
2017-07-15 21:50:05 +00:00
|
|
|
end
|
2017-07-15 22:27:52 +00:00
|
|
|
return tImage
|
|
|
|
end
|
2017-05-01 13:32:39 +00:00
|
|
|
|
|
|
|
function loadImage( sPath )
|
2019-05-30 18:36:28 +00:00
|
|
|
expect(1, sPath, "string")
|
2017-05-01 13:32:39 +00:00
|
|
|
|
|
|
|
if fs.exists( sPath ) then
|
2017-07-15 21:50:05 +00:00
|
|
|
local file = io.open( sPath, "r" )
|
2017-09-12 08:52:21 +00:00
|
|
|
local sContent = file:read("*a")
|
2017-05-01 13:32:39 +00:00
|
|
|
file:close()
|
2017-07-17 05:16:36 +00:00
|
|
|
return parseImage( sContent ) -- delegate image parse to parseImage
|
2017-05-01 13:32:39 +00:00
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function drawPixel( xPos, yPos, nColour )
|
2019-05-30 18:36:28 +00:00
|
|
|
expect(1, xPos, "number")
|
|
|
|
expect(2, yPos, "number")
|
|
|
|
expect(3, nColour, "number", "nil")
|
2017-05-01 13:32:39 +00:00
|
|
|
if nColour then
|
|
|
|
term.setBackgroundColor( nColour )
|
|
|
|
end
|
2019-05-30 18:36:28 +00:00
|
|
|
return drawPixelInternal( xPos, yPos )
|
2017-05-01 13:32:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function drawLine( startX, startY, endX, endY, nColour )
|
2019-05-30 18:36:28 +00:00
|
|
|
expect(1, startX, "number")
|
|
|
|
expect(2, startY, "number")
|
|
|
|
expect(3, endX, "number")
|
|
|
|
expect(4, endY, "number")
|
|
|
|
expect(5, nColour, "number", "nil")
|
2018-12-17 17:22:15 +00:00
|
|
|
|
2017-05-01 13:32:39 +00:00
|
|
|
startX = math.floor(startX)
|
|
|
|
startY = math.floor(startY)
|
|
|
|
endX = math.floor(endX)
|
|
|
|
endY = math.floor(endY)
|
|
|
|
|
|
|
|
if nColour then
|
|
|
|
term.setBackgroundColor( nColour )
|
|
|
|
end
|
|
|
|
if startX == endX and startY == endY then
|
|
|
|
drawPixelInternal( startX, startY )
|
|
|
|
return
|
|
|
|
end
|
2018-12-17 17:22:15 +00:00
|
|
|
|
2017-07-25 19:01:04 +00:00
|
|
|
local minX = math.min( startX, endX )
|
|
|
|
local maxX, minY, maxY
|
2017-05-01 13:32:39 +00:00
|
|
|
if minX == startX then
|
|
|
|
minY = startY
|
|
|
|
maxX = endX
|
|
|
|
maxY = endY
|
|
|
|
else
|
|
|
|
minY = endY
|
|
|
|
maxX = startX
|
|
|
|
maxY = startY
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: clip to screen rectangle?
|
2018-12-17 17:22:15 +00:00
|
|
|
|
2017-05-01 13:32:39 +00:00
|
|
|
local xDiff = maxX - minX
|
|
|
|
local yDiff = maxY - minY
|
2018-12-17 17:22:15 +00:00
|
|
|
|
2017-05-01 13:32:39 +00:00
|
|
|
if xDiff > math.abs(yDiff) then
|
|
|
|
local y = minY
|
|
|
|
local dy = yDiff / xDiff
|
|
|
|
for x=minX,maxX do
|
|
|
|
drawPixelInternal( x, math.floor( y + 0.5 ) )
|
|
|
|
y = y + dy
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local x = minX
|
|
|
|
local dx = xDiff / yDiff
|
|
|
|
if maxY >= minY then
|
|
|
|
for y=minY,maxY do
|
|
|
|
drawPixelInternal( math.floor( x + 0.5 ), y )
|
|
|
|
x = x + dx
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for y=minY,maxY,-1 do
|
|
|
|
drawPixelInternal( math.floor( x + 0.5 ), y )
|
|
|
|
x = x - dx
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function drawBox( startX, startY, endX, endY, nColour )
|
2019-05-30 18:36:28 +00:00
|
|
|
expect(1, startX, "number")
|
|
|
|
expect(2, startY, "number")
|
|
|
|
expect(3, endX, "number")
|
|
|
|
expect(4, endY, "number")
|
|
|
|
expect(5, nColour, "number", "nil")
|
2017-05-01 13:32:39 +00:00
|
|
|
|
|
|
|
startX = math.floor(startX)
|
|
|
|
startY = math.floor(startY)
|
|
|
|
endX = math.floor(endX)
|
|
|
|
endY = math.floor(endY)
|
|
|
|
|
|
|
|
if nColour then
|
|
|
|
term.setBackgroundColor( nColour )
|
|
|
|
end
|
|
|
|
if startX == endX and startY == endY then
|
|
|
|
drawPixelInternal( startX, startY )
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-07-25 19:01:04 +00:00
|
|
|
local minX = math.min( startX, endX )
|
2018-12-17 17:22:15 +00:00
|
|
|
local maxX, minY, maxY
|
2017-05-01 13:32:39 +00:00
|
|
|
if minX == startX then
|
|
|
|
minY = startY
|
|
|
|
maxX = endX
|
|
|
|
maxY = endY
|
|
|
|
else
|
|
|
|
minY = endY
|
|
|
|
maxX = startX
|
|
|
|
maxY = startY
|
|
|
|
end
|
|
|
|
|
|
|
|
for x=minX,maxX do
|
|
|
|
drawPixelInternal( x, minY )
|
|
|
|
drawPixelInternal( x, maxY )
|
|
|
|
end
|
|
|
|
|
|
|
|
if (maxY - minY) >= 2 then
|
|
|
|
for y=(minY+1),(maxY-1) do
|
|
|
|
drawPixelInternal( minX, y )
|
|
|
|
drawPixelInternal( maxX, y )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function drawFilledBox( startX, startY, endX, endY, nColour )
|
2019-05-30 18:36:28 +00:00
|
|
|
expect(1, startX, "number")
|
|
|
|
expect(2, startY, "number")
|
|
|
|
expect(3, endX, "number")
|
|
|
|
expect(4, endY, "number")
|
|
|
|
expect(5, nColour, "number", "nil")
|
2017-05-01 13:32:39 +00:00
|
|
|
|
|
|
|
startX = math.floor(startX)
|
|
|
|
startY = math.floor(startY)
|
|
|
|
endX = math.floor(endX)
|
|
|
|
endY = math.floor(endY)
|
|
|
|
|
|
|
|
if nColour then
|
|
|
|
term.setBackgroundColor( nColour )
|
|
|
|
end
|
|
|
|
if startX == endX and startY == endY then
|
|
|
|
drawPixelInternal( startX, startY )
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-07-25 19:01:04 +00:00
|
|
|
local minX = math.min( startX, endX )
|
|
|
|
local maxX, minY, maxY
|
2017-05-01 13:32:39 +00:00
|
|
|
if minX == startX then
|
|
|
|
minY = startY
|
|
|
|
maxX = endX
|
|
|
|
maxY = endY
|
|
|
|
else
|
|
|
|
minY = endY
|
|
|
|
maxX = startX
|
|
|
|
maxY = startY
|
|
|
|
end
|
|
|
|
|
|
|
|
for x=minX,maxX do
|
|
|
|
for y=minY,maxY do
|
|
|
|
drawPixelInternal( x, y )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function drawImage( tImage, xPos, yPos )
|
2019-05-30 18:36:28 +00:00
|
|
|
expect(1, tImage, "table")
|
|
|
|
expect(2, xPos, "number")
|
|
|
|
expect(3, yPos, "number")
|
2017-05-01 13:32:39 +00:00
|
|
|
for y=1,#tImage do
|
|
|
|
local tLine = tImage[y]
|
|
|
|
for x=1,#tLine do
|
|
|
|
if tLine[x] > 0 then
|
|
|
|
term.setBackgroundColor( tLine[x] )
|
|
|
|
drawPixelInternal( x + xPos - 1, y + yPos - 1 )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|