9551

NIMG editor V1. image/animation creator/editor/loader

Sep 12th, 2021 (edited)
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 65.87 KB | None | 0 0
  1. --------------------NIMG EDITOR------------------------
  2. ------------------IMAGE SPLASH EDITOR------------------
  3. --------------------by 9551 DEV------------------------
  4. ---Copyright (c) 2021-2022 9551------------9551#0001---
  5. ---using this code in your project is fine!------------
  6. ---as long as you dont claim you made it---------------
  7. ---im cool with it, feel free to include---------------
  8. ---in your projects!   discord: 9551#0001--------------
  9. ---you dont have to but giving credits is nice :)------
  10. -------------------------------------------------------
  11. -------------------------------------------------------
  12. ---NIMG is a program for creating and loading images---
  13. local keybinds = {
  14.     delete = keys.x,
  15. }
  16. local openCreateFile = true
  17. local chars = "0123456789abcdef"
  18. local saveCols, loadCols = {}, {}
  19. for i = 0, 15 do
  20.   saveCols[2^i] = chars:sub(i + 1, i + 1)
  21.   loadCols[chars:sub(i + 1, i + 1)] = 2^i
  22. end
  23. local decode = function(tbl)
  24.   local output = setmetatable({},{
  25.       __index=function(t,k)
  26.       local new = {}
  27.       t[k]=new
  28.       return new
  29.     end
  30.   })
  31.   output["offset"] = tbl["offset"]
  32.   for k,v in pairs(tbl) do
  33.      for ko,vo in pairs(v) do
  34.         if type(vo) == "table" then
  35.             output[k][ko] = {}
  36.             if vo then
  37.                 output[k][ko].t = loadCols[vo.t]
  38.                 output[k][ko].b = loadCols[vo.b]
  39.                 output[k][ko].s = vo.s
  40.             end
  41.         end
  42.      end
  43.   end
  44.   return setmetatable(output,getmetatable(tbl))
  45. end
  46. local encode = function(tbl)
  47.   local output = setmetatable({},{
  48.       __index=function(t,k)
  49.       local new = {}
  50.       t[k]=new
  51.       return new
  52.     end
  53.   })
  54.   output["offset"] = tbl["offset"]
  55.   for k,v in pairs(tbl) do
  56.     for ko,vo in pairs(v) do
  57.         if type(vo) == "table" then
  58.             output[k][ko] = {}
  59.             if vo then
  60.                 output[k][ko].t = saveCols[vo.t]
  61.                 output[k][ko].b = saveCols[vo.b]
  62.                 output[k][ko].s = vo.s
  63.             end
  64.         end
  65.      end
  66.   end
  67.   return setmetatable(output,getmetatable(tbl))
  68. end
  69. local remover = function(c)
  70.   return c:gsub(" ","")
  71. end
  72. local monitorP = function(...)
  73.     local tArgs = { ... }
  74.     local sName = tArgs[2]
  75.     local sProgram = tArgs[1]
  76.     local sPath = sProgram
  77.     if sName == "find" then
  78.         local m = peripheral.find("monitor")
  79.         if m then sName = peripheral.getName(m)
  80.         else sName = "noMonitorError" end
  81.     end
  82.     local monitor = peripheral.wrap(sName)
  83.     if not monitor then error("not a valid monitor!",0) end
  84.     tArgs[5] = tonumber(tArgs[5])
  85.     monitor.setTextScale(math.max(math.min(tArgs[5] or 0.5,4),0.5))
  86.     local previousTerm = term.redirect(monitor)
  87.     print(sPath)
  88.     local co = coroutine.create(function()
  89.         (shell.execute or shell.run)(sProgram, tArgs[3], tArgs[4])
  90.     end)
  91.     local printError = function(param)
  92.         error(param,0)
  93.     end
  94.     local function resume(...)
  95.         local ok, param = coroutine.resume(co, ...)
  96.         if not ok then
  97.             printError(param)
  98.         end
  99.         return param
  100.     end
  101.     local timers = {}
  102.     local ok, param = pcall(function()
  103.         local sFilter = resume()
  104.         while coroutine.status(co) ~= "dead" do
  105.             local tEvent = table.pack(os.pullEventRaw())
  106.             if sFilter == nil or tEvent[1] == sFilter or tEvent[1] == "terminate" then sFilter = resume(table.unpack(tEvent, 1, tEvent.n))
  107.             end
  108.             if coroutine.status(co) ~= "dead" and (sFilter == nil or sFilter == "mouse_click") then
  109.                 if tEvent[1] == "monitor_touch" and tEvent[2] == sName then timers[os.startTimer(0.1)] = { tEvent[3], tEvent[4] } sFilter = resume("mouse_click", 1, table.unpack(tEvent, 3, tEvent.n))
  110.                 end
  111.             end
  112.             if coroutine.status(co) ~= "dead" and (sFilter == nil or sFilter == "term_resize") then
  113.                 if tEvent[1] == "monitor_resize" and tEvent[2] == sName then sFilter = resume("term_resize")
  114.                 end
  115.             end
  116.             if coroutine.status(co) ~= "dead" and (sFilter == nil or sFilter == "mouse_up") then
  117.                 if tEvent[1] == "timer" and timers[tEvent[2]] then sFilter = resume("mouse_up", 1, table.unpack(timers[tEvent[2]], 1, 2)) timers[tEvent[2]] = nil
  118.                 end
  119.             end
  120.         end
  121.     end)
  122.     term.redirect(previousTerm)
  123.     if not ok then
  124.         printError(param)
  125.     end
  126. end
  127. local useAccelerated
  128. local expect = require("cc.expect").expect
  129. local nimg = shell.getRunningProgram()
  130. local args = {...}
  131. ----------------------------
  132. --https://github.com/cc-tweaked/CC-Tweaked/blob/544bcaa599b296aaf9affe55c68ee1810c6a38c6/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua#L710
  133. --serialising function
  134. local e={["and"]=true,["break"]=true,["do"]=true,["else"]=true,["elseif"]=true,["end"]=true,["false"]=true,["for"]=true,["function"]=true,["if"]=true,["in"]=true,["local"]=true,["nil"]=true,["not"]=true,["or"]=true,["repeat"]=true,["return"]=true,["then"]=true,["true"]=true,["until"]=true,["while"]=true,}local
  135. t=math.huge local a=dofile("rom/modules/main/cc/expect.lua")local
  136. o,i=a.expect,a.field local function a(n,s,h,r)local d=type(n)if d=="table"then
  137. if s[n]~=nil then if s[n]==false then
  138. error("Cannot serialize table with repeated entries",0)else
  139. error("Cannot serialize table with recursive entries",0)end end s[n]=true local
  140. l if next(n)==nil then l="{}"else local
  141. u,c,m,f,w,y="{\n",h.."  ","[ "," ] = "," = ",",\n"if r.compact then
  142. u,c,m,f,w,y="{","","[","]=","=",","end l=u local p={}for v,b in ipairs(n)do
  143. p[v]=true l=l..c..a(b,s,c,r)..y end for g,k in pairs(n)do if not p[g]then local
  144. q if type(g)=="string"and not e[g]and string.match(g,"^[%a_][%a%d_]*$")then
  145. q=g..w..a(k,s,c,r)..y else q=m..a(g,s,c,r)..f..a(k,s,c,r)..y end l=l..c..q end
  146. end l=l..h.."}"end if r.allow_repetitions then s[n]=nil else s[n]=false end
  147. return l elseif d=="string"then return string.format("%q",n)elseif
  148. d=="number"then if n~=n then return"0/0"elseif n==t then return"1/0"elseif
  149. n==-t then return"-1/0"else return tostring(n)end elseif d=="boolean"or
  150. d=="nil"then return tostring(n)else error("Cannot serialize type "..d,0)end end
  151. function serialize(j,x)local z={}o(2,x,"table","nil")if x then
  152. i(x,"compact","boolean","nil")i(x,"allow_repetitions","boolean","nil")else
  153. x={}end return
  154. a(j,z,"",x)end
  155. textutils.serialise = serialize
  156. textutils.serialize = serialize
  157. ------------------------
  158. if arg[3] and args[1] ~= "addFrame" then
  159.     if args[1] ~= "addFrame" then
  160.         monitorP(nimg,args[3],args[1],args[2],args[4])
  161.     end
  162. else
  163.     local arg = args[2]
  164.     local xs, ys = term.getSize()
  165.     local win
  166.     local oldTerm
  167.     local m
  168.     if args[2] and args[1] == "edit" then
  169.         win = window.create(term.current(), 1, 1, xs, ys)
  170.         oldTerm = term.redirect(win)
  171.     end
  172.     if not fs.exists("ButtonH") then shell.run("pastebin get LTDZZZEJ ButtonH") end
  173.     local b = require("ButtonH").terminal
  174.     local pocketpc = {26,20}
  175.     local standart = {51,19}
  176.     local monitor = {7,5}
  177.     local neural = {39,13}
  178.     local turtle = neural
  179.     local presets = {}
  180.     if pocketpc[1] < xs-8  and pocketpc[2] < ys-3 then presets[1] = {pocket,"pocket"} end
  181.     if standart[1] < xs-8  and standart[2] < ys-3 then presets[2] = {standart,"standart"} end
  182.     if monitor[1] < xs-8  and monitor[2] < ys-3 then presets[3] = {monitor,"monitor"} end
  183.     if neural[1] < xs-8  and neural[2] < ys-3 then presets[4] = {neural,"neural"} end
  184.     if turtle[1] < xs-8  and turtle[2] < ys-3 then presets[5] = {turtle,"turtle"} end
  185.     local pixelmap
  186.     local drawmap
  187.     local openmap
  188.     local openpixelmap
  189.     local cCol = colors.black
  190.     local cBac = colors.white
  191.     local cSym = " "
  192.     local ft = ""
  193.     local dfs = {3,7,11,4}
  194.     local defs = {3,9,11,4}
  195.     local block = {0,0,0,0}
  196.     local windowOpen = ""
  197.     local symbolTab = "basic"
  198.     local miscTab = "settings"
  199.     local exitMode = false
  200.     local windowbug = 1
  201.     local wincount = 0
  202.     local updateSkip = 20
  203.     local updateCur = 0
  204.     local yy = 2
  205.     local isSaved = true
  206.     local charset = {}
  207.     local alphabet = {}
  208.     local upAlphabet = {}
  209.     local numbers = {}
  210.     local drawChars = {}
  211.     local other = {}
  212.     local symMCol = {}
  213.     local misMCol = {}
  214.     local autoSave = false
  215.     local doSaveAll = false
  216.     local asCol = colors.red
  217.     local fileSize
  218.     local changeBg = false
  219.     local bg = {tc="gray",bg="black"}
  220.     for i=1,255 do
  221.         charset[i] = string.char(i)
  222.     end
  223.     for i=65,90 do
  224.         table.insert(alphabet,(string.char(i)):lower())
  225.         table.insert(upAlphabet,string.char(i))
  226.     end
  227.     for i=128, 159 do
  228.         table.insert(drawChars,i-128,string.char(i))
  229.     end
  230.     for i=0,9 do
  231.         table.insert(numbers,tostring(i))
  232.     end
  233.     for i=1,47 do table.insert(other,string.char(i)) end
  234.     for i=58,64 do table.insert(other,string.char(i)) end
  235.     for i=91,96 do table.insert(other,string.char(i)) end
  236.     for i=123,127 do table.insert(other,string.char(i)) end
  237.     for i=160,255 do table.insert(other,string.char(i)) end
  238.     local mainCode = function()
  239.         local getSize = function(tbl)
  240.             local xindex = {}
  241.             local yindex = {}
  242.             for k,v in pairs(tbl) do
  243.                 if type(k) == "number" then
  244.                     table.insert(xindex,k-tbl.offset[1]+1)
  245.                     for k2 in pairs(v) do
  246.                         table.insert(yindex,k2-(tbl.offset[2]))
  247.                     end
  248.                 end
  249.             end
  250.             return {
  251.                 math.min(table.unpack(xindex) or 0),
  252.                 math.min(table.unpack(yindex) or 0),
  253.                 math.max(table.unpack(xindex) or 0),
  254.                 math.max(table.unpack(yindex) or 0)
  255.             }
  256.         end
  257.         local count = function(tbl)
  258.             local count = 0
  259.             for k,v in pairs(tbl) do
  260.                 if type(k) == "number" then
  261.                     for k,v in pairs(v) do
  262.                         count = count + 1
  263.                     end
  264.                 end
  265.             end
  266.             return count
  267.         end
  268.         local sTime = os.time()
  269.         local frames = 0
  270.         local FPS = 0
  271.         local cTime = os.time()
  272.         local calcFPS = function()
  273.             local cTime = os.time()
  274.             local tDif = (cTime-sTime)/0.02
  275.             FPS = frames/tDif
  276.         end
  277.         local getColors = function()
  278.             local cols = {}
  279.             for k,v in pairs(colors) do
  280.                 if type(v) == "number" then
  281.                     table.insert(cols,v)
  282.                 end
  283.             end
  284.             return cols
  285.         end
  286.         local function divideTable(tableToDivide, timesToDivide)
  287.             local retTable = {}
  288.             for i = 1, timesToDivide do
  289.                 retTable[i] = {}
  290.             end
  291.             local elementsPerTable = math.ceil(#tableToDivide/ timesToDivide)
  292.             local curTable = 1
  293.             for i = 1, #tableToDivide do
  294.                 table.insert(retTable[curTable], tableToDivide[i])
  295.                 if #retTable[curTable] >= elementsPerTable then
  296.                     curTable = curTable + 1
  297.                 end
  298.             end
  299.             return retTable
  300.         end
  301.         local function length(tbl)
  302.             local biggest = 0
  303.             for k in pairs(tbl) do
  304.                 if type(k) == "number" then
  305.                     biggest = math.max(biggest, k)
  306.                 end
  307.             end
  308.             return biggest
  309.         end
  310.         if args[1] ~= "edit" and args[1] ~= "create" and args[1] ~= "createImageSet" and args[1] ~= "addFrame" and args[1] ~= "view" and args[1] ~= "update" and args[1] ~= "getAPI" and not args[2] then error("use the help command: nimg help <images,image_sets>\nor use another command: nimg <update/getAPI/view file> ",0) end
  311.         if args[2] and args[1] == "edit" then
  312.             if pocket then error("pocket pc not supported!",0) end
  313.             if not drawmap then
  314.                 if arg then
  315.                     if fs.exists(arg..".nimg") then
  316.                         local file =  fs.open(arg..".nimg","r")
  317.                         drawmap = textutils.unserialize(file.readAll())
  318.                         if drawmap then
  319.                             drawmap = decode(drawmap)
  320.                         end
  321.                         file.close()
  322.                     else
  323.                         error("not found. try using file name wihnout .nimg note that all files need to have .nimg extension to work",0)
  324.                     end
  325.                 end
  326.             end
  327.             local sclick = {"tout",1,-1,-1}
  328.             local dclick = {"tout",1,-1,-1}
  329.             if not pixelmap then pixelmap = {} end
  330.             if not drawmap then drawmap = {} end
  331.             if not openmap then openmap = {} end
  332.             if not openpixelmap then openpixelmap = {} end
  333.             local sizeUpdate = function()
  334.                 win.reposition(1,1,term.getSize())
  335.                 xs, ys = win.getSize()
  336.                 term.setBackgroundColor(colors.black)
  337.                 term.setTextColor(colors.white)
  338.                 return xs, ys
  339.             end
  340.             sizeUpdate()
  341.             local cfilter = {
  342.                 [2]=true,
  343.                 [3]=true,
  344.                 [4]=true,
  345.                 [5]=true
  346.             }
  347.             local c2filter = {
  348.                 [1]=true,
  349.                 [3]=true
  350.             }
  351.             local eventQueue = {}
  352.             local menuClick = function()
  353.                 while true do
  354.                     sclick = b.timetouch(1,cfilter,false)
  355.                 end
  356.             end
  357.             local drawClick = function()
  358.                 while true do
  359.                     dclick = b.timetouch(.5,cfilter,true)
  360.                 end
  361.             end
  362.             local keyDown = function()
  363.                 while true do
  364.                     _,keyDownEv = os.pullEvent("key")
  365.                     if not eventQueue[keyDownEv] then
  366.                         eventQueue[keyDownEv] = true
  367.                     end
  368.                 end
  369.             end
  370.             local keyUp = function()
  371.                 while true do
  372.                     _,keyUpEv = os.pullEvent("key_up")
  373.                     eventQueue[keyUpEv] = false
  374.                 end
  375.             end
  376.             local bgswitch = function()
  377.                 while true do
  378.                     sleep(1)
  379.                     if changeBg  then
  380.                         if bg.tc == "black" then
  381.                             bg.tc = "gray"
  382.                             bg.bg = "black"
  383.                         else
  384.                             bg.tc = "black"
  385.                             bg.bg = "gray"
  386.                         end
  387.                     end
  388.                 end
  389.             end
  390.             local saving = function(bypass)
  391.                 if autoSave or bypass then
  392.                     if doSaveAll then
  393.                         isSaved = true
  394.                         drawmap["offset"] = defs
  395.                         local file = fs.open(args[2]..".nimg","w")
  396.                         local tosave = encode(drawmap)
  397.                         local ser = textutils.serialize(tosave,{compact = true})
  398.                         file.write(ser)
  399.                         file.close()
  400.                     else
  401.                         isSaved = true
  402.                         openpixelmap["offset"] = defs
  403.                         local file = fs.open(args[2]..".nimg","w")
  404.                         local tosave = encode(openpixelmap)
  405.                         local ser = textutils.serialize(tosave,{compact = true})
  406.                         file.write(ser)
  407.                         file.close()
  408.                     end
  409.                 end
  410.             end
  411.             local draw = function()
  412.                 local drawBox = function(xs,ys)
  413.                     sizeUpdate()
  414.                     win.setVisible(false)
  415.                     if wincount >= windowbug then
  416.                         win.clear()
  417.                         wincount = 0
  418.                     end
  419.                     wincount = wincount + 1
  420.                     if dfs[3] < xs-12  then
  421.                         if windowOpen == "" or dfs[3] < xs/2-20 then
  422.                             term.setTextColor(colors.green)
  423.                             if b.boxButton(1,dclick,dfs[1]+dfs[3]+3,dfs[2]-2,"green","black","+",2,1) then
  424.                                 dfs[3] = dfs[3] + 1
  425.                                 if not doSaveAll then
  426.                                     saving()
  427.                                 end
  428.                             end
  429.                         end
  430.                     end
  431.                     if dfs[3] > 2 then
  432.                         if windowOpen == "" or dfs[3] < xs/2-19 then
  433.                             term.setTextColor(colors.red)
  434.                             if b.boxButton(1,dclick,dfs[1]+dfs[3]+3,dfs[2]+2,"red","black","-",2,1) then
  435.                                 dfs[3] = dfs[3] - 1
  436.                                 if not doSaveAll then
  437.                                     saving()
  438.                                 end
  439.                             end
  440.                         end
  441.                     end
  442.                     if dfs[4]+dfs[2] < ys-6 then
  443.                         if b.boxButton(1,dclick,dfs[1]+(dfs[3]/2)-3,dfs[4]*2+5,"green","black","+",2,1) then
  444.                             dfs[4] = dfs[4] + 0.5
  445.                             dfs[2] = dfs[2] + 0.5
  446.                             yy = yy + 1
  447.                             if not doSaveAll then
  448.                                 saving()
  449.                             end
  450.                         end
  451.                     end
  452.                     if dfs[4] > 2 then
  453.                         if b.boxButton(1,dclick,dfs[1]+(dfs[3]/2)+3,dfs[4]*2+5,"red","black","-",2,1) then
  454.                             dfs[4] = dfs[4] - 0.5
  455.                             dfs[2] = dfs[2] - 0.5
  456.                             yy = yy - 1
  457.                             if not doSaveAll then
  458.                                 saving()
  459.                             end
  460.                         end
  461.                     end
  462.                     term.setBackgroundColor(colors.black)
  463.                     term.setTextColor(colors[bg.tc])
  464.                     b.fill(nil,nil,nil,nil,"\127")
  465.                     b.frame(dfs[1],dfs[2],dfs[3],dfs[4],"lightGray",bg.bg,false)
  466.                     for x=dfs[1], dfs[1]+dfs[3]-2 do
  467.                         for y=dfs[2]-dfs[4]+2, dfs[2]+dfs[4] do
  468.                             if b.API(dclick,x,y-1,1,1) then
  469.                                 if not b.API(dclick,block[1],block[2],block[3],block[4]) then
  470.                                     isSaved = false
  471.                                     saving()
  472.                                     if not pixelmap[x-dfs[1]+1] then pixelmap[x-dfs[1]+1] = {} end
  473.                                     if not pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] then pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] = {} end
  474.                                     if not drawmap[x] then drawmap[x] = {} end
  475.                                     if not drawmap[x][y] then drawmap[x][y] = {} end
  476.                                     if b.switch("db",1) or eventQueue[keybinds.delete] then
  477.                                         pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] = nil
  478.                                         drawmap[x][y] = nil
  479.                                         dclick = {"tout",1,-1,-1}
  480.                                     else
  481.                                         pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] = {
  482.                                             t = cCol,
  483.                                             b = cBac,
  484.                                             s = cSym
  485.                                         }
  486.                                         drawmap[x][y] = {}
  487.                                         drawmap[x][y] = {
  488.                                             t = cCol,
  489.                                             b = cBac,
  490.                                             s = cSym
  491.                                         }
  492.                                     end
  493.                                     dclick = {"tout",1,-1,-1}
  494.                                 end
  495.                             end
  496.                         end
  497.                     end
  498.                     openmap = {}
  499.                     openpixelmap = {}
  500.                     for k,v in pairs(drawmap) do
  501.                         if type(k) == "number" then
  502.                             for k2,v2 in pairs(v) do
  503.                                 if k < dfs[1]+dfs[3]-1 and k >= dfs[1] then
  504.                                     if k2 < dfs[2]+dfs[4]+1 and k2 > 4 then
  505.                                         term.setCursorPos(k,k2-1)
  506.                                         term.setBackgroundColor(v2.b)
  507.                                         term.setTextColor(v2.t)
  508.                                         term.write(v2.s)
  509.                                         if not openmap[k-dfs[1]+1] then openmap[k-dfs[1]+1] = {} end
  510.                                         if not openmap[k-dfs[1]+1][k2-dfs[2]+yy+1] then openmap[k-dfs[1]+1][k2-dfs[2]+yy+1] = {} end
  511.                                         if not openpixelmap[k] then openpixelmap[k] = {} end
  512.                                         if not openpixelmap[k][k2] then openpixelmap[k][k2] = {} end
  513.                                         openmap[k-dfs[1]+1][k2-dfs[2]+yy+1] = {
  514.                                             t = v2.t,
  515.                                             b = v2.b,
  516.                                             s = v2.s
  517.                                         }
  518.                                         openpixelmap[k][k2] = {
  519.                                             t = v2.t,
  520.                                             b = v2.b,
  521.                                             s = v2.s
  522.                                         }
  523.                                     end
  524.                                 end
  525.                             end
  526.                         end
  527.                     end
  528.                 end
  529.                 local offsetDrawmap = function(x,y)
  530.                     local offsetmap = drawmap
  531.                     drawmap = {}
  532.                     for k,v in pairs(offsetmap) do
  533.                         if type(k) == "number" then
  534.                             for k2,v2 in pairs(v) do
  535.                                 if not drawmap[k+x] then drawmap[k+x] = {} end
  536.                                 if not drawmap[k+x][k2+y] then drawmap[k+x][k2+y] = {} end
  537.                                 drawmap[k+x][k2+y] = v2
  538.                             end
  539.                         end
  540.                     end
  541.                 end
  542.                 local menu = function(xs, ys)
  543.                     term.setCursorPos(1,1)
  544.                     term.setBackgroundColor(colors.gray)
  545.                     term.write((" "):rep(xs))
  546.                     term.setCursorPos(1,2)
  547.                     term.write((" "):rep(xs))
  548.                     term.setBackgroundColor(colors.black)
  549.                     if not isSaved then
  550.                         term.setBackgroundColor(colors.red)
  551.                         term.setTextColor(colors.white)
  552.                     else
  553.                         term.setBackgroundColor(colors.green)
  554.                         term.setTextColor(colors.black)
  555.                     end
  556.                     if b.button(1,sclick,1,1,"SAVE") then
  557.                         saving(true)
  558.                     end
  559.                     if b.boxButton(1,sclick,4,dfs[4]*2+8,"lightGray","black","\27",1) then
  560.                         offsetDrawmap(-1,0)
  561.                         isSaved = false
  562.                     end
  563.                     if b.boxButton(1,sclick,7,dfs[4]*2+8,"lightGray","black","\26",1) then
  564.                         offsetDrawmap(1,0)
  565.                         isSaved = false
  566.                     end
  567.                     if b.boxButton(1,sclick,11,dfs[4]*2+8,"gray","black","\25",1) then
  568.                         offsetDrawmap(0,1)
  569.                         isSaved = false
  570.                     end
  571.                     if b.boxButton(1,sclick,14,dfs[4]*2+8,"gray","black","\24",1) then
  572.                         offsetDrawmap(0,-1)
  573.                         isSaved = false
  574.                     end
  575.                     changeBg = b.switch(1,3,sclick,19,dfs[4]*2+8,"red","green","white","\182",2)
  576.                     useAccelerated = b.switch(1,2,sclick,xs-1,dfs[4]*2+8,"gray","yellow","white",("\187"):rep(2),1)
  577.                     term.setTextColor(colors.white)
  578.                     term.setBackgroundColor(colors.gray)
  579.                     b.switch(1,1,sclick,xs-4,1,"red","green","white","\8",5,1)
  580.                     if updateCur >= updateSkip then
  581.                         term.setCursorPos(1,2)
  582.                         fileSize = fs.getSize(args[2]..".nimg")/1024
  583.                         local fsd = "fs: "..("%.1fKb"):format(fileSize)
  584.                         term.write(fsd)
  585.                         term.setCursorPos(6,1)
  586.                         if doSaveAll then
  587.                             ft = tostring(count(drawmap))
  588.                         else
  589.                             ft = tostring(count(openpixelmap))
  590.                         end
  591.                         term.write("chars:"..ft)
  592.                         term.setCursorPos(dfs[3]+2,dfs[2]+dfs[4])
  593.                         term.setBackgroundColor(colors.gray)
  594.                         term.write(("x%d y%d"):format(dfs[3]-1,dfs[2]+dfs[4]-4))
  595.                         term.setCursorPos(dfs[3]+2,dfs[2]+dfs[4]+1)
  596.                         term.setTextColor(colors.white)
  597.                         term.write("t col:")
  598.                         term.setCursorPos(dfs[3]+8,dfs[2]+dfs[4]+1)
  599.                         term.setTextColor(cCol)
  600.                         term.setBackgroundColor(colors.lightGray)
  601.                         term.write("\127")
  602.                         term.setBackgroundColor(colors.gray)
  603.                         term.setCursorPos(dfs[3]+2,dfs[2]+dfs[4]+2)
  604.                         term.setTextColor(colors.white)
  605.                         term.write("b col:")
  606.                         term.setBackgroundColor(colors.lightGray)
  607.                         term.setCursorPos(dfs[3]+8,dfs[2]+dfs[4]+2)
  608.                         term.setTextColor(cBac)
  609.                         term.write("\127")
  610.                         term.setCursorPos(dfs[3]+2,dfs[2]+dfs[4]+3)
  611.                         term.setBackgroundColor(colors.gray)
  612.                         term.setTextColor(colors.white)
  613.                         term.write("sym:")
  614.                         term.setTextColor(cCol)
  615.                         term.setBackgroundColor(cBac)
  616.                         term.write(cSym)
  617.                     end
  618.                     term.setTextColor(colors.white)
  619.                     term.setBackgroundColor(colors.gray)
  620.                     local ft = "   "
  621.                     local mval = #ft+xs-50
  622.                     if xs > 51 then mval = #ft+((xs)-51)/2 end
  623.                     if windowOpen == "backgroundColorMenu" then term.setBackgroundColor(colors.green) end
  624.                     if b.button(1,sclick,14+mval,1,"background") then
  625.                         windowOpen = "backgroundColorMenu"
  626.                         symbolTab = "basic"
  627.                         miscTab = "tools"
  628.                     end
  629.                     term.setBackgroundColor(colors.gray)
  630.                     if windowOpen == "textColorMenu" then term.setBackgroundColor(colors.green) end
  631.                     if b.button(1,sclick,25+mval,1,"text-color") then
  632.                         windowOpen = "textColorMenu"
  633.                         symbolTab = "basic"
  634.                         miscTab = "tools"
  635.                     end
  636.                     term.setBackgroundColor(colors.gray)
  637.                     if windowOpen == "symbolMenu" then term.setBackgroundColor(colors.green) end
  638.                     if b.button(1,sclick,14+mval,2,"symbols") then
  639.                         windowOpen = "symbolMenu"
  640.                         symbolTab = "basic"
  641.                         miscTab = "tools"
  642.                     end
  643.                     term.setBackgroundColor(colors.gray)
  644.                     if windowOpen == "miscMenu" then term.setBackgroundColor(colors.green) end
  645.                     if b.button(1,sclick,25+mval,2,"misc") then
  646.                         windowOpen = "miscMenu"
  647.                         symbolTab = "basic"
  648.                         miscTab = "tools"
  649.                     end
  650.                     if windowOpen == "" then
  651.                         block = {0,0,0,0}
  652.                     end
  653.                     if windowOpen == "backgroundColorMenu" then
  654.                         block = {13+mval,4,20,4}
  655.                         b.fill(nil,nil,nil,nil,"\127")
  656.                         term.setBackgroundColor(colors.gray)
  657.                         term.setTextColor(cBac)
  658.                         b.frame(14+mval,5,19,2,"lightGray","gray")
  659.                         term.setTextColor(colors.black)
  660.                         term.setBackgroundColor(colors.red)
  661.                         if b.button(1,sclick,31+mval,4,"\xD7") then
  662.                             windowOpen = ""
  663.                             dclick = {"tout",1,-1,-1}
  664.                         end
  665.                         for k,v in pairs(getColors()) do
  666.                             term.setCursorPos(16+k,5)
  667.                             term.setBackgroundColor(v)
  668.                             if b.button(1,sclick,14+k+mval,5," ") then
  669.                                 cBac = v
  670.                             end
  671.                         end
  672.                     end
  673.                     if windowOpen == "textColorMenu" then
  674.                         block = {13+mval,4,20,4}
  675.                         b.fill(nil,nil,nil,nil,"\127")
  676.                         term.setBackgroundColor(colors.gray)
  677.                         term.setTextColor(cCol)
  678.                         b.frame(14+mval,5,19,2,"lightGray","gray")
  679.                         term.setTextColor(colors.black)
  680.                         term.setBackgroundColor(colors.red)
  681.                         if b.button(1,sclick,31+mval,4,"\xD7") then
  682.                             windowOpen = ""
  683.                             dclick = {"tout",1,-1,-1}
  684.                         end
  685.                         for k,v in pairs(getColors()) do
  686.                             term.setCursorPos(16+k,5)
  687.                             term.setBackgroundColor(v)
  688.                             if b.button(1,sclick,14+k+mval,5," ") then
  689.                                 cCol = v
  690.                             end
  691.                         end
  692.                     end
  693.                     if windowOpen == "symbolMenu" then
  694.                         block = {13+mval,4,31,14}
  695.                         term.setTextColor(colors.black)
  696.                         b.fill(nil,nil,nil,nil,"\127")
  697.                         b.frame(14+mval,10,30,7,"lightGray","gray")
  698.                         term.setBackgroundColor(colors.gray)
  699.                         term.setTextColor(colors.black)
  700.                         term.setCursorPos(18-#ft+mval-1,7)
  701.                         term.write(("\131"):rep(29))
  702.                         term.setTextColor(colors.black)
  703.                         term.setBackgroundColor(colors.red)
  704.                         if b.button(1,sclick,42+mval,8,"\xD7") then
  705.                             windowOpen = ""
  706.                             symbolTab = "basic"
  707.                             dclick = {"tout",1,-1,-1}
  708.                         end
  709.                         if symbolTab == "basic" then symMCol[1] = "green"
  710.                         else symMCol[1] = "red" end
  711.                         if symbolTab == "draw-chars" then symMCol[2] = "green"
  712.                         else symMCol[2] = "red" end
  713.                         if symbolTab == "other" then symMCol[3] = "green"
  714.                         else symMCol[3] = "red" end
  715.                         if b.boxButton(1,sclick,20-#ft+mval,5,symMCol[1],"black","basic",1.5,1) then
  716.                             symbolTab = "basic"
  717.                         end
  718.                         if b.boxButton(1,sclick,30-#ft+mval-2,5,symMCol[2],"black","draw-chars",1.5,1) then
  719.                             symbolTab = "draw-chars"
  720.                         end
  721.                         if b.boxButton(1,sclick,42-#ft+mval-1,5,symMCol[3],"black","other",1.5,1) then
  722.                             symbolTab = "other"
  723.                         end
  724.                         term.setBackgroundColor(colors.black)
  725.                         term.setTextColor(colors.white)
  726.                         if symbolTab == "basic" then
  727.                             for k,v in pairs(alphabet) do
  728.                                 if v == cSym then term.setBackgroundColor(colors.green)
  729.                                 else term.setBackgroundColor(colors.gray)
  730.                                 end
  731.                                 if b.button(1,dclick,17-#ft+mval+k,9,v) then cSym = v
  732.                                 end
  733.                             end
  734.                             for k,v in pairs(upAlphabet) do
  735.                                 if v == cSym then term.setBackgroundColor(colors.green)
  736.                                 else term.setBackgroundColor(colors.gray)
  737.                                 end
  738.                                 if b.button(1,dclick,17-#ft+mval+k,12,v) then
  739.                                     cSym = v
  740.                                 end
  741.                             end
  742.                             for k,v in pairs(numbers) do
  743.                                 if v == cSym then term.setBackgroundColor(colors.green)
  744.                                 else term.setBackgroundColor(colors.gray)
  745.                                 end
  746.                                 if b.button(1,dclick,17-#ft+mval+k,15,v) then cSym = v
  747.                                 end
  748.                             end
  749.                             if cSym == " " then term.setBackgroundColor(colors.green)
  750.                             else term.setBackgroundColor(colors.gray) end
  751.                             if b.button(1,dclick,29-#ft+mval,15,"PIXEL") then
  752.                                 cSym = " "
  753.                             end
  754.                         end
  755.                         if symbolTab == "draw-chars" then
  756.                             local function addSym(draw,x,y)
  757.                                 for i=1,#draw do
  758.                                     local v = draw[i]
  759.                                     if v == cSym then term.setBackgroundColor(colors.green)
  760.                                     else term.setBackgroundColor(colors.gray)
  761.                                     end
  762.                                     if b.button(1,dclick,17-#ft+mval+(i*2)+x,12+(y*2),v) then
  763.                                         cSym = v
  764.                                     end
  765.                                 end
  766.                             end
  767.                             local out = divideTable(drawChars,4)
  768.                             addSym(out[1],5,-1.5)
  769.                             addSym(out[2],5,-0.5)
  770.                             addSym(out[3],5,0.5)
  771.                             addSym(out[4],5,1.5)
  772.                         end
  773.                     end
  774.                     if symbolTab == "other" then
  775.                         local function addSym(draw,x,y)
  776.                             for i=1,#draw do
  777.                                 local v = draw[i]
  778.                                 if v == cSym then term.setBackgroundColor(colors.green)
  779.                                 else term.setBackgroundColor(colors.gray)
  780.                                 end
  781.                                 if b.button(1,dclick,17-#ft+mval+(i)+x,12+(y),v) then
  782.                                     cSym = v
  783.                                 end
  784.                             end
  785.                         end
  786.                         local out = divideTable(other,6)
  787.                         local count = 1
  788.                         for k,v in pairs(out) do
  789.                             addSym(out[count],0,count-3)
  790.                             count = count + 1
  791.                         end
  792.                     end
  793.                     if windowOpen == "miscMenu" then
  794.                         term.setBackgroundColor(colors.gray)
  795.                         block = {13+mval,4,30,14}
  796.                         term.setTextColor(colors.black)
  797.                         b.fill(nil,nil,nil,nil,"\127")
  798.                         b.frame(14+mval,10,29,7,"lightGray","gray")
  799.                         term.setBackgroundColor(colors.gray)
  800.                         term.setTextColor(colors.black)
  801.                         term.setCursorPos(18-#ft+mval-1,7)
  802.                         term.write(("\131"):rep(28))
  803.                         term.setTextColor(colors.black)
  804.                         term.setBackgroundColor(colors.red)
  805.                         if b.button(1,sclick,41+mval,8,"\xD7") then
  806.                             windowOpen = ""
  807.                             dclick = {"tout",1,-1,-1}
  808.                         end
  809.                         term.setBackgroundColor(colors.lightGray)
  810.                         term.setTextColor(colors.white)
  811.                         if b.button(1,sclick,14+mval,8,"UPDATE") then
  812.                             saving(true)
  813.                             term.setBackgroundColor(colors.gray)
  814.                             term.setTextColor(colors.white)
  815.                             term.clear()
  816.                             term.setCursorPos(xs/2-6/2,ys/2)
  817.                             term.write("UPDATING")
  818.                             win.setVisible(true)
  819.                             fs.delete(nimg)
  820.                             local web = http.get("https://pastebin.com/raw/2nbDhRXC")
  821.                             local webdata = web.readAll()
  822.                             local file = fs.open(nimg,"w")
  823.                             file.write(webdata)
  824.                             web.close()
  825.                             file.close()
  826.                             win.setVisible(false)
  827.                             error("updated",0)
  828.                         end
  829.                         if miscTab == "FILE" then misMCol[1] = "green"
  830.                         else misMCol[1] = "red" end
  831.                         if miscTab == "tools" then misMCol[2] = "green"
  832.                         else misMCol[2] = "red" end
  833.                         if b.boxButton(1,sclick,20-#ft+mval,5,misMCol[1],"black","  FILE    ",2,1) then
  834.                             miscTab = "FILE"
  835.                         end
  836.                         if b.boxButton(1,sclick,36-#ft+mval-2,5,misMCol[2],"black"," tools    ",2,1) then
  837.                             miscTab = "tools"
  838.                         end
  839.                         if miscTab == "FILE" then
  840.                             if autoSave then term.setBackgroundColor(colors.green)
  841.                             else term.setBackgroundColor(colors.red) end
  842.                             if b.button(1,sclick,31-#ft+mval-5,9,"auto-save") then
  843.                                 autoSave = not autoSave
  844.                                 saving()
  845.                             end
  846.                             if doSaveAll then term.setBackgroundColor(colors.green)
  847.                             else term.setBackgroundColor(colors.red) end
  848.                             if b.button(1,sclick,28-#ft+mval-5,11,"save all (mode)") then
  849.                                 doSaveAll = not doSaveAll
  850.                             end
  851.                             term.setTextColor(colors.white)
  852.                             term.setBackgroundColor(colors.gray)
  853.                             term.setCursorPos(28-#ft+mval-5,13)
  854.                             term.write("filesize: "..("%.1fKb"):format(fileSize))
  855.                             term.setCursorPos(29-#ft+mval-5,15)
  856.                             if doSaveAll then
  857.                                 term.write("characters: "..tostring(count(drawmap)))
  858.                             else
  859.                                 term.write("characters: "..tostring(count(openpixelmap)))
  860.                             end
  861.                         end
  862.                         if miscTab == "tools" then
  863.                             term.setTextColor(colors.white)
  864.                             term.setBackgroundColor(colors.gray)
  865.                             if b.button(1,sclick,33-#ft+mval-5,9,"clear") then
  866.                                 pixelmap = {}
  867.                                 drawmap = {}
  868.                                 openpixelmap = {}
  869.                                 openmap = {}
  870.                                 isSaved = false
  871.                             end
  872.                             if b.button(1,sclick,29-#ft+mval-5,13,"fill all open") then
  873.                                 for x=dfs[1], dfs[1]+dfs[3]-2 do
  874.                                     for y=dfs[2]-dfs[4]+2, dfs[2]+dfs[4] do
  875.                                         if not drawmap[x] then drawmap[x] = {} end
  876.                                         if not drawmap[x][y] then drawmap[x][y] = {} end
  877.                                         drawmap[x][y] = {
  878.                                             t = cCol,
  879.                                             b = cBac,
  880.                                             s = cSym
  881.                                         }
  882.                                     end
  883.                                 end
  884.                                 isSaved = false
  885.                             end
  886.                             if b.button(1,sclick,28-#ft+mval-5,15,"fill background") then
  887.                                 local mergemap = {}
  888.                                 for x=dfs[1], dfs[1]+dfs[3]-2 do
  889.                                     for y=dfs[2]-dfs[4]+2, dfs[2]+dfs[4] do
  890.                                         if not mergemap[x] then mergemap[x] = {} end
  891.                                         if not mergemap[x][y] then mergemap[x][y] = {} end
  892.                                         mergemap[x][y] = {
  893.                                             t = cCol,
  894.                                             b = cBac,
  895.                                             s = cSym
  896.                                         }
  897.                                     end
  898.                                 end
  899.                                 for k,v in pairs(mergemap) do
  900.                                     if type(k) ~= "string" then
  901.                                         for k2,v2 in pairs(v) do
  902.                                             if not drawmap[k] then drawmap[k] = {} end
  903.                                             if not drawmap[k][k2] then drawmap[k][k2] = {}  end
  904.                                             if not next(drawmap[k][k2]) then
  905.                                                 drawmap[k][k2] = {
  906.                                                     t = cCol,
  907.                                                     b = cBac,
  908.                                                     s = cSym
  909.                                                 }
  910.                                             end
  911.                                         end
  912.                                     end
  913.                                 end
  914.                                 isSaved = false
  915.                             end
  916.                             if b.button(1,sclick,31-#ft+mval-5,11,"load file") then
  917.                                 term.clear()
  918.                                 term.setCursorPos(xs/2-19/2,ys/2)
  919.                                 term.write("what file to load ?")
  920.                                 term.setCursorPos(xs/2-18/2,ys/2+1)
  921.                                 win.setVisible(true)
  922.                                 local input = read()
  923.                                 win.setVisible(false)
  924.                                 local loadmap
  925.                                 if fs.exists(input..".nimg") then
  926.                                     local file =  fs.open(input..".nimg","r")
  927.                                     loadmap = textutils.unserialize(file.readAll())
  928.                                     loadmap = decode(loadmap)
  929.                                     file.close()
  930.                                 else
  931.                                     error("Terminated",0)
  932.                                 end
  933.                                 for k,v in pairs(loadmap) do
  934.                                     for k2,v2 in pairs(v) do
  935.                                         if not drawmap[k] then drawmap[k] = {} end
  936.                                         if not drawmap[k][k2] then drawmap[k][k2] = {} end
  937.                                         drawmap[k][k2] = v2
  938.                                     end
  939.                                 end
  940.                                 isSaved = false
  941.                             end
  942.                         end
  943.                     end
  944.                     --
  945.                     term.setBackgroundColor(colors.red)
  946.                     term.setTextColor(colors.white)
  947.                     if b.button(1,sclick,xs-3,4,"EXIT") then
  948.                         if isSaved == true then
  949.                             error("nimg closed",0)
  950.                         else
  951.                             windowOpen = ""
  952.                             exitMode = true
  953.                         end
  954.                     end
  955.                     if exitMode then
  956.                         block = {1,1,xs,ys}
  957.                         miscTab = ""
  958.                         windowOpen = "FILE"
  959.                         symbolTab = "basic"
  960.                         term.setBackgroundColor(colors.gray)
  961.                         b.frame(14+mval,5,27,2,"lightGray","gray")
  962.                         term.setTextColor(colors.red)
  963.                         term.setCursorPos(14+mval,4)
  964.                         term.write("you have unsaved progress.")
  965.                         term.setCursorPos(14+mval,5)
  966.                         term.write("do you want to continue ?")
  967.                         term.setTextColor(colors.black)
  968.                         term.setBackgroundColor(colors.green)
  969.                         if b.button(1,sclick,20+mval,6,"YES") then
  970.                             error("nimg closed",0)
  971.                         end
  972.                         term.setBackgroundColor(colors.red)
  973.                         if b.button(1,sclick,29+mval,6,"NO!") then
  974.                             exitMode = false
  975.                             block = {0,0,0,0}
  976.                             windowOpen = ""
  977.                         end
  978.                     end
  979.                     updateCur = updateCur + 1
  980.                     calcFPS()
  981.                     win.setVisible(true)
  982.                     sclick = {"tout",1,-1,-1}
  983.                     frames = frames + 1
  984.                     if useAccelerated then
  985.                         os.queueEvent("fake")
  986.                         os.pullEvent("fake")
  987.                     else
  988.                         sleep()
  989.                     end
  990.                 end
  991.                 while true do
  992.                     local xs, ys = sizeUpdate()
  993.                     drawBox(xs, ys)
  994.                     menu(xs, ys)
  995.                 end
  996.             end
  997.             parallel.waitForAll(bgswitch,menuClick,drawClick,draw,keyDown,keyUp)
  998.         elseif args[2] and args[1] == "create" then
  999.             if fs.exists(args[2]..".nimg") then error("file exists!",0) end
  1000.             local file = fs.open(args[2]..".nimg","w")
  1001.             file.write("{}")
  1002.             file.close()
  1003.             if openCreateFile then
  1004.                 shell.run(nimg,"edit",args[2],args[3] or "",args[4] or "")
  1005.             end
  1006.             error()
  1007.         elseif args[2] and args[1] == "view" then
  1008.             if fs.exists(args[2]..".nimg") then
  1009.                 b.fill(nil,nil,nil,nil,"\127")
  1010.                 term.setBackgroundColor(colors.black)
  1011.                 term.setTextColor(colors.black)
  1012.                 local index = {}
  1013.                 function index:draw(x, y)
  1014.                     if not x then x = 0 end
  1015.                     if not y then y = 0 end
  1016.                     for k,v in pairs(self) do
  1017.                         if type(k) == "number" then
  1018.                             for k2,v2 in pairs(v) do
  1019.                                 term.setCursorPos(k-self.offset[1]+x,k2-(self.offset[2]+1)+y)
  1020.                                 term.setBackgroundColor(v2.b)
  1021.                                 term.setTextColor(v2.t)
  1022.                                 term.write(v2.s)
  1023.                             end
  1024.                         end
  1025.                     end
  1026.                 end
  1027.                 function index:size()
  1028.                     return getSize(self)
  1029.                 end
  1030.                 local function loadImage(name)
  1031.                     if not name then name = "" end
  1032.                     if not fs.exists(name..".nimg") then error("not found. try using file name wihnout .nimg note that all files need to have .nimg extension to work",2) end
  1033.                     local file = fs.open(name..".nimg","r")
  1034.                     local image = textutils.unserialize(file.readAll())
  1035.                     local image = decode(image)
  1036.                     file.close()
  1037.                     return setmetatable(image, {__index = index})
  1038.                 end
  1039.                 term.setBackgroundColor(colors.black)
  1040.                 term.clear()
  1041.                 local img = loadImage(args[2])
  1042.                 b.frame(3,ys/2+1,xs-2,ys/2-1,"lightGray","gray")
  1043.                 img:draw(3,(ys/2))
  1044.                 local txt = "press any char to continue..."
  1045.                 term.setCursorPos(xs/2-#txt/2+1,ys)
  1046.                 term.setBackgroundColor(colors.gray)
  1047.                 term.write(txt)
  1048.                 os.pullEvent("char")
  1049.                 term.setTextColor(colors.white)
  1050.                 term.setBackgroundColor(colors.black)
  1051.                 term.clear()
  1052.                 term.setCursorPos(1,1)
  1053.                 error()
  1054.             else
  1055.                 error("not found. try using file name wihnout .nimg note that all files need to have .nimg extension to work",0)
  1056.             end
  1057.         elseif args[1] == "update" then
  1058.             term.setBackgroundColor(colors.gray)
  1059.             term.setTextColor(colors.white)
  1060.             term.clear()
  1061.             term.setCursorPos(xs/2-6/2,ys/2)
  1062.             term.write("UPDATING")
  1063.             if win then win.setVisible(true) end
  1064.             fs.delete(nimg)
  1065.             local web = http.get("https://pastebin.com/raw/2nbDhRXC")
  1066.             local webdata = web.readAll()
  1067.             local file = fs.open(nimg,"w")
  1068.             file.write(webdata)
  1069.             web.close()
  1070.             file.close()
  1071.             if win then win.setVisible(false) end
  1072.             term.setTextColor(colors.white)
  1073.             term.setBackgroundColor(colors.black)
  1074.             term.clear()
  1075.             term.setCursorPos(1,1)
  1076.             error("updated",0)
  1077.         elseif args[1] == "createImageSet" and args[2] then
  1078.             if not fs.exists(args[2]..".animg") then
  1079.                 fs.makeDir(args[2]..".animg")
  1080.             else
  1081.                 error("file exists!",0)
  1082.             end
  1083.             error()
  1084.         elseif args[1] == "addFrame" and args[2] then
  1085.             if not fs.exists(args[2]..".animg") then
  1086.                 error("image set doesnt exist!",0)
  1087.             else
  1088.                 if not args[3] then error("you need to provide frame name!",0) end
  1089.                 local count = 1
  1090.                 for k,v in pairs(fs.list(args[2]..".animg")) do
  1091.                     if (v):match("^(%d+)") then
  1092.                         count = count + 1
  1093.                     end
  1094.                 end
  1095.                 local file = fs.open(args[2]..".animg/"..tostring(count).."_"..args[3]..".nimg","w")
  1096.                 file.write("{}")
  1097.                 file.close()
  1098.                 error()
  1099.             end
  1100.         elseif args[1] == "getAPI" then
  1101.             shell.run("pastebin get zLnBmssj nimgAPI")
  1102.             error("NIMG API installed!",0)
  1103.         elseif args[1] == "help" and args[2] == "images" then
  1104.             error("edit: allows  you to edit a file using the nimg editor. usage:\nnimg edit <filename>\n\ncreate: allows you to create a new nimg file. using:\nnimg create <filename>\n",0)
  1105.         elseif args[1] == "help" and args[2] == "image_sets" then
  1106.             error("createImageSet: allows you to make image set. image sets allow you to store multiple images in a folder and then iterate/animate/draw them. usage:\nnimg createImageSet <name>\n\naddFrame: allows you  to add new images into image sets, image sets images need  to have pretty specific names and addFrame handles it for you\nnimg addFrame <imageSet> <frameName>",0)
  1107.         else
  1108.             local index = {}
  1109.             local indexAnimate = {}
  1110.             local indexBuffer = {}
  1111.             local function strech(tbl,x,y)
  1112.                 if type(x) == "number" and type(y) == "number" then
  1113.                     local strechmap = {}
  1114.                     local strechmapy = {}
  1115.                     local final
  1116.                     local mulx = 1  
  1117.                     local muly = 1
  1118.                     strechmap["offset"] = tbl.offset
  1119.                     strechmapy["offset"] = tbl.offset
  1120.                     for k,v in pairs(tbl) do
  1121.                         if type(k) == "number" then
  1122.                             for k2,v2 in pairs(v) do
  1123.                                 for i=1,x do
  1124.                                     if x > 1 then mulx = x end
  1125.                                     if not strechmap[k*mulx+i] then strechmap[k*mulx+i] = {} end
  1126.                                     if not strechmap[k*mulx+i][k2] then strechmap[k*mulx+i][k2] = {} end
  1127.                                     strechmap[k*mulx+i][k2] = tbl[k][k2]
  1128.                                 end
  1129.                             end
  1130.                         end
  1131.                     end
  1132.                     if y > 1 then
  1133.                         for k,v in pairs(strechmap) do
  1134.                             if type(k) == "number" then
  1135.                                 for k2,v2 in pairs(v) do
  1136.                                     for i=1,y do
  1137.                                         if y > 1 then muly = y  end
  1138.                                         if not strechmapy[k] then strechmapy[k] = {} end
  1139.                                         if not strechmapy[k][k2*muly+i] then strechmapy[k][k2*muly+i] = {} end
  1140.                                         strechmapy[k][k2*muly+i] = strechmap[k][k2]
  1141.                                     end
  1142.                                 end
  1143.                             end
  1144.                         end
  1145.                     end
  1146.                     if not next(strechmap) then strechmap = tbl end
  1147.                     if y > 1 then final = strechmapy
  1148.                     else final = strechmap end
  1149.                     return setmetatable(final,getmetatable(tbl))
  1150.                 else
  1151.                     return tbl
  1152.                 end
  1153.             end
  1154.             function index:strech(x,y)
  1155.                 local tbl = self
  1156.                 if not self then error("try using : instead of .",0) end
  1157.                 if type(x) == "number" and type(y) == "number" then
  1158.                     local strechmap = {}
  1159.                     local strechmapy = {}
  1160.                     local final
  1161.                     local mulx = 1  
  1162.                     local muly = 1
  1163.                     strechmap["offset"] = tbl.offset
  1164.                     strechmapy["offset"] = tbl.offset
  1165.                     for k,v in pairs(tbl) do
  1166.                         if type(k) == "number" then
  1167.                             for k2,v2 in pairs(v) do
  1168.                                 for i=1,x do
  1169.                                     if x > 1 then mulx = x end
  1170.                                     if not strechmap[k*mulx+i] then strechmap[k*mulx+i] = {} end
  1171.                                     if not strechmap[k*mulx+i][k2] then strechmap[k*mulx+i][k2] = {} end
  1172.                                     strechmap[k*mulx+i][k2] = tbl[k][k2]
  1173.                                 end
  1174.                             end
  1175.                         end
  1176.                     end
  1177.                     if y > 1 then
  1178.                         for k,v in pairs(strechmap) do
  1179.                             if type(k) == "number" then
  1180.                                 for k2,v2 in pairs(v) do
  1181.                                     for i=1,y do
  1182.                                         if y > 1 then muly = y  end
  1183.                                         if not strechmapy[k] then strechmapy[k] = {} end
  1184.                                         if not strechmapy[k][k2*muly+i] then strechmapy[k][k2*muly+i] = {} end
  1185.                                         strechmapy[k][k2*muly+i] = strechmap[k][k2]
  1186.                                     end
  1187.                                 end
  1188.                             end
  1189.                         end
  1190.                     end
  1191.                     if not next(strechmap) then strechmap = tbl end
  1192.                     if y > 1 then final = strechmapy
  1193.                     else final = strechmap end
  1194.                     return setmetatable(final,getmetatable(tbl))
  1195.                 else
  1196.                     return tbl
  1197.                 end
  1198.             end
  1199.             function index:draw(termobj, x, y, ix, iy)
  1200.                 expect(1,termobj,"table","nil")
  1201.                 expect(2,x,"number","nil")
  1202.                 expect(3,y,"number","nil")
  1203.                 local self = strech(self,ix,iy)
  1204.                 local terms = termobj or term
  1205.                 local x = x or 0
  1206.                 local y = y or 0
  1207.                 local obc = terms.getBackgroundColor()
  1208.                 local otc = terms.getTextColor()
  1209.                 for k,v in pairs(self) do
  1210.                     if type(k) == "number" then
  1211.                         for k2,v2 in pairs(v) do
  1212.                             terms.setCursorPos(k-(self.offset[1])+x,k2-(self.offset[2]/2)+y)
  1213.                             terms.setBackgroundColor(v2.b)
  1214.                             terms.setTextColor(v2.t)
  1215.                             terms.write(v2.s)
  1216.                         end
  1217.                     end
  1218.                 end
  1219.                 terms.setBackgroundColor(obc)
  1220.                 terms.setTextColor(otc)
  1221.             end
  1222.             function indexAnimate:drawImage(termobj,frame,x,y,ix,iy)
  1223.                 expect(1,termobj,"table","nil")
  1224.                 expect(2,frame,"number")
  1225.                 expect(3,x,"number","nil")
  1226.                 expect(4,y,"number","nil")
  1227.                 if not self then return false end
  1228.                 local frame = frame or 1
  1229.                 local frameToLoad = self[1][math.min(frame,#self[1])]
  1230.                 local frameToLoad = strech(frameToLoad,ix,iy)
  1231.                 if frameToLoad then
  1232.                     local terms = termobj or term
  1233.                     if not x then x = 0 end
  1234.                     if not y then y = 0 end
  1235.                     local obc = terms.getBackgroundColor()
  1236.                     local otc = terms.getTextColor()
  1237.                     for k,v in pairs(frameToLoad) do
  1238.                         if type(k) == "number" then
  1239.                             for k2,v2 in pairs(v) do
  1240.                                 terms.setCursorPos(k-(frameToLoad.offset[1])+x,k2-(frameToLoad.offset[2]/2)+y)
  1241.                                 terms.setBackgroundColor(v2.b)
  1242.                                 terms.setTextColor(v2.t)
  1243.                                 terms.write(v2.s)
  1244.                             end
  1245.                         end
  1246.                     end
  1247.                     terms.setBackgroundColor(obc)
  1248.                     terms.setTextColor(otc)
  1249.                 end
  1250.             end
  1251.             function indexAnimate:animate(termobj,x,y,speed,buffer,clears,ix,iy)
  1252.                 expect(1,termobj,"table")
  1253.                 expect(2,x,"number")
  1254.                 expect(3,y,"number")
  1255.                 expect(4,speed,"number")
  1256.                 expect(5,buffer,"boolean")
  1257.                 expect(6,clears,"boolean")
  1258.                 local terms = termobj or term.current()
  1259.                 local tx, ty = terms.getSize()
  1260.                 if buffer then terms = window.create(terms,1,1,tx,ty)  end
  1261.                 local x = x or 0
  1262.                 local y = y or 0
  1263.                 if not speed then speed = 1 end
  1264.                 if clears == nil then clears = false end
  1265.                 if buffer == nil then buffer = false end
  1266.                 local obc = terms.getBackgroundColor()
  1267.                 local otc = terms.getTextColor()
  1268.                 for k1,v1 in ipairs(self[1]) do
  1269.                     local v1 = strech(v1,ix,iy)
  1270.                     if buffer then terms.setVisible(false) end
  1271.                     terms.setBackgroundColor(obc)
  1272.                     terms.setTextColor(otc)
  1273.                     if clears then terms.clear() end
  1274.                     local count = 0
  1275.                     local curcount = 0
  1276.                     for k,v in pairs(v1) do
  1277.                         count = count + 1
  1278.                     end
  1279.                     for k,v in pairs(v1) do
  1280.                         if type(k) == "number" then
  1281.                             for k2,v2 in pairs(v) do
  1282.                                 terms.setCursorPos(k-(v1.offset[1])+x,k2-(v1.offset[2]/2)+y)
  1283.                                 terms.setBackgroundColor(v2.b)
  1284.                                 terms.setTextColor(v2.t)
  1285.                                 terms.write(v2.s)
  1286.                             end
  1287.                         end
  1288.                     end
  1289.                     if buffer then terms.setVisible(true) end
  1290.                     curcount = curcount + 1
  1291.                     if curcount < #self[1] then sleep(speed) end
  1292.                 end
  1293.                 terms.setBackgroundColor(obc)
  1294.                 terms.setTextColor(otc)
  1295.             end
  1296.             function indexAnimate:slide(termobj,x,y,ix,iy)
  1297.                 expect(1,termobj,"table")
  1298.                 expect(2,x,"number")
  1299.                 expect(3,y,"number")
  1300.                 local animation = self[1]
  1301.                 local loadFrame =  self[2] + 1
  1302.                 if not animation[loadFrame] then
  1303.                     return setmetatable({animation,0,false}, {__index = indexAnimate})
  1304.                 end
  1305.                 local terms = termobj or term
  1306.                 local x = x or 0
  1307.                 local y = y or 0
  1308.                 local obc = terms.getBackgroundColor()
  1309.                 local otc = terms.getTextColor()
  1310.                 animation[loadFrame] = strech(animation[loadFrame],ix,iy)
  1311.                 for k,v in pairs(animation[loadFrame]) do
  1312.                     if type(k) == "number" then
  1313.                         for k2,v2 in pairs(v) do
  1314.                             terms.setCursorPos(k-(animation[loadFrame].offset[1])+x,k2-(animation[loadFrame].offset[2]/2)+y)
  1315.                             terms.setBackgroundColor(v2.b)
  1316.                             terms.setTextColor(v2.t)
  1317.                             terms.write(v2.s)
  1318.                         end
  1319.                     end
  1320.                 end
  1321.                 terms.setBackgroundColor(obc)
  1322.                 terms.setTextColor(otc)
  1323.                 return setmetatable({animation,loadFrame,true}, {__index = indexAnimate})
  1324.             end
  1325.             function indexAnimate:iterate()
  1326.                 local currentImage = 0
  1327.                 local allFramesRaw = self[1]
  1328.                 return function()
  1329.                     currentImage = currentImage + 1
  1330.                     if allFramesRaw[currentImage] then
  1331.                         return setmetatable(allFramesRaw[currentImage], {__index = index}), allFramesRaw, currentImage
  1332.                     end
  1333.                 end
  1334.             end
  1335.             local function loadImage(name)
  1336.                 expect(1,name,"string")
  1337.                 local name = name or ""
  1338.                 if not fs.exists(name..".nimg") then error("not found. try using file name wihnout .nimg note that all files need to have .nimg extension to work",2) end
  1339.                 local file = fs.open(name..".nimg","r")
  1340.                 local image = textutils.unserialize(file.readAll())
  1341.                 local image = decode(image)
  1342.                 file.close()
  1343.                 return setmetatable(image, {__index = index})
  1344.             end
  1345.             local function loadImageSet(name)
  1346.                 local frames = {}
  1347.                 expect(1,name,"string")
  1348.                 local name = name or ""
  1349.                 if not fs.isDir(name..".animg") then error("not found. try using file name wihnout .animg note that all image sets need to have .animg extension to work",2) end
  1350.                 for k,v in pairs(fs.list(name..".animg")) do
  1351.                     local fn, ext = v:match("^(%d+).+(%..-)$")
  1352.                     if ext == ".nimg" then
  1353.                         local file = fs.open(name..".animg/"..v,"r")
  1354.                         frames[tonumber(fn)] = textutils.unserialise(file.readAll())
  1355.                         frames[tonumber(fn)] = decode(frames[tonumber(fn)])
  1356.                         file.close()
  1357.                     end
  1358.                 end
  1359.                 return setmetatable({frames,0}, {__index = indexAnimate})
  1360.             end
  1361.             function indexBuffer:startBuffer(noclear)
  1362.                 expect(1,noclear,"boolean","nil")
  1363.                 self.setVisible(false)
  1364.                 if not noclear then buffer.clear() end
  1365.             end
  1366.             function indexBuffer:endBuffer()
  1367.                 self.setVisible(true)
  1368.             end
  1369.             local function createBuffer(termObj)
  1370.                 expect(1,termObj,"table")
  1371.                 local terms = termObj or term.current()
  1372.                 local xs, ys = terms.getSize()
  1373.                 return setmetatable(window.create(terms,1,1,xs,ys),{__index=indexBuffer})
  1374.             end
  1375.             local function getButtonH()
  1376.                 return require("ButtonH")
  1377.             end
  1378.             local function downloadPastekage(pasteCode)
  1379.                 --pasteges are packages for pastes:
  1380.                 --they allow downloading a set of images by putting into an paste
  1381.                 --pastekage format:
  1382.                 --l1: **PASTEKAGE**
  1383.                 --l2: [NAME]:PASTEBINCODE
  1384.                 --l3: [NAME]:PASTEBINCODE
  1385.                 --l4: etc...
  1386.                 local pastecage, err = http.get("https://pastebin.com/raw/"..pasteCode)
  1387.                 if err then error(err,0) end
  1388.                 local rand = tostring(math.random(1,1000000))
  1389.                 local temp = fs.open("9551_NIMG_MAIN_API_TEMP_PASTKAGE_"..rand,"w")
  1390.                 temp.write(pastecage.readAll())
  1391.                 temp.close()
  1392.                 pastecage.close()
  1393.                 local lc = 0
  1394.                 for l in io.lines("9551_NIMG_MAIN_API_TEMP_PASTKAGE_"..rand) do
  1395.                     lc = lc + 1
  1396.                     if lc == 1 then if l ~= "**PASTEKAGE**" then error("not an pastekage!",0) end end
  1397.                     if lc > 1 then
  1398.                         local name, code = l:match("^%[(.+)%]:(.+)")
  1399.                         local web, err = http.get("https://pastebin.com/raw/"..code)
  1400.                         if err then error(err,0) end
  1401.                         local file = fs.open(name,"w")
  1402.                         file.write(web.readAll())
  1403.                         file.close()
  1404.                         web.close()
  1405.                     end
  1406.                 end
  1407.                 fs.delete("9551_NIMG_MAIN_API_TEMP_PASTKAGE_"..rand)
  1408.             end
  1409.             return {
  1410.                 loadImage = loadImage,
  1411.                 loadImageSet = loadImageSet,
  1412.                 getButtonH = getButtonH,
  1413.                 createBuffer = createBuffer,
  1414.                 stretch2DMap = strech,
  1415.                 downloadPastekage = downloadPastekage,
  1416.                 encode = encode,
  1417.                 decode = decode
  1418.             }
  1419.         end
  1420.     end
  1421.     local ok, err = true
  1422.     while ok do
  1423.         ok, err = pcall(mainCode)
  1424.         if not ok and err == "Terminated" then ok = true end
  1425.         if type(err) == "table" then ok = false return err end
  1426.         sleep()
  1427.     end
  1428.     if args[2] and args[1] == "edit" then
  1429.         if arg[3] ~= "monitorLaunch" then
  1430.             term.redirect(oldTerm)
  1431.         end
  1432.         term.setTextColor(colors.white)
  1433.         term.setBackgroundColor(colors.black)
  1434.         term.clear()
  1435.         term.setCursorPos(1,1)
  1436.     end
  1437.     if not ok then
  1438.         error(err,2)
  1439.     end
  1440. end
Add Comment
Please, Sign In to add comment