Clem105

Progress Bar API

Jun 21st, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1.    
  2.  
  3.     local monitor
  4.     local hasMonitor = false
  5.     local ProgressBar = {}
  6.     local FillColor = colors.orange
  7.     local EmptyColor = colors.blue
  8.     local TextColor = colors.white
  9.      
  10.     function SetPeripheral(side)
  11.       if (peripheral.isPresent(side)) then
  12.         monitor = peripheral.wrap(side)
  13.         hasMonitor = true
  14.       end
  15.     end
  16.      
  17.     function SetTable(name, maxVal, curVal, xmin, xmax, y)
  18.       ProgressBar[name] = {}
  19.       ProgressBar[name]["Max"] = maxVal
  20.       ProgressBar[name]["Current"] = curVal
  21.       ProgressBar[name]["XMin"] = xmin
  22.       ProgressBar[name]["XMax"] = xmax
  23.       ProgressBar[name]["YVal"] = y
  24.     end
  25.      
  26.     function ClearTable()
  27.       if (hasMonitor) then
  28.         ProgressBar = {}
  29.       end
  30.     end
  31.      
  32.     function SetFillColor(color)
  33.       if (colors.combine(color,color)) then  
  34.         FillColor = color
  35.       end
  36.     end
  37.      
  38.     function SetEmptyColor(color)
  39.       if (colors.combine(color,color)) then
  40.         EmptyColor = color
  41.       end
  42.     end
  43.      
  44.     function SetTextColor(color)
  45.       if (colors.combine(color,color)) then
  46.         TextColor = color
  47.       end
  48.     end
  49.      
  50.     function SetMaxValue(name, intVal)
  51.       if (ProgressBar[name]) then
  52.         ProgressBar[name]["Max"] = intVal
  53.       end
  54.     end
  55.      
  56.     function SetCurValue(name, intVal)
  57.       if (ProgressBar[name]) then
  58.         ProgressBar[name]["Current"] = intVal
  59.       end
  60.     end
  61.      
  62.     function DrawToPeripheral(index)
  63.       if (hasMonitor) then
  64.           DrawBar(index,ProgressBar[index])
  65.       end
  66.     end
  67.      
  68.     function DrawBar(name, arr)
  69.       local y = arr["YVal"]
  70.       local fill = math.floor((arr["XMax"] - arr["XMin"]) * (arr["Current"] / arr["Max"]))
  71.      
  72.       monitor.setBackgroundColor(FillColor)
  73.       monitor.setTextColor(TextColor)
  74.      
  75.       for x = arr["XMin"], arr["XMax"] do
  76.         local num = math.floor(x - arr["XMin"])
  77.         monitor.setCursorPos(x,y)
  78.        
  79.         if (num > fill) then
  80.           monitor.setBackgroundColor(EmptyColor)
  81.         end
  82.        
  83.         if (num == 0) then
  84.           monitor.write("[")
  85.         end
  86.         if (x == arr["XMax"]) then
  87.           monitor.write("]")
  88.         else
  89.           monitor.write(" ")
  90.         end
  91.       end
  92.      
  93.       monitor.setBackgroundColor(colors.black)
  94.     end
Advertisement
Add Comment
Please, Sign In to add comment