Advertisement
incinirate

Paint Program In Computercraft

Jun 5th, 2014
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.94 KB | None | 0 0
  1. bg=2
  2. --Change this to change the background.
  3. --The current first line MUST remain the first or this will break itself
  4.  
  5.  
  6. --[[
  7.  
  8. Welcome to the GameEngine's Personal Painting Program
  9.  
  10. This was invented and developed by
  11. incinirate, http://www.youtube.com/user/guildwarsmem
  12.  
  13. You may modify this, however any
  14. distribution of this must include
  15. credit to me
  16.  
  17. You may use this in your game development,
  18. but you must give credit to me
  19. ]]--
  20.  
  21. col={
  22.   [1]='0',
  23.   [2]='1',
  24.   [4]='2',
  25.   [8]='3',
  26.   [16]='4',
  27.   [32]='5',
  28.   [64]='6',
  29.   [128]='7',
  30.   [256]='8',
  31.   [512]='9',
  32.   [1024]='a',
  33.   [2048]='b',
  34.   [4096]='c',
  35.   [8192]='d',
  36.   [16384]='e',
  37.   [32768]='f',
  38.   [0]='g',      --This one is for transparent blocks
  39. }
  40.  
  41. colt = {
  42.     ['0']=1,
  43.     ['1']=2,
  44.     ['2']=4,
  45.     ['3']=8,
  46.     ['4']=16,
  47.     ['5']=32,
  48.     ['6']=64,
  49.     ['7']=128,
  50.     ['8']=256,
  51.     ['9']=512,
  52.     ['a']=1024,
  53.     ['b']=2048,
  54.     ['c']=4096,
  55.     ['d']=8192,
  56.     ['e']=16384,
  57.     ['f']=32768,
  58.     ['g']=0,      --This one is for transparent blocks
  59.   }
  60.  
  61. transp=false
  62.  
  63. termx,termy = term.getSize()
  64.  
  65. tabs={
  66.   [1]={
  67.     ["x1"]=1,
  68.     ["x2"]=6,
  69.     ["New"]=function()
  70.       if not saved then
  71.         if question("Do you want to save this?") then
  72.           local file = fs.open(docname,"w")
  73.           file.write(serial())
  74.           file.close()
  75.         end
  76.       end
  77.       construct()
  78.       docname = getString("File Name:")
  79.       docx = getString("Width:")
  80.       docy = getString("Height:")
  81.       for i=1,docx do
  82.       doc[i]={}
  83.         for j=1,docy do
  84.           doc[i][j]=0
  85.         end
  86.       end
  87.       top=false
  88.       paint()
  89.     end,
  90.     ["Save"]=function()
  91.       if docname==nil then
  92.         docname=getString("Enter a name for this:")
  93.       end
  94.       local file = fs.open(docname,"w")
  95.       file.write(serial())
  96.       file.close()
  97.       top=false
  98.       paint()
  99.       info("Saved!")
  100.       saved=true
  101.       if Timer==nil then
  102.         Timer1=nil
  103.         Timer = os.startTimer(0.2)
  104.       else
  105.         Timer=nil
  106.         Timer1 = os.startTimer(0.2)
  107.       end
  108.     end,
  109.     ["SaveAs"]=function()
  110.       docname=getString("Enter a name for this:")
  111.       local file = fs.open(docname,"w")
  112.       file.write(serial())
  113.       file.close()
  114.       top=false
  115.       paint()
  116.       info("Saved!")
  117.       saved=true
  118.       if Timer==nil then
  119.         Timer1=nil
  120.         Timer = os.startTimer(0.2)
  121.       else
  122.         Timer=nil
  123.         Timer1 = os.startTimer(0.2)
  124.       end
  125.     end,
  126.     ["Close"]=function()
  127.       if not saved then
  128.         if question("Do you want to save this?") then
  129.           local file = fs.open(docname,"w")
  130.           file.write(serial())
  131.           file.close()
  132.         end
  133.       end
  134.       construct()
  135.       repeat
  136.         menu()
  137.       until not ta
  138.       ta=nil
  139.       paint()
  140.       if Timer==nil then
  141.         Timer1=nil
  142.         Timer = os.startTimer(0.2)
  143.       else
  144.         Timer=nil
  145.         Timer1 = os.startTimer(0.2)
  146.       end
  147.     end,
  148.     ["Exit"]=function()
  149.       if not saved then
  150.         if question("Do you want to save this?") then
  151.           local file = fs.open(docname,"w")
  152.           file.write(serial())
  153.           file.close()
  154.         end
  155.       end
  156.       os.reboot()
  157.     end,
  158.     ["Chng B"]=function()
  159.       info("Pick the background on the right")
  160.       while true do
  161.         e,k,x,y=os.pullEvent("mouse_click")
  162.         if x == termx or x == termx-1 then
  163.           if y~= 19 and y~= 1 and y~= 18 then
  164.             bg=2^(y-2)
  165.             local fil=fs.open(shell.dir().."/"..shell.getRunningProgram(),"r")
  166.             local prgm = {}
  167.             while true do
  168.               local ln=fil.readLine()
  169.               if ln==nil then break end
  170.               table.insert(prgm,ln)
  171.             end
  172.             fil.close()
  173.             local fil=fs.open(shell.dir().."/"..shell.getRunningProgram(),"w")
  174.             fil.writeLine("bg="..bg)
  175.             for i=2,#prgm do
  176.               fil.writeLine(prgm[i])
  177.             end
  178.             fil.close()
  179.             break
  180.           elseif y==18 then
  181.             info("Cannot be transparent")
  182.           end
  183.         end
  184.       end
  185.       if Timer==nil then
  186.         Timer1=nil
  187.         Timer = os.startTimer(0.2)
  188.       else
  189.         Timer=nil
  190.         Timer1 = os.startTimer(0.2)
  191.       end
  192.     end,
  193.   },
  194.   [2]={
  195.     ["x1"]=8,
  196.     ["x2"]=15,
  197.     ["Back"]=function()
  198.       top=false
  199.       paint()
  200.     end
  201.   }
  202. }
  203.  
  204. selcol=1
  205.  
  206. --Draw Funcs
  207.  
  208. function drawBox(x,y,width,height,col)
  209.   col=col or colors.black --Makes the "col" argument optional
  210.   term.setCursorPos(x,y)
  211.   term.setBackgroundColor(col)
  212.   write(string.rep(" ",width))
  213.   for i=1,height-2 do
  214.     term.setCursorPos(x,y+i)
  215.     write(" ")
  216.     term.setCursorPos(x+width-1,y+i)
  217.     write(" ")
  218.   end
  219.   term.setCursorPos(x,y+height-1)
  220.   write(string.rep(" ",width))
  221.   col=nil
  222. end
  223.  
  224. function fillBox(x,y,width,height,col)
  225.   col=col or colors.lightGray --Makes the "col" argument optional
  226.   term.setBackgroundColor(col)
  227.   for i=1,height do
  228.     term.setCursorPos(x,y+i-1)
  229.     write(string.rep(" ",width))
  230.   end
  231. end
  232. --End Draw Funcs
  233.  
  234. --Dialogs
  235.  
  236. function question(str)
  237.   if #str < 7 then
  238.     drawBox(math.floor(termx/2)-3,math.floor(termy/2)-3,7,7)
  239.     fillBox(math.floor(termx/2)-2,math.floor(termy/2)-2,5,5)  
  240.   else
  241.     drawBox(math.floor(termx/2-(#str/2)-2),math.floor(termy/2-3),#str+4,7)
  242.     fillBox(math.floor(termx/2-(#str/2)-1),math.floor(termy/2-2),#str+2,5)
  243.   end
  244.   term.setCursorPos(math.floor(termx/2-(#str/2)),math.floor(termy/2)-1)
  245.   write(str)
  246.   term.setCursorPos(math.floor(termx/2-(#str/2)),math.floor(termy/2)+1)
  247.   term.setBackgroundColor(colors.lime)
  248.   write("Yes")
  249.   term.setCursorPos(math.floor(termx/2+(#str/2))-3,math.floor(termy/2)+1)
  250.   write(" No")
  251.   while true do
  252.     e,p,x,y = os.pullEvent("mouse_click")
  253.     if y==math.floor(termy/2)+1 then
  254.       if x>=math.floor(termx/2+(#str/2))-3 and x<=math.floor(termx/2+(#str/2))-1 then
  255.         return false
  256.       elseif x>=math.floor(termx/2-(#str/2)) and x<=math.floor(termx/2-(#str/2))+2 then
  257.         return true
  258.       end
  259.     end
  260.   end
  261. end
  262.  
  263. function getString(str)
  264.   paint()
  265.   drawBox(math.floor(termx/2-14),math.floor(termy/2)-3,28,7)
  266.   fillBox(math.floor(termx/2-13),math.floor(termy/2)-2,26,5)
  267.   term.setCursorPos(math.floor(termx/2-(#str/2)),math.floor(termy/2)-1)
  268.   write(str)
  269.   term.setBackgroundColor(128)
  270.   term.setCursorPos(math.floor(termx/2)-12,math.floor(termy/2)+1)
  271.   write("                        ")
  272.   term.setCursorPos(math.floor(termx/2)-12,math.floor(termy/2)+1)
  273.   local stpos = math.floor(termx/2)-12
  274.   local substr = ""
  275.   local tbpos = 0
  276.   local tbs = 0
  277.   local spos = 0 --Limit: 24
  278.   term.setCursorBlink(true)
  279.   while true do
  280.     e,k = os.pullEvent()
  281.     if e=="key" then
  282.       if k==205 then --Right
  283.         if #substr+1>spos then
  284.           spos=spos+1
  285.           if tbs+23<spos then tbs=tbs+1 else
  286.             tbpos=tbpos+1
  287.           end
  288.         end
  289.       elseif k==203 then --Left
  290.         if 0<spos then
  291.           spos=spos-1
  292.           if tbs>spos then tbs=tbs-1 else
  293.             tbpos=tbpos-1
  294.           end
  295.         end
  296.       elseif k==28 then --Enter
  297.         break
  298.       elseif k==14 then --Backspace
  299.         substr=substr:sub(1,spos-1)..substr:sub(spos+1,#substr)
  300.         if 0<spos then
  301.           spos=spos-1
  302.           if tbs>spos then tbs=tbs-1 else
  303.             tbpos=tbpos-1
  304.           end
  305.         end
  306.       end
  307.     elseif e=="char" then
  308.       substr=substr:sub(1,spos)..k..substr:sub(spos+1,#substr)
  309.       spos=spos+1
  310.       if tbs+24 <= spos then
  311.         tbs=tbs+1
  312.         --if tbpos==23 then
  313.         --  tbs=tbs+1
  314.         --end
  315.       else
  316.         tbpos=tbpos+1
  317.       end
  318.      
  319.       if tbpos>23 then
  320.         tbpos=23
  321.       end
  322.     end
  323.     term.setCursorPos(stpos,math.floor(termy/2)+1)
  324.     write("                        ")
  325.     term.setCursorPos(stpos,math.floor(termy/2)+1)
  326.     write(substr:sub(tbs,tbs+24))  
  327.     term.setCursorPos(stpos+24,math.floor(termy/2)+1)
  328.     term.setBackgroundColor(colors.lightGray)
  329.     write(" ")
  330.     term.setBackgroundColor(colors.gray)
  331.     term.setCursorPos(stpos+tbpos,math.floor(termy/2)+1)
  332.   end
  333.   term.setCursorBlink(false)
  334.   return substr
  335. end
  336.  
  337. function info(str)
  338.   if #str < 2 then
  339.     drawBox(math.floor(termx/2)-3,math.floor(termy/2)-3,7,7)
  340.     fillBox(math.floor(termx/2)-2,math.floor(termy/2)-2,5,5)
  341.   else
  342.     drawBox(math.floor(termx/2-(#str/2)-2),math.floor(termy/2-3),#str+4,7)
  343.     fillBox(math.floor(termx/2-(#str/2)-1),math.floor(termy/2-2),#str+2,5)
  344.   end
  345.   term.setCursorPos(math.floor(termx/2-(#str/2)),math.floor(termy/2)-1)
  346.   write(str)
  347.   term.setCursorPos(math.floor(termx/2-1),math.floor(termy/2)+1)
  348.   term.setBackgroundColor(colors.lime)
  349.   write("Ok")
  350.   while true do
  351.     local e,p,x,y = os.pullEvent("mouse_click")
  352.     if x==math.floor(termx/2-1) or x==math.floor(termx/2) then
  353.       if y==math.floor(termy/2)+1 then
  354.         break
  355.       end
  356.     end
  357.   end
  358.   paint()
  359. end
  360.  
  361. --End Dialogs
  362.  
  363. function getNumInd(tn)
  364.   local itn=0
  365.   for i,v in pairs(tabs[tsel]) do
  366.     itn=itn+1
  367.   end
  368.   return itn
  369. end
  370.  
  371. function uz(num)
  372.   if num==0 then
  373.     return bg
  374.   end
  375.   return num
  376. end
  377.  
  378. function cht(vart)
  379.   if transp then
  380.     return vart+1
  381.   end
  382.   return vart
  383. end
  384.  
  385. function paint()
  386.   term.setBackgroundColor(bg)
  387.   shell.run("clear")
  388.  
  389.   --Draw Current Picture
  390.   spot=0
  391.   for i=1,docx do
  392.     for j=1,docy do
  393.       spot=spot+1
  394.       term.setCursorPos(i+scrollx,j+scrolly+1)
  395.       if doc[i][j]==0 then
  396.         if cht(i)%2 == 0 then
  397.           term.setBackgroundColor(uz(256*(j%2)))
  398.         else
  399.           term.setBackgroundColor(uz(256*((j+1)%2)))
  400.         end
  401.         write(" ")
  402.       else
  403.         term.setBackgroundColor(doc[i][j])
  404.         write(" ")
  405.       end
  406.     end
  407.   end
  408.   term.setCursorPos(1,1)
  409.   term.setBackgroundColor(colors.gray)
  410.   term.clearLine(1)
  411.   term.setCursorPos(1,1)
  412.   term.write(dtt("File",1)--[[.." "..dtt("Colors",2)]])
  413.  
  414.   if top then
  415.     local x1 = tabs[tsel]["x1"]
  416.     local x2 = tabs[tsel]["x2"]
  417.     local itn = 1
  418.     for i,v in pairs(tabs[tsel]) do
  419.       if i~="x1" and i~="x2" then
  420.         itn=itn+1
  421.         drawBox(x1,itn,x2-x1+1,1,colors.lightGray)
  422.         term.setCursorPos(x1,itn)
  423.         write(i)
  424.       end
  425.     end
  426.   end
  427.   term.setBackgroundColor(128)
  428.   for i=2,19 do
  429.     term.setCursorPos(termx-2,i)
  430.     write(" ")
  431.   end
  432.   for i=1,16 do
  433.     term.setCursorPos(termx-1,i+1)
  434.     term.setBackgroundColor(2^(i-1))
  435.     write("  ")
  436.   end
  437.   term.setCursorPos(termx-1,18)
  438.   write("XX")
  439.   term.setCursorPos(termx-1,19)
  440.   if selcol ~=0 then
  441.     term.setBackgroundColor(selcol)
  442.     write(" ")
  443.   else
  444.     term.setBackgroundColor(1)
  445.     term.setTextColor(2^15)
  446.     write("X")
  447.     term.setTextColor(1)
  448.   end
  449.   term.setBackgroundColor(2^15)
  450.   write("X")
  451.  
  452. end
  453.  
  454. function dtt(str,id)
  455.   if selsc(id) then
  456.     return "["..str.."]"
  457.   end
  458.   return "|"..str.."|"
  459. end
  460.  
  461. function selsc(n)
  462.   if n==tsel then
  463.     return true
  464.   end
  465.   return false
  466. end
  467.  
  468. function construct()
  469.   docx=0
  470.   docy=0
  471.   doc={}
  472.   cto=false
  473.   tsel=1
  474.   top=false
  475.   saved=false
  476.   docname=nil
  477.   selcol=1
  478.   scrollx=4
  479.   scrolly=4
  480. end
  481.  
  482.  
  483.  
  484. function unserial(handle)
  485.   local fil = fs.open(handle,"r")
  486.   local spr=fil.readAll()
  487.   fil.close()
  488.   i=1
  489.   repeat
  490.     i=i+1
  491.   until spr:sub(i,i) == "/"
  492.   x=spr:sub(1,i-1)
  493.   j=i
  494.   repeat
  495.     j=j+1
  496.   until spr:sub(j,j) == "/"
  497.   y=spr:sub(i+1,j-1)
  498.   local k=j
  499.   local kp=j+1
  500.   local newspr = {}
  501.   for a=1,x do
  502.     newspr[a]={}
  503.     for s=1,y do
  504.       repeat
  505.         k=k+1
  506.       until spr:sub(k,k) == "/"
  507.       newspr[a][s]=colt[spr:sub(kp,k-1)]
  508.       kp=k+1
  509.     end
  510.   end
  511.   return x,y,newspr
  512. end
  513.  
  514. function serial()
  515.   str=docx.."/"..docy.."/"
  516.   for i=1,#doc do
  517.     for j=1,#doc[i] do
  518.       str=str..col[doc[i][j]].."/"
  519.     end
  520.   end
  521.   return str
  522. end
  523.  
  524. ---------------------------------------------
  525. ---------------------------------------------
  526. ---------------------------------------------
  527. --Menu Below Here
  528.  
  529. function paintMenu(sel)
  530.   local a,b,c = colors.lime,colors.lime,colors.lime
  531.   if sel==0 then
  532.     a = colors.red
  533.   elseif sel==1 then
  534.     b = colors.red
  535.   else
  536.     c = colors.red
  537.   end
  538.   term.setBackgroundColor(colors.lightGray)
  539.   shell.run("clear")
  540.   fillBox(2,2,termx-2,math.floor(termy/3)-1,a)
  541.   fillBox(2,math.floor(termy/3)+2,termx-2,math.floor(termy/3)-1,b)
  542.   fillBox(2,math.floor((termy/3)*2)+2,termx-2,math.floor(termy/3)-1,c)
  543.   term.setBackgroundColor(a)
  544.   term.setCursorPos(termx/2-#"New File"/2,math.floor(termy/3)-2)
  545.   write("New File")
  546.  
  547.   term.setBackgroundColor(b)
  548.   term.setCursorPos(termx/2-#"Load File"/2,(math.floor(termy/3)+2)+( -(math.floor(termy/3)-1)+(math.floor(termy/3)+2) )/2+1)
  549.   write("Load File")
  550.  
  551.   term.setBackgroundColor(c)
  552.   term.setCursorPos(termx/2-#"Exit"/2,(math.floor((termy/3)*2)+2)+( -(math.floor(termy/3)-1)+(math.floor((termy/3)*2)+2) )/2-2)
  553.   write("Exit")
  554. end
  555.  
  556. function menu()
  557.   ta=false
  558.   local sel=0 --0=New, 1=Load, 2=Exit
  559.   while true do
  560.     paintMenu(sel)
  561.     e,k = os.pullEvent("key")
  562.     if k==200 then
  563.       if sel==0 then sel=2 else sel=sel-1 end
  564.     elseif k==208 then
  565.       if sel==2 then sel=0 else sel=sel+1 end
  566.     elseif k==28 then
  567.       break
  568.     end
  569.   end
  570.   if sel==0 then
  571.     docname=getString("Enter Document Name:")
  572.     docx=getString("Enter Width")
  573.     docy=getString("Enter Height")
  574.     for i=1,docx do
  575.       doc[i]={}
  576.       for j=1,docy do
  577.         doc[i][j]=0
  578.       end
  579.     end
  580.     Timer1=os.startTimer(1)
  581.   elseif sel==1 then
  582.     docname=getString("Enter Document Name:")
  583.     if not fs.exists(docname) then
  584.       repeat
  585.         docname=getString("Invalid, Try Again:")
  586.         if not fs.exists(docname) then
  587.           if not question("Invalid again, try again?") then
  588.             ta=true
  589.           end
  590.         end
  591.       until fs.exists(docname) or ta
  592.     end
  593.     if not ta then
  594.       docx,docy,doc = unserial(docname)
  595.       saved=true
  596.     end
  597.   elseif sel==2 then
  598.     os.reboot()
  599.   end
  600. end
  601.  
  602. --Menu Stops Here
  603. -------------------------------------------
  604.  
  605. construct()
  606.  
  607. repeat
  608.   menu()
  609. until not ta
  610.  
  611. ta=nil
  612.  
  613. paint()
  614.  
  615. Timer=os.startTimer(0.2)
  616.  
  617. while true do
  618.   e,k,p,o=os.pullEvent()
  619.   if e=="mouse_click" or e=="mouse_drag" then
  620.     if o==1 then
  621.       if p<7 then
  622.         tsel=1
  623.         top=not top
  624.       --elseif p>7 and p<16 then
  625.       --  tsel=2
  626.       --  top=not top
  627.       else
  628.         top=false
  629.       end
  630.     else
  631.       if top and p >= tabs[tsel]["x1"] and p <= tabs[tsel]["x2"] then
  632.         if o < getNumInd() then
  633.           itn=1
  634.           for i,v in pairs(tabs[tsel]) do
  635.             if i~="x1" and i~="x2" then
  636.               itn=itn+1
  637.               if o==itn then
  638.                 v()
  639.               end
  640.             end
  641.           end
  642.         end
  643.       elseif p < termx-2 then --Painting
  644.         if o>scrolly+1 and o<scrolly+docy+2 then
  645.           if p>scrollx and p<scrollx+docx+1 then
  646.             --X and Y in
  647.             doc[p-scrollx][o-scrolly-1] = selcol
  648.             saved=false
  649.           end
  650.         end
  651.       elseif p == termx or p == termx-1 then --Selecting Color
  652.         if o~= 19 and o~= 1 then
  653.           if o == 18 then
  654.             selcol=0
  655.           else
  656.             selcol=2^(o-2)
  657.           end
  658.         end
  659.       end
  660.      
  661.       --all stuff b4 here
  662.       top=false
  663.     end
  664.   elseif e=="key" then
  665.     if k==200 then --UP
  666.       scrolly=scrolly-1
  667.     elseif k==208 then --DOWN
  668.       scrolly=scrolly+1
  669.     elseif k==203 then --LEFT
  670.       scrollx=scrollx-1
  671.     elseif k==205 then --RIGHT
  672.       scrollx=scrollx+1
  673.     end
  674.   elseif e=="timer" then
  675.     if k==Timer then
  676.       transp=not transp
  677.       Timer = nil
  678.       Timer1 = os.startTimer(0.2)
  679.     elseif k==Timer1 then
  680.       transp=not transp
  681.       Timer1 = nil
  682.       Timer = os.startTimer(0.2)
  683.     end
  684.   end
  685.  
  686.   --All Logic Above Here
  687.   paint()
  688. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement