Advertisement
HPWebcamAble

[CC][3.3] Advanced Paint

Dec 15th, 2013
3,420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.40 KB | None | 0 0
  1. --[[
  2. Coded by HPWebcamAble
  3.  
  4. Name: Advanced Paint
  5. Description: The CC Paint program, but made for monitors!
  6. ComputerCraft Forum Post:
  7. http://www.computercraft.info/forums2/index.php?/topic/21558-advanced-paint
  8. Youtube Video:
  9. https://youtu.be/8jiNTgAaeos
  10.  
  11. Version History:
  12. ####### 3.3 #######
  13. -Bug Fixes
  14.   *ACTUALLY fixed monitor reassigning. Promise :)
  15. -You can no longer try to edit or draw a directory (oops)
  16. -Program now stops if a monitor is disconected or resized (a block is added or removed)
  17. -When reassinging monitors, the size is given in blocks and pixels
  18. -Added some more comments (in case somone is trying to figure out what I did)
  19.  
  20. ####### 3.2 #######
  21. -Bug Fixes
  22.   *Reassigning a monitor sometimes didn't work
  23. -Slightly tweaked reassigning monitors
  24.  
  25. ####### 3.1 #######
  26. -Cleaned up Code
  27.   *Removed a log method that wasn't used
  28.   *Changed the way the screen is drawn
  29.   *Implemented some debug errors
  30. -Added the Utiltiy Monitor!
  31.   *change color
  32.   *turn on and off clear mode
  33.   *change the utility monitor
  34.   *and save/exit from a monitor!
  35. -Fixed some bugs
  36.   *invalid key to next when removing a remote monitor from the save
  37.  
  38. ####### 3.0 and below #######
  39. Seperate programs
  40. ]]
  41.  
  42. --Variables--
  43. args = {...}
  44. w,h = term.getSize()
  45. local fileName = "ap" --What this file should be called
  46. local offset
  47. local image = {} --a table of the monitors, which are a table of the pixels
  48. monitors = {} --a table of the monitors
  49. local dColor = colors.white --The default color of the monitors
  50. local cColor = colors.black --The current color
  51. local cColorPos = 16
  52. local clearM = false --Clear Mode - Touching a monitor with this on clears that monitor
  53. local lastColor = 32768 --the color black
  54. local rMonitors = {}
  55. local utilMonitor --The utility monitor, when the program starts, there isn't one
  56. local changeUtilMon = false --If the utility monitor is being changed or not
  57. local skip = {}
  58.  
  59. local _color = 1
  60. colorPos = {} -- Table with colors to know which color was clicked
  61. for i = 1, 17 do -- Define all the colors up to black
  62.   colorPos[i] = _color
  63.   _color = _color*2
  64.   if _color > lastColor then
  65.     break
  66.   end
  67. end
  68.  
  69. --Functions--
  70. --Some functions to make codeing a bit faster; less typing XD
  71. function tw(...) term.write(...) end
  72. function cp(...) term.setCursorPos(...) end
  73. function clear() term.clear() end
  74. function color(textColor,backgroundColor)
  75.   if term.isColor() then
  76.     if textColor then
  77.       term.setTextColor(textColor)
  78.     end
  79.     if backgroundColor then
  80.       term.setBackgroundColor(backgroundColor)
  81.     end
  82.   end
  83. end
  84.  
  85. local function usages()
  86.   print("Usages:")
  87.   print("ap draw <ap3 Save File>")
  88.   print("ap edit <name>")
  89. end
  90.  
  91. local function waitForKey(_key)
  92.   while true do
  93.     local event,p1 = os.pullEvent("key")
  94.     if p1 == _key then
  95.       break
  96.     end
  97.   end
  98. end
  99.  
  100. function printC(text,y)
  101.   if not y then
  102.     error("printC:for text "..text..", y value expected")
  103.   end
  104.   tLenght = #tostring(text)
  105.   local sStart = math.ceil(w/2-tLenght/2)
  106.   local sEnd = sStart + tLenght
  107.   cp(sStart,y)
  108.   tw(text)
  109.   return sStart,sEnd
  110. end
  111.  
  112. local function registerMonitors()
  113.   monitors = {}
  114.   for a,b in pairs(peripheral.getNames()) do
  115.     if peripheral.getType(b) == "monitor" and peripheral.call(b,"isColor") then
  116.       table.insert(monitors,b)
  117.     end
  118.   end
  119.   if #monitors == 0 then
  120.     print("No monitors could be found")
  121.     print("-Make sure any wired modems are on (Right click)")
  122.     print("-Only Advanced (Gold) monitors work")
  123.     error("FORCEQUIT")
  124.   end
  125. end
  126.  
  127. local function mW(mon,toWrite) --Write something on a monitor
  128.   if not mon or not color then
  129.     error("expected string,string",2)
  130.   end
  131.   peripheral.call(mon,"write",toWrite)
  132. end
  133.  
  134. local function mC(mon,color) --Change a monitor's background color
  135.   if not mon or not color then
  136.     error("expected string,number",2)
  137.   end
  138.   peripheral.call(mon,"setBackgroundColor",color)
  139. end
  140.  
  141. local function mP(mon,x,y) --Set the cursor's position on a monitor
  142.   if not mon or not x or not y then
  143.     error("expected string,number,number",2)
  144.   end
  145.   peripheral.call(mon,"setCursorPos",x,y)
  146. end
  147.  
  148. local function itemsIn(tTable) --Count the items in a table
  149.   if not tTable then return 0 end
  150.   if not type(tTable) == "table" then error("Expected table",2) end
  151.   local items = 0
  152.   for a,b in pairs(tTable) do
  153.     items = items+1
  154.   end
  155.   return items
  156. end
  157.  
  158. local function checkImage() --Check all the monitors are there
  159.   for a,b in pairs(image) do
  160.     if not peripheral.isPresent(a) and itemsIn(image[a]) > 0 then
  161.       if not skip[a] then
  162.         return a --returns the first missing one
  163.       end
  164.     end
  165.   end
  166.   return false
  167. end
  168.  
  169. local function draw()
  170.   if not args[2] then
  171.     print("Usage:")
  172.     print("ap draw <ap3 save file>")
  173.     error("FORCEQUIT")
  174.   end
  175.   if not fs.exists(args[2]) and not fs.isDir(args[2]) then
  176.     print("That file doesn't exist")
  177.     print("You can make it with 'ap edit <name>'")
  178.     error("FORCEQUIT")
  179.   else
  180.     f = fs.open(args[2],"r")
  181.     image = textutils.unserialize(f.readAll())
  182.     f.close()
  183.   end
  184.   if not image then
  185.     print("That file exists, but it doesn't seem to be in the correct Advanced Paint 3 format")
  186.     error("FORCEQUIT")
  187.   end
  188.   if checkImage() then
  189.     print("A monitor in this save is missing!")
  190.     print("The image can't be displayed unless all monitors are present")
  191.     print("Use 'ap edit "..args[2].."' to reassign the missing monitors")
  192.     error("FORCEQUIT")
  193.   end
  194.   registerMonitors()
  195.   for i = 1, #monitors do --Clear all the monitors
  196.     if not rMonitors[monitors[i]] then
  197.       mC(monitors[i],dColor)
  198.       peripheral.call(monitors[i],"clear")
  199.     end
  200.   end
  201.   for i = 1, #monitors do --Draw the image on the monitors
  202.     if image[monitors[i]] then
  203.       for a,b in pairs(image[monitors[i]]) do
  204.         for c,d in pairs(b) do
  205.           if type(c) == "number" and type(a) == "number" then
  206.             mP(monitors[i],c,a)
  207.             mC(monitors[i],d)
  208.             mW(monitors[i]," ")
  209.           end
  210.         end
  211.       end
  212.     end
  213.   end
  214.   --Draw the thing the terminal displays
  215.   color(nil,colors.white)
  216.   clear()
  217.   cp(1,1)
  218.   color(colors.lightGray,colors.yellow)
  219.   tw(string.rep(" ",w))
  220.   printC("Advanced Paint 3",1)
  221.   color(colors.black,colors.white)
  222.   printC("'"..args[2].."'",6)
  223.   printC("Is currently being displayed",7)
  224.   color(colors.white,colors.red)
  225.   printC("   Exit   ",9)
  226.   while true do
  227.     local event,p1,p2,p3 = os.pullEvent("mouse_click")
  228.     if p3 == 9 then --exit
  229.       break
  230.     end
  231.   end
  232. end
  233.  
  234. local function isSide(side)
  235.   for a,b in pairs(rs.getSides()) do
  236.     if side == b then
  237.       return true
  238.     end
  239.   end
  240.   return false
  241. end
  242.  
  243. local function isMonitor(mon) --Checks if a monitor is part of the save
  244.   for a,b in pairs(image) do
  245.     if a == mon then
  246.       return true
  247.     end
  248.   end
  249.   return false
  250. end
  251.  
  252. local function drawColorScreen()
  253.   color(nil,colors.white)
  254.   clear()
  255.   cp(1,1)
  256.   color(colors.lightGray,colors.yellow)
  257.   tw(string.rep(" ",w))
  258.   printC("Advanced Paint 3",1)
  259.   color(colors.black,colors.white)
  260.   if clearM  then
  261.     printC("Clear Mode is on",5)
  262.     printC("Touch a monitor to clear it",7)
  263.     color(colors.white,colors.lime)
  264.     printC(" Done ",9)
  265.   elseif changeUtilMon then
  266.     printC("Change the Utility Monitor",5)
  267.     printC("Touch a monitor to select it",7)
  268.     printC("Minium size of 2x2 blocks",9)
  269.     color(colors.white,colors.lime)
  270.     printC("Cancel",11)
  271.   else
  272.     printC("Select Color",7)
  273.     cp(18,9)
  274.     for i = 1, #colorPos do
  275.       color(nil,colorPos[i])
  276.       tw(" ")
  277.     end
  278.     color(colors.black,colors.white)
  279.     cp(1,8)
  280.     tw(string.rep(" ",w))
  281.     local cPos = 1
  282.     local _cColor = cColor
  283.     while _cColor>1 do
  284.       _cColor = _cColor/2
  285.       cPos = cPos+1
  286.     end
  287.     cp(cPos+17,8)
  288.     tw("v")
  289.     cp(18,10)
  290.     tw(string.rep("^",16))
  291.     color(colors.white,colors.lime)
  292.     printC("Clear Mode",11)
  293.     printC("Change Util Monitor",13)
  294.     cp(1,h)
  295.     tw(string.rep(" ",w))
  296.     printC("Save and Exit",h)
  297.   end
  298. end
  299.  
  300. local function drawUtilMon() --Draws the screen of the utility monitor
  301.   local uW,uH = 18,12 --Width and hight of the box
  302.   mC(utilMonitor,colors.gray) --Draw the background
  303.   for a = 1, uH do
  304.     mP(utilMonitor,1,a)
  305.     for b = 1, uW do
  306.       mW(utilMonitor," ")
  307.     end
  308.   end
  309.   peripheral.call(utilMonitor,"setTextColor",colors.white)
  310.   if clearM then
  311.     mP(utilMonitor,2,4)
  312.     mW(utilMonitor,"Touch a monitor")
  313.     mP(utilMonitor,4,5)
  314.     mW(utilMonitor,"to clear it")
  315.     mC(utilMonitor,colors.lime)
  316.     mP(utilMonitor,7,7)
  317.     mW(utilMonitor,"Done")
  318.   elseif changeUtilMon then
  319.     mP(utilMonitor,2,4)
  320.     mW(utilMonitor,"Touch a monitor")
  321.     mP(utilMonitor,4,5)
  322.     mW(utilMonitor,"to choose it")
  323.     mP(utilMonitor,1,7)
  324.     mW(utilMonitor,"Minimum 2x2 blocks")
  325.     mC(utilMonitor,colors.lime)
  326.     mP(utilMonitor,7,9)
  327.     mW(utilMonitor,"Cancel")
  328.   else
  329.     mP(utilMonitor,4,2)
  330.     mW(utilMonitor,"Select Color")
  331.     mP(utilMonitor,2,4)
  332.     local _color = 1
  333.     for i = 1, 17 do
  334.       mC(utilMonitor,_color)
  335.       mW(utilMonitor," ")
  336.       _color = _color*2
  337.       if _color > lastColor then
  338.         break
  339.       end
  340.     end
  341.     mP(utilMonitor,1,3)
  342.     mC(utilMonitor,colors.gray)
  343.     mW(string.rep(" ",uW))
  344.     local cPos = 1
  345.     local _cColor = cColor
  346.     while _cColor>1 do
  347.       _cColor = _cColor/2
  348.       cPos = cPos+1
  349.     end
  350.     mP(utilMonitor,cPos+1,3)
  351.     mW(utilMonitor,"v")
  352.     mP(utilMonitor,2,5)
  353.     for i = 1, 16 do
  354.       mW(utilMonitor,"^")
  355.     end
  356.     mC(utilMonitor,colors.lime)
  357.     mP(utilMonitor,5,6)
  358.     mW(utilMonitor,"Clear Mode")
  359.     mP(utilMonitor,3,8)
  360.     mW(utilMonitor,"Save and Exit")
  361.     mP(utilMonitor,1,10)
  362.     mW(utilMonitor,"Change Utility Mon")
  363.   end
  364. end
  365.  
  366. local function redrawScreens()
  367.   drawColorScreen()
  368.   if utilMonitor then
  369.     drawUtilMon()
  370.   end
  371. end
  372.  
  373. local function edit()
  374.   if not args[2] then
  375.     print("Usage:")
  376.     print("ap edit <ap3 save file>")
  377.     error("FORCEQUIT")
  378.   end
  379.   if fs.isDir(args[2]) then
  380.     print("That's a directory")
  381.     print("Usage:")
  382.     print("ap edit <ap3 save file>")
  383.     error("FORCEQUIT")
  384.   end
  385.   if not fs.exists(args[2]) then
  386.     print("This file doesn't exist, so it will be created")
  387.     print("Disconnect any monitors that you don't want included")
  388.     color(colors.lime)
  389.     print("Press 'Enter' to coninue")
  390.     waitForKey(keys.enter)
  391.     f = fs.open(args[2],"w")
  392.     f.close()
  393.     image = {}
  394.   else
  395.     f = fs.open(args[2],"r")
  396.     image = textutils.unserialize(f.readAll())
  397.     f.close()
  398.     if not image then image = {} end
  399.   end
  400.   if image then --Load the image if it exists
  401.     skip = {}
  402.     while checkImage() do
  403.       local a = checkImage()
  404.       clear()
  405.       cp(1,1)
  406.       print("A monitor in this save is missing!")
  407.       print("Name:"..a)
  408.       if image[a]["size"] and image[a]["size"][1] and image[a]["size"][2] then
  409.         print("Size: "..(image[a]["size"][1]/7).."x"..(image[a]["size"][2]/5).." blocks ("..image[a]["size"][1].."x"..image[a]["size"][2].." pixels)")
  410.       else
  411.         print("Size: Unknown")
  412.       end
  413.       print("1:Choose another to take its place this time")
  414.       print("2:Choose another to permanantly take its place")
  415.       print("3:Skip the monitor this time")
  416.       print("4:Remove this monitor from the save")
  417.       print("5:Cancel loading")
  418.       local continue = true
  419.       while continue do
  420.         local event,p1 = os.pullEvent("key")
  421.         if p1 == keys.one then --temp replace
  422.           clear()
  423.           cp(1,1)
  424.           print("Touch a monitor to replace "..a)
  425.           while true do
  426.             local _event,_p1 = os.pullEvent("monitor_touch")
  427.             if isMonitor(_p1) then
  428.               print(_p1.." is already part of the save file")
  429.             else
  430.               skip[a] = true
  431.               rMonitors[_p1] = a
  432.               mC(_p1,dColor)
  433.               peripheral.call(_p1,"clear")
  434.               for a,b in pairs(image[a]) do
  435.                 for c,d in pairs(b) do
  436.                   if type(c) == "number" and type(a) == "number" then
  437.                     mP(_p1,c,a)
  438.                     mC(_p1,d)
  439.                     mW(_p1," ")
  440.                   end
  441.                 end
  442.               end
  443.               continue = false
  444.               break
  445.             end
  446.           end
  447.         elseif p1 == keys.two then --perm replace
  448.           clear()
  449.           cp(1,1)
  450.           print("Touch a monitor to replace "..a.." permanantly")
  451.           while true do
  452.             local _event,_p1 = os.pullEvent("monitor_touch")
  453.             if isMonitor(_p1) then
  454.               print(_p1.." is already part of the save file")
  455.             else
  456.               image[_p1] = image[a]
  457.               image[a] = nil
  458.               continue = false
  459.               break
  460.             end
  461.           end
  462.         elseif p1 == keys.three then --skip this time
  463.           skip[a] = true
  464.           break
  465.         elseif p1 == keys.four then --Remove
  466.           image[a] = nil
  467.           break
  468.         elseif p1 == keys.five  then --Cancel
  469.           color(colors.red)
  470.           print("Canceled")
  471.           error("FORCEQUIT")
  472.         end
  473.       end
  474.     end
  475.     registerMonitors()
  476.     for i = 1, #monitors do --Clear all the monitors
  477.       if not rMonitors[monitors[i]] then
  478.         mC(monitors[i],dColor)
  479.         peripheral.call(monitors[i],"clear")
  480.       end
  481.     end
  482.     for i = 1, #monitors do --Draw the image on the monitors
  483.       if image[monitors[i]] then
  484.         for a,b in pairs(image[monitors[i]]) do
  485.           for c,d in pairs(b) do
  486.             if type(c) == "number" and type(a) == "number" then
  487.               mP(monitors[i],c,a)
  488.               mC(monitors[i],d)
  489.               mW(monitors[i]," ")
  490.             end
  491.           end
  492.         end
  493.       end
  494.     end
  495.   else --If the image doesn't exsist yet
  496.     image["compID"] = os.getComputerID()  --adds the comp id to the table. This is unused but could be nice to have someday
  497.   end
  498.   drawColorScreen()
  499.   while true do --The main loop of the program
  500.     event,p1,p2,p3 = os.pullEvent()
  501.     if event == "monitor_touch" then --A monitor was right clicked
  502.       if utilMonitor and p1 == utilMonitor then --Its the util monitor
  503.         if clearM then --Turning clearmode off
  504.           if p3 == 7 then
  505.             clearM = false
  506.             redrawScreens()
  507.           end
  508.         elseif changeUtilMon then --cancel this
  509.           if p3 == 9 then
  510.             changeUtilMon = false
  511.             redrawScreens()
  512.           end
  513.         else --Change color,exit,clearmode,or change util mon
  514.           if p3 == 4 and p2>1 and p2<18 then --color
  515.             cColor = colorPos[p2-1]
  516.             redrawScreens()
  517.           elseif p3 == 6 then --Clear mode
  518.             clearM = true
  519.             redrawScreens()
  520.           elseif p3 == 8 then --save and exit
  521.             f = fs.open(args[2],"w")
  522.             f.write(textutils.serialize(image))
  523.             f.close()
  524.             error("SAVED")
  525.           elseif p3 == 10 then --Change util mon
  526.             changeUtilMon = true
  527.             redrawScreens()
  528.           end
  529.         end
  530.       else --A regular Monitor
  531.         if clearM then --Clear the monitor
  532.           if rMonitors[p1] then --if the monitor is standing in for another
  533.             p1 = rMonitors[p1]
  534.           end
  535.           image[p1] = {}
  536.           image[p1]["size"] = {peripheral.call(p1,"getSize")}
  537.           mC(p1,dColor)
  538.           peripheral.call(p1,"clear")
  539.         elseif changeUtilMon then --Possible select monitor for util monitor
  540.           local _x,_y = peripheral.call(p1,"getSize")
  541.           if _x > 17 and _y > 11 then --if its the right size
  542.             if utilMonitor then
  543.               mC(utilMonitor,dColor)
  544.               peripheral.call(utilMonitor,"clear")
  545.               if image[utilMonitor] then
  546.                 for a,b in pairs(image[utilMonitor]) do
  547.                   for c,d in pairs(b) do
  548.                     if type(c) == "number" and type(a) == "number" then
  549.                       mP(utilMonitor,c,a)
  550.                       mC(utilMonitor,d)
  551.                       mW(utilMonitor," ")
  552.                     end
  553.                   end
  554.                 end
  555.               end
  556.             end
  557.             utilMonitor = p1
  558.             changeUtilMon = false
  559.             redrawScreens()
  560.           end
  561.         else --Drawing a pixel
  562.           mP(p1,p2,p3) --Draw the pixel on the monitor
  563.           mC(p1,cColor)
  564.           mW(p1," ")
  565.           if rMonitors[p1] then --if the monitor is standing in for another
  566.             p1 = rMonitors[p1]
  567.           end
  568.           if not image[p1] then  --Checks to make sure the tables exist
  569.             image[p1] = {}
  570.             image[p1]["size"] = {peripheral.call(p1,"getSize")}
  571.           end
  572.           if not image[p1][p3] then
  573.             image[p1][p3] = {}
  574.           end
  575.           image[p1][p3][p2] = cColor --adds color here
  576.         end
  577.       end
  578.     elseif event == "mouse_click" then --The mouse was clicked in the terminal
  579.       if clearM then
  580.         if p3 == 9 then
  581.           clearM = false
  582.           redrawScreens()
  583.         end
  584.       elseif changeUtilMon then
  585.         if p3 == 11 then
  586.           changeUtilMon = false
  587.           redrawScreens()
  588.         end
  589.       else
  590.         if p3 == 9 then --Select a color
  591.           if p2>17 and p2<34 then
  592.             cColor = colorPos[p2-17]
  593.             redrawScreens()
  594.           end
  595.         elseif p3 == 11 then --Turn on clear mode
  596.           clearM = not clearM
  597.           redrawScreens()
  598.         elseif p3 == 13 then --Change util mon
  599.           changeUtilMon = true
  600.           redrawScreens()
  601.         elseif p3 == h then --save and exit
  602.           f = fs.open(args[2],"w")
  603.           f.write(textutils.serialize(image))
  604.           f.close()
  605.           error("SAVED")
  606.         end
  607.       end
  608.     elseif event == "peripheral" then --a Peripheral was added
  609.       if peripheral.getType(p1) == "monitor" then
  610.         mC(p1,dColor)
  611.         peripheral.call(p1,"clear")
  612.         table.insert(monitors,p1)
  613.       end
  614.     elseif event == "monitor_resize" then --a monitor was broken or placed (on an exsisting monitor)
  615.       error("STOPTOBESAFE:resize")
  616.     elseif event == "peripheral_detach" then
  617.       if itemsIn(image[p1]) > 0 then
  618.         error("STOPTOBESAFE:detach")
  619.       end
  620.     end
  621.   end
  622. end
  623.  
  624.  
  625. --Program--
  626. --Check everything is good
  627. color(colors.white,colors.black)
  628. if h ~= 19 or w ~= 51 then
  629.   print("This doesn't work on pocket computers")
  630.   return
  631. elseif shell.getRunningProgram() ~= fileName then
  632.   print("This program should be named '"..fileName.."', not "..shell.getRunningProgram())
  633.   if fs.exists(fileName) then
  634.     color(colors.red)
  635.     print("A program called '"..fileName.."' already exists, so this one couldn't be renamed")
  636.     print("Please remove the program already named '"..fileName"'")
  637.     color(colors.white)
  638.     return
  639.   end
  640.   fs.move(shell.getRunningProgram(),fileName)
  641.   print("File has been renamed")
  642.   print("Try your operation again...")
  643.   return
  644. end
  645.  
  646. if args[1] == "draw" then  --Displays a file, doens't allow editing
  647.   state,err = pcall(function() draw() end)
  648. elseif args[1] == "edit" then --Displays a file
  649.   state,err = pcall(function() edit() end)
  650. elseif args[1] == "delete" then
  651.   print("Trying to delete something?")
  652.   print("Just use 'delete <file>'")
  653. else
  654.   usages()
  655.   return
  656. end
  657.  
  658. if not state then
  659.   if string.find(err,"Terminated") then --Ctrl+t held
  660.     term.setCursorBlink(false)
  661.     color(colors.white,colors.black)
  662.     clear()
  663.     cp(1,1)
  664.     print("Program terminated")
  665.   elseif string.find(err,"FORCEQUIT") then --When i need to stop the program with no output
  666.     --*clears throat*
  667.     --Nothing to do here
  668.   elseif string.find(err,"SAVED") then --When the file has been saved
  669.     color(colors.lime,colors.black)
  670.     clear()
  671.     cp(1,1)
  672.     print("Saved to '"..args[2].."'")
  673.     color(colors.white)
  674.   elseif string.find(err,"STOPTOBESAFE") then --When something went wrong
  675.     color(colors.red,colors.black)
  676.     clear()
  677.     cp(1,1)
  678.     if string.find(err,"resize") then
  679.       print("A monitor's size was changed!")
  680.     elseif string.find(err,"detach") then
  681.       print("A monitor was removed!")
  682.     end
  683.     color(colors.white)
  684.     print("The program stopped to prevent the save from being corrupted")
  685.     print(" ")
  686.     print("Do you want to save the changes made so far?")
  687.     print("1:Yes")
  688.     print("2:No")
  689.     while true do
  690.       local event,p1 = os.pullEvent("key")
  691.       if p1 == keys.one then
  692.         f = fs.open(args[2],"w")
  693.         f.write(textutils.serialize(image))
  694.         f.close()
  695.         color(colors.lime)
  696.         print("Saved")
  697.         break
  698.       elseif p1 == keys.two then
  699.         color(colors.red)
  700.         print("Not saved")
  701.         break
  702.       end
  703.     end
  704.     os.pullEvent("char")
  705.   else --An actual error occurd
  706.     color(colors.red,colors.black)
  707.     tw("X")
  708.     color(colors.white,colors.red)
  709.     cp(1,1)
  710.     tw(string.rep(" ",w))
  711.     printC("Error - Press 'Tab'",1)
  712.     waitForKey(keys.tab)
  713.     color(colors.white,colors.black)
  714.     clear()
  715.     printC("Critical Error",1)
  716.     cp(1,3)
  717.     color(colors.white)
  718.     if not err then
  719.       print("The program errored, but no reason was given")
  720.     else
  721.       print(err)
  722.     end
  723.   end
  724. else
  725.   color(colors.white,colors.black)
  726.   clear()
  727.   cp(1,1)
  728.   print("Program Closed")
  729. end
  730.  
  731. for i = 1, #monitors do --Clear all the monitors again
  732.   mC(monitors[i],colors.black)
  733.   peripheral.call(monitors[i],"clear")
  734. end
  735.  
  736. if f then
  737.   f.close()
  738. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement