9551

Untitled

Sep 11th, 2021 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 20.92 KB | None | 0 0
  1. local args = {...}
  2. local arg = args[2]
  3. if not fs.exists("ButtonH") then shell.run("pastebin get LTDZZZEJ ButtonH") end
  4. local b = require("ButtonH").terminal
  5. local xs, ys = term.getSize()
  6. local win
  7. local oldTerm
  8. if args[2] and args[1] == "edit" then
  9.     win = window.create(term.current(), 1, 1, xs, ys)
  10.     oldTerm = term.redirect(win)
  11. end
  12. local keybinds = {
  13.     delete = keys.x
  14. }
  15. local pixelmap
  16. local drawmap
  17. local openmap
  18. local openpixelmap
  19. local cCol = colors.black
  20. local cBac = colors.white
  21. local cSym = " "
  22. local ft = ""
  23. local dfs = {3,7,11,4}
  24. local block = {0,0,0,0}
  25. local windowOpen = ""
  26. local symbolTab = "alphabet"
  27. local miscTab = ""
  28. local exitMode = false
  29. local windowbug = 1
  30. local wincount = 0
  31. local updateSkip = 20
  32. local updateCur = 0
  33. local yy = 2
  34. local isSaved = true
  35. local mainCode = function()
  36.     local getSize = function(tbl)
  37.         local xindex = {}
  38.         local yindex = {}
  39.         for k,v in pairs(tbl) do
  40.             if type(k) == "number" then
  41.                 table.insert(xindex,k-tbl.offset[1]+1)
  42.                 for k2 in pairs(v) do
  43.                     table.insert(yindex,k2-(tbl.offset[2]))
  44.                 end
  45.             end
  46.         end
  47.         return {
  48.             math.min(table.unpack(xindex) or 0),
  49.             math.min(table.unpack(yindex) or 0),
  50.             math.max(table.unpack(xindex) or 0),
  51.             math.max(table.unpack(yindex) or 0)
  52.         }
  53.     end
  54.     local count = function(tbl)
  55.         local count = 0
  56.         for k,v in pairs(tbl) do
  57.             if type(k) == "number" then
  58.                 for k,v in pairs(v) do
  59.                     count = count + 1
  60.                 end
  61.             end
  62.         end
  63.         return count
  64.     end
  65.     local sTime = os.time()
  66.     local frames = 0
  67.     local FPS = 0
  68.     local cTime = os.time()
  69.     local calcFPS = function()
  70.         local cTime = os.time()
  71.         local tDif = (cTime-sTime)/0.02
  72.         FPS = frames/tDif
  73.     end
  74.     local getColors = function()
  75.         local cols = {}
  76.         for k,v in pairs(colors) do
  77.             if type(v) == "number" then
  78.                 table.insert(cols,v)
  79.             end
  80.         end
  81.         return cols
  82.     end
  83.     if args[1] ~= "edit" and args[1] ~= "create" and args[1] ~= "view" and not args[2] then error("usage: program-name edit/create/view <file>",0) end
  84.     if args[2] and args[1] == "edit" then
  85.         if not drawmap then
  86.             if arg then
  87.                 if fs.exists(arg..".nimg") then
  88.                     local file =  fs.open(arg..".nimg","r")
  89.                     drawmap = textutils.unserialize(file.readAll())
  90.                     file.close()
  91.                 else
  92.                     error("not found. try using file name wihnout .nimg",0)
  93.                 end
  94.             end
  95.         end
  96.         local sclick = {"tout",1,-1,-1}
  97.         local dclick = {"tout",1,-1,-1}
  98.         if not pixelmap then pixelmap = {} end
  99.         if not drawmap then drawmap = {} end
  100.         if not openmap then openmap = {} end
  101.         if not openpixelmap then openpixelmap = {} end
  102.         local sizeUpdate = function()
  103.             win.reposition(1,1,term.getSize())
  104.             xs, ys = win.getSize()
  105.             term.setBackgroundColor(colors.black)
  106.             term.setTextColor(colors.white)
  107.             return xs, ys
  108.         end
  109.         sizeUpdate()
  110.         local cfilter = {
  111.             [2]=true,
  112.             [3]=true,
  113.             [4]=true,
  114.             [5]=true
  115.         }
  116.         local c2filter = {
  117.             [1]=true,
  118.             [3]=true
  119.         }
  120.         local eventQueue = {}
  121.         local menuClick = function()
  122.             while true do
  123.                 sclick = b.timetouch(1,cfilter,false)
  124.             end
  125.         end
  126.         local drawClick = function()
  127.             while true do
  128.                 dclick = b.timetouch(.5,cfilter,true)
  129.             end
  130.         end
  131.         local keyDown = function()
  132.             while true do
  133.                 _,keyDownEv = os.pullEvent("key")
  134.                 if not eventQueue[keyDownEv] then
  135.                     eventQueue[keyDownEv] = true
  136.                 end
  137.             end
  138.         end
  139.         local keyUp = function()
  140.             while true do
  141.                 _,keyUpEv = os.pullEvent("key_up")
  142.                 eventQueue[keyUpEv] = false
  143.             end
  144.         end
  145.         local draw = function()
  146.             local drawBox = function(xs,ys)
  147.                 sizeUpdate()
  148.                 win.setVisible(false)
  149.                 if wincount >= windowbug then
  150.                     win.clear()
  151.                     wincount = 0
  152.                 end
  153.                 wincount = wincount + 1
  154.                 if dfs[3] < xs-9  then
  155.                     if windowOpen == "" then
  156.                         term.setTextColor(colors.green)
  157.                         if b.boxButton(1,dclick,dfs[1]+dfs[3]+3,dfs[2]-2,"green","black","+",2,1) then
  158.                             dfs[3] = dfs[3] + 1
  159.                         end
  160.                     end
  161.                 end
  162.                 if dfs[3] > 11 then
  163.                     if windowOpen == "" then
  164.                         term.setTextColor(colors.red)
  165.                         if b.boxButton(1,dclick,dfs[1]+dfs[3]+3,dfs[2]+2,"red","black","-",2,1) then
  166.                             dfs[3] = dfs[3] - 1
  167.                         end
  168.                     end
  169.                 end
  170.                 if dfs[4]+dfs[2] < ys-4 then
  171.                     if b.boxButton(1,dclick,dfs[1]+(dfs[3]/2)-3,dfs[4]*2+5,"green","black","+",2,1) then
  172.                         dfs[4] = dfs[4] + 0.5
  173.                         dfs[2] = dfs[2] + 0.5
  174.                         yy = yy + 1
  175.                     end
  176.                 end
  177.                 if dfs[4] > 4 then
  178.                     if b.boxButton(1,dclick,dfs[1]+(dfs[3]/2)+3,dfs[4]*2+5,"red","black","-",2,1) then
  179.                         dfs[4] = dfs[4] - 0.5
  180.                         dfs[2] = dfs[2] - 0.5
  181.                         yy = yy - 1
  182.                     end
  183.                 end
  184.                 term.setBackgroundColor(colors.black)
  185.                 term.setTextColor(colors.black)
  186.                 b.fill(nil,nil,nil,nil,"\127")
  187.                 b.frame(dfs[1],dfs[2],dfs[3],dfs[4],"lightGray","gray",false)
  188.                 for x=dfs[1], dfs[1]+dfs[3]-2 do
  189.                     for y=dfs[2]-dfs[4]+2, dfs[2]+dfs[4] do
  190.                         if b.API(dclick,x,y-1,1,1) then
  191.                             if not b.API(dclick,block[1],block[2],block[3],block[4]) then
  192.                                 isSaved = false
  193.                                 if not pixelmap[x-dfs[1]+1] then pixelmap[x-dfs[1]+1] = {} end
  194.                                 if not pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] then pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] = {} end
  195.                                 if not drawmap[x] then drawmap[x] = {} end
  196.                                 if not drawmap[x][y] then drawmap[x][y] = {} end
  197.                                 if b.switch("db",1) or eventQueue[keybinds.delete] then
  198.                                     pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] = nil
  199.                                     drawmap[x][y] = nil
  200.                                     dclick = {"tout",1,-1,-1}
  201.                                 else
  202.                                     pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] = {
  203.                                         tcol = cCol,
  204.                                         bcol = cBac,
  205.                                         sym = cSym
  206.                                     }
  207.                                     drawmap[x][y] = {}
  208.                                     drawmap[x][y] = {
  209.                                         tcol = cCol,
  210.                                         bcol = cBac,
  211.                                         sym = cSym
  212.                                     }
  213.                                 end
  214.                             end
  215.                         end
  216.                     end
  217.                 end
  218.                 openmap = {}
  219.                 openpixelmap = {}
  220.                 for k,v in pairs(drawmap) do
  221.                     if type(k) == "number" then
  222.                         for k2,v2 in pairs(v) do
  223.                             if k < dfs[1]+dfs[3]-1 then
  224.                                 if k2 < dfs[2]+dfs[4]+1 then
  225.                                     term.setCursorPos(k,k2-1)
  226.                                     term.setBackgroundColor(v2.bcol)
  227.                                     term.setTextColor(v2.tcol)
  228.                                     term.write(v2.sym)
  229.                                     if not openmap[k-dfs[1]+1] then openmap[k-dfs[1]+1] = {} end
  230.                                     if not openmap[k-dfs[1]+1][k2-dfs[2]+yy+1] then openmap[k-dfs[1]+1][k2-dfs[2]+yy+1] = {} end
  231.                                     if not openpixelmap[k] then openpixelmap[k] = {} end
  232.                                     if not openpixelmap[k][k2] then openpixelmap[k][k2] = {} end
  233.                                     openmap[k-dfs[1]+1][k2-dfs[2]+yy+1] = {
  234.                                         tcol = v2.tcol,
  235.                                         bcol = v2.bcol,
  236.                                         sym = v2.sym
  237.                                     }
  238.                                     openpixelmap[k][k2] = {
  239.                                         tcol = v2.tcol,
  240.                                         bcol = v2.bcol,
  241.                                         sym = v2.sym
  242.                                     }
  243.                                 end
  244.                             end
  245.                         end
  246.                     end
  247.                 end
  248.                 openpixelmap["offset"] = dfs
  249.             end
  250.             local menu = function(xs, ys)
  251.                 term.setCursorPos(1,1)
  252.                 term.setBackgroundColor(colors.gray)
  253.                 term.write((" "):rep(xs))
  254.                 term.setCursorPos(1,2)
  255.                 term.write((" "):rep(xs))
  256.                 term.setBackgroundColor(colors.black)
  257.                 if not isSaved then
  258.                     term.setBackgroundColor(colors.red)
  259.                     term.setTextColor(colors.white)
  260.                 else
  261.                     term.setBackgroundColor(colors.green)
  262.                     term.setTextColor(colors.black)
  263.                 end
  264.                 if b.button(1,sclick,1,1,"SAVE") then
  265.                     isSaved = true
  266.                     local file = fs.open(args[2]..".nimg","w")
  267.                     file.write(textutils.serialize(openpixelmap))
  268.                     file.close()
  269.                 end
  270.                 term.setTextColor(colors.white)
  271.                 term.setBackgroundColor(colors.gray)
  272.                 b.switch(1,1,sclick,xs-4,1,"red","green","white","\8",5,1)
  273.                 if updateCur >= updateSkip then
  274.                     term.setCursorPos(1,2)
  275.                     local fsd = "fs: "..("%.1fKb"):format(fs.getSize(args[2]..".nimg")/1024)
  276.                     term.write(fsd)
  277.                     term.setCursorPos(6,1)
  278.                     ft = tostring(count(openpixelmap))
  279.                     term.write("chars:"..ft)
  280.                     term.setCursorPos(dfs[3]+2,dfs[2]+dfs[4])
  281.                     term.setBackgroundColor(colors.gray)
  282.                     term.write(("x%d y%d"):format(dfs[3]-1,dfs[2]+dfs[4]-4))
  283.                     term.setCursorPos(dfs[3]+2,dfs[2]+dfs[4]+1)
  284.                     term.setTextColor(colors.white)
  285.                     term.write("t col:")
  286.                     term.setCursorPos(dfs[3]+8,dfs[2]+dfs[4]+1)
  287.                     term.setTextColor(cCol)
  288.                     term.setBackgroundColor(colors.lightGray)
  289.                     term.write("\127")
  290.                     term.setBackgroundColor(colors.gray)
  291.                     term.setCursorPos(dfs[3]+2,dfs[2]+dfs[4]+2)
  292.                     term.setTextColor(colors.white)
  293.                     term.write("b col:")
  294.                     term.setBackgroundColor(colors.lightGray)
  295.                     term.setCursorPos(dfs[3]+8,dfs[2]+dfs[4]+2)
  296.                     term.setTextColor(cBac)
  297.                     term.write("\127")
  298.                 end
  299.                 term.setTextColor(colors.white)
  300.                 term.setBackgroundColor(colors.gray)
  301.                 if windowOpen == "backgroundColorMenu" then term.setBackgroundColor(colors.green) end
  302.                 if b.button(1,sclick,14+#ft,1,"background") then
  303.                     windowOpen = "backgroundColorMenu"
  304.                 end
  305.                 term.setBackgroundColor(colors.gray)
  306.                 if windowOpen == "textColorMenu" then term.setBackgroundColor(colors.green) end
  307.                 if b.button(1,sclick,25+#ft,1,"text-color") then
  308.                     windowOpen = "textColorMenu"
  309.                 end
  310.                 term.setBackgroundColor(colors.gray)
  311.                 if windowOpen == "symbolMenu" then term.setBackgroundColor(colors.green) end
  312.                 if b.button(1,sclick,14+#ft,2,"symbols") then
  313.                     windowOpen = "symbolMenu"
  314.                 end
  315.                 term.setBackgroundColor(colors.gray)
  316.                 if windowOpen == "miscMenu" then term.setBackgroundColor(colors.green) end
  317.                 if b.button(1,sclick,25+#ft,2,"misc") then
  318.                     windowOpen = "miscMenu"
  319.                 end
  320.                 if windowOpen == "" then
  321.                     block = {0,0,0,0}
  322.                 end
  323.                 if windowOpen == "backgroundColorMenu" then
  324.                     block = {13+#ft,4,20,4}
  325.                     b.fill(nil,nil,nil,nil,"\127")
  326.                     term.setBackgroundColor(colors.gray)
  327.                     term.setTextColor(cBac)
  328.                     b.frame(14+#ft,5,19,2,"lightGray","gray")
  329.                     term.setTextColor(colors.black)
  330.                     term.setBackgroundColor(colors.red)
  331.                     if b.button(1,sclick,31+#ft,4,"\xD7") then
  332.                         windowOpen = ""
  333.                         dclick = {"tout",1,-1,-1}
  334.                     end
  335.                     for k,v in pairs(getColors()) do
  336.                         term.setCursorPos(16+k,5)
  337.                         term.setBackgroundColor(v)
  338.                         if b.button(1,sclick,16+k,5," ") then
  339.                             cBac = v
  340.                         end
  341.                     end
  342.                 end
  343.                 if windowOpen == "textColorMenu" then
  344.                     block = {13+#ft,4,20,4}
  345.                     b.fill(nil,nil,nil,nil,"\127")
  346.                     term.setBackgroundColor(colors.gray)
  347.                     term.setTextColor(cCol)
  348.                     b.frame(14+#ft,5,19,2,"lightGray","gray")
  349.                     term.setTextColor(colors.black)
  350.                     term.setBackgroundColor(colors.red)
  351.                     if b.button(1,sclick,31+#ft,4,"\xD7") then
  352.                         windowOpen = ""
  353.                         dclick = {"tout",1,-1,-1}
  354.                     end
  355.                     for k,v in pairs(getColors()) do
  356.                         term.setCursorPos(16+k,5)
  357.                         term.setBackgroundColor(v)
  358.                         if b.button(1,sclick,16+k,5," ") then
  359.                             cCol = v
  360.                         end
  361.                     end
  362.                 end
  363.                 --
  364.                 term.setBackgroundColor(colors.red)
  365.                 term.setTextColor(colors.white)
  366.                 if b.button(1,sclick,xs-4,4,"EXIT") then
  367.                     if isSaved == true then
  368.                         error("closed",0)
  369.                     else
  370.                         exitMode = true
  371.                     end
  372.                 end
  373.                 if exitMode then
  374.                     block = {1,1,xs,ys}
  375.                     term.setBackgroundColor(colors.gray)
  376.                     b.frame(14+#ft,5,27,2,"lightGray","gray")
  377.                     term.setTextColor(colors.red)
  378.                     term.setCursorPos(14+#ft,4)
  379.                     term.write("you have unsaved progress.")
  380.                     term.setCursorPos(14+#ft,5)
  381.                     term.write("do you want to continue ?")
  382.                     term.setTextColor(colors.black)
  383.                     term.setBackgroundColor(colors.green)
  384.                     if b.button(1,sclick,20+#ft,6,"YES") then
  385.                         error("closed",0)
  386.                     end
  387.                     term.setBackgroundColor(colors.red)
  388.                     if b.button(1,sclick,29+#ft,6,"NO!") then
  389.                         exitMode = false
  390.                     end
  391.                 end
  392.                 updateCur = updateCur + 1
  393.                 calcFPS()
  394.                 win.setVisible(true)
  395.                 sclick = {"tout",1,-1,-1}
  396.                 frames = frames + 1
  397.                 sleep()
  398.             end
  399.             while true do
  400.                 local xs, ys = sizeUpdate()
  401.                 drawBox(xs, ys)
  402.                 menu(xs, ys)
  403.             end
  404.         end
  405.         parallel.waitForAll(menuClick,drawClick,draw,keyDown,keyUp)
  406.     elseif args[2] and args[1] == "create" then
  407.         if fs.exists(args[2]..".nimg") then error("file exists!",0) end
  408.         local file = fs.open(args[2]..".nimg","w")
  409.         file.write("{}")
  410.         file.close()
  411.         error()
  412.     elseif args[2] and args[1] == "view" then
  413.         if fs.exists(args[2]..".nimg") then
  414.             b.fill(nil,nil,nil,nil,"\127")
  415.             term.setBackgroundColor(colors.black)
  416.             term.setTextColor(colors.black)
  417.             local index = {}
  418.             function index:draw(x, y)
  419.                 if not x then x = 0 end
  420.                 if not y then y = 0 end
  421.                 for k,v in pairs(self) do
  422.                     if type(k) == "number" then
  423.                         for k2,v2 in pairs(v) do
  424.                             term.setCursorPos(k-self.offset[1]+x,k2-(self.offset[2]+1)+y)
  425.                             term.setBackgroundColor(v2.bcol)
  426.                             term.setTextColor(v2.tcol)
  427.                             term.write(v2.sym)
  428.                         end
  429.                     end
  430.                 end
  431.             end
  432.             function index:size()
  433.                 return getSize(self)
  434.             end
  435.             local function loadImage(name)
  436.                 if not name then name = "" end
  437.                 if not fs.exists(name..".nimg") then error("Error: "..name.." not found",2) end
  438.                 local file = fs.open(name..".nimg","r")
  439.                 local image = textutils.unserialize(file.readAll())
  440.                 file.close()
  441.                 return setmetatable(image, {__index = index})
  442.             end
  443.             term.setBackgroundColor(colors.black)
  444.             term.clear()
  445.             local img = loadImage(args[2])
  446.             local xss,yss,xe,ye = table.unpack(img:size())
  447.             b.frame(3,ys/2+1,xs-2,ys/2-1,"lightGray","gray")
  448.             img:draw(3,(ys/2))
  449.             local txt = "press any char to continue..."
  450.             term.setCursorPos(xs/2-#txt/2+1,ys)
  451.             term.setBackgroundColor(colors.gray)
  452.             term.write(txt)
  453.             os.pullEvent("char")
  454.             term.setTextColor(colors.white)
  455.             term.setBackgroundColor(colors.black)
  456.             term.clear()
  457.             term.setCursorPos(1,1)
  458.             error()
  459.         else
  460.             error("no image found",0)
  461.         end
  462.     else
  463.         local index = {}
  464.         function index:draw(termobj, x, y)
  465.             local terms = termobj or term
  466.             if not x then x = 0 end
  467.             if not y then y = 0 end
  468.             local obc = terms.getBackgroundColor()
  469.             local otc = terms.getTextColor()
  470.             for k,v in pairs(self) do
  471.                 if type(k) == "number" then
  472.                     for k2,v2 in pairs(v) do
  473.                         terms.setCursorPos(k-self.offset[1]+x,k2-(self.offset[2]+1)+y)
  474.                         terms.setBackgroundColor(v2.bcol)
  475.                         terms.setTextColor(v2.tcol)
  476.                         terms.write(v2.sym)
  477.                     end
  478.                 end
  479.             end
  480.             terms.setBackgroundColor(obc)
  481.             terms.setTextColor(otc)
  482.         end
  483.         function index:size()
  484.             return getSize(self)
  485.         end
  486.         local function loadImage(name)
  487.             if not name then name = "" end
  488.             if not fs.exists(name..".nimg") then error("Error: "..name.." not found",2) end
  489.             local file = fs.open(name..".nimg","r")
  490.             local image = textutils.unserialize(file.readAll())
  491.             file.close()
  492.             return setmetatable(image, {__index = index})
  493.         end
  494.         return {
  495.             loadImage = loadImage
  496.         }
  497.     end
  498. end
  499. local ok, err = true
  500. while ok do
  501.     ok, err = pcall(mainCode)
  502.     if not ok and err == "Terminated" then ok = true end
  503.     if type(err) == "table" then ok = false return err end
  504.     sleep()
  505. end
  506. if args[2] and args[1] == "edit" then
  507.     term.redirect(oldTerm)
  508.     term.setTextColor(colors.white)
  509.     term.setBackgroundColor(colors.black)
  510.     term.clear()
  511.     term.setCursorPos(1,1)
  512. end
  513. if not ok then
  514.     error(err,2)
  515. end
  516.  
Add Comment
Please, Sign In to add comment