Advertisement
Redxone

ComputerCraft - Ascii Paint

Feb 13th, 2016
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.32 KB | None | 0 0
  1. -- ]] Ascii Editor by Redxone [[ --
  2.  
  3. -- ]] Simple API
  4. local stc = term.setTextColor
  5. local sbc = term.setBackgroundColor
  6. local scp = term.setCursorPos
  7. local cls = term.clear
  8. local w, h = term.getSize()
  9. local function printpos(text,x,y)
  10.     scp(x,y)
  11.     print(text)
  12.     scp(x,y+1)
  13. end
  14. local function writepos(text,x,y)
  15.     local xx, yy = term.getCursorPos()
  16.     scp(x,y)
  17.     write(text)
  18.     scp(xx,yy)
  19. end
  20.  
  21. -- Program start
  22. local char = "/"
  23. local takinginput = false
  24. local running = true
  25. local inMenu = false
  26. local canvas = {}
  27. local selectedcolorleft = 0
  28. local selectedcolorright = 4
  29. local selectedcharleft = "/"
  30. local selectedcharright = "-"
  31. local selectedoption = 0
  32. local file = "/"
  33. local inputspot = 0
  34. local targ = { ... }
  35.  
  36. function checkSelected(left,right,equal)
  37.     if tonumber(left) == equal
  38.     and tonumber(right) ~= equal then
  39.         return 1
  40.     elseif tonumber(right) == equal
  41.         and tonumber(left) ~= equal then
  42.         return 2
  43.     elseif tonumber(right) == equal and
  44.         tonumber(left) == equal then
  45.         return 3
  46.     else
  47.         return 0
  48.     end
  49. end
  50.  
  51. function drawSelected(left,right,equal,x,y,ltext,rtext,btext,ntext)
  52.     if tonumber(left) == equal
  53.     and tonumber(right) ~= equal then
  54.         printpos(ltext, x, y)
  55.     elseif tonumber(right) == equal
  56.         and tonumber(left) ~= equal then
  57.         printpos(rtext, x, y)
  58.     elseif tonumber(right) == equal and
  59.         tonumber(left) == equal then
  60.         printpos(btext, x, y)
  61.     else
  62.         printpos(ntext, x, y)
  63.     end
  64. end
  65.  
  66. -- ]] GUI Visuals
  67. local function drawGUI()
  68.  
  69.     sbc(colors.black)
  70.     cls()
  71.     scp(1,1)
  72.  
  73.     for ch = 0, h-1 do
  74.         for cw = 0, w-1 do
  75.             stc(tonumber(canvas[ch][cw].color))
  76.             writepos(canvas[ch][cw].char,cw,ch)
  77.         end
  78.     end
  79.  
  80.     -- Draw colors
  81.         for i = 0, 15 do
  82.             stc(colors.black)
  83.             drawSelected(selectedcolorleft,selectedcolorright,math.pow(2,i),w-1, i,"L/","R/","B/","-/")
  84.             sbc( tonumber( math.pow(2,i) ) )
  85.         end
  86.  
  87.         sbc(colors.black)
  88.         stc(colors.lightGray)
  89.         drawSelected(selectedcolorleft,selectedcolorright,0,w-1, h-3,"|\127","\127|","||","\127\127")
  90.         drawSelected(selectedcolorleft,selectedcolorright,65536,w-1, h-2,"L/","R/","B/","-/")
  91.         sbc(colors.blue)
  92.         stc(colors.white)
  93.         printpos(selectedcharleft,w-1,h-1)
  94.         sbc(colors.lightBlue)
  95.         printpos(selectedcharright,w,h-1)
  96.  
  97.         paintutils.drawLine(1, h, w, h, colors.black)
  98.  
  99.         stc(colors.yellow)
  100.         writepos("Press ctrl to open menu. ",1,h)
  101.  
  102. end
  103.  
  104. -- ]] Logic functions
  105.  
  106. local function updateGUI()
  107.         for i = 0, 15 do
  108.             stc(colors.black)
  109.             drawSelected(selectedcolorleft,selectedcolorright,math.pow(2,i),w-1, i,"L/","R/","B/","-/")
  110.             sbc( tonumber( math.pow(2,i) ) )
  111.         end
  112.  
  113.         sbc(colors.black)
  114.         stc(colors.lightGray)
  115.         drawSelected(selectedcolorleft,selectedcolorright,0,w-1, h-3,"|\127","\127|","||","\127\127")
  116.         drawSelected(selectedcolorleft,selectedcolorright,65536,w-1, h-2,"L/","R/","B/","-/")
  117.         sbc(colors.blue)
  118.         stc(colors.white)
  119.         printpos(selectedcharleft,w-1,h-1)
  120.         sbc(colors.lightBlue)
  121.         printpos(selectedcharright,w,h-1)
  122. end
  123.  
  124. local function redrawChar(x,y)
  125.     for yy = 0, h-1 do
  126.         for xx = 0, w-1 do
  127.             if(yy == y and xx == x)then
  128.                 scp(x,y)
  129.                 stc(canvas[yy][xx].color)
  130.                 write(canvas[yy][xx].char)
  131.             end
  132.         end
  133.     end
  134. end
  135.  
  136. local menu = {
  137.     ['Save'] = {func=function()
  138.         paintutils.drawLine(1, h, w, h, colors.black)
  139.         scp(1,h)
  140.         local f = fs.open(file,"w")
  141.         f.write(textutils.serialize(canvas):gsub("\n%s*",""))
  142.         f.close()
  143.         write("Ascii saved to: " .. file)
  144.         os.pullEvent("key")
  145.         inMenu = false
  146.         drawGUI()
  147.     end},
  148.     ['Load'] = {func=function()
  149.         paintutils.drawLine(1, h, w, h, colors.black)
  150.         scp(1,h)
  151.         write("Load ascii from?: ")
  152.         file_temp = read()
  153.         if(fs.exists(file_temp))then
  154.             local f = fs.open(file_temp,"r")
  155.             local canvas_temp = textutils.unserialize(f.readAll())
  156.             f.close()
  157.             if(type(canvas_temp) == "table")then
  158.                 file = file_temp
  159.                 canvas = canvas_temp
  160.                 drawGUI()
  161.                 updateGUI()
  162.                 sbc(colors.black)
  163.                 stc(colors.white)
  164.                 scp(1,h)
  165.                 write("Ascii loaded from: " .. file)
  166.                 os.pullEvent("key")
  167.                 inMenu = false
  168.                 drawGUI()
  169.             else
  170.                 scp(1,h-1)
  171.                 write("Invalid file type!")
  172.                 os.pullEvent("key")
  173.                 inMenu = false
  174.                 drawGUI()
  175.             end
  176.         else
  177.             scp(1,h-1)
  178.             write("No such file: " .. file_temp)
  179.             os.pullEvent("key")
  180.             inMenu = false
  181.             drawGUI()
  182.         end
  183.     end},
  184.     ['Exit'] = {func=function()
  185.         running = false
  186.         sbc(colors.black)
  187.         stc(colors.white)
  188.         cls()
  189.         scp(1,1)
  190.         print("Thank you for using Ascii Editor!, have a nice day.")
  191.     end},
  192.     ['Help'] = {func=function()
  193.         sbc(colors.blue)
  194.         cls()
  195.         stc(colors.white)
  196.         printpos("How to use the Ascii editor.",2,1)
  197.         printpos("1. How to select colors",2,3)
  198.         printpos("Left click a color to assign it to",4,4)
  199.         printpos("the left click on you're mouse.",4,5)
  200.         printpos("Right click a color to assign it to",4,6)
  201.         printpos("the right click on your're mouse.",4,7)
  202.         printpos("2. Changing ascii characters.",2,9)
  203.         printpos("Left click the blue or light blue square,",4,10)
  204.         printpos("hit enter if you dont want to change the char",4,11)
  205.         printpos("or press a key on you're keyboard to change.",4,12)
  206.         printpos("press enter when you have you're desired key.",4,13)
  207.         printpos("Press any key to return to Ascii.",5,15)
  208.         sbc(colors.black)
  209.         stc(colors.white)
  210.         os.pullEvent("key")
  211.         inMenu = false
  212.         drawGUI()
  213.     end},
  214. }
  215.  
  216. local function drawMenu()
  217.     local i = 0
  218.     for k,v in pairs(menu) do
  219.         if(selectedoption == i)then
  220.             scp(1 + (i * 6),h)
  221.             stc(colors.yellow)
  222.             write("[")
  223.             stc(colors.white)
  224.             write(k)
  225.             stc(colors.yellow)
  226.             write("]")
  227.         else
  228.             scp(1 + (i * 6),h)
  229.             stc(colors.white)
  230.             write(" " .. k .. " ")
  231.         end
  232.         i = i + 1
  233.     end
  234. end
  235.  
  236. local function updateMenu(event)
  237.     if(event[1] == "key")then
  238.         if(event[2] == keys['left'] and selectedoption > 0)then
  239.             selectedoption = selectedoption - 1
  240.             drawMenu()
  241.         end
  242.  
  243.         if(event[2] == keys['right'] and selectedoption < 3 )then
  244.             selectedoption = selectedoption + 1
  245.             drawMenu()
  246.         end
  247.  
  248.         if(event[2] == keys['enter'])then
  249.             local i = 0
  250.                 for k,v in pairs(menu) do
  251.                     if(selectedoption == i)then
  252.                         v.func()
  253.                     end
  254.                     i = i + 1
  255.                 end
  256.         end
  257.     end
  258. end
  259.  
  260. local function updateSelector(ev)
  261.     if(not takinginput)then term.setCursorBlink(false) end
  262.  
  263.     if(ev[1] == "mouse_click")then
  264.         if(takinginput)then takinginput = false end
  265.  
  266.         for i = 0, 15 do
  267.             if(ev[3] >= w-1 and ev[3] <= w and ev[4] == i)then
  268.                 if(ev[2] == 1)then -- left click
  269.                     selectedcolorleft = math.pow(2,i)
  270.                 end
  271.                 if(ev[2] == 2)then -- right click
  272.                     selectedcolorright = math.pow(2,i)
  273.                 end
  274.                 updateGUI()
  275.             end
  276.         end
  277.  
  278.         -- black and transparent selection
  279.         if(ev[3] >= w-1 and ev[3] <= w and ev[4] == 16)then
  280.             if(ev[2] == 1)then -- left click
  281.                 selectedcolorleft = 0
  282.             end
  283.             if(ev[2] == 2)then -- right click
  284.                 selectedcolorright = 0
  285.             end
  286.             updateGUI()
  287.         end
  288.  
  289.         if(ev[3] >= w-1 and ev[3] <= w and ev[4] == 17)then
  290.             if(ev[2] == 1)then -- left click
  291.                 selectedcolorleft = 65536
  292.             end
  293.             if(ev[2] == 2)then -- right click
  294.                 selectedcolorright = 65536
  295.             end
  296.             updateGUI()
  297.         end
  298.  
  299.         if(ev[3] >= w-1 and ev[4] == h-1 and takinginput == false )then
  300.             takinginput = true
  301.  
  302.             sbc(colors.blue)
  303.             stc(colors.white)
  304.             printpos(" ",w-1,h-1)
  305.             sbc(colors.lightBlue)
  306.             printpos(" ",w,h-1)
  307.             scp(w-1,h-1)
  308.  
  309.             term.setCursorBlink(true)
  310.         end
  311.  
  312.     end
  313.    
  314.     if(ev[1] == 'key' and takinginput)then
  315.         if(ev[2] == keys.enter)then
  316.             sbc(colors.blue)
  317.             stc(colors.white)
  318.             if(inputspot == 1)then
  319.                 takinginput = false;
  320.                 inputspot = 0
  321.                 sbc(colors.black)
  322.                 sbc(colors.lightBlue)
  323.                 printpos(selectedcharright,w,h-1)
  324.                 term.setCursorBlink(false)
  325.             elseif(inputspot == 0 )then
  326.                 inputspot = 1
  327.                 term.setCursorBlink(false)
  328.                 printpos(selectedcharleft,w-1,h-1)
  329.                 scp(w,h-1)
  330.                 term.setCursorBlink(true)
  331.             end
  332.         end
  333.     end
  334.  
  335.     if(ev[1] == 'char' and takinginput)then
  336.         sbc(colors.blue)
  337.         stc(colors.white)
  338.         if(inputspot == 0)then
  339.             selectedcharleft = ev[2]
  340.             printpos(selectedcharleft,w-1,h-1)
  341.             term.setCursorBlink(false)
  342.         elseif(inputspot == 1)then
  343.             selectedcharright = ev[2]
  344.             sbc(colors.lightBlue)
  345.             printpos(selectedcharright,w,h-1)
  346.             term.setCursorBlink(false)
  347.         end
  348.     end
  349. end
  350.  
  351. local function asciiDraw(ev)
  352.     if(ev[1] == "mouse_click" or ev[1] == "mouse_drag")then
  353.         if(ev[3] < w-1 and ev[4] < h-1)then
  354.             local xx = ev[3]
  355.             local yy = ev[4]
  356.  
  357.             -- Draw to screen based on left or right click
  358.             if(ev[2] == 1 and selectedcolorleft ~= 65536)then
  359.                 -- left click
  360.                     canvas[yy][xx] = {char=selectedcharleft,color=selectedcolorleft/2}
  361.             elseif(selectedcolorright ~= 0 and selectedcolorright ~= 65536)then
  362.                 -- right click
  363.                     canvas[yy][xx] = {char=selectedcharright,color=selectedcolorright/2}
  364.             end
  365.  
  366.             if(ev[2] == 1 and selectedcolorleft == 0)then
  367.                 canvas[yy][xx] = {char="\127",color=tonumber(colors.lightGray)}
  368.             elseif(ev[2] == 1 and selectedcolorleft == 65536)then
  369.                 canvas[yy][xx] = {char=selectedcharleft,color=tonumber(colors.black)}
  370.             end
  371.  
  372.             if(ev[2] == 2 and selectedcolorright == 0)then
  373.                 canvas[yy][xx] = {char="\127",color=tonumber(colors.lightGray)}
  374.             elseif(ev[2] == 2 and selectedcolorright == 65536)then
  375.                 canvas[yy][xx] = {char=selectedcharright,color=tonumber(colors.black)}
  376.             end
  377.  
  378.             redrawChar(xx,yy)
  379.         end
  380.     end
  381. end
  382.  
  383. local function loop()
  384.     while running do
  385.         sbc(colors.black)
  386.         ev = {os.pullEvent()}
  387.  
  388.         if inMenu then
  389.              updateMenu(ev)
  390.         else
  391.             updateSelector(ev)
  392.             asciiDraw(ev)
  393.         end
  394.  
  395.         if(ev[1] == "key" and ev[2] == keys['leftCtrl'])then
  396.             if(inMenu)then
  397.                 inMenu = false
  398.                 paintutils.drawLine(1, h, w, h, colors.black)
  399.                 stc(colors.yellow)
  400.                 writepos("Press ctrl to access menu. ",1,h)
  401.             else
  402.                 inMenu = true
  403.                 paintutils.drawLine(1, h, w, h, colors.black)
  404.                 drawMenu()
  405.             end
  406.         end
  407.     end
  408. end
  409.  
  410. local function init()
  411.  
  412.  
  413.  
  414.     if(#targ <= 0)then
  415.         error("Usage: Ascii <file>")
  416.     else
  417.         file = targ[1]
  418.     end
  419.  
  420.     if(not fs.exists(file))then
  421.     -- Create canvas
  422.         for ch = 0, h-1 do
  423.             canvas[ch] = {}
  424.             for cw = 0, w-1 do
  425.                 canvas[ch][cw] = {char="\127", color=colors.lightGray}
  426.             end
  427.         end
  428.     else
  429.         f = fs.open(file,"r")
  430.         local canvas_temp = textutils.unserialize(f.readAll())
  431.         f.close()
  432.         if(type(canvas_temp) == "table")then
  433.             canvas = canvas_temp
  434.         else
  435.             error("Invalid file type!")
  436.         end
  437.     end
  438.  
  439.     drawGUI();
  440.     loop()
  441. end
  442.  
  443. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement