Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not fs.exists(shell.resolve("blittle")) then shell.run("/rom/programs/http/pastebin get ujchRSnU blittle") end
- os.loadAPI(shell.resolve("blittle"))
- local args = {...}
- if #args < 1 then
- printError("Usage: "..shell.getRunningProgram().." <filename>")
- return
- end
- --[[
- {
- ["width"] = (number) image width
- ["height"] = (number) image height
- [1] = {
- [1] = First text row.
- [2] = Second text row.
- -- etc
- },
- [2] = {
- [1] = First textColour row.
- [2] = Second textColour row.
- -- etc
- },
- [3] = {
- [1] = First backgroundColour row.
- [2] = Second backgroundColour row.
- -- etc
- }
- }
- ]]
- local lookup = "0123456789abcdef"
- local precomp2H = {}
- for i=1,#lookup do
- precomp2H[2^(i-1)] = lookup:sub(i,i)
- end
- local function toHex(dec)
- return precomp2H[dec]
- end
- local precomp2D = {}
- for i=1,#lookup do
- precomp2D[lookup:sub(i,i)] = 2^(i-1)
- end
- local function toDec(hex)
- return precomp2D[hex]
- end
- local fn = args[1]
- local bimg = {}
- if fs.exists(fn) then
- bimg = blittle.load(fn)
- else
- bimg = {width=0,height=0,[1]={" "},[2]={"f"},[3]={"f"}}
- end
- local transpChar = "\127"
- local offx,offy = 1,1
- local scnw,scnh = term.getSize()
- local showToolbar = true
- local pallette = {"f","f"}
- local dirtyInfo = true
- local dirtyLine = -1
- local dirtyPallette = true
- local blobj = {width=0,height=0,{},{},{}}
- local function convertToBLittle()
- local eobj = {}
- for i=1,#bimg[3] do
- eobj[i] = {}
- for n in bimg[3][i]:gmatch(".") do
- table.insert(eobj[i],toDec(n))
- end
- end
- blobj = blittle.shrink(eobj)
- end
- --sticky is 1 of the following: 0-topLeft 1-topRight 2-bottomLeft 3-bottomRight
- local function enlargeCanvas(dx,dy,sticky)
- local newWidth = bimg.width + dx
- local newHeight = bimg.height + dy
- local newData = {width=newWidth,height=newHeight,{},{},{}}
- local stickyX = sticky % 2 == 0 and true or false
- local stickyY = sticky < 2 and true or false
- for i=1,newHeight do
- newData[1][i] = (" "):rep(newWidth)
- newData[2][i] = ("0"):rep(newWidth)
- newData[3][i] = ("f"):rep(newWidth)
- end
- --Now copy old data into new canvas
- local counter = 1
- for i = (stickyY and 1 or 1+dy), (stickyY and bimg.height or newHeight) do
- if stickyX then
- newData[3][i] = bimg[3][counter]..("f"):rep(dx) --left aligned
- else
- newData[3][i] = ("f"):rep(dx)..bimg[3][counter] --right aligned
- end
- counter = counter + 1
- end
- offx = offx - (stickyX and 0 or dx)
- offy = offy - (stickyY and 0 or dy)
- dirtyInfo = true
- bimg = newData
- end
- local drawMode = false --false=normal true=blittle
- local function draw()
- if dirtyInfo then
- for i=1,scnh do
- term.setCursorPos(1,i)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.gray)
- term.write(transpChar:rep(scnw))
- end
- end
- if drawMode then
- --blittle.draw(table blittle image [, number xPos] [, number yPos] [, table terminal])
- if dirtyInfo then
- blittle.draw(blobj, offx, offy, term.current())
- end
- else
- if dirtyLine < 1 and not dirtyInfo and not dirtyPallette then
- return
- end
- if dirtyInfo then
- for i=1,scnh do
- --for i=offy,math.min(#bimg[1]) do
- if i-offy < bimg.height and i-offy+1 > 0 then
- local py = i-offy+1
- term.setCursorPos(offx,i)
- term.blit(bimg[1][py],bimg[2][py],bimg[3][py])
- end
- end
- elseif dirtyLine > 0 then
- term.setCursorPos(offx,offy+dirtyLine-1)
- term.blit(bimg[1][dirtyLine],bimg[2][dirtyLine],bimg[3][dirtyLine])
- end
- if (dirtyInfo or dirtyPallette) and showToolbar then
- for i=1,16 do
- term.setBackgroundColor(2^(i-1))
- term.setCursorPos(scnw-1,i)
- term.write(" ")
- end
- for i=1,2 do
- term.setBackgroundColor(toDec(pallette[i]))
- term.setTextColor(colors.white)
- term.setCursorPos(scnw-2+i,17)
- term.write("\7\7")
- end
- end
- dirtyInfo = false
- end
- end
- while true do
- draw()
- local e,p1,p2,p3,p4,p5 = os.pullEvent()
- if e=="key" then
- local k = p1
- local ox,oy = offx, offy
- if k == keys.left or k == keys.a then
- offx = offx - 1
- elseif k == keys.right or k == keys.d then
- offx = offx + 1
- elseif k == keys.up or k == keys.w then
- offy = offy - 1
- elseif k == keys.down or k == keys.d then
- offy = offy + 1
- elseif k == keys.grave then
- drawMode = not drawMode
- if drawMode then
- convertToBLittle()
- end
- dirtyInfo = true
- end
- if ox~=offx or oy~=offy then
- dirtyInfo = true
- end
- elseif e=="mouse_click" or e=="mouse_drag" then
- local b,x,y = p1,p2,p3
- if x>scnw-2 and y<17 then
- --Picking a color
- pallette[b] = toHex(2^(y-1))
- dirtyPallette = true
- elseif x>scnw-2 and y==17 then
- --Copy color
- pallette[b] = pallette[x-(scnw-2)]
- dirtyPallette = true
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement