monster010

CC - Bars API

Apr 20th, 2016
3,009
2
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.72 KB | None | 2 0
  1. -- Bars API - (c) monster010
  2. local monitor
  3. local bar = {}
  4.  
  5. function construct(montor)
  6.     monitor = montor
  7. end
  8.  
  9. function getBars()
  10.     return bar
  11. end
  12.  
  13. function add(name, typ, maxval, curval, x, y, width, height, clfill, clempty)
  14.     if not clfill then clfill = colors.green end
  15.     if not clempty then clempty = colors.gray end
  16.  
  17.     bar[name] = {}
  18.     bar[name]["typ"] = typ
  19.     bar[name]["max"] = maxval
  20.     bar[name]["cur"] = curval
  21.     bar[name]["xmin"] = x
  22.     bar[name]["ymin"] = y
  23.     bar[name]["xmax"] = x + width - 1
  24.     bar[name]["ymax"] = y + height - 1
  25.     bar[name]["clfill"] = clfill
  26.     bar[name]["clempty"] = clempty
  27. end
  28.  
  29. function clear()
  30.     bars = {}
  31. end
  32.  
  33. function screen()
  34.     for name, data in pairs(bar) do
  35.         fill(name, data)
  36.     end
  37. end
  38.  
  39. function set(name, key, val)
  40.     bar[name][key] = val
  41. end
  42.  
  43. function fill(name, data)
  44.     if data["typ"] == "hor" then
  45.         local val = data["ymin"]
  46.         local fill = math.floor((data["xmax"] - data["xmin"]) * (data["cur"] / data["max"]))
  47.        
  48.         monitor.setBackgroundColor(data["clfill"])
  49.  
  50.         for x = data["xmin"], data["xmax"] do
  51.             local num = math.floor(x - data["xmin"])
  52.             monitor.setCursorPos(x, val)
  53.  
  54.             if (num > fill) then
  55.                 monitor.setBackgroundColor(data["clempty"])
  56.             end
  57.  
  58.             monitor.write(" ")
  59.         end
  60.     elseif data["typ"] == "ver" then
  61.         local val = data["xmin"]
  62.         local fill = math.floor((data["ymax"] - data["ymin"]) * (data["cur"] / data["max"]))
  63.  
  64.         monitor.setBackgroundColor(data["clfill"])
  65.  
  66.         for y = data["ymin"], data["ymax"] do
  67.             local num = math.floor(y - data["ymin"])
  68.             monitor.setCursorPos(val, y)
  69.  
  70.             if (num > fill) then
  71.                 monitor.setBackgroundColor(data["clempty"])
  72.             end
  73.  
  74.             monitor.write(" ")
  75.         end
  76.     end
  77.  
  78.     monitor.setBackgroundColor(colors.black)
  79. end
Advertisement
Add Comment
Please, Sign In to add comment