JavySirius

button

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