Advertisement
TangentDelta

TangentPaint 0.5

Oct 12th, 2012
1,758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | None | 0 0
  1. --TangentPaint V.0.5
  2. --Part one of the final product!
  3.  
  4. --VARIABLE SETUP--
  5. width,height=term.getSize()
  6. print(height) print(width)
  7.  
  8. colorNames=
  9. {
  10. "white",
  11. "orange",
  12. "magenta",
  13. "lightblue",
  14. "yellow",
  15. "lime",
  16. "pink",
  17. "gray",
  18. "lightgray",
  19. "cyan",
  20. "purple",
  21. "blue",
  22. "brown",
  23. "green",
  24. "red",
  25. "black"
  26. }
  27.  
  28. decHexTable={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}
  29.  
  30. canvas={}
  31. for i=1,height-1 do
  32.   canvas[i]={}
  33.   for j=1,width do
  34.     canvas[i][j]=0
  35.   end
  36. end
  37.  
  38. overlay={}
  39. for i=1,height do
  40.   overlay[i]={}
  41.   for j=1,width do
  42.     overlay[i][j]=0
  43.   end
  44. end
  45.  
  46. lastPosition={0,0}
  47. colorSelected=1
  48. alive=true
  49. --FUNCTIONS--
  50. function drawTools()
  51.   term.setBackgroundColor(colors.gray)
  52.   term.setCursorPos(1,height)
  53.   overlay={1,1,1,0,2,2,2,0,3,3,3,0,4,4,4}
  54.   io.write("[L] [S] [M] [X]")
  55.   term.setBackgroundColor(2^colorSelected-1)
  56.   io.write(colorNames[colorSelected])
  57. end
  58.  
  59. function drawCanvas()
  60.   for i=1,height-1 do
  61.     for j=1,width do
  62.       term.setBackgroundColor(2^canvas[i][j])
  63.       io.write(" ")
  64.       term.setBackgroundColor(2^0)
  65.     end
  66.     io.write("\n")
  67.   end
  68. end
  69.  
  70. function paintLoad()
  71.     loadData={}
  72.     term.setBackgroundColor(colors.gray)
  73.     term.setCursorPos(1,height)
  74.     for i=1,width do io.write(" ") end
  75.     term.setCursorPos(1,height)
  76.     loadName=io.read()
  77.  
  78.     if fs.exists(loadName) then
  79.         file = io.open(loadName, "r")
  80.         line = file:read()
  81.         io.write(line)
  82.         file:close()
  83.     end
  84.  
  85.     for i=1,string.len(line) do
  86.         loadData[i]=string.sub(line,i,i)
  87.     end
  88.     for i=1,#loadData do
  89.         io.write(loadData[i])
  90.     end
  91.  
  92.     k=1
  93.     for i=1,height-1 do
  94.         for j=1,width do
  95.             for decode=1,16 do if loadData[k]==decHexTable[decode] then canvas[i][j]=decode-1 end end
  96.             k=k+1
  97.         end
  98.     end
  99.  
  100.     for i=1,#loadData do
  101.         print(loadData[i])
  102.     end
  103.  
  104.  
  105.     for i=1,height-1 do
  106.         for j=1,width do
  107.             io.write(canvas[i][j])
  108.         end
  109.         io.write("\n")
  110.     end
  111.  
  112.  
  113.  
  114.     drawCanvas()
  115. end
  116.  
  117. function paintSave()
  118.     term.setBackgroundColor(colors.gray)
  119.     term.setCursorPos(1,height)
  120.     for i=1,width do io.write(" ") end
  121.     term.setCursorPos(1,height)
  122.     saveName=io.read()
  123.  
  124.     local saveDir = saveName:sub(1, saveName:len() - fs.getName(saveName):len() )
  125.     if not fs.exists(saveDir) then
  126.         fs.makeDir( saveName )
  127.     end
  128.  
  129.     file = fs.open(saveName, "w")
  130.     --file.write(width..","..height.."|")
  131.     for i=1,height-1 do
  132.         for j=1,width do
  133.             print(i..","..j..":"..canvas[i][j])
  134.             index=canvas[i][j]+1
  135.             file.write(decHexTable[index])
  136.         end
  137.     end
  138.     file.close()
  139. end
  140.  
  141. function paintMenu()
  142. end
  143.  
  144. function paintExit()
  145.   alive=false
  146.   term.setBackgroundColor(colors.black)
  147.   shell.run("clear")
  148.   error()
  149. end
  150.  
  151. function screenInteract()
  152.   if mouseY==height then checkToolBar() return end
  153.  
  154.   if mouseButton==2 then
  155.     canvas[mouseY][mouseX]=0
  156.   end
  157.  
  158.   if mouseButton==1 then
  159.     canvas[mouseY][mouseX]=colorSelected-1
  160.   end
  161.  
  162.   if mouseButton==5 then
  163.     colorSelected=colorSelected+1
  164.     if colorSelected==17 then colorSelected=1 end
  165.   end
  166.  
  167.   if mouseButton==4 then
  168.     colorSelected=colorSelected-1
  169.     if colorSelected==0 then colorSelected=16 end
  170.   end
  171. end
  172.  
  173. function checkToolBar()
  174.     toolbarSelection=overlay[mouseX]
  175.     if toolbarSelection==1 then paintLoad() end
  176.     if toolbarSelection==2 then paintSave() end
  177.     if toolbarSelection==3 then paintMenu() end
  178.     if toolbarSelection==4 then paintExit() end
  179. end
  180.  
  181. --MAIN PROGRAM--
  182. while alive do
  183. drawCanvas()
  184. drawTools()
  185.  
  186. event,mouseX,mouseY,mouseButton=os.pullEvent("click")
  187. screenInteract()
  188. drawCanvas()
  189. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement