Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ]] Ascii Editor by Redxone [[ --
- -- ]] Simple API
- local stc = term.setTextColor
- local sbc = term.setBackgroundColor
- local scp = term.setCursorPos
- local cls = term.clear
- local w, h = term.getSize()
- local function printpos(text,x,y)
- scp(x,y)
- print(text)
- scp(x,y+1)
- end
- local function writepos(text,x,y)
- local xx, yy = term.getCursorPos()
- scp(x,y)
- write(text)
- scp(xx,yy)
- end
- -- Program start
- local char = "/"
- local takinginput = false
- local running = true
- local inMenu = false
- local canvas = {}
- local selectedcolorleft = 0
- local selectedcolorright = 4
- local selectedcharleft = "/"
- local selectedcharright = "-"
- local selectedoption = 0
- local file = "/"
- local inputspot = 0
- local targ = { ... }
- function checkSelected(left,right,equal)
- if tonumber(left) == equal
- and tonumber(right) ~= equal then
- return 1
- elseif tonumber(right) == equal
- and tonumber(left) ~= equal then
- return 2
- elseif tonumber(right) == equal and
- tonumber(left) == equal then
- return 3
- else
- return 0
- end
- end
- function drawSelected(left,right,equal,x,y,ltext,rtext,btext,ntext)
- if tonumber(left) == equal
- and tonumber(right) ~= equal then
- printpos(ltext, x, y)
- elseif tonumber(right) == equal
- and tonumber(left) ~= equal then
- printpos(rtext, x, y)
- elseif tonumber(right) == equal and
- tonumber(left) == equal then
- printpos(btext, x, y)
- else
- printpos(ntext, x, y)
- end
- end
- -- ]] GUI Visuals
- local function drawGUI()
- sbc(colors.black)
- cls()
- scp(1,1)
- for ch = 0, h-1 do
- for cw = 0, w-1 do
- stc(tonumber(canvas[ch][cw].color))
- writepos(canvas[ch][cw].char,cw,ch)
- end
- end
- -- Draw colors
- for i = 0, 15 do
- stc(colors.black)
- drawSelected(selectedcolorleft,selectedcolorright,math.pow(2,i),w-1, i,"L/","R/","B/","-/")
- sbc( tonumber( math.pow(2,i) ) )
- end
- sbc(colors.black)
- stc(colors.lightGray)
- drawSelected(selectedcolorleft,selectedcolorright,0,w-1, h-3,"|\127","\127|","||","\127\127")
- drawSelected(selectedcolorleft,selectedcolorright,65536,w-1, h-2,"L/","R/","B/","-/")
- sbc(colors.blue)
- stc(colors.white)
- printpos(selectedcharleft,w-1,h-1)
- sbc(colors.lightBlue)
- printpos(selectedcharright,w,h-1)
- paintutils.drawLine(1, h, w, h, colors.black)
- stc(colors.yellow)
- writepos("Press ctrl to open menu. ",1,h)
- end
- -- ]] Logic functions
- local function updateGUI()
- for i = 0, 15 do
- stc(colors.black)
- drawSelected(selectedcolorleft,selectedcolorright,math.pow(2,i),w-1, i,"L/","R/","B/","-/")
- sbc( tonumber( math.pow(2,i) ) )
- end
- sbc(colors.black)
- stc(colors.lightGray)
- drawSelected(selectedcolorleft,selectedcolorright,0,w-1, h-3,"|\127","\127|","||","\127\127")
- drawSelected(selectedcolorleft,selectedcolorright,65536,w-1, h-2,"L/","R/","B/","-/")
- sbc(colors.blue)
- stc(colors.white)
- printpos(selectedcharleft,w-1,h-1)
- sbc(colors.lightBlue)
- printpos(selectedcharright,w,h-1)
- end
- local function redrawChar(x,y)
- for yy = 0, h-1 do
- for xx = 0, w-1 do
- if(yy == y and xx == x)then
- scp(x,y)
- stc(canvas[yy][xx].color)
- write(canvas[yy][xx].char)
- end
- end
- end
- end
- local menu = {
- ['Save'] = {func=function()
- paintutils.drawLine(1, h, w, h, colors.black)
- scp(1,h)
- local f = fs.open(file,"w")
- f.write(textutils.serialize(canvas):gsub("\n%s*",""))
- f.close()
- write("Ascii saved to: " .. file)
- os.pullEvent("key")
- inMenu = false
- drawGUI()
- end},
- ['Load'] = {func=function()
- paintutils.drawLine(1, h, w, h, colors.black)
- scp(1,h)
- write("Load ascii from?: ")
- file_temp = read()
- if(fs.exists(file_temp))then
- local f = fs.open(file_temp,"r")
- local canvas_temp = textutils.unserialize(f.readAll())
- f.close()
- if(type(canvas_temp) == "table")then
- file = file_temp
- canvas = canvas_temp
- drawGUI()
- updateGUI()
- sbc(colors.black)
- stc(colors.white)
- scp(1,h)
- write("Ascii loaded from: " .. file)
- os.pullEvent("key")
- inMenu = false
- drawGUI()
- else
- scp(1,h-1)
- write("Invalid file type!")
- os.pullEvent("key")
- inMenu = false
- drawGUI()
- end
- else
- scp(1,h-1)
- write("No such file: " .. file_temp)
- os.pullEvent("key")
- inMenu = false
- drawGUI()
- end
- end},
- ['Exit'] = {func=function()
- running = false
- sbc(colors.black)
- stc(colors.white)
- cls()
- scp(1,1)
- print("Thank you for using Ascii Editor!, have a nice day.")
- end},
- ['Help'] = {func=function()
- sbc(colors.blue)
- cls()
- stc(colors.white)
- printpos("How to use the Ascii editor.",2,1)
- printpos("1. How to select colors",2,3)
- printpos("Left click a color to assign it to",4,4)
- printpos("the left click on you're mouse.",4,5)
- printpos("Right click a color to assign it to",4,6)
- printpos("the right click on your're mouse.",4,7)
- printpos("2. Changing ascii characters.",2,9)
- printpos("Left click the blue or light blue square,",4,10)
- printpos("hit enter if you dont want to change the char",4,11)
- printpos("or press a key on you're keyboard to change.",4,12)
- printpos("press enter when you have you're desired key.",4,13)
- printpos("Press any key to return to Ascii.",5,15)
- sbc(colors.black)
- stc(colors.white)
- os.pullEvent("key")
- inMenu = false
- drawGUI()
- end},
- }
- local function drawMenu()
- local i = 0
- for k,v in pairs(menu) do
- if(selectedoption == i)then
- scp(1 + (i * 6),h)
- stc(colors.yellow)
- write("[")
- stc(colors.white)
- write(k)
- stc(colors.yellow)
- write("]")
- else
- scp(1 + (i * 6),h)
- stc(colors.white)
- write(" " .. k .. " ")
- end
- i = i + 1
- end
- end
- local function updateMenu(event)
- if(event[1] == "key")then
- if(event[2] == keys['left'] and selectedoption > 0)then
- selectedoption = selectedoption - 1
- drawMenu()
- end
- if(event[2] == keys['right'] and selectedoption < 3 )then
- selectedoption = selectedoption + 1
- drawMenu()
- end
- if(event[2] == keys['enter'])then
- local i = 0
- for k,v in pairs(menu) do
- if(selectedoption == i)then
- v.func()
- end
- i = i + 1
- end
- end
- end
- end
- local function updateSelector(ev)
- if(not takinginput)then term.setCursorBlink(false) end
- if(ev[1] == "mouse_click")then
- if(takinginput)then takinginput = false end
- for i = 0, 15 do
- if(ev[3] >= w-1 and ev[3] <= w and ev[4] == i)then
- if(ev[2] == 1)then -- left click
- selectedcolorleft = math.pow(2,i)
- end
- if(ev[2] == 2)then -- right click
- selectedcolorright = math.pow(2,i)
- end
- updateGUI()
- end
- end
- -- black and transparent selection
- if(ev[3] >= w-1 and ev[3] <= w and ev[4] == 16)then
- if(ev[2] == 1)then -- left click
- selectedcolorleft = 0
- end
- if(ev[2] == 2)then -- right click
- selectedcolorright = 0
- end
- updateGUI()
- end
- if(ev[3] >= w-1 and ev[3] <= w and ev[4] == 17)then
- if(ev[2] == 1)then -- left click
- selectedcolorleft = 65536
- end
- if(ev[2] == 2)then -- right click
- selectedcolorright = 65536
- end
- updateGUI()
- end
- if(ev[3] >= w-1 and ev[4] == h-1 and takinginput == false )then
- takinginput = true
- sbc(colors.blue)
- stc(colors.white)
- printpos(" ",w-1,h-1)
- sbc(colors.lightBlue)
- printpos(" ",w,h-1)
- scp(w-1,h-1)
- term.setCursorBlink(true)
- end
- end
- if(ev[1] == 'key' and takinginput)then
- if(ev[2] == keys.enter)then
- sbc(colors.blue)
- stc(colors.white)
- if(inputspot == 1)then
- takinginput = false;
- inputspot = 0
- sbc(colors.black)
- sbc(colors.lightBlue)
- printpos(selectedcharright,w,h-1)
- term.setCursorBlink(false)
- elseif(inputspot == 0 )then
- inputspot = 1
- term.setCursorBlink(false)
- printpos(selectedcharleft,w-1,h-1)
- scp(w,h-1)
- term.setCursorBlink(true)
- end
- end
- end
- if(ev[1] == 'char' and takinginput)then
- sbc(colors.blue)
- stc(colors.white)
- if(inputspot == 0)then
- selectedcharleft = ev[2]
- printpos(selectedcharleft,w-1,h-1)
- term.setCursorBlink(false)
- elseif(inputspot == 1)then
- selectedcharright = ev[2]
- sbc(colors.lightBlue)
- printpos(selectedcharright,w,h-1)
- term.setCursorBlink(false)
- end
- end
- end
- local function asciiDraw(ev)
- if(ev[1] == "mouse_click" or ev[1] == "mouse_drag")then
- if(ev[3] < w-1 and ev[4] < h-1)then
- local xx = ev[3]
- local yy = ev[4]
- -- Draw to screen based on left or right click
- if(ev[2] == 1 and selectedcolorleft ~= 65536)then
- -- left click
- canvas[yy][xx] = {char=selectedcharleft,color=selectedcolorleft/2}
- elseif(selectedcolorright ~= 0 and selectedcolorright ~= 65536)then
- -- right click
- canvas[yy][xx] = {char=selectedcharright,color=selectedcolorright/2}
- end
- if(ev[2] == 1 and selectedcolorleft == 0)then
- canvas[yy][xx] = {char="\127",color=tonumber(colors.lightGray)}
- elseif(ev[2] == 1 and selectedcolorleft == 65536)then
- canvas[yy][xx] = {char=selectedcharleft,color=tonumber(colors.black)}
- end
- if(ev[2] == 2 and selectedcolorright == 0)then
- canvas[yy][xx] = {char="\127",color=tonumber(colors.lightGray)}
- elseif(ev[2] == 2 and selectedcolorright == 65536)then
- canvas[yy][xx] = {char=selectedcharright,color=tonumber(colors.black)}
- end
- redrawChar(xx,yy)
- end
- end
- end
- local function loop()
- while running do
- sbc(colors.black)
- ev = {os.pullEvent()}
- if inMenu then
- updateMenu(ev)
- else
- updateSelector(ev)
- asciiDraw(ev)
- end
- if(ev[1] == "key" and ev[2] == keys['leftCtrl'])then
- if(inMenu)then
- inMenu = false
- paintutils.drawLine(1, h, w, h, colors.black)
- stc(colors.yellow)
- writepos("Press ctrl to access menu. ",1,h)
- else
- inMenu = true
- paintutils.drawLine(1, h, w, h, colors.black)
- drawMenu()
- end
- end
- end
- end
- local function init()
- if(#targ <= 0)then
- error("Usage: Ascii <file>")
- else
- file = targ[1]
- end
- if(not fs.exists(file))then
- -- Create canvas
- for ch = 0, h-1 do
- canvas[ch] = {}
- for cw = 0, w-1 do
- canvas[ch][cw] = {char="\127", color=colors.lightGray}
- end
- end
- else
- f = fs.open(file,"r")
- local canvas_temp = textutils.unserialize(f.readAll())
- f.close()
- if(type(canvas_temp) == "table")then
- canvas = canvas_temp
- else
- error("Invalid file type!")
- end
- end
- drawGUI();
- loop()
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement