1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-13 03:43:08 +00:00

Reverting requested changes. Take 1

Reverting rewrites to painutils, paint and edit - keept the paste logic change in edit.
Changed and removed misc parts as requested in PR.
This commit is contained in:
Wojbie
2017-06-28 22:58:57 +02:00
parent 6255314ba8
commit bf533dd00a
7 changed files with 444 additions and 412 deletions

View File

@@ -900,7 +900,6 @@ settings.set( "shell.allow_disk_startup", (commands == nil) )
settings.set( "shell.autocomplete", true ) settings.set( "shell.autocomplete", true )
settings.set( "edit.autocomplete", true ) settings.set( "edit.autocomplete", true )
settings.set( "edit.default_extension", "lua" ) settings.set( "edit.default_extension", "lua" )
settings.set( "edit.advanced_functionality", false )
settings.set( "paint.default_extension", "nfp" ) settings.set( "paint.default_extension", "nfp" )
settings.set( "lua.autocomplete", true ) settings.set( "lua.autocomplete", true )
settings.set( "list.show_hidden", false ) settings.set( "list.show_hidden", false )

View File

@@ -1,20 +1,12 @@
local setPos, write, setCol, blit, rep, concat = term.setCursorPos, term.write, term.setBackgroundColour, term.blit, string.rep, table.concat
local maxn = table.maxn or function( tTable ) local function drawPixelInternal( xPos, yPos )
local maxn = 0 term.setCursorPos( xPos, yPos )
for n in pairs( tTable ) do term.write(" ")
if type( n ) == "number" and n > maxn then
maxn = n
end
end
return maxn
end end
local tColourLookup = {} local tColourLookup = {}
local tColourReverseLookup = {}
for n=1,16 do for n=1,16 do
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1) tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
tColourReverseLookup[ 2^(n-1) ] = string.sub( "0123456789abcdef",n,n )
end end
function loadImage( sPath ) function loadImage( sPath )
@@ -24,64 +16,30 @@ function loadImage( sPath )
local tImage = {} local tImage = {}
if fs.exists( sPath ) then if fs.exists( sPath ) then
for sLine in io.lines(sPath) do local file = io.open(sPath, "r" )
local sLine = file:read()
while sLine do
local tLine = {} local tLine = {}
for x=1,#sLine do for x=1,sLine:len() do
tLine[x] = tColourLookup[ string.byte( sLine,x ) ] or 0 tLine[x] = tColourLookup[ string.byte(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( "bad argument #1 (expected table, got " .. type( tImage ) .. ")", 2 ) end
if type( sPath ) ~= "string" then error( "bad argument #2 (expected string, got " .. type( sPath ) .. ")", 2 ) end
local file, lines, lastRow = fs.open(sPath, "w" ), {}, 0
if not file then return false end
for y=1,maxn( tImage ) do
local tIn, tOut, lastCol = tImage[y], {}, 0
if tIn then
for x=1,maxn( tIn ) do
local pixel = tColourReverseLookup[ tIn[x] ]
if pixel then
tOut[x], lastCol = pixel, x
else
tOut[x] = " "
end
end
end
if lastCol > 0 then
lines[y], lastRow = concat( tOut, "", 1, lastCol ), y
else
lines[y] = ""
end
end
for y=1,lastRow do
file.writeLine(lines[y])
end
file.close()
return true
end
function drawPixel( xPos, yPos, nColour ) function drawPixel( xPos, yPos, nColour )
if type( xPos ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( xPos ) .. ")", 2 ) end if type( xPos ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( xPos ) .. ")", 2 ) end
if type( yPos ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( yPos ) .. ")", 2 ) end if type( yPos ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( yPos ) .. ")", 2 ) end
if nColour ~= nil and type( nColour ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( nColour ) .. ")", 2 ) end if nColour ~= nil and type( nColour ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( nColour ) .. ")", 2 ) end
if nColour then if nColour then
setCol( nColour ) term.setBackgroundColor( nColour )
end end
setPos( xPos, yPos ) drawPixelInternal( xPos, yPos )
write( " " )
end end
function drawLine( startX, startY, endX, endY, nColour ) function drawLine( startX, startY, endX, endY, nColour )
@@ -97,15 +55,14 @@ function drawLine( startX, startY, endX, endY, nColour )
endY = math.floor(endY) endY = math.floor(endY)
if nColour then if nColour then
setCol( nColour ) term.setBackgroundColor( nColour )
end end
if startX == endX and startY == endY then if startX == endX and startY == endY then
setPos( startX, startY ) drawPixelInternal( startX, startY )
write(" ")
return return
end end
local minX, minY, maxX, maxY = math.min( startX, endX ) local minX = math.min( startX, endX )
if minX == startX then if minX == startX then
minY = startY minY = startY
maxX = endX maxX = endX
@@ -120,28 +77,27 @@ function drawLine( startX, startY, endX, endY, nColour )
local xDiff = maxX - minX local xDiff = maxX - minX
local yDiff = maxY - minY local yDiff = maxY - minY
if minY == maxY then
setPos( minX, minY )
write( rep( " ", xDiff + 1 ) )
return
end
if xDiff > math.abs(yDiff) then if xDiff > math.abs(yDiff) then
local y = minY local y = minY
local dy = yDiff / xDiff local dy = yDiff / xDiff
for x=minX,maxX do for x=minX,maxX do
setPos( x, math.floor( y + 0.5 ) ) drawPixelInternal( x, math.floor( y + 0.5 ) )
write( " " )
y = y + dy y = y + dy
end end
else else
local x, mul = minX, maxY >= minY and 1 or -1 local x = minX
local dx = xDiff / yDiff * mul local dx = xDiff / yDiff
for y=minY,maxY,mul do if maxY >= minY then
setPos( math.floor( x + 0.5 ), y ) for y=minY,maxY do
write( " " ) drawPixelInternal( math.floor( x + 0.5 ), y )
x = x + dx 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 end
end end
@@ -159,29 +115,34 @@ function drawBox( startX, startY, endX, endY, nColour )
endY = math.floor(endY) endY = math.floor(endY)
if nColour then if nColour then
setCol( nColour ) term.setBackgroundColor( nColour )
end end
if startX == endX and startY == endY then if startX == endX and startY == endY then
setPos( startX, startY ) drawPixelInternal( startX, startY )
write( " " )
return return
end end
local minX, minY, maxX, maxY
if startX < endX then minX, maxX = startX, endX else minX, maxX = endX, startX end
if startY < endY then minY, maxY = startY, endY else minY, maxY = endY, startY end
local sStr = rep( " ", maxX - minX + 1 ) local minX = math.min( startX, endX )
setPos( minX, minY ) if minX == startX then
write( sStr ) minY = startY
setPos( minX, maxY ) maxX = endX
write( sStr ) maxY = endY
else
for y=(minY+1),(maxY-1) do minY = endY
setPos( minX, y ) maxX = startX
write( " " ) maxY = startY
setPos( maxX, y ) end
write( " " )
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
end end
@@ -198,22 +159,28 @@ function drawFilledBox( startX, startY, endX, endY, nColour )
endY = math.floor(endY) endY = math.floor(endY)
if nColour then if nColour then
setCol( nColour ) term.setBackgroundColor( nColour )
end end
if startX == endX and startY == endY then if startX == endX and startY == endY then
setPos( startX, startY ) drawPixelInternal( startX, startY )
write( " " )
return return
end end
local minX, minY, maxX, maxY local minX = math.min( startX, endX )
if startX < endX then minX, maxX = startX, endX else minX, maxX = endX, startX end if minX == startX then
if startY < endY then minY, maxY = startY, endY else minY, maxY = endY, startY end minY = startY
maxX = endX
maxY = endY
else
minY = endY
maxX = startX
maxY = startY
end
local sStr = rep( " ", maxX - minX + 1 ) for x=minX,maxX do
for y=minY,maxY do for y=minY,maxY do
setPos( minX, y ) drawPixelInternal( x, y )
write( sStr ) end
end end
end end
@@ -222,18 +189,12 @@ function drawImage( tImage, xPos, yPos )
if type( xPos ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( xPos ) .. ")", 2 ) end if type( xPos ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( xPos ) .. ")", 2 ) end
if type( yPos ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( yPos ) .. ")", 2 ) end if type( yPos ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( yPos ) .. ")", 2 ) end
for y=1,#tImage do for y=1,#tImage do
local tLine, sBG, counter = tImage[y], {}, 0 local tLine = tImage[y]
if tLine then for x=1,#tLine+1 do for x=1,#tLine do
local px = tLine[x] or 0 if tLine[x] > 0 then
if px > 0 then term.setBackgroundColor( tLine[x] )
counter = counter + 1 drawPixelInternal( x + xPos - 1, y + yPos - 1 )
sBG[counter] = tColourReverseLookup[ px ]
elseif counter > 0 then
setPos( x + xPos - 1 - counter, y + yPos - 1 )
local sT = rep( " ", counter )
blit( sT, sT, concat( sBG ) )
sBG, counter = {}, 0
end end
end end end
end end
end end

View File

@@ -7,14 +7,14 @@ local tReceivedMessageTimeouts = {}
local tHostnames = {} local tHostnames = {}
function open( sModem ) function open( sModem )
if type( sModem ) ~= "string" then if type( sModem ) ~= "string" then
error( "expected string", 2 ) error( "expected string", 2 )
end end
if peripheral.getType( sModem ) ~= "modem" then if peripheral.getType( sModem ) ~= "modem" then
error( "No such modem: "..sModem, 2 ) error( "No such modem: "..sModem, 2 )
end end
peripheral.call( sModem, "open", os.getComputerID() ) peripheral.call( sModem, "open", os.getComputerID() )
peripheral.call( sModem, "open", CHANNEL_BROADCAST ) peripheral.call( sModem, "open", CHANNEL_BROADCAST )
end end
function close( sModem ) function close( sModem )
@@ -55,7 +55,7 @@ function isOpen( sModem )
end end
end end
end end
return false return false
end end
function send( nRecipient, message, sProtocol ) function send( nRecipient, message, sProtocol )
@@ -93,7 +93,7 @@ function send( nRecipient, message, sProtocol )
end end
function broadcast( message, sProtocol ) function broadcast( message, sProtocol )
send( CHANNEL_BROADCAST, message, sProtocol ) send( CHANNEL_BROADCAST, message, sProtocol )
end end
function receive( sProtocolFilter, nTimeout ) function receive( sProtocolFilter, nTimeout )
@@ -103,31 +103,31 @@ function receive( sProtocolFilter, nTimeout )
end end
-- Start the timer -- Start the timer
local timer = nil local timer = nil
local sFilter = nil local sFilter = nil
if nTimeout then if nTimeout then
timer = os.startTimer( nTimeout ) timer = os.startTimer( nTimeout )
sFilter = nil sFilter = nil
else else
sFilter = "rednet_message" sFilter = "rednet_message"
end end
-- Wait for events -- Wait for events
while true do while true do
local sEvent, p1, p2, p3 = os.pullEvent( sFilter ) local sEvent, p1, p2, p3 = os.pullEvent( sFilter )
if sEvent == "rednet_message" then if sEvent == "rednet_message" then
-- Return the first matching rednet_message -- Return the first matching rednet_message
local nSenderID, message, sProtocol = p1, p2, p3 local nSenderID, message, sProtocol = p1, p2, p3
if sProtocolFilter == nil or sProtocol == sProtocolFilter then if sProtocolFilter == nil or sProtocol == sProtocolFilter then
return nSenderID, message, sProtocol return nSenderID, message, sProtocol
end end
elseif sEvent == "timer" then elseif sEvent == "timer" then
-- Return nil if we timeout -- Return nil if we timeout
if p1 == timer then if p1 == timer then
return nil return nil
end end
end end
end end
end end
function host( sProtocol, sHostname ) function host( sProtocol, sHostname )
@@ -219,41 +219,41 @@ end
local bRunning = false local bRunning = false
function run() function run()
if bRunning then if bRunning then
error( "rednet is already running", 2 ) error( "rednet is already running", 2 )
end end
bRunning = true bRunning = true
while bRunning do while bRunning do
local sEvent, p1, p2, p3, p4 = os.pullEventRaw() local sEvent, p1, p2, p3, p4 = os.pullEventRaw()
if sEvent == "modem_message" then if sEvent == "modem_message" then
-- Got a modem message, process it and add it to the rednet event queue -- Got a modem message, process it and add it to the rednet event queue
local sModem, nChannel, nReplyChannel, tMessage = p1, p2, p3, p4 local sModem, nChannel, nReplyChannel, tMessage = p1, p2, p3, p4
if isOpen( sModem ) and ( nChannel == os.getComputerID() or nChannel == CHANNEL_BROADCAST ) then if isOpen( sModem ) and ( nChannel == os.getComputerID() or nChannel == CHANNEL_BROADCAST ) then
if type( tMessage ) == "table" and tMessage.nMessageID then if type( tMessage ) == "table" and tMessage.nMessageID then
if not tReceivedMessages[ tMessage.nMessageID ] then if not tReceivedMessages[ tMessage.nMessageID ] then
tReceivedMessages[ tMessage.nMessageID ] = true tReceivedMessages[ tMessage.nMessageID ] = true
tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = tMessage.nMessageID tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = tMessage.nMessageID
os.queueEvent( "rednet_message", nReplyChannel, tMessage.message, tMessage.sProtocol ) os.queueEvent( "rednet_message", nReplyChannel, tMessage.message, tMessage.sProtocol )
end end
end end
end end
elseif sEvent == "rednet_message" then elseif sEvent == "rednet_message" then
-- Got a rednet message (queued from above), respond to dns lookup -- Got a rednet message (queued from above), respond to dns lookup
local nSenderID, tMessage, sProtocol = p1, p2, p3 local nSenderID, tMessage, sProtocol = p1, p2, p3
if sProtocol == "dns" and type(tMessage) == "table" and tMessage.sType == "lookup" then if sProtocol == "dns" and type(tMessage) == "table" and tMessage.sType == "lookup" then
local sHostname = tHostnames[ tMessage.sProtocol ] local sHostname = tHostnames[ tMessage.sProtocol ]
if sHostname ~= nil and (tMessage.sHostname == nil or tMessage.sHostname == sHostname) then if sHostname ~= nil and (tMessage.sHostname == nil or tMessage.sHostname == sHostname) then
rednet.send( nSenderID, { rednet.send( nSenderID, {
sType = "lookup response", sType = "lookup response",
sHostname = sHostname, sHostname = sHostname,
sProtocol = tMessage.sProtocol, sProtocol = tMessage.sProtocol,
}, "dns" ) }, "dns" )
end end
end end
elseif sEvent == "timer" then elseif sEvent == "timer" then
-- Got a timer event, use it to clear the event queue -- Got a timer event, use it to clear the event queue
local nTimer = p1 local nTimer = p1
local nMessage = tReceivedMessageTimeouts[ nTimer ] local nMessage = tReceivedMessageTimeouts[ nTimer ]
@@ -261,6 +261,6 @@ function run()
tReceivedMessageTimeouts[ nTimer ] = nil tReceivedMessageTimeouts[ nTimer ] = nil
tReceivedMessages[ nMessage ] = nil tReceivedMessages[ nMessage ] = nil
end end
end end
end end
end end

View File

@@ -1,6 +1,4 @@
edit is a text editor for creating or modifying programs or text files. After creating a program with edit, type its filename in the shell to run it. You can open any of the builtin programs with edit to learn how to program. edit is a text editor for creating or modifying programs or text files. After creating a program with edit, type its filename in the shell to run it. You can open any of the builtin programs with edit to learn how to program.
ex: ex:
"edit hello" opens a file called "hello" for editing. "edit hello" opens a file called "hello" for editing.
By changing setting "edit.advanced_functionality" to true you can unlock ability to jump to specific line in longer files.

View File

@@ -59,9 +59,6 @@ end
if peripheral.find( "printer" ) then if peripheral.find( "printer" ) then
table.insert( tMenuItems, "Print" ) table.insert( tMenuItems, "Print" )
end end
if settings.get( "edit.advanced_functionality" ) then
table.insert( tMenuItems, "Jump" )
end
table.insert( tMenuItems, "Exit" ) table.insert( tMenuItems, "Exit" )
local sStatus = "Press Ctrl to access menu" local sStatus = "Press Ctrl to access menu"
@@ -137,65 +134,39 @@ local tKeywords = {
["while"] = true, ["while"] = true,
} }
local tHex = { local function tryWrite( sLine, regex, colour )
[ colors.white ] = "0", local match = string.match( sLine, regex )
[ colors.orange ] = "1", if match then
[ colors.magenta ] = "2", if type(colour) == "number" then
[ colors.lightBlue ] = "3", term.setTextColour( colour )
[ colors.yellow ] = "4", else
[ colors.lime ] = "5", term.setTextColour( colour(match) )
[ colors.pink ] = "6", end
[ colors.gray ] = "7", term.write( match )
[ colors.lightGray ] = "8", term.setTextColour( textColour )
[ colors.cyan ] = "9", return string.sub( sLine, string.len(match) + 1 )
[ colors.purple ] = "a", end
[ colors.blue ] = "b", return nil
[ colors.brown ] = "c", end
[ colors.green ] = "d",
[ colors.red ] = "e",
[ colors.black ] = "f",
}
local tPattern = {
[ 1 ] = "^(%-%-%[%[.-%]%])()",
[ 2 ] = "^(%-%-.*)()",
[ 3 ] = "^(\"\")()",
[ 4 ] = "^(\".-[^\\]\")()",
[ 5 ] = "^(\'\')()",
[ 6 ] = "^(\'.-[^\\]\')()",
[ 7 ] = "^(%[%[.-%]%])()",
[ 8 ] = "^([%w_]+)()",
[ 9 ] = "^([^%w_])()",
}
local tPatternColors = {
[ 1 ] = commentColour,
[ 2 ] = commentColour,
[ 3 ] = stringColour,
[ 4 ] = stringColour,
[ 5 ] = stringColour,
[ 6 ] = stringColour,
[ 7 ] = stringColour,
[ 8 ] = function( sMatch ) return tKeywords[ sMatch ] and keywordColour or textColour end,
[ 9 ] = textColour,
}
local function writeHighlighted( sLine ) local function writeHighlighted( sLine )
local tColor = {} while string.len(sLine) > 0 do
local nPosition = 1 sLine =
local sMatch,nNextPosition tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or
local nColor tryWrite( sLine, "^%-%-.*", commentColour ) or
while nPosition <= #sLine do tryWrite( sLine, "^\"\"", stringColour ) or
local i = 0 tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or
repeat tryWrite( sLine, "^\'\'", stringColour ) or
i=i+1 tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or
sMatch,nNextPosition = string.match( sLine, tPattern[i] , nPosition) tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or
until sMatch tryWrite( sLine, "^[%w_]+", function( match )
nColor = tPatternColors[i] if tKeywords[ match ] then
tColor[#tColor+1] = string.rep( tHex[ type(nColor) == "number" and nColor or nColor(sMatch) ] , nNextPosition - nPosition) return keywordColour
nPosition = nNextPosition end
return textColour
end ) or
tryWrite( sLine, "^[^%w_]", textColour )
end end
term.blit( sLine, table.concat( tColor ), string.rep( tHex[bgColour], #sLine ) )
end end
local tCompletions local tCompletions
@@ -204,7 +175,7 @@ local nCompletion
local tCompleteEnv = _ENV local tCompleteEnv = _ENV
local function complete( sLine ) local function complete( sLine )
if settings.get( "edit.autocomplete" ) then if settings.get( "edit.autocomplete" ) then
local nStartPos = string.find( sLine, "[a-zA-Z0-9_%.:]+$" ) local nStartPos = string.find( sLine, "[a-zA-Z0-9_%.]+$" )
if nStartPos then if nStartPos then
sLine = string.sub( sLine, nStartPos ) sLine = string.sub( sLine, nStartPos )
end end
@@ -311,47 +282,6 @@ local function redrawMenu()
term.setCursorPos( x - scrollX, y - scrollY ) term.setCursorPos( x - scrollX, y - scrollY )
end end
local function setCursor( newX, newY )
local oldX, oldY = x, y
x, y = newX, newY
local screenX = x - scrollX
local screenY = y - scrollY
local bRedraw = false
if screenX < 1 then
scrollX = x - 1
screenX = 1
bRedraw = true
elseif screenX > w then
scrollX = x - w
screenX = w
bRedraw = true
end
if screenY < 1 then
scrollY = y - 1
screenY = 1
bRedraw = true
elseif screenY > h-1 then
scrollY = y - (h-1)
screenY = h-1
bRedraw = true
end
recomplete()
if bRedraw then
redrawText()
elseif y ~= oldY then
redrawLine( oldY )
redrawLine( y )
else
redrawLine( y )
end
term.setCursorPos( screenX, screenY )
redrawMenu()
end
local tMenuFuncs = { local tMenuFuncs = {
Save = function() Save = function()
if bReadOnly then if bReadOnly then
@@ -465,26 +395,7 @@ local tMenuFuncs = {
sStatus="Error saving to "..sTempPath sStatus="Error saving to "..sTempPath
end end
redrawMenu() redrawMenu()
end, end
Jump = function()
term.setCursorPos( 1, h )
term.clearLine()
term.setTextColour( highlightColour )
term.write( "Line : " )
term.setTextColour( textColour )
local nLine = tonumber(read())
if type(nLine) == "number" then
nLine = math.min(math.max( 1, nLine ), #tLines )
setCursor( 1, nLine )
sStatus="Jumped to line "..nLine
redrawMenu()
redrawText()
else
sStatus="Invalid line number"
redrawMenu()
redrawText()
end
end,
} }
local function doMenuItem( _n ) local function doMenuItem( _n )
@@ -496,6 +407,47 @@ local function doMenuItem( _n )
redrawMenu() redrawMenu()
end end
local function setCursor( newX, newY )
local oldX, oldY = x, y
x, y = newX, newY
local screenX = x - scrollX
local screenY = y - scrollY
local bRedraw = false
if screenX < 1 then
scrollX = x - 1
screenX = 1
bRedraw = true
elseif screenX > w then
scrollX = x - w
screenX = w
bRedraw = true
end
if screenY < 1 then
scrollY = y - 1
screenY = 1
bRedraw = true
elseif screenY > h-1 then
scrollY = y - (h-1)
screenY = h-1
bRedraw = true
end
recomplete()
if bRedraw then
redrawText()
elseif y ~= oldY then
redrawLine( oldY )
redrawLine( y )
else
redrawLine( y )
end
term.setCursorPos( screenX, screenY )
redrawMenu()
end
-- Actual program functionality begins -- Actual program functionality begins
load(sPath) load(sPath)

View File

@@ -1,4 +1,4 @@
-- Paint created by nitrogenfingers (edited by dan200 & co) -- Paint created by nitrogenfingers (edited by dan200)
-- http://www.youtube.com/user/NitrogenFingers -- http://www.youtube.com/user/NitrogenFingers
------------ ------------
@@ -21,15 +21,9 @@ local mChoices = { "Save","Exit" }
-- The message displayed in the footer bar -- The message displayed in the footer bar
local fMessage = "Press Ctrl to access menu" local fMessage = "Press Ctrl to access menu"
-- Colour to character conversions -------------------------
local tColourLookup = {} -- Initialisation --
for n=1,16 do -------------------------
tColourLookup[ 2^(n-1) ] = string.sub( "0123456789abcdef",n,n )
end
-----------------------
-- Pre-Flight Checks --
-----------------------
-- Determine if we can even run this -- Determine if we can even run this
if not term.isColour() then if not term.isColour() then
@@ -38,13 +32,14 @@ if not term.isColour() then
end end
-- Determines if the file exists, and can be edited on this computer -- Determines if the file exists, and can be edited on this computer
local sPath = ... local tArgs = {...}
if not sPath then if #tArgs == 0 then
print("Usage: paint <path>") print("Usage: paint <path>")
return return
end end
sPath = shell.resolve( sPath ) local sPath = shell.resolve(tArgs[1])
if fs.isDir( sPath ) then local bReadOnly = fs.isReadOnly(sPath)
if fs.exists(sPath) and fs.isDir(sPath) then
print("Cannot edit a directory.") print("Cannot edit a directory.")
return return
end end
@@ -57,14 +52,117 @@ if not fs.exists( sPath ) and not string.find( sPath, "%." ) then
end end
end end
local bReadOnly = fs.isReadOnly(sPath)
--------------- ---------------
-- Functions -- -- Functions --
--------------- ---------------
local function getCanvasPixel( x, y )
if canvas[y] then
return canvas[y][x]
end
return nil
end
--[[
Converts a colour value to a text character
params: colour = the number to convert to a hex value
returns: a string representing the chosen colour
]]
local function getCharOf( colour )
-- Incorrect values always convert to nil
if type(colour) == "number" then
local value = math.floor( math.log(colour) / math.log(2) ) + 1
if value >= 1 and value <= 16 then
return string.sub( "0123456789abcdef", value, value )
end
end
return " "
end
--[[
Converts a text character to colour value
params: char = the char (from string.byte) to convert to number
returns: the colour number of the hex value
]]
local tColourLookup = {}
for n=1,16 do
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
end
local function getColourOf( char )
-- Values not in the hex table are transparent (canvas coloured)
return tColourLookup[char]
end
--[[
Loads the file into the canvas
params: path = the path of the file to open
returns: nil
]]
local function load(path)
-- Load the file
if fs.exists(path) then
local file = fs.open(sPath, "r")
local sLine = file.readLine()
while sLine do
local line = {}
for x=1,w-2 do
line[x] = getColourOf( string.byte(sLine,x,x) )
end
table.insert( canvas, line )
sLine = file.readLine()
end
file.close()
end
end
--[[ --[[
Draws colour picker sidebar, the palette and the footer Saves the current canvas to file
params: path = the path of the file to save
returns: true if save was successful, false otherwise
]]
local function save(path)
-- Open file
local sDir = string.sub(sPath, 1, #sPath - #fs.getName(sPath))
if not fs.exists(sDir) then
fs.makeDir(sDir)
end
local file = fs.open( path, "w" )
if not file then
return false
end
-- Encode (and trim)
local tLines = {}
local nLastLine = 0
for y=1,h-1 do
local sLine = ""
local nLastChar = 0
for x=1,w-2 do
local c = getCharOf( getCanvasPixel( x, y ) )
sLine = sLine .. c
if c ~= " " then
nLastChar = x
end
end
sLine = string.sub( sLine, 1, nLastChar )
tLines[y] = sLine
if string.len( sLine ) > 0 then
nLastLine = y
end
end
-- Save out
for n=1,nLastLine do
file.writeLine( tLines[ n ] )
end
file.close()
return true
end
--[[
Draws colour picker sidebar, the pallette and the footer
returns: nil returns: nil
]] ]]
local function drawInterface() local function drawInterface()
@@ -88,22 +186,24 @@ local function drawInterface()
term.write("\127\127") term.write("\127\127")
-- Left and Right Selected Colours -- Left and Right Selected Colours
term.setCursorPos(w-1, 18) for i=18,18 do
if leftColour ~= nil then term.setCursorPos(w-1, i)
term.setBackgroundColour( leftColour ) if leftColour ~= nil then
term.write(" ") term.setBackgroundColour( leftColour )
else term.write(" ")
term.setBackgroundColour( canvasColour ) else
term.setTextColour( colours.grey ) term.setBackgroundColour( canvasColour )
term.write("\127") term.setTextColour( colours.grey )
end term.write("\127")
if rightColour ~= nil then end
term.setBackgroundColour( rightColour ) if rightColour ~= nil then
term.write(" ") term.setBackgroundColour( rightColour )
else term.write(" ")
term.setBackgroundColour( canvasColour ) else
term.setTextColour( colours.grey ) term.setBackgroundColour( canvasColour )
term.write("\127") term.setTextColour( colours.grey )
term.write("\127")
end
end end
-- Padding -- Padding
@@ -114,24 +214,41 @@ local function drawInterface()
end end
end end
--[[
Converts a single pixel of a single line of the canvas and draws it
returns: nil
]]
local function drawCanvasPixel( x, y )
local pixel = getCanvasPixel( x, y )
if pixel then
term.setBackgroundColour( pixel or canvasColour )
term.setCursorPos(x, y)
term.write(" ")
else
term.setBackgroundColour( canvasColour )
term.setTextColour( colours.grey )
term.setCursorPos(x, y)
term.write("\127")
end
end
--[[
Converts each colour in a single line of the canvas and draws it
returns: nil
]]
local function drawCanvasLine( y )
for x = 1, w-2 do
drawCanvasPixel( x, y )
end
end
--[[ --[[
Converts each colour in the canvas and draws it Converts each colour in the canvas and draws it
returns: nil returns: nil
]] ]]
local function drawCanvas() local function drawCanvas()
local TC = string.rep( "7", w-2 )
for y = 1, h-1 do for y = 1, h-1 do
local T, BC = {}, {} drawCanvasLine( y )
for x = 1, w-2 do
local pixel = canvas[y] and canvas[y][x]
if pixel and pixel ~= 0 then
T[x], BC[x] = " ", tColourLookup[pixel]
else
T[x], BC[x] = "\127", tColourLookup[canvasColour]
end
end
term.setCursorPos( 1, y )
term.blit( table.concat( T ), TC, table.concat( BC ) )
end end
end end
@@ -142,15 +259,22 @@ end
local function accessMenu() local function accessMenu()
-- Selected menu option -- Selected menu option
local selection = 1 local selection = 1
term.setTextColour(colours.white)
term.setBackgroundColour(colours.black) term.setBackgroundColour(colours.black)
while true do while true do
-- Draw the menu -- Draw the menu
term.setCursorPos(1,h) term.setCursorPos(1,h)
term.clearLine() term.clearLine()
term.setTextColour(colours.white)
for k,v in pairs(mChoices) do for k,v in pairs(mChoices) do
if selection==k then if selection==k then
term.blit( "["..v.."]", "4"..string.rep(" ",#v).."4", string.rep("f",#v+2) ) term.setTextColour(colours.yellow)
local ox,_ = term.getCursorPos()
term.write("["..string.rep(" ",#v).."]")
term.setCursorPos(ox+1,h)
term.setTextColour(colours.white)
term.write(v)
term.setCursorPos(term.getCursorPos()+1,h)
else else
term.write(" "..v.." ") term.write(" "..v.." ")
end end
@@ -158,39 +282,51 @@ local function accessMenu()
-- Handle input in the menu -- Handle input in the menu
local id,key = os.pullEvent("key") local id,key = os.pullEvent("key")
if id == "key" then
-- S and E are shortcuts -- S and E are shortcuts
if key == keys.s then if key == keys.s then
selection = 1 selection = 1
key = keys.enter key = keys.enter
elseif key == keys.e then elseif key == keys.e then
selection = 2 selection = 2
key = keys.enter key = keys.enter
end end
if key == keys.right then if key == keys.right then
-- Move right -- Move right
selection = selection == #mChoices and 1 or (selection + 1) selection = selection + 1
if selection > #mChoices then
elseif key == keys.left then selection = 1
-- Move left end
selection = selection == 1 and #mChoices or (selection - 1)
elseif key == keys.left and selection > 1 then
elseif key == keys.enter then -- Move left
-- Select an option selection = selection - 1
if mChoices[selection]=="Save" then if selection < 1 then
if bReadOnly then selection = #mChoices
fMessage = "Access denied" end
return false
end elseif key == keys.enter then
fMessage = (paintutils.saveImage( canvas, sPath ) and "Saved to " or "Error saving to ")..sPath -- Select an option
return false if mChoices[selection]=="Save" then
elseif mChoices[selection]=="Exit" then if bReadOnly then
return true fMessage = "Access denied"
return false
end
local success = save(sPath)
if success then
fMessage = "Saved to "..sPath
else
fMessage = "Error saving to "..sPath
end
return false
elseif mChoices[selection]=="Exit" then
return true
end
elseif key == keys.leftCtrl or keys == keys.rightCtrl then
-- Cancel the menu
return false
end end
elseif key == keys.leftCtrl or keys == keys.rightCtrl then
-- Cancel the menu
return false
end end
end end
end end
@@ -201,10 +337,11 @@ end
returns: nil returns: nil
]] ]]
local function handleEvents() local function handleEvents()
while true do local programActive = true
while programActive do
local id,p1,p2,p3 = os.pullEvent() local id,p1,p2,p3 = os.pullEvent()
if id=="mouse_click" or id=="mouse_drag" and p1 < 3 and p2 > 0 and p2 <= w and p3 > 0 and p3 < h then if id=="mouse_click" or id=="mouse_drag" then
if p2 >= w-1 and p3 <= 17 then if p2 >= w-1 and p3 >= 1 and p3 <= 17 then
if id ~= "mouse_drag" then if id ~= "mouse_drag" then
-- Selecting an items in the colour picker -- Selecting an items in the colour picker
if p3 <= 16 then if p3 <= 16 then
@@ -220,14 +357,15 @@ local function handleEvents()
rightColour = nil rightColour = nil
end end
end end
--drawCanvas()
drawInterface() drawInterface()
end end
elseif p2 < w-1 then elseif p2 < w-1 and p3 <= h-1 then
-- Clicking on the canvas -- Clicking on the canvas
local paintColour local paintColour = nil
if p1==1 then if p1==1 then
paintColour = leftColour paintColour = leftColour
else elseif p1==2 then
paintColour = rightColour paintColour = rightColour
end end
if not canvas[p3] then if not canvas[p3] then
@@ -235,16 +373,11 @@ local function handleEvents()
end end
canvas[p3][p2] = paintColour canvas[p3][p2] = paintColour
term.setCursorPos( p2, p3 ) drawCanvasPixel( p2, p3 )
if paintColour then
term.blit( " ", " ", tColourLookup[paintColour] )
else
term.blit( "\127", "7", tColourLookup[canvasColour] )
end
end end
elseif id=="key" then elseif id=="key" then
if p1==keys.leftCtrl or p1==keys.rightCtrl then if p1==keys.leftCtrl or p1==keys.rightCtrl then
if accessMenu() then return end programActive = not accessMenu()
drawInterface() drawInterface()
end end
elseif id=="term_resize" then elseif id=="term_resize" then
@@ -255,24 +388,15 @@ local function handleEvents()
end end
end end
-------------------- -- Init
-- Initialisation -- load(sPath)
--------------------
if fs.exists( sPath ) then canvas = paintutils.loadImage( sPath ) end
drawCanvas() drawCanvas()
drawInterface() drawInterface()
--------------- -- Main loop
-- Main Loop --
---------------
handleEvents() handleEvents()
-------------- -- Shutdown
-- Shutdown --
--------------
term.setBackgroundColour(colours.black) term.setBackgroundColour(colours.black)
term.setTextColour(colours.white) term.setTextColour(colours.white)
term.clear() term.clear()

View File

@@ -208,9 +208,7 @@ shell.setCompletionFunction( "rom/programs/command/exec.lua", completeExec )
if turtle then if turtle then
local tGoOptions = { "left", "right", "forward", "back", "down", "up" } local tGoOptions = { "left", "right", "forward", "back", "down", "up" }
local function completeGo( shell, nIndex, sText ) local function completeGo( shell, nIndex, sText )
if nIndex == 1 then return completeMultipleChoice(sText,tGoOptions)
return completeMultipleChoice(sText,tGoOptions)
end
end end
local tTurnOptions = { "left", "right" } local tTurnOptions = { "left", "right" }
local function completeTurn( shell, nIndex, sText ) local function completeTurn( shell, nIndex, sText )