Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- args = {...}
- local colorList = { colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.black, colors.yellow, colors.lime, colors.pink, colors.gray, colors.lightGray, colors.cyan, colors.purple, colors.blue, colors.brown, colors.green, colors.red }
- local pickedColor = {colors.blue, colors.red, colors.black}
- local buttons = { "L", "R", "M" }
- local paintSurface = {}
- local colorPickingMode = 0 -- 0 = mouse, 1 = background
- local backgroundColor = colors.black
- function setPixel(x, y, value)
- row = paintSurface[y]
- if row == nil then
- paintSurface[y] = {}
- row = paintSurface[y]
- end
- row[x] = value
- end
- function getPixel(x, y)
- row = paintSurface[y]
- if row == nil then return nil end
- return row[x]
- end
- local colorIndexes = {}
- for i=0,16 do
- colorIndexes[2^i] = i
- end
- local sColorIndex = "0123456789abcdef"
- function exportNFP(filename)
- file = io.open(filename, "w")
- for y=1, 19 do
- for x=1,51 do
- color = getPixel(x, y)
- if color == nil then
- color = " "
- else
- --print(color)
- --os.pullEvent("char")
- color = colorIndexes[color] + 1
- color = string.sub(sColorIndex, color, color)
- end
- file:write(color)
- end
- file:write("\n")
- end
- file:close()
- end
- function save(filename)
- local saveFile = {}
- saveFile.version = 1.1
- saveFile.image = paintSurface
- local savingString = textutils.serialize(saveFile)
- file = io.open(filename, "w")
- file:write(savingString)
- file:close()
- end
- function loadNFP(filename)
- local file = fs.open(filename, "r")
- paintSurface = {}
- local y = 1
- while true do
- local line = file.readLine()
- if line == nil then break end
- for i=1, #line do
- local char = string.sub(line, i, i)
- local color = nil
- local colorNumber = string.find(sColorIndex, char)
- if colorNumber ~= nil then
- color = 2^(colorNumber - 1)
- end
- if color ~= nil then
- setPixel(i, y, color)
- end
- end
- y = y + 1
- end
- file.close()
- end
- function load(filename)
- local file = io.open(filename)
- local imageText = file:read("*l")
- file:close()
- local inTable = textutils.unserialize(imageText)
- if type(inTable) ~= "table" then -- We are loading NFP file
- loadNFP(filename)
- return
- end
- if inTable.version == nil then -- We are using old save format
- for k,v in pairs(inTable) do
- local x = math.floor(k / 52)
- local y = k % 52
- setPixel(x, y, v)
- end
- return
- end
- paintSurface = {}
- paintSurface = inTable.image
- end
- function messageDialog(message)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(2, 2)
- term.write(message)
- term.setCursorPos(2, 4)
- term.write("Press any key to continue...")
- os.pullEvent("key")
- end
- function exportDialog()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(2, 2)
- term.write("Enter save name:")
- term.setCursorPos(19, 2)
- local filename = read()
- exportNFP(filename)
- messageDialog("Image exported sucessfully!")
- end
- function saveDialog()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(2, 2)
- term.write("Enter save name:")
- term.setCursorPos(19, 2)
- local filename = read()
- save(filename)
- messageDialog("Image saved sucessfully!")
- end
- function loadDialog()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(2, 2)
- term.write("Enter name of the saved picture:")
- term.setCursorPos(35, 2)
- local filename = read()
- if not fs.exists(filename) then
- messageDialog("Entered file does not exist!")
- return
- end
- load(filename)
- messageDialog("Image loaded sucessfully!")
- end
- function colorPicked(color, button)
- if colorPickingMode == 0 then
- pickedColor[button] = color
- elseif colorPickingMode == 1 then
- backgroundColor = color
- colorPickingMode = 0
- end
- end
- function handleMenuClick(x, y, button)
- if y == 3 then -- Color picker
- for k,v in pairs(colorList) do
- colorStartX = (k - 1) * 2 + 1
- if x >= colorStartX and x <= colorStartX + 2 then
- colorPicked(v, button)
- break
- end
- end
- elseif y == 5 then -- Color options
- if x >= 21 and x <= 22 then --Background
- colorPickingMode = 1
- end
- elseif y == 17 then -- Buttons
- if x >= 34 and x <= 39 then -- EXIT button
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,1)
- term.clear()
- return false
- elseif x >= 2 and x <= 6 then -- NEW button
- paintSurface = {}
- return
- elseif x >= 12 and x <= 17 then -- SAVE button
- saveDialog()
- return
- elseif x >= 23 and x <= 28 then -- LOAD button
- loadDialog()
- return
- end
- elseif y == 19 then -- Second row of buttons
- if x >= 2 and x <= 13 then -- EXPORT button
- exportDialog()
- return
- end
- end
- return true
- end
- function showMenu()
- term.setBackgroundColor(colors.black)
- while true do
- term.clear()
- term.setCursorPos(2,1)
- term.write("Color Picker:")
- local colorX = 2
- for k,v in pairs(colorList) do
- term.setCursorPos(colorX, 3)
- term.setBackgroundColor(v)
- term.write("||")
- colorX = colorX + 2
- end
- colorX = colorX + 4
- for i=1,3 do
- term.setCursorPos(colorX, 3)
- term.setBackgroundColor(pickedColor[i])
- term.write("||")
- term.setCursorPos(colorX, 2)
- term.setBackgroundColor(colors.black)
- term.write(buttons[i])
- colorX = colorX + 2
- end
- term.setBackgroundColor(colors.black)
- term.setCursorPos(2, 5)
- term.write("Background Color: ")
- term.setCursorPos(21, 5)
- if colorPickingMode == 1 then
- term.write("?")
- else
- term.setBackgroundColor(backgroundColor)
- term.write("||")
- term.setBackgroundColor(colors.black)
- end
- term.setCursorPos(2, 14)
- term.write("Options:")
- term.setCursorPos(2, 17)
- term.write("[NEW]")
- term.setCursorPos(12, 17)
- term.write("[SAVE]")
- term.setCursorPos(23, 17)
- term.write("[LOAD]")
- term.setCursorPos(34, 17)
- term.write("[EXIT]")
- term.setCursorPos(2, 19)
- term.write("[EXPORT NFP]")
- while true do
- event, button, x, y = os.pullEvent()
- if event == "mouse_click" then -- Handle clicks
- local returned = handleMenuClick(x, y, button)
- if returned == false then return false end -- We got exit. Terminate everything
- if returned == nil then return true end -- We got no return. Just close down menu.
- break
- elseif event == "key" and button == 57 then -- Return to painting if space is pressed
- return true
- end
- end
- end
- end
- function initPaintingSurface()
- term.setBackgroundColor(backgroundColor)
- term.clear()
- -- Load everything from saved image
- for y,row in pairs(paintSurface) do
- for x,color in pairs(row) do
- term.setBackgroundColor(color)
- term.setCursorPos(x, y)
- term.write(" ")
- end
- end
- end
- if args[1] ~= nil and fs.exists(args[1]) then
- load(args[1])
- end
- initPaintingSurface()
- while true do
- event, key, x, y = os.pullEvent()
- if event == "mouse_click" then
- local color = pickedColor[key]
- if color == nil then
- pickedColor = color[3]
- end
- setPixel(x, y, color)
- term.setBackgroundColor(color)
- term.setCursorPos(x, y)
- term.write(" ")
- --print(x .. " " .. y)
- elseif event == "key" and key == 57 then
- if not showMenu() then break end
- initPaintingSurface()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment