Advertisement
Guest User

blueprint_gui

a guest
Sep 29th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.35 KB | None | 0 0
  1. local termWidth, termHeight = term.getSize()
  2. local blueprints = fs.list("/bps")
  3. local selectedBlueprintIndex = -1
  4. local currentBlueprint
  5.  
  6. local BP_LIST_W = 18
  7. local BP_DATA_X = BP_LIST_W + 2
  8. local BP_DATA_W = 18
  9.  
  10. -- black out
  11. paintutils.drawFilledBox(1, 1, termWidth, termHeight, colors.black)
  12.  
  13. function checkBuilditClick(x, y)
  14.   if x >= BP_DATA_X + 9 and x <= BP_DATA_X + 19 and y == 13 then
  15.     if selectedBlueprintIndex ~= -1 then
  16.       shell.run("blueprint_builder " .. blueprints[selectedBlueprintIndex])
  17.     end
  18.   end
  19. end
  20.  
  21. function drawBuildButton()
  22.   term.setCursorPos(BP_DATA_X + 9, 13)
  23.   term.setBackgroundColor(colors.green)
  24.   term.setTextColor(colors.lime)
  25.   term.write("[Build It]")
  26. end
  27.  
  28. function formatMaterial(s, count)
  29.   local colonIndex = string.find(s, ":")
  30.   if colonIndex then
  31.     s = string.sub(s, colonIndex+1, string.len(s))
  32.   end
  33.   return string.sub(count .. ": " ..s, 1, BP_DATA_W)
  34. end
  35.  
  36. function drawBlueprintData()
  37.   -- Draw the data Header
  38.   term.setBackgroundColor(colors.black)
  39.   term.setTextColor(colors.lightGray)
  40.   term.setCursorPos(BP_DATA_X, 3)
  41.   print("Required Blocks")
  42.  
  43.   paintutils.drawFilledBox(BP_DATA_X, 4, BP_DATA_X + BP_DATA_W, 10, colors.gray)
  44.    
  45.   if currentBlueprint then
  46.     local matY = 4
  47.     term.setTextColor(colors.white)
  48.     for k, v in pairs(currentBlueprint.quantities) do
  49.       if matY <= 10 then
  50.         term.setCursorPos(BP_DATA_X, matY)
  51.         term.write(formatMaterial(k, v))
  52.       end
  53.       matY = matY + 1
  54.     end
  55.   end
  56.  
  57.   term.setCursorPos(1, 12)
  58.   term.setBackgroundColor(colors.black)
  59.   print("Dimensions")
  60.   paintutils.drawLine(1, 13, BP_DATA_W, 13, colors.gray)
  61.   term.setCursorPos(1, 13)
  62.   term.setBackgroundColor(colors.gray)
  63.   -- buid a string for the dimensions
  64.   if currentBlueprint then
  65.     local dimString = string.format("H:%d W:%d D:%d", currentBlueprint.dimensions.height, currentBlueprint.dimensions.width, currentBlueprint.dimensions.depth)
  66.     term.write(dimString)
  67.   end
  68. end
  69.  
  70. function drawCloseButton()
  71.   -- Draw an X button in the top Right
  72.   term.setCursorPos(termWidth, 1)
  73.   term.setBackgroundColor(colors.gray)
  74.   term.setTextColor(colors.lightGray)
  75.   print("x")
  76. end
  77.  
  78. function drawMainMenu()
  79.   -- Draw the [Builder] button
  80.   term.setCursorPos(1, 1)
  81.   term.setBackgroundColor(colors.orange)
  82.   term.setTextColor(colors.gray)
  83.   print("[Builder]")
  84.   paintutils.drawLine(10,1, termWidth-1, 1, colors.lightGray)
  85.  
  86.   drawCloseButton()
  87. end
  88.  
  89. function getBlueprints()
  90.   local bp = {}
  91.   for key, value in pairs(blueprints) do
  92.     if string.len(value) > BP_LIST_W then
  93.       bp[key] = string.sub(blueprints[key], 1, BP_LIST_W-2) .. ".."
  94.     else
  95.       bp[key] = blueprints[key]
  96.     end
  97.   end  
  98.   return bp
  99. end
  100.  
  101. function drawBlueprintsList()
  102.   -- The header
  103.   term.setCursorPos(1, 3)
  104.   term.setBackgroundColor(colors.black)
  105.   term.setTextColor(colors.lightGray)
  106.   print("Blueprints")
  107.  
  108.   -- The list of blueprints to click on
  109.   paintutils.drawFilledBox(1, 4, BP_LIST_W, 10, colors.gray)
  110.   term.setBackgroundColor(colors.gray)
  111.   term.setTextColor(colors.white)
  112.   local yStart = 4
  113.   for key, value in pairs(getBlueprints()) do
  114.     if selectedBlueprintLine == yStart then
  115.       term.setBackgroundColor(colors.cyan)
  116.       paintutils.drawLine(1, yStart, BP_LIST_W, yStart, colors.cyan)
  117.     else
  118.       term.setBackgroundColor(colors.gray)
  119.       paintutils.drawLine(1, yStart, BP_LIST_W, yStart, colors.gray)
  120.     end
  121.     term.setCursorPos(1, yStart)
  122.     print(value)
  123.     yStart = yStart + 1
  124.   end
  125. end
  126.  
  127. function checkBlueprintsClick(x, y)
  128.   if x < BP_LIST_W and y >= 4 and y <= 10 then
  129.     selectedBlueprintLine = y
  130.     drawBlueprintsList()
  131.     loadNewBlueprint()
  132.   end
  133. end
  134.  
  135. function loadNewBlueprint()
  136.   local bpFile = fs.open("/bps/" .. blueprints[selectedBlueprintLine - 3], "r")
  137.   currentBlueprint = textutils.unserialise(bpFile.readAll())
  138.   selectedBlueprintIndex = selectedBlueprintLine - 3
  139.   bpFile.close()
  140.   drawBlueprintData()
  141. end
  142.  
  143. drawMainMenu()
  144. drawBlueprintsList()
  145. drawBlueprintData()
  146. drawBuildButton()
  147.  
  148. while true do
  149.   local event, button, x, y = os.pullEventRaw()
  150.   if event == "mouse_click" then
  151.     if x == termWidth and y == 1 then
  152.       term.setBackgroundColor(colors.black)
  153.       shell.run("clear")
  154.       return -- close program
  155.     end
  156.    
  157.     checkBlueprintsClick(x, y)
  158.     checkBuilditClick(x, y)
  159.   end
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement