Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --local oe = os.pullEvent
- --os.pullEvent = os.pullEventRaw
- local args = {...}
- local arg = args[2]
- if not fs.exists("ButtonH") then shell.run("pastebin get LTDZZZEJ ButtonH") end
- local b = require("ButtonH").terminal
- local xs, ys = term.getSize()
- local win
- local oldTerm
- if args[2] and args[1] == "edit" then
- win = window.create(term.current(), 1, 1, xs, ys)
- oldTerm = term.redirect(win)
- end
- local keybinds = {
- delete = keys.x
- }
- local mainCode = function()
- local getSize = function(tbl)
- local xindex = {}
- local yindex = {}
- for k,v in pairs(tbl) do
- if type(k) == "number" then
- table.insert(xindex,k-tbl.offset[1]+1)
- for k2 in pairs(v) do
- table.insert(yindex,k2-(tbl.offset[2]))
- end
- end
- end
- return {
- math.min(table.unpack(xindex) or 0),
- math.min(table.unpack(yindex) or 0),
- math.max(table.unpack(xindex) or 0),
- math.max(table.unpack(yindex) or 0)
- }
- end
- local count = function(tbl)
- local count = 0
- for k,v in pairs(tbl) do
- if type(k) == "number" then
- for k,v in pairs(v) do
- count = count + 1
- end
- end
- end
- return count
- end
- local sTime = os.time()
- local frames = 0
- local FPS = 0
- local cTime = os.time()
- local calcFPS = function()
- local cTime = os.time()
- local tDif = (cTime-sTime)/0.02
- FPS = frames/tDif
- end
- local pixelmap
- local drawmap
- local openmap
- local openpixelmap
- 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
- if args[2] and args[1] == "edit" then
- if arg then
- if fs.exists(arg..".nimg") then
- local file = fs.open(arg..".nimg","r")
- drawmap = textutils.unserialize(file.readAll())
- file.close()
- else
- error("not found. try using file name wihnout .nimg",0)
- end
- end
- local sclick = {"tout",1,-1,-1}
- local dclick = {"tout",1,-1,-1}
- if not pixelmap then pixelmap = {} end
- if not drawmap then drawmap = {} end
- if not openmap then openmap = {} end
- if not openpixelmap then openpixelmap = {} end
- local cCol = colors.black
- local cBac = colors.white
- local cSym = " "
- local sizeUpdate = function()
- win.reposition(1,1,term.getSize())
- xs, ys = win.getSize()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- return xs, ys
- end
- sizeUpdate()
- local cfilter = {
- [2]=true,
- [3]=true,
- [4]=true,
- [5]=true
- }
- local c2filter = {
- [1]=true,
- [3]=true
- }
- local eventQueue = {}
- local menuClick = function()
- while true do
- sclick = b.timetouch(10,cfilter,false)
- end
- end
- local drawClick = function()
- while true do
- dclick = b.timetouch(10,cfilter,true)
- end
- end
- local keyDown = function()
- while true do
- _,keyDownEv = os.pullEvent("key")
- if not eventQueue[keyDownEv] then
- eventQueue[keyDownEv] = true
- end
- end
- end
- local keyUp = function()
- while true do
- _,keyUpEv = os.pullEvent("key_up")
- eventQueue[keyUpEv] = false
- end
- end
- local dfs = {3,7,11,4}
- local windowbug = 1
- local wincount = 0
- local updateSkip = 100
- local updateCur = 0
- local yy = 2
- local isSaved = true
- local draw = function()
- local drawBox = function(xs,ys)
- sizeUpdate()
- win.setVisible(false)
- if wincount >= windowbug then
- win.clear()
- wincount = 0
- end
- wincount = wincount + 1
- if dfs[3] < xs-9 then
- term.setTextColor(colors.green)
- if b.boxButton(1,dclick,dfs[1]+dfs[3]+3,dfs[2]-2,"green","black","+",2,1) then
- dfs[3] = dfs[3] + 1
- end
- end
- if dfs[3] > 11 then
- term.setTextColor(colors.red)
- if b.boxButton(1,dclick,dfs[1]+dfs[3]+3,dfs[2]+2,"red","black","-",2,1) then
- dfs[3] = dfs[3] - 1
- end
- end
- if dfs[4]+dfs[2] < ys-4 then
- if b.boxButton(1,dclick,dfs[1]+(dfs[3]/2)-3,dfs[4]*2+5,"green","black","+",2,1) then
- dfs[4] = dfs[4] + 0.5
- dfs[2] = dfs[2] + 0.5
- yy = yy + 1
- end
- end
- if dfs[4] > 4 then
- if b.boxButton(1,dclick,dfs[1]+(dfs[3]/2)+3,dfs[4]*2+5,"red","black","-",2,1) then
- dfs[4] = dfs[4] - 0.5
- dfs[2] = dfs[2] - 0.5
- yy = yy - 1
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.black)
- b.fill(nil,nil,nil,nil,"\127")
- b.frame(dfs[1],dfs[2],dfs[3],dfs[4],"lightGray","gray",false)
- for x=dfs[1], dfs[1]+dfs[3]-2 do
- for y=dfs[2]-dfs[4]+2, dfs[2]+dfs[4] do
- if b.API(dclick,x,y-1,1,1) then
- isSaved = false
- if not pixelmap[x-dfs[1]+1] then pixelmap[x-dfs[1]+1] = {} end
- if not pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] then pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] = {} end
- if not drawmap[x] then drawmap[x] = {} end
- if not drawmap[x][y] then drawmap[x][y] = {} end
- if b.switch("db",1) or eventQueue[keybinds.delete] then
- pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] = nil
- drawmap[x][y] = nil
- dclick = {"tout",1,-1,-1}
- else
- pixelmap[x-dfs[1]+1][y-dfs[2]+yy+1] = {
- tcol = cCol,
- bcol = cBac,
- sym = cSym
- }
- drawmap[x][y] = {}
- drawmap[x][y] = {
- tcol = cCol,
- bcol = cBac,
- sym = cSym
- }
- end
- end
- end
- end
- openmap = {}
- openpixelmap = {}
- for k,v in pairs(drawmap) do
- if type(k) == "number" then
- for k2,v2 in pairs(v) do
- if k < dfs[1]+dfs[3]-1 then
- if k2 < dfs[2]+dfs[4]+1 then
- term.setCursorPos(k,k2-1)
- term.setBackgroundColor(v2.bcol)
- term.setTextColor(v2.tcol)
- term.write(v2.sym)
- if not openmap[k-dfs[1]+1] then openmap[k-dfs[1]+1] = {} end
- if not openmap[k-dfs[1]+1][k2-dfs[2]+yy+1] then openmap[k-dfs[1]+1][k2-dfs[2]+yy+1] = {} end
- if not openpixelmap[k] then openpixelmap[k] = {} end
- if not openpixelmap[k][k2] then openpixelmap[k][k2] = {} end
- openmap[k-dfs[1]+1][k2-dfs[2]+yy+1] = {
- tcol = v2.tcol,
- bcol = v2.bcol,
- sym = v2.sym
- }
- openpixelmap[k][k2] = {
- tcol = v2.tcol,
- bcol = v2.bcol,
- sym = v2.sym
- }
- end
- end
- end
- end
- end
- openpixelmap["offset"] = dfs
- end
- local menu = function(xs, ys)
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.gray)
- term.write((" "):rep(xs))
- term.setCursorPos(1,2)
- term.write((" "):rep(xs))
- term.setBackgroundColor(colors.black)
- if not isSaved then
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- else
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.black)
- end
- if b.button(1,sclick,1,1,"SAVE") then
- isSaved = true
- local file = fs.open(args[2]..".nimg","w")
- file.write(textutils.serialize(openpixelmap))
- file.close()
- end
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.gray)
- b.switch(1,1,sclick,xs-4,1,"red","green","white","\8",5,1)
- if updateCur >= updateSkip then
- term.setCursorPos(1,2)
- term.write(args[2]..".nimg fs: "..("%.1fKb"):format(fs.getSize(args[2]..".nimg")/1024))
- term.setCursorPos(6,1)
- term.write("chars: "..count(openpixelmap))
- term.setCursorPos(dfs[3]+2,dfs[2]+dfs[4])
- term.write(("x%d y%d"):format(dfs[3]-1,dfs[2]+dfs[4]-4))
- end
- updateCur = updateCur + 1
- calcFPS()
- --code
- win.setVisible(true)
- sclick = {"tout",1,-1,-1}
- frames = frames + 1
- sleep()
- end
- while true do
- local xs, ys = sizeUpdate()
- drawBox(xs, ys)
- menu(xs, ys)
- end
- end
- parallel.waitForAll(menuClick,drawClick,draw,keyDown,keyUp)
- elseif args[2] and args[1] == "create" then
- if fs.exists(args[2]..".nimg") then error("file exists!",0) end
- local file = fs.open(args[2]..".nimg","w")
- file.write("{}")
- file.close()
- elseif args[2] and args[1] == "view" then
- if fs.exists(args[2]..".nimg") then
- b.fill(nil,nil,nil,nil,"\127")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.black)
- local index = {}
- function index:draw(x, y)
- if not x then x = 0 end
- if not y then y = 0 end
- for k,v in pairs(self) do
- if type(k) == "number" then
- for k2,v2 in pairs(v) do
- term.setCursorPos(k-self.offset[1]+x,k2-(self.offset[2]+1)+y)
- term.setBackgroundColor(v2.bcol)
- term.setTextColor(v2.tcol)
- term.write(v2.sym)
- end
- end
- end
- end
- function index:size()
- return getSize(self)
- end
- local function loadImage(name)
- if not name then name = "" end
- if not fs.exists(name..".nimg") then error("Error: "..name.." not found",2) end
- local file = fs.open(name..".nimg","r")
- local image = textutils.unserialize(file.readAll())
- file.close()
- return setmetatable(image, {__index = index})
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- local img = loadImage(args[2])
- local xss,yss,xe,ye = table.unpack(img:size())
- b.frame(3,ys/2+1,xs-2,ys/2-1,"lightGray","gray")
- img:draw(3,(ys/2)-1)
- os.pullEvent("char")
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- error()
- else
- error("no image found",0)
- end
- else
- local index = {}
- function index:draw(termobj, x, y)
- local terms = termobj or term
- if not x then x = 0 end
- if not y then y = 0 end
- local obc = terms.getBackgroundColor()
- local otc = terms.getTextColor()
- for k,v in pairs(self) do
- if type(k) == "number" then
- for k2,v2 in pairs(v) do
- terms.setCursorPos(k-self.offset[1]+x,k2-(self.offset[2]+1)+y)
- terms.setBackgroundColor(v2.bcol)
- terms.setTextColor(v2.tcol)
- terms.write(v2.sym)
- end
- end
- end
- terms.setBackgroundColor(obc)
- terms.setTextColor(otc)
- end
- function index:size()
- return getSize(self)
- end
- local function loadImage(name)
- if not name then name = "" end
- if not fs.exists(name..".nimg") then error("Error: "..name.." not found",2) end
- local file = fs.open(name..".nimg","r")
- local image = textutils.unserialize(file.readAll())
- file.close()
- return setmetatable(image, {__index = index})
- end
- return {
- loadImage = loadImage
- }
- end
- end
- local ok,err = pcall(mainCode)
- --os.pullEvent = oe
- if ok and type(err) == "table" then
- return err
- end
- if args[2] and args[1] == "edit" then
- term.redirect(oldTerm)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- end
- if not ok then
- error(err,2)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement