Stravides

CreativeFiller v1.0.0

Dec 6th, 2014
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.05 KB | None | 0 0
  1. -- Some global variables
  2. shell.run("rm", "startup")
  3. shell.run("pastebin", "get", "g9x4DRnJ", "startup")
  4.  
  5. --Declaring variables
  6.  
  7. --Local variables
  8.     local turtleID=420
  9.     local bInactiveBackground
  10.     local bActiveBackground
  11.     local progVer = "1.0.0"
  12.     local progName = "Metal-O-Matic"
  13.     local sideClick, xClick, yClick = nil, 0, 0
  14.     local loopTime = 2
  15.     local debugMode = false
  16.     local buttonWidth=10
  17.     local buttonHeight=3
  18.  
  19. --Global variables
  20.         bData = {}
  21.         pbData = {}
  22.         matData={}
  23.         clickedID = ""
  24.         m = ""
  25. --              m = peripheral.wrap("front")
  26.  
  27. --Monitor Functions
  28. function findMonitor()
  29.         local p = peripheral.getNames()
  30.         for k,v in pairs(p) do
  31.                 if (v == "top" or v == "bottom" or v == "left" or v == "back" or v == "right" or v == "front") then
  32.                         if (string.find(peripheral.getType(v), "monitor")) then
  33.                                 m = peripheral.wrap(v)
  34.                         end
  35.                 elseif (string.find(v, "monitor")) then
  36.                         m = peripheral.wrap(v)
  37.                 end
  38.         end
  39. end
  40.  
  41. function mClear()
  42.         m.clear()
  43.     m.setTextScale(.9)
  44.     m.setTextColor(colors.white)
  45.         m.setBackgroundColor(colors["black"])
  46. end
  47.  
  48. function mCreatePage()
  49. end
  50.  
  51.  local function drawLine(yPos)
  52.     local width, height = m.getSize()
  53.  
  54.     for i=1, width do
  55.         m.setCursorPos(i, yPos)
  56.         m.write("-")
  57.     end
  58. end -- function drawLine(yPos,monitorIndex)
  59.  
  60.  
  61. function mHeadline(text)
  62.         local x,y = m.getSize()
  63.         xText = math.floor((x / 2) - (#text / 2) + 1)
  64.         m.setCursorPos(xText, 1)
  65.         m.write(text)
  66. end
  67.  
  68. function mOverrideInactiveBackground(color)
  69.         bInactiveBackground = tostring(color)
  70. end
  71.  
  72. function mOverrideActiveBackground(color)
  73.         bActiveBackground = tostring(color)
  74. end
  75.  
  76. function setMaterials(ID,text)
  77.     matData[ID] = {}
  78.     matData[ID]["text"] = text
  79.  end
  80.  
  81. function mCreateButton(ID, text, pX, qX, pY, qY)
  82.         bData[ID] = {}
  83.         bData[ID]["text"] = text
  84.         bData[ID]["pX"] = pX
  85.         bData[ID]["qX"] = qX
  86.         bData[ID]["pY"] = pY
  87.         bData[ID]["qY"] = qY
  88.         bData[ID]["state"] = false
  89.         mDrawButton(ID)
  90. end
  91.  
  92. function mDrawButton(ID)
  93.         if (not bData[ID]["state"] and not bInactiveBackground) then
  94.                 m.setBackgroundColor(colors["red"])
  95.         elseif (not bData[ID]["state"]) then
  96.                 m.setBackgroundColor(colors[bInactiveBackground])
  97.         elseif (bData[ID]["state"] and not bActiveBackground) then
  98.                 m.setBackgroundColor(colors["lime"])
  99.         elseif (bData[ID]["state"]) then
  100.                 m.setBackgroundColor(colors[bActiveBackground])
  101.         end
  102.  
  103.         local yText = math.floor((bData[ID]["pY"] + bData[ID]["qY"]) / 2)
  104.         local xText = math.floor(((bData[ID]["pX"] + bData[ID]["qX"]) / 2) - (#bData[ID]["text"] / 2) + 1)
  105.         for y = bData[ID]["pY"], bData[ID]["qY"] do
  106.                 if (y == yText) then
  107.                         for x = bData[ID]["pX"], (bData[ID]["pX"] + bData[ID]["qX"]) / 2 do
  108.                                 m.setCursorPos(x,y)
  109.                                 m.write(" ")
  110.                         end
  111.                         m.setCursorPos(xText, y)
  112.                         m.write(bData[ID]["text"])
  113.                         for x = xText + #bData[ID]["text"], bData[ID]["qX"] do
  114.                                 m.setCursorPos(x,y)
  115.                                 m.write(" ")
  116.                         end
  117.                 else
  118.                         for x = bData[ID]["pX"], bData[ID]["qX"] do
  119.                                 m.setCursorPos(x,y)
  120.                                 m.write(" ")
  121.                         end
  122.                 end
  123.         end
  124.         m.setBackgroundColor(colors["black"])
  125. end
  126.  
  127. function splitText(xText, xSteps, y, text, color)
  128.         local dataText = {}
  129.         for k,v in text:gmatch"." do
  130.                 dataText[#dataText + 1] = tostring(k)
  131.         end
  132.  
  133.         local lettersColored = xSteps - xText + 1
  134.         m.setBackgroundColor(colors[color])
  135.         for x = xText, xText + lettersColored - 1 do
  136.                 m.setCursorPos(x, y)
  137.                 m.write(dataText[x - (xText - 1)])
  138.         end
  139.         m.setBackgroundColor(colors["black"])
  140.         for x = xText + lettersColored - 1, xText + #text - 1 do
  141.                 m.setCursorPos(x, y)
  142.                 m.write(dataText[x - (xText - 1)])
  143.         end
  144. end
  145.  
  146. function mDrawProgressBar(ID)
  147.         local xSize, ySize = m.getSize()
  148.         local xTitle = math.floor(((1 + xSize) / 2) - (#pbData[ID]["title"] / 2) + 1)
  149.         m.setCursorPos(xTitle, pbData[ID]["yTitle"])
  150.         m.setTextColor(colors[pbData[ID]["titleColor"]])
  151.         m.write(pbData[ID]["title"])
  152.  
  153.         local percent = pbData[ID]["value"] / pbData[ID]["maximum"]
  154.         local xSteps = math.floor(xSize * percent)
  155.         local text = pbData[ID]["value"].." / "..pbData[ID]["maximum"]
  156.         local textLength = #(text)
  157.         local xText = math.floor(((1 + xSize) / 2) - (textLength / 2) + 1)
  158.  
  159.         m.setBackgroundColor(colors[pbData[ID]["barColor"]])
  160.         m.setTextColor(colors["white"])
  161.         if (xSteps < xText) then
  162.                 for x = 1, xSteps do
  163.                         m.setCursorPos(x, pbData[ID]["yBar"])
  164.                         m.write(" ")
  165.                 end
  166.                 m.setBackgroundColor(colors["black"])
  167.                 m.setCursorPos(xText, pbData[ID]["yBar"])
  168.                 m.write(text)
  169.                 for x = xText + #text, xSize do
  170.                         m.setCursorPos(x, pbData[ID]["yBar"])
  171.                         m.write(" ")
  172.                 end
  173.         elseif (xSteps == (xText + #text - 1)) then
  174.                 for x = 1, xText - 1 do
  175.                         m.setCursorPos(x, pbData[ID]["yBar"])
  176.                         m.write(" ")
  177.                 end
  178.                 m.write(text)
  179.                 m.setBackgroundColor(colors["black"])
  180.                 for x = xText + #text, xSize do
  181.                         m.setCursorPos(x, pbData[ID]["yBar"])
  182.                         m.write(" ")
  183.                 end
  184.         elseif (xSteps >= xText and xSteps < (xText + #text - 1)) then
  185.                 for x = 1, xText - 1 do
  186.                         m.setCursorPos(x, pbData[ID]["yBar"])
  187.                         m.write(" ")
  188.                 end
  189.                 splitText(xText, xSteps, pbData[ID]["yBar"], text, pbData[ID]["barColor"])
  190.                 m.setBackgroundColor(colors["black"])
  191.                 for x = xText + #text, xSize do
  192.                         m.setCursorPos(x, pbData[ID]["yBar"])
  193.                         m.write(" ")
  194.                 end
  195.         else
  196.                 for x = 1, xSteps do
  197.                         m.setCursorPos(x, pbData[ID]["yBar"])
  198.                         m.write(" ")
  199.                 end
  200.                 m.setCursorPos(xText, pbData[ID]["yBar"])
  201.                 m.write(text)
  202.                 for x = xText + #text, xSteps do
  203.                         m.setCursorPos(x, pbData[ID]["yBar"])
  204.                         m.write(" ")
  205.                 end
  206.                 if xSteps ~= xSize then
  207.                         m.setBackgroundColor(colors["black"])
  208.                         for x = xSteps + 1, xSize do
  209.                                 m.setCursorPos(x, pbData[ID]["yBar"])
  210.                                 m.write(" ")
  211.                         end
  212.                 end
  213.         end
  214.         m.setBackgroundColor(colors["black"])
  215. end
  216.  
  217. function mCreateProgressBar(ID, title, yTitle, titleColor, value, maximum, yBar, barColor)
  218.         pbData[ID] = {}
  219.         pbData[ID]["title"] = title
  220.         pbData[ID]["yTitle"] = yTitle
  221.         pbData[ID]["titleColor"] = titleColor
  222.         pbData[ID]["value"] = value
  223.         pbData[ID]["maximum"] = maximum
  224.         pbData[ID]["yBar"] = yBar
  225.         pbData[ID]["barColor"] = barColor
  226.         mDrawProgressBar(ID)
  227. end
  228.  
  229. function outputSelected(key,x,y)
  230.    m.setCursorPos(x,y)
  231.    m.write("                    ")
  232.    m.setCursorPos(x,y)
  233.    m.write(matData[key]["text"])
  234.    m.setCursorPos(x,y+1)
  235.    m.write("Selected")
  236.    sleep(2)
  237.    m.setCursorPos(x,y)
  238.    m.write("                    ")
  239.    m.setCursorPos(x,y+1)
  240.    m.write("                    ")
  241. end
  242.  
  243. function sendInput(key)
  244.   msg=key.."|".."Pouring Now"
  245.   rednet.open("back") -- Wireless Modem placed upon the Bottom of the Computer
  246.   shell.run("clear")
  247.   rednet.send(420,msg) -- Main screen
  248. end
  249.  
  250.  
  251. function mButtonListen()
  252.         event, _, x, y = os.pullEvent()
  253.         if (event == "monitor_touch") then
  254.                 for key, value in pairs(bData) do
  255.                         if (x >= bData[key]["pX"] and x <= bData[key]["qX"] and y >= bData[key]["pY"] and y <= bData[key]["qY"]) then
  256.                                 clickedID = key
  257.                                 sendInput(key)
  258.                                 outputSelected(key,40,21)
  259.                         end
  260.                 end      
  261.         end
  262. end
  263.  
  264. function writeAt(x,y,text)
  265.   m.setCursorPos(x,y)
  266.   m.write(text)
  267. end
  268.  
  269. function setButtons(ID,text,x,y)
  270.    mCreateButton(ID, text, x, x+buttonWidth, y, y+(buttonHeight-1))
  271.    setMaterials(ID, text)
  272.    mDrawButton(ID)
  273. end
  274.  
  275. function main()
  276.    -- Top Row
  277.    setButtons("A1", "Iron", 2, 3)
  278.    setButtons("A2", "Gold", 14,3)
  279.    setButtons("A3", "Copper", 26,3)
  280.    setButtons("A4", "Silver", 38,3)
  281.    -- 2nd Row
  282.    setButtons("B1", "Manyullyn",2, 9)
  283.    setButtons("B2", "Bronze", 14,9)
  284.    setButtons("B3", "Ardite", 26,9)
  285.    setButtons("B4", "Cobalt", 38,9)
  286.   -- 3rd Row
  287.    setButtons("C1", "Steel",2, 15)
  288.    setButtons("C2", "Tin", 14,15)
  289.    setButtons("C3", "Electrum", 26,15)
  290.    setButtons("C4", "Invar", 38,15)
  291.  
  292.   -- Bottom Row
  293.    setButtons("D1", "ShinyMetal",2, 21)
  294.    setButtons("D2", "Lead", 14,21)
  295.    setButtons("D3", "Aluminium", 26,21)
  296.    -- THIS IS LEFT EMPTY AS SLOT 16 IS THE REMOVAL SPACE...
  297. end
  298.  
  299.  
  300.    
  301. findMonitor()
  302. mClear()
  303. mHeadline("Program: "..progName.."   Version: "..progVer)
  304. drawLine(2)
  305.  
  306. while not finished do
  307.  
  308.    parallel.waitForAny(main,mButtonListen())
  309. ---   main()
  310.    sleep(loopTime)
  311. end -- while not finished do
Advertisement
Add Comment
Please, Sign In to add comment