Advertisement
Guest User

DireWolf Edited Button API

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