Guest User

CC Paint v1.1

a guest
Oct 16th, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.42 KB | None | 0 0
  1. args = {...}
  2.  
  3. 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 }
  4. local pickedColor = {colors.blue, colors.red, colors.black}
  5. local buttons = { "L", "R", "M" }
  6. local paintSurface = {}
  7.  
  8. local colorPickingMode = 0 -- 0 = mouse, 1 = background
  9.  
  10. local backgroundColor = colors.black
  11.  
  12. function setPixel(x, y, value)
  13.     row = paintSurface[y]
  14.     if row == nil then
  15.         paintSurface[y] = {}
  16.         row = paintSurface[y]
  17.     end
  18.    
  19.     row[x] = value
  20. end
  21.  
  22. function getPixel(x, y)
  23.     row = paintSurface[y]
  24.    
  25.     if row == nil then return nil end
  26.     return row[x]
  27. end
  28.  
  29. local colorIndexes = {}
  30. for i=0,16 do
  31.     colorIndexes[2^i] = i
  32. end
  33.  
  34. local sColorIndex = "0123456789abcdef"
  35.  
  36. function exportNFP(filename)   
  37.     file = io.open(filename, "w")
  38.    
  39.     for y=1, 19 do
  40.         for x=1,51 do
  41.             color = getPixel(x, y)
  42.             if color == nil then
  43.                 color = " "
  44.             else
  45.                 --print(color)
  46.                 --os.pullEvent("char")
  47.                 color = colorIndexes[color] + 1
  48.                 color = string.sub(sColorIndex, color, color)
  49.             end
  50.            
  51.             file:write(color)
  52.         end
  53.             file:write("\n")
  54.        
  55.     end
  56.    
  57.     file:close()
  58. end
  59.  
  60. function save(filename)
  61.     local saveFile = {}
  62.     saveFile.version = 1.1
  63.     saveFile.image = paintSurface
  64.    
  65.     local savingString = textutils.serialize(saveFile)
  66.    
  67.     file = io.open(filename, "w")
  68.     file:write(savingString)
  69.     file:close()
  70. end
  71.  
  72. function loadNFP(filename)
  73.     local file = fs.open(filename, "r")
  74.        
  75.     paintSurface = {}
  76.    
  77.     local y = 1
  78.    
  79.     while true do
  80.         local line = file.readLine()
  81.         if line == nil then break end
  82.                                
  83.         for i=1, #line do
  84.             local char = string.sub(line, i, i)
  85.            
  86.             local color = nil
  87.                        
  88.             local colorNumber = string.find(sColorIndex, char)
  89.             if colorNumber ~= nil then
  90.                 color = 2^(colorNumber - 1)
  91.             end
  92.                        
  93.             if color ~= nil then
  94.                 setPixel(i, y, color)
  95.             end
  96.            
  97.         end
  98.        
  99.         y = y + 1
  100.     end
  101.    
  102.    
  103.        
  104.     file.close()
  105. end
  106.  
  107. function load(filename)
  108.     local file = io.open(filename)
  109.     local imageText = file:read("*l")
  110.     file:close()
  111.    
  112.     local inTable = textutils.unserialize(imageText)
  113.    
  114.     if type(inTable) ~= "table" then -- We are loading NFP file
  115.         loadNFP(filename)
  116.         return
  117.     end
  118.    
  119.     if inTable.version == nil then -- We are using old save format
  120.         for k,v in pairs(inTable) do
  121.                 local x = math.floor(k / 52)
  122.                 local y = k % 52
  123.                
  124.                 setPixel(x, y, v)
  125.         end
  126.         return
  127.     end
  128.    
  129.     paintSurface = {}
  130.     paintSurface = inTable.image
  131. end
  132.  
  133. function messageDialog(message)
  134.     term.setBackgroundColor(colors.black)
  135.     term.clear()
  136.    
  137.     term.setCursorPos(2, 2)
  138.     term.write(message)
  139.     term.setCursorPos(2, 4)
  140.     term.write("Press any key to continue...")
  141.    
  142.     os.pullEvent("key")
  143. end
  144.  
  145. function exportDialog()
  146.     term.setBackgroundColor(colors.black)
  147.     term.clear()
  148.    
  149.     term.setCursorPos(2, 2)
  150.     term.write("Enter save name:")
  151.     term.setCursorPos(19, 2)
  152.    
  153.     local filename = read()
  154.    
  155.     exportNFP(filename)
  156.    
  157.     messageDialog("Image exported sucessfully!")
  158. end
  159.  
  160. function saveDialog()
  161.     term.setBackgroundColor(colors.black)
  162.     term.clear()
  163.    
  164.     term.setCursorPos(2, 2)
  165.     term.write("Enter save name:")
  166.     term.setCursorPos(19, 2)
  167.    
  168.     local filename = read()
  169.        
  170.     save(filename)
  171.    
  172.     messageDialog("Image saved sucessfully!")
  173. end
  174.  
  175.  
  176. function loadDialog()
  177.     term.setBackgroundColor(colors.black)
  178.     term.clear()
  179.    
  180.     term.setCursorPos(2, 2)
  181.     term.write("Enter name of the saved picture:")
  182.     term.setCursorPos(35, 2)
  183.    
  184.     local filename = read()
  185.    
  186.     if not fs.exists(filename) then
  187.         messageDialog("Entered file does not exist!")
  188.         return
  189.     end
  190.    
  191.     load(filename)
  192.    
  193.     messageDialog("Image loaded sucessfully!")
  194. end
  195.  
  196. function colorPicked(color, button)
  197.     if colorPickingMode == 0 then
  198.         pickedColor[button] = color
  199.     elseif colorPickingMode == 1 then
  200.         backgroundColor = color
  201.         colorPickingMode = 0
  202.     end
  203. end
  204.  
  205. function handleMenuClick(x, y, button)
  206.     if y == 3 then -- Color picker
  207.         for k,v in pairs(colorList) do
  208.             colorStartX = (k - 1) * 2 + 1
  209.             if x >= colorStartX and x <= colorStartX + 2 then
  210.                 colorPicked(v, button)
  211.                 break
  212.             end
  213.         end
  214.     elseif y == 5 then -- Color options
  215.         if x >= 21 and x <= 22 then --Background
  216.             colorPickingMode = 1
  217.         end
  218.     elseif y == 17 then -- Buttons
  219.         if x >= 34 and x <= 39 then -- EXIT button
  220.             term.setBackgroundColor(colors.black)
  221.             term.setCursorPos(1,1)
  222.             term.clear()
  223.            
  224.             return false
  225.         elseif x >= 2 and x <= 6 then -- NEW button
  226.             paintSurface = {}
  227.             return
  228.         elseif x >= 12 and x <= 17 then -- SAVE button
  229.             saveDialog()
  230.             return
  231.         elseif x >= 23 and x <= 28 then -- LOAD button
  232.             loadDialog()
  233.             return
  234.         end
  235.     elseif y == 19 then -- Second row of buttons
  236.         if x >= 2 and x <= 13 then -- EXPORT button
  237.             exportDialog()
  238.             return
  239.         end
  240.     end
  241.    
  242.     return true
  243. end
  244.  
  245. function showMenu()
  246.     term.setBackgroundColor(colors.black)
  247.  
  248.     while true do
  249.         term.clear()
  250.        
  251.         term.setCursorPos(2,1)
  252.         term.write("Color Picker:")
  253.                
  254.         local colorX = 2
  255.         for k,v in pairs(colorList) do
  256.             term.setCursorPos(colorX, 3)
  257.             term.setBackgroundColor(v)
  258.             term.write("||")
  259.            
  260.             colorX = colorX + 2
  261.         end
  262.        
  263.         colorX = colorX + 4
  264.        
  265.         for i=1,3 do
  266.             term.setCursorPos(colorX, 3)
  267.             term.setBackgroundColor(pickedColor[i])
  268.             term.write("||")
  269.            
  270.             term.setCursorPos(colorX, 2)
  271.             term.setBackgroundColor(colors.black)
  272.             term.write(buttons[i])
  273.            
  274.             colorX = colorX + 2
  275.            
  276.         end
  277.        
  278.         term.setBackgroundColor(colors.black)
  279.        
  280.         term.setCursorPos(2, 5)
  281.         term.write("Background Color: ")
  282.         term.setCursorPos(21, 5)
  283.         if colorPickingMode == 1 then
  284.             term.write("?")
  285.         else
  286.             term.setBackgroundColor(backgroundColor)
  287.             term.write("||")
  288.             term.setBackgroundColor(colors.black)
  289.         end
  290.        
  291.         term.setCursorPos(2, 14)
  292.         term.write("Options:")
  293.        
  294.         term.setCursorPos(2, 17)
  295.         term.write("[NEW]")
  296.        
  297.         term.setCursorPos(12, 17)
  298.         term.write("[SAVE]")
  299.        
  300.         term.setCursorPos(23, 17)
  301.         term.write("[LOAD]")
  302.        
  303.         term.setCursorPos(34, 17)
  304.         term.write("[EXIT]")
  305.        
  306.         term.setCursorPos(2, 19)
  307.         term.write("[EXPORT NFP]")
  308.  
  309.        
  310.         while true do
  311.             event, button, x, y = os.pullEvent()
  312.            
  313.             if event == "mouse_click" then -- Handle clicks
  314.                 local returned = handleMenuClick(x, y, button)
  315.                 if returned == false then return false end -- We got exit. Terminate everything
  316.                 if returned == nil then return true end -- We got no return. Just close down menu.
  317.                
  318.                 break
  319.             elseif event == "key" and button == 57 then -- Return to painting if space is pressed
  320.                 return true
  321.             end
  322.         end
  323.     end
  324. end
  325.  
  326. function initPaintingSurface()
  327.     term.setBackgroundColor(backgroundColor)
  328.     term.clear()
  329.    
  330.     -- Load everything from saved image
  331.     for y,row in pairs(paintSurface) do
  332.         for x,color in pairs(row) do
  333.             term.setBackgroundColor(color)
  334.                
  335.             term.setCursorPos(x, y)
  336.             term.write(" ")
  337.  
  338.         end
  339.     end
  340. end
  341.  
  342. if args[1] ~= nil and fs.exists(args[1]) then
  343.     load(args[1])
  344. end
  345.  
  346. initPaintingSurface()
  347.  
  348. while true do
  349.     event, key, x, y = os.pullEvent()
  350.    
  351.     if event == "mouse_click" then
  352.         local color = pickedColor[key]
  353.         if color == nil then
  354.             pickedColor = color[3]
  355.         end
  356.        
  357.         setPixel(x, y, color)
  358.         term.setBackgroundColor(color)
  359.         term.setCursorPos(x, y)
  360.         term.write(" ")
  361.         --print(x .. " " .. y)
  362.     elseif event == "key" and key == 57 then
  363.         if not showMenu() then break end
  364.         initPaintingSurface()
  365.     end
  366. end
Advertisement
Add Comment
Please, Sign In to add comment