Advertisement
9551

Untitled

Sep 10th, 2021 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.73 KB | None | 0 0
  1. --local oe = os.pullEvent
  2. --os.pullEvent = os.pullEventRaw
  3. local args = {...}
  4. local arg = args[2]
  5. if not fs.exists("ButtonH") then shell.run("pastebin get LTDZZZEJ ButtonH") end
  6. local b = require("ButtonH").terminal
  7. local xs, ys = term.getSize()
  8. local win
  9. local oldTerm
  10. if args[2] and args[1] == "edit" then
  11.     win = window.create(term.current(), 1, 1, xs, ys)
  12.     oldTerm = term.redirect(win)
  13. end
  14. local keybinds = {
  15.     delete = keys.x
  16. }
  17. local mainCode = function()
  18.     local getSize = function(tbl)
  19.         local xindex = {}
  20.         local yindex = {}
  21.         for k,v in pairs(tbl) do
  22.             if type(k) == "number" then
  23.                 table.insert(xindex,k-tbl.offset[1]+1)
  24.                 for k2 in pairs(v) do
  25.                     table.insert(yindex,k2-(tbl.offset[2]))
  26.                 end
  27.             end
  28.         end
  29.         return {
  30.             math.min(table.unpack(xindex) or 0),
  31.             math.min(table.unpack(yindex) or 0),
  32.             math.max(table.unpack(xindex) or 0),
  33.             math.max(table.unpack(yindex) or 0)
  34.         }
  35.     end
  36.     local count = function(tbl)
  37.         local count = 0
  38.         for k,v in pairs(tbl) do
  39.             if type(k) == "number" then
  40.                 for k,v in pairs(v) do
  41.                     count = count + 1
  42.                 end
  43.             end
  44.         end
  45.         return count
  46.     end
  47.     local sTime = os.time()
  48.     local frames = 0
  49.     local FPS = 0
  50.     local cTime = os.time()
  51.     local calcFPS = function()
  52.         local cTime = os.time()
  53.         local tDif = (cTime-sTime)/0.02
  54.         FPS = frames/tDif
  55.     end
  56.     local pixelmap
  57.     local drawmap
  58.     local openmap
  59.     local openpixelmap
  60.     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
  61.     if args[2] and args[1] == "edit" then
  62.         if arg then
  63.             if fs.exists(arg..".nimg") then
  64.                 local file =  fs.open(arg..".nimg","r")
  65.                 drawmap = textutils.unserialize(file.readAll())
  66.                 file.close()
  67.             else
  68.                 error("not found. try using file name wihnout .nimg",0)
  69.             end
  70.         end
  71.         local sclick = {"tout",1,-1,-1}
  72.         local dclick = {"tout",1,-1,-1}
  73.         if not pixelmap then pixelmap = {} end
  74.         if not drawmap then drawmap = {} end
  75.         if not openmap then openmap = {} end
  76.         if not openpixelmap then openpixelmap = {} end
  77.         local cCol = colors.black
  78.         local cBac = colors.white
  79.         local cSym = " "
  80.         local sizeUpdate = function()
  81.             win.reposition(1,1,term.getSize())
  82.             xs, ys = win.getSize()
  83.             term.setBackgroundColor(colors.black)
  84.             term.setTextColor(colors.white)
  85.             return xs, ys
  86.         end
  87.         sizeUpdate()
  88.         local cfilter = {
  89.             [2]=true,
  90.             [3]=true,
  91.             [4]=true,
  92.             [5]=true
  93.         }
  94.         local c2filter = {
  95.             [1]=true,
  96.             [3]=true
  97.         }
  98.         local eventQueue = {}
  99.         local menuClick = function()
  100.             while true do
  101.                 sclick = b.timetouch(10,cfilter,false)
  102.             end
  103.         end
  104.         local drawClick = function()
  105.             while true do
  106.                 dclick = b.timetouch(10,cfilter,true)
  107.             end
  108.         end
  109.         local keyDown = function()
  110.             while true do
  111.                 _,keyDownEv = os.pullEvent("key")
  112.                 if not eventQueue[keyDownEv] then
  113.                     eventQueue[keyDownEv] = true
  114.                 end
  115.             end
  116.         end
  117.         local keyUp = function()
  118.             while true do
  119.                 _,keyUpEv = os.pullEvent("key_up")
  120.                 eventQueue[keyUpEv] = false
  121.             end
  122.         end
  123.         local dfs = {3,7,11,4}
  124.         local windowbug = 1
  125.         local wincount = 0
  126.         local updateSkip = 100
  127.         local updateCur = 0
  128.         local yy = 2
  129.         local isSaved = true
  130.         local draw = function()
  131.             local drawBox = function(xs,ys)
  132.                 sizeUpdate()
  133.                 win.setVisible(false)
  134.                 if wincount >= windowbug then
  135.                     win.clear()
  136.                     wincount = 0
  137.                 end
  138.                 wincount = wincount + 1
  139.                 if dfs[3] < xs-9  then
  140.                     term.setTextColor(colors.green)
  141.                     if b.boxButton(1,dclick,dfs[1]+dfs[3]+3,dfs[2]-2,"green","black","+",2,1) then
  142.                         dfs[3] = dfs[3] + 1
  143.                     end
  144.                 end
  145.                 if dfs[3] > 11 then
  146.                     term.setTextColor(colors.red)
  147.                     if b.boxButton(1,dclick,dfs[1]+dfs[3]+3,dfs[2]+2,"red","black","-",2,1) then
  148.                         dfs[3] = dfs[3] - 1
  149.                     end
  150.                 end
  151.                 if dfs[4]+dfs[2] < ys-4 then
  152.                     if b.boxButton(1,dclick,dfs[1]+(dfs[3]/2)-3,dfs[4]*2+5,"green","black","+",2,1) then
  153.                         dfs[4] = dfs[4] + 0.5
  154.                         dfs[2] = dfs[2] + 0.5
  155.                         yy = yy + 1
  156.                     end
  157.                 end
  158.                 if dfs[4] > 4 then
  159.                     if b.boxButton(1,dclick,dfs[1]+(dfs[3]/2)+3,dfs[4]*2+5,"red","black","-",2,1) then
  160.                         dfs[4] = dfs[4] - 0.5
  161.                         dfs[2] = dfs[2] - 0.5
  162.                         yy = yy - 1
  163.                     end
  164.                 end
  165.                 term.setBackgroundColor(colors.black)
  166.                 term.setTextColor(colors.black)
  167.                 b.fill(nil,nil,nil,nil,"\127")
  168.                 b.frame(dfs[1],dfs[2],dfs[3],dfs[4],"lightGray","gray",false)
  169.                 for x=dfs[1], dfs[1]+dfs[3]-2 do
  170.                     for y=dfs[2]-dfs[4]+2, dfs[2]+dfs[4] do
  171.                         if b.API(dclick,x,y-1,1,1) then
  172.                             isSaved = false
  173.                             if not pixelmap[x-dfs[1]+1] then pixelmap[x-dfs[1]+1] = {} end
  174.                             if not pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] then pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] = {} end
  175.                             if not drawmap[x] then drawmap[x] = {} end
  176.                             if not drawmap[x][y] then drawmap[x][y] = {} end
  177.                             if b.switch("db",1) or eventQueue[keybinds.delete] then
  178.                                 pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] = nil
  179.                                 drawmap[x][y] = nil
  180.                                 dclick = {"tout",1,-1,-1}
  181.                             else
  182.                                 pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] = {
  183.                                     tcol = cCol,
  184.                                     bcol = cBac,
  185.                                     sym = cSym
  186.                                 }
  187.                                 drawmap[x][y] = {}
  188.                                 drawmap[x][y] = {
  189.                                     tcol = cCol,
  190.                                     bcol = cBac,
  191.                                     sym = cSym
  192.                                 }
  193.                             end
  194.                         end
  195.                     end
  196.                 end
  197.                 openmap = {}
  198.                 openpixelmap = {}
  199.                 for k,v in pairs(drawmap) do
  200.                     if type(k) == "number" then
  201.                         for k2,v2 in pairs(v) do
  202.                             if k < dfs[1]+dfs[3]-1 then
  203.                                 if k2 < dfs[2]+dfs[4]+1 then
  204.                                     term.setCursorPos(k,k2-1)
  205.                                     term.setBackgroundColor(v2.bcol)
  206.                                     term.setTextColor(v2.tcol)
  207.                                     term.write(v2.sym)
  208.                                     if not openmap[k-dfs[1]+1] then openmap[k-dfs[1]+1] = {} end
  209.                                     if not openmap[k-dfs[1]+1][k2-dfs[2]+yy+1] then openmap[k-dfs[1]+1][k2-dfs[2]+yy+1] = {} end
  210.                                     if not openpixelmap[k] then openpixelmap[k] = {} end
  211.                                     if not openpixelmap[k][k2] then openpixelmap[k][k2] = {} end
  212.                                     openmap[k-dfs[1]+1][k2-dfs[2]+yy+1] = {
  213.                                         tcol = v2.tcol,
  214.                                         bcol = v2.bcol,
  215.                                         sym = v2.sym
  216.                                     }
  217.                                     openpixelmap[k][k2] = {
  218.                                         tcol = v2.tcol,
  219.                                         bcol = v2.bcol,
  220.                                         sym = v2.sym
  221.                                     }
  222.                                 end
  223.                             end
  224.                         end
  225.                     end
  226.                 end
  227.                 openpixelmap["offset"] = dfs
  228.             end
  229.             local menu = function(xs, ys)
  230.                 term.setCursorPos(1,1)
  231.                 term.setBackgroundColor(colors.gray)
  232.                 term.write((" "):rep(xs))
  233.                 term.setCursorPos(1,2)
  234.                 term.write((" "):rep(xs))
  235.                 term.setBackgroundColor(colors.black)
  236.                 if not isSaved then
  237.                     term.setBackgroundColor(colors.red)
  238.                     term.setTextColor(colors.white)
  239.                 else
  240.                     term.setBackgroundColor(colors.green)
  241.                     term.setTextColor(colors.black)
  242.                 end
  243.                 if b.button(1,sclick,1,1,"SAVE") then
  244.                     isSaved = true
  245.                     local file = fs.open(args[2]..".nimg","w")
  246.                     file.write(textutils.serialize(openpixelmap))
  247.                     file.close()
  248.                 end
  249.                 term.setTextColor(colors.white)
  250.                 term.setBackgroundColor(colors.gray)
  251.                 b.switch(1,1,sclick,xs-4,1,"red","green","white","\8",5,1)
  252.                 if updateCur >= updateSkip then
  253.                     term.setCursorPos(1,2)
  254.                     term.write(args[2]..".nimg fs: "..("%.1fKb"):format(fs.getSize(args[2]..".nimg")/1024))
  255.                     term.setCursorPos(6,1)
  256.                     term.write("chars: "..count(openpixelmap))
  257.                     term.setCursorPos(dfs[3]+2,dfs[2]+dfs[4])
  258.                     term.write(("x%d y%d"):format(dfs[3]-1,dfs[2]+dfs[4]-4))
  259.                 end
  260.                 updateCur = updateCur + 1
  261.                 calcFPS()
  262.                 --code
  263.                 win.setVisible(true)
  264.                 sclick = {"tout",1,-1,-1}
  265.                 frames = frames + 1
  266.                 sleep()
  267.             end
  268.             while true do
  269.                 local xs, ys = sizeUpdate()
  270.                 drawBox(xs, ys)
  271.                 menu(xs, ys)
  272.             end
  273.         end
  274.         parallel.waitForAll(menuClick,drawClick,draw,keyDown,keyUp)
  275.     elseif args[2] and args[1] == "create" then
  276.         if fs.exists(args[2]..".nimg") then error("file exists!",0) end
  277.         local file = fs.open(args[2]..".nimg","w")
  278.         file.write("{}")
  279.         file.close()
  280.     elseif args[2] and args[1] == "view" then
  281.         if fs.exists(args[2]..".nimg") then
  282.             b.fill(nil,nil,nil,nil,"\127")
  283.             term.setBackgroundColor(colors.black)
  284.             term.setTextColor(colors.black)
  285.             local index = {}
  286.             function index:draw(x, y)
  287.                 if not x then x = 0 end
  288.                 if not y then y = 0 end
  289.                 for k,v in pairs(self) do
  290.                     if type(k) == "number" then
  291.                         for k2,v2 in pairs(v) do
  292.                             term.setCursorPos(k-self.offset[1]+x,k2-(self.offset[2]+1)+y)
  293.                             term.setBackgroundColor(v2.bcol)
  294.                             term.setTextColor(v2.tcol)
  295.                             term.write(v2.sym)
  296.                         end
  297.                     end
  298.                 end
  299.             end
  300.             function index:size()
  301.                 return getSize(self)
  302.             end
  303.             local function loadImage(name)
  304.                 if not name then name = "" end
  305.                 if not fs.exists(name..".nimg") then error("Error: "..name.." not found",2) end
  306.                 local file = fs.open(name..".nimg","r")
  307.                 local image = textutils.unserialize(file.readAll())
  308.                 file.close()
  309.                 return setmetatable(image, {__index = index})
  310.             end
  311.             term.setBackgroundColor(colors.black)
  312.             term.clear()
  313.             local img = loadImage(args[2])
  314.             local xss,yss,xe,ye = table.unpack(img:size())
  315.             b.frame(3,ys/2+1,xs-2,ys/2-1,"lightGray","gray")
  316.             img:draw(3,(ys/2)-1)
  317.             os.pullEvent("char")
  318.             term.setTextColor(colors.white)
  319.             term.setBackgroundColor(colors.black)
  320.             term.clear()
  321.             term.setCursorPos(1,1)
  322.             error()
  323.         else
  324.             error("no image found",0)
  325.         end
  326.     else
  327.         local index = {}
  328.         function index:draw(termobj, x, y)
  329.             local terms = termobj or term
  330.             if not x then x = 0 end
  331.             if not y then y = 0 end
  332.             local obc = terms.getBackgroundColor()
  333.             local otc = terms.getTextColor()
  334.             for k,v in pairs(self) do
  335.                 if type(k) == "number" then
  336.                     for k2,v2 in pairs(v) do
  337.                         terms.setCursorPos(k-self.offset[1]+x,k2-(self.offset[2]+1)+y)
  338.                         terms.setBackgroundColor(v2.bcol)
  339.                         terms.setTextColor(v2.tcol)
  340.                         terms.write(v2.sym)
  341.                     end
  342.                 end
  343.             end
  344.             terms.setBackgroundColor(obc)
  345.             terms.setTextColor(otc)
  346.         end
  347.         function index:size()
  348.             return getSize(self)
  349.         end
  350.         local function loadImage(name)
  351.             if not name then name = "" end
  352.             if not fs.exists(name..".nimg") then error("Error: "..name.." not found",2) end
  353.             local file = fs.open(name..".nimg","r")
  354.             local image = textutils.unserialize(file.readAll())
  355.             file.close()
  356.             return setmetatable(image, {__index = index})
  357.         end
  358.         return {
  359.             loadImage = loadImage
  360.         }
  361.     end
  362. end
  363.  
  364. local ok,err = pcall(mainCode)
  365. --os.pullEvent = oe
  366. if ok and type(err) == "table" then
  367.     return err
  368. end
  369. if args[2] and args[1] == "edit" then
  370.     term.redirect(oldTerm)
  371.     term.setTextColor(colors.white)
  372.     term.setBackgroundColor(colors.black)
  373.     term.clear()
  374.     term.setCursorPos(1,1)
  375. end
  376. if not ok then
  377.     error(err,2)
  378. end
  379.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement