Advertisement
Guest User

button

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