Advertisement
9551

Untitled

Sep 11th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.27 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.                     term.setCursorPos(dfs[3]+2,dfs[2]+dfs[4]+3)
  299.                     term.setBackgroundColor(colors.gray)
  300.                     term.setTextColor(colors.white)
  301.                     term.write("sym:")
  302.                     term.setTextColor(cCol)
  303.                     term.setBackgroundColor(cBac)
  304.                     term.write(cSym)
  305.                 end
  306.                 term.setTextColor(colors.white)
  307.                 term.setBackgroundColor(colors.gray)
  308.                 if windowOpen == "backgroundColorMenu" then term.setBackgroundColor(colors.green) end
  309.                 if b.button(1,sclick,14+#ft,1,"background") then
  310.                     windowOpen = "backgroundColorMenu"
  311.                 end
  312.                 term.setBackgroundColor(colors.gray)
  313.                 if windowOpen == "textColorMenu" then term.setBackgroundColor(colors.green) end
  314.                 if b.button(1,sclick,25+#ft,1,"text-color") then
  315.                     windowOpen = "textColorMenu"
  316.                 end
  317.                 term.setBackgroundColor(colors.gray)
  318.                 if windowOpen == "symbolMenu" then term.setBackgroundColor(colors.green) end
  319.                 if b.button(1,sclick,14+#ft,2,"symbols") then
  320.                     windowOpen = "symbolMenu"
  321.                 end
  322.                 term.setBackgroundColor(colors.gray)
  323.                 if windowOpen == "miscMenu" then term.setBackgroundColor(colors.green) end
  324.                 if b.button(1,sclick,25+#ft,2,"misc") then
  325.                     windowOpen = "miscMenu"
  326.                 end
  327.                 if windowOpen == "" then
  328.                     block = {0,0,0,0}
  329.                 end
  330.                 if windowOpen == "backgroundColorMenu" then
  331.                     block = {13+#ft,4,20,4}
  332.                     b.fill(nil,nil,nil,nil,"\127")
  333.                     term.setBackgroundColor(colors.gray)
  334.                     term.setTextColor(cBac)
  335.                     b.frame(14+#ft,5,19,2,"lightGray","gray")
  336.                     term.setTextColor(colors.black)
  337.                     term.setBackgroundColor(colors.red)
  338.                     if b.button(1,sclick,31+#ft,4,"\xD7") then
  339.                         windowOpen = ""
  340.                         dclick = {"tout",1,-1,-1}
  341.                     end
  342.                     for k,v in pairs(getColors()) do
  343.                         term.setCursorPos(16+k,5)
  344.                         term.setBackgroundColor(v)
  345.                         if b.button(1,sclick,16+k,5," ") then
  346.                             cBac = v
  347.                         end
  348.                     end
  349.                 end
  350.                 if windowOpen == "textColorMenu" then
  351.                     block = {13+#ft,4,20,4}
  352.                     b.fill(nil,nil,nil,nil,"\127")
  353.                     term.setBackgroundColor(colors.gray)
  354.                     term.setTextColor(cCol)
  355.                     b.frame(14+#ft,5,19,2,"lightGray","gray")
  356.                     term.setTextColor(colors.black)
  357.                     term.setBackgroundColor(colors.red)
  358.                     if b.button(1,sclick,31+#ft,4,"\xD7") then
  359.                         windowOpen = ""
  360.                         dclick = {"tout",1,-1,-1}
  361.                     end
  362.                     for k,v in pairs(getColors()) do
  363.                         term.setCursorPos(16+k,5)
  364.                         term.setBackgroundColor(v)
  365.                         if b.button(1,sclick,16+k,5," ") then
  366.                             cCol = v
  367.                         end
  368.                     end
  369.                 end
  370.                 --
  371.                 term.setBackgroundColor(colors.red)
  372.                 term.setTextColor(colors.white)
  373.                 if b.button(1,sclick,xs-4,4,"EXIT") then
  374.                     if isSaved == true then
  375.                         error("closed",0)
  376.                     else
  377.                         exitMode = true
  378.                     end
  379.                 end
  380.                 if exitMode then
  381.                     block = {1,1,xs,ys}
  382.                     term.setBackgroundColor(colors.gray)
  383.                     b.frame(14+#ft,5,27,2,"lightGray","gray")
  384.                     term.setTextColor(colors.red)
  385.                     term.setCursorPos(14+#ft,4)
  386.                     term.write("you have unsaved progress.")
  387.                     term.setCursorPos(14+#ft,5)
  388.                     term.write("do you want to continue ?")
  389.                     term.setTextColor(colors.black)
  390.                     term.setBackgroundColor(colors.green)
  391.                     if b.button(1,sclick,20+#ft,6,"YES") then
  392.                         error("closed",0)
  393.                     end
  394.                     term.setBackgroundColor(colors.red)
  395.                     if b.button(1,sclick,29+#ft,6,"NO!") then
  396.                         exitMode = false
  397.                     end
  398.                 end
  399.                 updateCur = updateCur + 1
  400.                 calcFPS()
  401.                 win.setVisible(true)
  402.                 sclick = {"tout",1,-1,-1}
  403.                 frames = frames + 1
  404.                 sleep()
  405.             end
  406.             while true do
  407.                 local xs, ys = sizeUpdate()
  408.                 drawBox(xs, ys)
  409.                 menu(xs, ys)
  410.             end
  411.         end
  412.         parallel.waitForAll(menuClick,drawClick,draw,keyDown,keyUp)
  413.     elseif args[2] and args[1] == "create" then
  414.         if fs.exists(args[2]..".nimg") then error("file exists!",0) end
  415.         local file = fs.open(args[2]..".nimg","w")
  416.         file.write("{}")
  417.         file.close()
  418.         error()
  419.     elseif args[2] and args[1] == "view" then
  420.         if fs.exists(args[2]..".nimg") then
  421.             b.fill(nil,nil,nil,nil,"\127")
  422.             term.setBackgroundColor(colors.black)
  423.             term.setTextColor(colors.black)
  424.             local index = {}
  425.             function index:draw(x, y)
  426.                 if not x then x = 0 end
  427.                 if not y then y = 0 end
  428.                 for k,v in pairs(self) do
  429.                     if type(k) == "number" then
  430.                         for k2,v2 in pairs(v) do
  431.                             term.setCursorPos(k-self.offset[1]+x,k2-(self.offset[2]+1)+y)
  432.                             term.setBackgroundColor(v2.bcol)
  433.                             term.setTextColor(v2.tcol)
  434.                             term.write(v2.sym)
  435.                         end
  436.                     end
  437.                 end
  438.             end
  439.             function index:size()
  440.                 return getSize(self)
  441.             end
  442.             local function loadImage(name)
  443.                 if not name then name = "" end
  444.                 if not fs.exists(name..".nimg") then error("Error: "..name.." not found",2) end
  445.                 local file = fs.open(name..".nimg","r")
  446.                 local image = textutils.unserialize(file.readAll())
  447.                 file.close()
  448.                 return setmetatable(image, {__index = index})
  449.             end
  450.             term.setBackgroundColor(colors.black)
  451.             term.clear()
  452.             local img = loadImage(args[2])
  453.             local xss,yss,xe,ye = table.unpack(img:size())
  454.             b.frame(3,ys/2+1,xs-2,ys/2-1,"lightGray","gray")
  455.             img:draw(3,(ys/2))
  456.             local txt = "press any char to continue..."
  457.             term.setCursorPos(xs/2-#txt/2+1,ys)
  458.             term.setBackgroundColor(colors.gray)
  459.             term.write(txt)
  460.             os.pullEvent("char")
  461.             term.setTextColor(colors.white)
  462.             term.setBackgroundColor(colors.black)
  463.             term.clear()
  464.             term.setCursorPos(1,1)
  465.             error()
  466.         else
  467.             error("no image found",0)
  468.         end
  469.     else
  470.         local index = {}
  471.         function index:draw(termobj, x, y)
  472.             local terms = termobj or term
  473.             if not x then x = 0 end
  474.             if not y then y = 0 end
  475.             local obc = terms.getBackgroundColor()
  476.             local otc = terms.getTextColor()
  477.             for k,v in pairs(self) do
  478.                 if type(k) == "number" then
  479.                     for k2,v2 in pairs(v) do
  480.                         terms.setCursorPos(k-self.offset[1]+x,k2-(self.offset[2]+1)+y)
  481.                         terms.setBackgroundColor(v2.bcol)
  482.                         terms.setTextColor(v2.tcol)
  483.                         terms.write(v2.sym)
  484.                     end
  485.                 end
  486.             end
  487.             terms.setBackgroundColor(obc)
  488.             terms.setTextColor(otc)
  489.         end
  490.         function index:size()
  491.             return getSize(self)
  492.         end
  493.         local function loadImage(name)
  494.             if not name then name = "" end
  495.             if not fs.exists(name..".nimg") then error("Error: "..name.." not found",2) end
  496.             local file = fs.open(name..".nimg","r")
  497.             local image = textutils.unserialize(file.readAll())
  498.             file.close()
  499.             return setmetatable(image, {__index = index})
  500.         end
  501.         return {
  502.             loadImage = loadImage
  503.         }
  504.     end
  505. end
  506. local ok, err = true
  507. while ok do
  508.     ok, err = pcall(mainCode)
  509.     if not ok and err == "Terminated" then ok = true end
  510.     if type(err) == "table" then ok = false return err end
  511.     sleep()
  512. end
  513. if args[2] and args[1] == "edit" then
  514.     term.redirect(oldTerm)
  515.     term.setTextColor(colors.white)
  516.     term.setBackgroundColor(colors.black)
  517.     term.clear()
  518.     term.setCursorPos(1,1)
  519. end
  520. if not ok then
  521.     error(err,2)
  522. end
  523.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement