Advertisement
Dojnaz

Sketch It ComputerCraft

Dec 26th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.56 KB | None | 0 0
  1. --this is the sketch-it program
  2. --copyright of BlueTide Programs.inc, Benedict Allen, all members of BlueTide Programs.inc and all members of programcraft.inc
  3. --if you wish to use this program please contact Benedict Allen(awsumben13 on cc forums) and make sure you give the credit to BlueTide Programs
  4. --usability functions
  5. local function clear(x,y)
  6. if x == nil or y == nil then
  7.   term.clear()
  8.   term.setCursorPos(1,1)
  9. else
  10.   term.clear()
  11.   term.setCursorPos(x,y)
  12. end
  13. end
  14. function writePos(x,y,text)
  15. term.setCursorPos(x,y)
  16. term.write(text)
  17. end
  18. function clearPrint(x,y,text)
  19. term.clear()
  20. term.setCursorPos(x,y)
  21. term.write(text)
  22. end
  23. function reset(file, x, y)
  24. if fs.exists(file) then
  25.   h = fs.open(file, "w")
  26.   h.write("")
  27.   h.close()
  28.   term.setCursorPos(x,y)
  29.   term.write("File reset")
  30.   sleep(2)
  31. else
  32.   term.setCursorPos(x,y)
  33.   term.write("file not found")
  34.   sleep(2)
  35. end
  36. end
  37. function tip(help)
  38. while true do
  39.   clear()
  40.   print(help)
  41.   writePos(1,17,"press enter to exit")
  42.   event, key = os.pullEvent()
  43.   if key == 28 then break
  44.   end
  45. end
  46. end
  47. function drawBox(x,y,z,n)
  48. for k = 1,n-y do
  49.   for i = 1,z-x do
  50.    writePos(x+i,y+k," ")
  51.   end
  52. end
  53. writePos(x,y,"+")
  54. term.setCursorPos(x+1,y)
  55. for i = 1,(z-x)-1 do
  56.   write("-")
  57. end
  58. writePos(z,y,"+")
  59. term.setCursorPos(x+1,n)
  60. for i = 1,(z-x)-1 do
  61.   write("-")
  62. end
  63. for i = 1,(n-y)-1 do
  64.   writePos(x,y+i,"|")
  65. end
  66. for i = 1,(n-y)-1 do
  67.   writePos(z,y+i,"|")
  68. end
  69. writePos(x,n,"+")
  70. writePos(z,n,"+")
  71. end
  72. function saveBox(type)
  73. if type == nil then
  74.   add("+", cursorPosX, boxY)
  75.   for i = 1,((cursorPosX+1)-boxX)-2 do
  76.    add("-", boxX+i, boxY)
  77.   end
  78.   add("+", boxX, boxY)
  79.   add("+", cursorPosX, cursorPosY)
  80.   for i = 1,((cursorPosX+1)-boxX)-2 do
  81.    add("-", boxX+i, cursorPosY)
  82.   end
  83.   add("+",boxX, cursorPosY)
  84.   for i = 1,((cursorPosY+1)-boxY)-2 do
  85.    add("|", boxX, boxY+i)
  86.   end
  87.   for i = 1,((cursorPosY+1)-boxY)-2 do
  88.    add("|", cursorPosX, boxY+i)
  89.   end
  90. else
  91.   for i = boxX, cursorPosX do
  92.    for k = boxY, cursorPosY do
  93.         add(type, i, k)
  94.    end
  95.   end
  96. end
  97. end
  98. function split(str, pat)
  99.    local t = {}
  100.    local fpat = "(.-)" .. pat
  101.    local last_end = 1
  102.    local s, e, cap = str:find(fpat, 1)
  103.    while s do
  104.           if s ~= 1 or cap ~= "" then
  105.   table.insert(t,cap)
  106.           end
  107.           last_end = e+1
  108.           s, e, cap = str:find(fpat, last_end)
  109.    end
  110.    if last_end <= #str then
  111.           cap = str:sub(last_end)
  112.           table.insert(t, cap)
  113.    end
  114.    return t
  115. end
  116. function animation(limit, item, time, x, y)
  117. yer = 0
  118. if x == nil or y == nil then
  119.   x, y = term.getCursorPos()
  120. end
  121. while true do
  122.   if yer > limit then
  123.    yer = 0
  124.   end
  125.   writePos(x, y+yer, item)
  126.   sleep(time)
  127.   yer = yer+1
  128. end
  129. end
  130. --end
  131. --programs functions
  132. function askToSave()
  133. while true do
  134.   drawBox(14,8,44,11)
  135.   writePos(16,9,"Do you want to save first?")
  136.   writePos(17,10,"Yes   No   Cancel")
  137.   if newPos == 1 then
  138.    writePos(16,10,"[")
  139.    writePos(20,10,"]")
  140.   elseif newPos == 2 then
  141.    writePos(22,10,"[")
  142.    writePos(25,10,"]")
  143.   elseif newPos == 3 then
  144.    writePos(27,10,"[")
  145.    writePos(34,10,"]")
  146.   end
  147.   event, key = os.pullEvent()
  148.   if key == 203 and newPos > 1 then
  149.    newPos = newPos-1
  150.   elseif key == 205 and newPos < 3 then
  151.    newPos = newPos+1
  152.   elseif key == 28 then
  153.    if newPos == 1 then
  154.         save()
  155.         xToPrint = {}
  156.         yToPrint = {}
  157.         itemToPrint = {}
  158.         fileOpen = "New File"
  159.         break
  160.    elseif newPos == 2 then
  161.         xToPrint = {}
  162.         yToPrint = {}
  163.         itemToPrint = {}
  164.         fileOpen = "New File"
  165.         break
  166.    elseif newPos == 3 then
  167.         loopBreaker = false
  168.         break
  169.    end
  170.   end
  171. end
  172. end
  173. function open()
  174. if changed == true then
  175.   askToSave()
  176. end
  177. openSel = 1
  178. while true do
  179.   variable = 0
  180.   bottomLimit = topLimit+14
  181.   clear()
  182.   drawBox(1,1,50,18)
  183.   writePos(2,2,"Select the file you want to open:")
  184.   files = fs.list("sketch-it/saves")
  185.   table.insert(files, "Cancel")
  186.   if #files < 15 then
  187.    bottomLimit = #files
  188.   end
  189.   for i = topLimit,bottomLimit do
  190.    variable = variable+1
  191.    writePos(3,2+variable,files[i])
  192.   end
  193.   writePos(2,2+openSel,">")
  194.   event, key = os.pullEvent()
  195.   if key == 200 and openSel > 1 then
  196.    openSel = openSel-1
  197.   elseif key == 208 and openSel < 14 and openSel < #files then
  198.    openSel = openSel+1
  199.   elseif key == 200 and openSel == 1 and topLimit > 1 then
  200.    topLimit = topLimit-1
  201.   elseif key == 208 and openSel == 14 and (bottomLimit <= #files or bottomLimit == 15) then
  202.    topLimit = topLimit+1
  203.   elseif key == 14 then
  204.    break
  205.   elseif key == 28 then
  206.    selectedFile = files[openSel+(topLimit-1)]
  207.    if fs.exists("sketch-it/saves/"..selectedFile.."/xpos") and fs.exists("sketch-it/saves/"..selectedFile.."/ypos") and fs.exists("sketch-it/saves/"..selectedFile.."/item") and selectedFile ~= #files then
  208.         xToPrint = {}
  209.         yToPrint = {}
  210.         itemToPrint = {}
  211.         writePos(2,18,"opening "..selectedFile)
  212.         sleep(1.3)
  213.         file = fs.open("sketch-it/saves/"..selectedFile.."/xpos","r")
  214.         while true do
  215.          line = file.readLine()
  216.          table.insert(xToPrint, tonumber(line))
  217.          if not line then
  218.           break
  219.          end
  220.         end
  221.         file = fs.open("sketch-it/saves/"..selectedFile.."/ypos","r")
  222.         while true do
  223.          line = file.readLine()
  224.          table.insert(yToPrint, tonumber(line))
  225.          if not line then
  226.           break
  227.          end
  228.         end
  229.         file = fs.open("sketch-it/saves/"..selectedFile.."/item","r")
  230.         while true do
  231.          line = file.readLine()
  232.          table.insert(itemToPrint, line)
  233.          if not line then
  234.           break
  235.          end
  236.         end
  237.         fileOpen = selectedFile
  238.         currentField = 2
  239.         break
  240.    elseif openSel == #files then
  241.         break
  242.    else
  243.         drawBox(17,8,33,10)
  244.         writePos(20,9,"broken file")
  245.         sleep(0.7)
  246.    end
  247.   end
  248. end
  249. sketchitWatermark = false
  250. end
  251. function save()
  252. m = 1
  253. secs = (#itemToPrint*3)/20
  254. clear()
  255. drawBox(1,1,50,17)
  256. files = fs.list("sketch-it/saves")
  257. if #files > 14 then
  258.   for i = 1,14 do
  259.    writePos(3,1+i,files[i])
  260.   end
  261.   if #files > 28 then
  262.    for i = 15,28 do
  263.         writePos(25,-13+i,files[i])
  264.    end
  265.   else
  266.    for i = 15,#files do
  267.         writePos(25,-13+i,files[i])
  268.    end
  269.   end
  270. else
  271.   for i = 1,#files do
  272.    writePos(3,1+i,files[i])
  273.   end
  274. end
  275. writePos(1,18,"What do you want to save the file as? ")
  276. input = read()
  277. if not fs.exists(shell.resolve(input)) then
  278.   fs.makeDir("sketch-it/saves/"..input)
  279. end
  280. writePos(1,19,"Saving...this may take a while...")
  281. file = fs.open("sketch-it/saves/"..input.."/xpos","w")
  282. for i = 1,#xToPrint do
  283.   file.writeLine(xToPrint[i])
  284.   sleep(0.001)
  285.   m = m+1
  286.   secs = secs-0.05
  287.   writePos(1,18,"What do you want to save the file as? approx "..secs.." seconds left")
  288.   writePos(35, 19, m.." Out of "..(#itemToPrint*3))
  289. end
  290. file.close()
  291. file = fs.open("sketch-it/saves/"..input.."/ypos","w")
  292. for i = 1,#yToPrint do
  293.   file.writeLine(yToPrint[i])
  294.   sleep(0.001)
  295.   m = m+1
  296.   secs = secs-0.05
  297.   writePos(1,18,"What do you want to save the file as? approx "..secs.." seconds left")
  298.   writePos(35, 19, m.." Out of "..(#itemToPrint*3))
  299. end
  300. file.close()
  301. file = fs.open("sketch-it/saves/"..input.."/item","w")
  302. for i = 1,#itemToPrint do
  303.   file.writeLine(itemToPrint[i])
  304.   sleep(0.001)
  305.   m = m+1
  306.   secs = secs-0.05
  307.   writePos(1,18,"What do you want to save the file as? approx "..secs.." seconds left")
  308.   writePos(35, 19, m.." Out of "..(#itemToPrint*3))
  309. end
  310. file.close()
  311. if input ~= "exit" then
  312.   fileOpen = input
  313. end
  314. changed = false
  315. end
  316. function runFieldOne()
  317. drawScreenOne()
  318. drawScreenTwo()
  319. drawBox(1,3,11,19)
  320. for i = 1,#mopt do
  321.   writePos(3,3+(i*2),mopt[i])
  322. end
  323. writePos(2,3+(f1Selected*2),">")
  324. event, key = os.pullEvent()
  325. if (key == 200 or key == 17)and f1Selected > 1 then
  326.   f1Selected = f1Selected-1
  327. elseif (key == 208 or key == 31)and f1Selected < #mopt then
  328.   f1Selected = f1Selected+1
  329. elseif key == 14 then
  330.   loopBreaker = true
  331. elseif key == 15 then
  332.   currentField = 2
  333. elseif key == 28 then
  334.   if f1Selected == 4 then
  335.    drawMode = 1
  336.   elseif f1Selected == 5 then
  337.    drawMode = 3
  338.   elseif f1Selected == 6 then
  339.    drawMode = 2
  340.   elseif f1Selected == 1 then
  341.    if changed ~= true then
  342.         xToPrint = {}
  343.         yToPrint = {}
  344.         itemToPrint = {}
  345.         fileOpen = "New File"
  346.         sketchitWatermark = false
  347.    else
  348.         askToSave()
  349.    end
  350.   elseif f1Selected == 2 then
  351.    open()
  352.   elseif f1Selected == 3 then
  353.    save()
  354.   end
  355. end
  356. end
  357. function drawScreenOne()
  358. if currentField == 1 then
  359.   clear()
  360.   drawBox(1,1,50,19)
  361.   writePos(1,3,"+------------------------------------------------+")
  362.   drawBox(1,3,11,19)
  363.   writePos(3,2,"Open: "..fileOpen.." - File size: "..#itemToPrint*3)
  364.   for i = 1,#mopt do
  365.    writePos(3,3+(i*2),mopt[i])
  366.   end
  367.   if drawMode == 1 then
  368.    writePos(50-string.len("Tool: pencil")-1, 2, "Tool: pencil")
  369.   elseif drawMode == 2 then
  370.    writePos(50-string.len("Tool: draw box")-1, 2, "Tool: draw box")
  371.   elseif drawMode == 3 then
  372.    writePos(50-string.len("Tool: rubber")-1, 2, "Tool: rubber")
  373.   end
  374. else
  375.   clear()
  376.   drawBox(1,1,50,19)
  377.   writePos(1,3,"+------------------------------------------------+")
  378.   writePos(3,2,"Open: "..fileOpen.." - File size: "..#itemToPrint*3)
  379.   writePos(9,13,"|")
  380.   drawBox(1,3,9,19)
  381.   for i = 1,#mopt do
  382.    writePos(2,3+(i*2),mopt[i])
  383.   end
  384.   writePos(9,15,"|") -- to cover up "draw box"
  385.   if drawMode == 1 then
  386.    writePos(50-string.len("Tool: pencil")-1, 2, "Tool: pencil")
  387.   elseif drawMode == 2 then
  388.    writePos(50-string.len("Tool: draw box")-1, 2, "Tool: draw box")
  389.   elseif drawMode == 3 then
  390.    writePos(50-string.len("Tool: rubber")-1, 2, "Tool: rubber")
  391.   end
  392. end
  393. end
  394. function add(item, x, y)
  395. if x == nil or y == nil then
  396.   x = cursorPosX
  397.   y = cursorPosY
  398. end
  399. for i = 1,#itemToPrint do
  400.   if xToPrint[i] == x and yToPrint[i] == y then
  401.    table.remove(xToPrint, i)
  402.    table.remove(yToPrint, i)
  403.    table.remove(itemToPrint, i)
  404.   end
  405. end
  406. table.insert(xToPrint, x)
  407. table.insert(yToPrint, y)
  408. table.insert(itemToPrint, item)
  409. end
  410. function runFieldTwo()
  411. drawScreenOne()
  412. drawScreenTwo()
  413. if sketchitWatermark == false then
  414.   k = "sketch-it version "..version
  415.   for i = 1,string.len(k) do
  416.    add(string.sub(k,i,i),10+i,18)
  417.   end
  418.   sketchitWatermark = true
  419. end
  420. if drawMode == 3 then
  421.   for i = 1,#itemToPrint do
  422.    if (cursorPosX == xToPrint[i] and cursorPosY == yToPrint[i]) then
  423.         table.remove(xToPrint, i)
  424.         table.remove(yToPrint, i)
  425.         table.remove(itemToPrint, i)
  426.         changed = true
  427.    end
  428.   end
  429. end
  430. if drawing == true and drawMode == 1 then
  431.   add(drawIcon)
  432.   changed = true
  433. end
  434. event, key, item = os.pullEvent()
  435. if key == 200 and cursorPosY > 4 then
  436.   cursorPosY = cursorPosY-1
  437. elseif key == 208 and cursorPosY < 17 then
  438.   cursorPosY = cursorPosY+1
  439. elseif key == 203 and cursorPosX > 10 then
  440.   cursorPosX = cursorPosX-1
  441. elseif key == 205 and cursorPosX < 49 then
  442.   cursorPosX = cursorPosX+1
  443. elseif event == "char" and drawMode == 2 and key ~= " " and boxStarted ~= false then
  444.   saveBox(key)
  445.   boxStarted = false
  446.   changed = true
  447. elseif event == "char" and drawMode == 2 and key == " " and boxStarted ~= false then
  448.   for i = 1,2 do
  449.    for z = boxX, cursorPosX do
  450.         for i = 1,#itemToPrint do
  451.          for k = boxY, cursorPosY do
  452.           if k == yToPrint[i] and z == xToPrint[i] then
  453.            table.remove(xToPrint, i)
  454.            table.remove(yToPrint, i)
  455.            table.remove(itemToPrint, i)
  456.           end
  457.          end
  458.         end
  459.    end
  460.   end
  461.   boxStarted = false
  462.   changed = true
  463. elseif key == 15 then
  464.   currentField = 1
  465. elseif key == 14 and boxStarted == false then
  466.   loopBreaker = true
  467.   if changed == true then
  468.    askToSave()
  469.   end
  470. elseif key == 42 and drawMode == 1 and drawing == false then
  471.   drawing = true
  472. elseif event == "char" and drawMode == 1 and key ~= " " then
  473.   add(key)
  474.   changed = true
  475.   if cursorPosX < 49 then
  476.    cursorPosX = cursorPosX+1
  477.   end
  478.   drawing = false
  479. elseif key == 57 and drawMode == 1 and cursorPosX < 49 then
  480.   for i = 1,#itemToPrint do
  481.    if cursorPosX == xToPrint[i] and cursorPosY == yToPrint[i] then
  482.         table.remove(xToPrint, i)
  483.         table.remove(yToPrint, i)
  484.         table.remove(itemToPrint, i)
  485.    end
  486.   end
  487.   cursorPosX = cursorPosX+1
  488. elseif key == 42 and drawMode == 1 and drawing == true then
  489.   drawing = false
  490. elseif key == 42 and drawMode == 3 then
  491.   changed = true
  492.   xToPrint = {}
  493.   yToPrint = {}
  494.   itemToPrint = {}
  495.   sketchitWatermark = false
  496. elseif key == 42 and drawMode == 2 and boxStarted ~= true then
  497.   boxStarted = true
  498.   boxX = cursorPosX
  499.   boxY = cursorPosY
  500. elseif key == 42 and drawMode == 2 and boxStarted == true then
  501.   boxStarted = false
  502.   saveBox()
  503.   changed = true
  504. elseif key == 14 and boxStarted == true then
  505.   boxStarted = false
  506. elseif key == 199 then
  507.   cursorPosX = 10
  508.   cursorPosY = 4
  509. elseif key == 29 then
  510.   os.startTimer(0.2)
  511.   event, key = os.pullEvent()
  512.   if key == 200 then
  513.    cursorPosY = 4
  514.   elseif key == 208 then
  515.    cursorPosY = 17
  516.   elseif key == 203 then
  517.    cursorPosX = 10
  518.   elseif key == 205 then
  519.    cursorPosX = 49
  520.   end
  521. end
  522. end
  523. function drawScreenTwo()
  524. drawScreenOne()
  525. if drawMode == 1 then
  526.   writePos(cursorPosX,cursorPosY,"x")
  527. end
  528. for i = 1,#itemToPrint do
  529.   writePos(xToPrint[i],yToPrint[i],itemToPrint[i])
  530. end
  531. if drawMode == 1 then
  532.   writePos(cursorPosX-1,cursorPosY+1,"/|")
  533.   writePos(cursorPosX-2,cursorPosY+2,"//")
  534.   writePos(cursorPosX-3,cursorPosY+3,"//")
  535.   writePos(cursorPosX-4,cursorPosY+4,"</")
  536. elseif drawMode == 2 then
  537.   writePos(cursorPosX,cursorPosY,"X")
  538. elseif drawMode == 3 then
  539.   drawBox(cursorPosX,cursorPosY,cursorPosX+2,cursorPosY+2)
  540.   writePos(cursorPosX,cursorPosY,"O")
  541. end
  542. if boxStarted == true then
  543.   drawBox(boxX,boxY,cursorPosX,cursorPosY)
  544. end
  545. end
  546. --end
  547. --variables
  548. sketchitWatermark = false
  549. drawing = false
  550. boxStarted = false
  551. changed = false
  552. drawMode = 1
  553. fileOpen = "New File"
  554. version = "1.0.2"
  555. mopt = {
  556. "New";
  557. "Open";
  558. "Save";
  559. "Pencil";
  560. "Rubber";
  561. "Draw box";
  562. "More";
  563. }
  564. currentField = 1
  565. f1Selected = 1
  566. loopBreaker = false
  567. cursorPosY = 4
  568. cursorPosX = 15
  569. xToPrint = {}
  570. yToPrint = {}
  571. itemToPrint = {}
  572. drawIcon = "o"
  573. newPos = 1
  574. topLimit = 1
  575. bottomLimit = topLimit+14
  576. openSel = 1
  577. --end
  578. --code
  579. if not fs.exists("sketch-it") then
  580. fs.makeDir("sketch-it")
  581. end
  582. if not fs.exists("sketch-it/saves") then
  583. fs.makeDir("sketch-it/saves")
  584. end
  585. while loopBreaker ~= true do
  586. if currentField == 1 then
  587.   runFieldOne()
  588. else
  589.   runFieldTwo()
  590. end
  591. end
  592. clear()
  593. writePos(1,1,"Thanks for using sketch-it!")
  594. --end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement