SpitefulJames

ButtonGUI.lua

Apr 23rd, 2021
595
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. mon = peripheral.find("monitor")
  2.  
  3. if not mon then
  4.   error("Please attatch a monitor")
  5. end
  6.  
  7. mon.setTextScale(1)
  8. mon.setTextColor(colors.white)
  9. mon.setBackgroundColor(colors.black)
  10. button = {}
  11. charts = {}
  12.  
  13. function clearAll()
  14.   button = {}
  15.   charts = {}
  16.   mon.clear()
  17. end
  18.  
  19. function addButton(name, func, xmin, xmax, ymin, ymax, color, activeColor)
  20.   color = color or colors.red
  21.   activeColor = activeColor or colors.lime
  22.   button[name] = {}
  23.   button[name]["func"] = func
  24.   button[name]["active"] = false
  25.   button[name]["xmin"] = xmin
  26.   button[name]["ymin"] = ymin
  27.   button[name]["xmax"] = xmax
  28.   button[name]["ymax"] = ymax
  29.   button[name]["color"] = color
  30.   button[name]["activeColor"] = activeColor
  31. end
  32.  
  33. function addChart(name, xmin, xmax, ymin, ymax, value, color, label)
  34.   charts[name] = {}
  35.   charts[name]["xmin"] = xmin
  36.   charts[name]["xmax"] = xmax
  37.   charts[name]["ymin"] = ymin
  38.   charts[name]["ymax"] = ymax
  39.   charts[name]["value"] = value
  40.   charts[name]["color"] = color
  41.   charts[name]["label"] = label
  42.   -- where the values are percentages
  43. end
  44.  
  45. function updateChart(name, value, color)
  46.   charts[name]["value"] = value
  47.   charts[name]["color"] = color
  48.   screenChart()
  49. end
  50.  
  51. function screenButton() --Show buttons on monitor
  52.   local currcolor
  53.   for name, data in pairs(button)do
  54.     local active = data["active"]
  55.     if active  == true then
  56.       currcolor = data["activeColor"]
  57.     else
  58.       currcolor = data["color"]
  59.     end
  60.     fillButton(name, currcolor, data)
  61.   end
  62. end
  63.  
  64. function screenChart()
  65.   for name, data in pairs(charts)do
  66.     fillChart(name, data)
  67.   end
  68. end
  69.  
  70. function fillButton(text, color, bData) --Add text and spaces to fill button area
  71.   mon.setBackgroundColor(color)
  72.   local yspot = math.floor((bData["ymin"] + bData["ymax"])/2)
  73.   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  74.   for j = bData["ymin"], bData["ymax"] do
  75.     mon.setCursorPos(bData["xmin"], j)
  76.     if j == yspot then
  77.       for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) + 1 do
  78.         if k == xspot then
  79.           mon.write(text)
  80.         else
  81.           mon.write(" ")
  82.         end
  83.       end
  84.     else
  85.       for i = bData["xmin"], bData["xmax"] do
  86.         mon.write(" ")
  87.       end
  88.     end
  89.   end
  90.    mon.setBackgroundColor(colors.black)
  91. end  
  92.  
  93. function fillChart(name, cData)
  94.   local h = cData["ymax"] - cData["ymin"]
  95.   local percentPerStep = math.floor(100/h)
  96.   local barHeight = math.floor(cData["value"]/percentPerStep)
  97.  
  98.   local xspot = math.floor((cData["xmax"] - cData["xmin"]) /2) -1
  99.   local yspot = math.floor((cData["ymin"] + cData["ymax"]) /2)
  100.   local text = cData["value"] .. "%"
  101.  
  102.   if cData["label"] ~= nil then
  103.     labelX = math.floor((cData["xmax"] - cData["xmin"] - string.len(cData["label"])) /2)
  104.     label(cData["xmin"] + labelX, cData["ymax"] + 1,  cData["label"])
  105.   end
  106.  
  107.   local emptyColor = 0
  108.   if cData["color"] == colors.gray then
  109.     emptyColor = colors.lightGray
  110.   else
  111.     emptyColor = colors.gray
  112.   end
  113.  
  114.   if cData["color"] == colors.white then
  115.     mon.setTextColor(colors.black)
  116.   end
  117.  
  118.   for j = cData["ymin"], cData["ymax"] do
  119.       mon.setCursorPos(cData["xmin"], j)
  120.       if j == yspot then
  121.         for k = 0, cData["xmax"] - cData["xmin"] - string.len(text) + 1 do
  122.           if barHeight + cData["ymax"] >= cData["ymax"] + (cData["ymax"] - j) then
  123.             mon.setBackgroundColor(cData["color"])
  124.           else
  125.             mon.setBackgroundColor(emptyColor)
  126.           end
  127.           if k == xspot then
  128.             mon.write(text)
  129.           else
  130.             mon.write(" ")
  131.           end
  132.         end
  133.       else
  134.         for i = cData["xmin"], cData["xmax"] do
  135.           if barHeight + cData["ymax"] >= cData["ymax"] + (cData["ymax"] - j)  then
  136.             mon.setBackgroundColor(cData["color"])
  137.           else
  138.             mon.setBackgroundColor(emptyColor)
  139.           end
  140.             mon.write(" ")
  141.          end
  142.       end
  143.    end
  144.    mon.setBackgroundColor(colors.black)
  145.    mon.setTextColor(colors.white)
  146. end
  147.  
  148. function toggleButton(name) --Turns active to inactive and inactive to active
  149.   button[name]["active"] = not button[name]["active"]
  150.   screenButton()
  151. end
  152.  
  153. function flash(name)
  154.   toggleButton(name)
  155.   sleep(0.15)
  156.   toggleButton(name)
  157. end
  158.  
  159. function checkxy(x, y) --Did we click the button?
  160.   for name, data in pairs(button) do
  161.     if y>=data["ymin"] and y<= data["ymax"] then
  162.       if x>=data["xmin"] and x<=data["xmax"] then
  163.         data["func"]()
  164.         flash(name)
  165.         return true
  166.       end
  167.     end
  168.   end
  169.   return false
  170. end
  171.          
  172.  
  173. function heading(text)
  174.   w, h = mon.getSize()
  175.   mon.setCursorPos((w-string.len(text))/2+1, 1)
  176.   mon.write(text)
  177. end
  178.  
  179. function label(w, h, text)
  180.   mon.setCursorPos(w, h)
  181.   mon.write(text)
  182. end
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×