Advertisement
UpZone

CC - Templater

Jun 16th, 2016
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.88 KB | None | 0 0
  1. ---------------
  2. -- BY UPZONE --
  3. ---------------
  4.  
  5. os.loadAPI("builder\\apis\\Button")
  6. os.loadAPI("builder\\apis\\Colors")
  7. os.loadAPI("builder\\apis\\Paint")
  8.  
  9. term.clear()
  10. term.setCursorPos(1, 1)
  11.  
  12. local args = {...}
  13. if args[1] == "-help" then
  14.     print("Color Legend:")
  15.     Paint.pixel(2, 3, " ", Colors.black, Colors.yellow)
  16.     term.setCursorPos(4, 3)
  17.     term.write("= Yellow represents Blocks which are placed last")
  18.     term.setCursorPos(6, 4)
  19.     term.write("(e.g. Torches)")
  20.     term.setCursorPos(4, 5)
  21.     term.write("= Black represents nothing/empty slot")
  22.     term.setCursorPos(1, 6)
  23.     return
  24. end
  25.  
  26. local width = 5
  27. local maxWidth = 23
  28. local height = 5
  29. local maxHeight = 15
  30. local layer = 1
  31. local maxLayer = 99
  32.  
  33. local running = true
  34.  
  35. local layers = {}
  36.  
  37. function updateLayer()
  38.   layer = 0
  39.   layers = {}
  40.   layerInc()
  41.  
  42.   -- reset
  43.   Paint.fill(26, 2, 50, 18, Colors.black)
  44.  
  45.   -- draw layers border
  46.   startX = 49 - width
  47.   startY = 2
  48.   endX = 50
  49.   endY = 3 + height
  50.  
  51.   Paint.border(startX, startY, endX, endY, Colors.white)
  52. end
  53.  
  54. function updateNumbers()
  55.   -- width
  56.   term.setCursorPos(14, 2)
  57.   if width < 10 then
  58.     term.write("0")
  59.   end
  60.   term.write(width)
  61.  
  62.   -- height
  63.   term.setCursorPos(14, 4)
  64.   if height < 10 then
  65.     term.write("0")
  66.   end
  67.   term.write(height)
  68.  
  69.   -- layer
  70.   term.setCursorPos(14, 6)
  71.   if layer < 10 then
  72.     term.write("0")
  73.   end
  74.   term.write(layer)
  75. end
  76. function widthDec()
  77.   if width == 1 then
  78.     return
  79.   end
  80.  
  81.   width = width - 1
  82.   updateLayer()
  83.   updateNumbers()
  84. end
  85. function widthInc()
  86.   if width == maxWidth then
  87.     return
  88.   end
  89.  
  90.   width = width + 1
  91.   updateLayer()
  92.   updateNumbers()
  93. end
  94. function heightDec()
  95.   if height == 1 then
  96.     return
  97.   end
  98.  
  99.   height = height - 1
  100.   updateLayer()
  101.   updateNumbers()
  102. end
  103. function heightInc()
  104.   if height == maxHeight then
  105.     return
  106.   end
  107.  
  108.   height = height + 1
  109.   updateLayer()
  110.   updateNumbers()
  111. end
  112. function layerDec()
  113.   if layer == 1 then
  114.     return
  115.   end
  116.  
  117.   layer = layer - 1
  118.   drawLayer(layer)
  119.   updateNumbers()
  120. end
  121. function drawLayer(level)
  122.   baseX = 49 - width
  123.   baseY = 2
  124.  
  125.   for y = 1, height do
  126.     for x = 1, width do
  127.       Paint.pixel(baseX + x, baseY + y, " ", Colors.black, layers[layer][((y-1) * width) + x])
  128.     end
  129.   end
  130. end
  131. function layerInc()
  132.   if layer == maxLayer then
  133.     return
  134.   end
  135.  
  136.   layer = layer + 1
  137.  
  138.   if layers[layer] == nil then
  139.     layers[layer] = {}
  140.     for i = 1, width * height do
  141.       layers[layer][i] = Colors.black
  142.     end
  143.   end
  144.  
  145.   drawLayer(layer)
  146.   updateNumbers()
  147. end
  148. function cleanupLayers()
  149.     countLayer = 0
  150.     for i, k in ipairs(layers) do
  151.         countLayer = countLayer + 1
  152.     end
  153.  
  154.     for i = countLayer, 1, -1 do
  155.         hasOnlyBlacks = true
  156.  
  157.         for k, v in ipairs(layers[i]) do
  158.             if v ~= Colors.black then
  159.                 hasOnlyBlacks = false
  160.                 break
  161.             end
  162.         end
  163.  
  164.         if hasOnlyBlacks then
  165.             layers[i] = nil
  166.         else
  167.             break
  168.         end
  169.     end
  170. end
  171. function save()
  172.     term.clear()
  173.     term.setCursorPos(1, 1)
  174.  
  175.     if not fs.exists("builder\\templates\\") then
  176.         shell.run("mkdir templates")
  177.     end
  178.  
  179.     term.write("Filename: ")
  180.     fileName = read()
  181.    
  182.     if fs.exists("builder\\templates\\" .. fileName) then
  183.         repeat
  184.             print("A file with this name already exists, please choose a different one")
  185.             term.write("Filename: ")
  186.             fileName = read()
  187.         until not fs.exists(fileName)  
  188.     end
  189.  
  190.     cleanupLayers()
  191.  
  192.     f = fs.open("builder\\templates\\" .. fileName, "w")
  193.     f.write(math.floor(width))
  194.     f.write(":")
  195.     f.write(math.floor(height))
  196.     f.write(":")
  197.  
  198.     for k, v in ipairs(inventoryFields) do
  199.         f.write(v[3])
  200.     end
  201.     f.write(":")   
  202.  
  203.     curLayer = 1
  204.  
  205.     while layers[curLayer] ~= nil do
  206.         for i = 1, width * height do
  207.             f.write(layers[curLayer][i])
  208.         end
  209.         curLayer = curLayer + 1
  210.     end
  211.    
  212.     f.close()
  213.     running = false
  214. end
  215. function exit()
  216.   running = false
  217. end
  218.  
  219. buttons = {
  220.   Button.create("<", 12, 2, 1, 1, widthDec, Colors.white, Colors.red, Colors.white, Colors.lime),
  221.   Button.create(">", 17, 2, 1, 1, widthInc, Colors.white, Colors.red, Colors.white, Colors.lime),
  222.   Button.create("<", 12, 4, 1, 1, heightDec, Colors.white, Colors.red, Colors.white, Colors.lime),
  223.   Button.create(">", 17, 4, 1, 1, heightInc, Colors.white, Colors.red, Colors.white, Colors.lime),
  224.   Button.create("<", 12, 6, 1, 1, layerDec, Colors.white, Colors.red, Colors.white, Colors.lime),
  225.   Button.create(">", 17, 6, 1, 1, layerInc, Colors.white, Colors.red, Colors.white, Colors.lime),
  226.   Button.create("Save", 12, 16, 6, 3, save, Colors.white, Colors.red, Colors.white, Colors.lime),
  227.   Button.create("Exit", 19, 16, 6, 3, exit, Colors.white, Colors.red, Colors.white, Colors.lime)
  228. }
  229. inventoryFields = {
  230.   { 3, 11, Colors.black }, { 5, 11, Colors.black }, { 7, 11, Colors.black }, { 9, 11, Colors.black },
  231.   { 3, 13, Colors.black }, { 5, 13, Colors.black }, { 7, 13, Colors.black }, { 9, 13, Colors.black },
  232.   { 3, 15, Colors.black }, { 5, 15, Colors.black }, { 7, 15, Colors.black }, { 9, 15, Colors.black },
  233.   { 3, 17, Colors.black }, { 5, 17, Colors.black }, { 7, 17, Colors.black }, { 9, 17, Colors.black }
  234. }
  235. currentColorField = { 23, 11, Colors.black }
  236.  
  237. function drawButtonsControls()
  238.   for i, btn in ipairs(buttons) do
  239.     btn:draw()
  240.   end
  241. end
  242.  
  243. function drawInventory()
  244.   for slot, field in ipairs(inventoryFields) do
  245.     Paint.pixel(field[1], field[2], " ", Colors.black, field[3])
  246.   end
  247. end
  248.  
  249. function drawText()
  250.   term.setCursorPos(2, 2)
  251.   term.write("Width:")
  252.   term.setCursorPos(2, 4)
  253.   term.write("Height:")
  254.   term.setCursorPos(2, 6)
  255.   term.write("Layer:")
  256.   term.setCursorPos(2, 8)
  257.   term.write("Inventory")
  258.   term.setCursorPos(12, 11)
  259.   term.write("Current:")
  260. end
  261.  
  262. function updateCurrentColor()
  263.   Paint.pixel(currentColorField[1], currentColorField[2], " ", Colors.black, currentColorField[3])
  264. end
  265.  
  266. function drawingPitchClicked(x, y)
  267.   pitchX = x - (49 - width)
  268.   pitchY =  y - 2
  269.  
  270.   if pitchX  < 1 or pitchX > width or pitchY < 1 or pitchY > height then
  271.     return
  272.   end
  273.  
  274.   layers[layer][((pitchY-1) * width) + pitchX] = currentColorField[3]
  275.   Paint.pixel(x, y, " ", Colors.black, currentColorField[3])
  276. end
  277.  
  278. function initialize()
  279.   Paint.fill(2, 10, 10, 18, Colors.white)
  280.   Paint.border(22, 10, 24, 12, Colors.white)
  281.   drawText()
  282.   drawButtonsControls()
  283.   drawInventory()
  284.   updateNumbers()
  285.   updateLayer()
  286. end
  287.  
  288. initialize()
  289.  
  290. while running do
  291.   event, side, xPos, yPos = os.pullEvent("mouse_click")
  292.   for i, btn in ipairs(buttons) do
  293.     if btn:intersects(xPos, yPos) then
  294.       btn:raiseOnClick()
  295.     end
  296.   end
  297.   for i, field in ipairs(inventoryFields) do
  298.     if xPos == field[1] and yPos == field[2] then
  299.       inventoryFields[i][3] = currentColorField[3]
  300.       drawInventory()
  301.     end
  302.   end
  303.   if xPos == currentColorField[1] and yPos == currentColorField[2] then
  304.     currentColorField[3] = Colors.change(currentColorField[3])
  305.     updateCurrentColor()
  306.   end
  307.   drawingPitchClicked(xPos, yPos)
  308. end
  309.  
  310. term.clear()
  311. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement