Advertisement
Stekeblad

progbar.lua

Mar 3rd, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.69 KB | None | 0 0
  1. -- progbar: Progress bar API for ComputerCraft
  2. -- By: Stekeblad
  3. -- License: MIT
  4. -- Version: 1.1
  5. -- Updated: 13 July 2019
  6.  
  7. function help()
  8.     local oldFgColor = term.getTextColor()
  9.     term.setTextColor(colors.lightGray)
  10.     print("usage for draw vertical/horrizontal bar:")
  11.     print("  topLeftX: a whole number")
  12.     print("  topLeftY: a whole number")
  13.     print("  width: a possitive whole number")
  14.     print("  height: a possitive whole number")
  15.     print("  progress: a number between 0 and 100, can have decimals")
  16.     print("  fillColor: the color to use for the bar in the progressbar")
  17.     print("  drawBorder: true/false, optional, use true if you want a border around the progress bar")
  18.     print("  borderColor: the color to use for the border, only needed if border is true")
  19.     term.setTextColor(oldFgColor)
  20.     error("",4)
  21. end
  22.  
  23. local function isWholeNumber(num)
  24.     return num ~= nil and type(num) == "number" and num == math.ceil(num)
  25. end
  26.  
  27. local function myRound(num)
  28.     if num % 1 < 0.5 then
  29.         return math.floor(num)
  30.     else
  31.         return math.ceil(num)
  32.     end
  33. end
  34.  
  35. local function checkParams(topLeftX, topLeftY, width,
  36.         height, progress, fillColor,
  37.         drawBorder, borderColor)
  38.     if topLeftX == nil or not isWholeNumber(topLeftX) then
  39.         help()
  40.     elseif topLeftY == nil or not isWholeNumber(topLeftY) then
  41.         help()
  42.     elseif width == nil or not isWholeNumber(width) or width < 1 then
  43.         help()
  44.     elseif height == nil or not isWholeNumber(height) or height < 1 then
  45.         help()
  46.     elseif progress == nil or not type(progress) == "number"
  47.             or progress < 0 or progress > 100 then
  48.         help()
  49.     elseif fillColor == nil then
  50.         help()
  51.     elseif drawBorder == true and borderColor == nil then
  52.         help()
  53.     end
  54. end
  55.  
  56.  
  57.  
  58. function drawVerticalBar(topLeftX, topLeftY, width,
  59.         height, progress, fillColor,
  60.         drawBorder, borderColor)
  61.    
  62.     checkParams(topLeftX, topLeftY, width,
  63.         height, progress, fillColor,
  64.         drawBorder, borderColor)
  65.    
  66.     local oldBG = term.getBackgroundColor()
  67.     local pxToFill
  68.     local border
  69.     -- clear area
  70.     paintutils.drawFilledBox(topLeftX, topLeftY,
  71.             topLeftX+width-1, topLeftY+height-1,
  72.             oldBG)
  73.     -- draw border
  74.     if drawBorder then
  75.         border = 1
  76.         pxToFill = myRound((height - 2) * (progress / 100))
  77.         paintutils.drawBox(topLeftX, topLeftY,
  78.                 topLeftX+width-1, topLeftY+height-1,
  79.                 borderColor)
  80.     else
  81.         border = 0
  82.         pxToFill = myRound((height) * (progress / 100))
  83.     end
  84.     -- draw progress bar
  85.     paintutils.drawFilledBox(topLeftX+border,
  86.             topLeftY+(height-pxToFill)-border,
  87.             topLeftX+width-1-border, topLeftY+height-1-border,
  88.             fillColor)
  89.     term.setBackgroundColor(oldBG)
  90.     term.setCursorPos(1, topLeftY+height+1)
  91. end
  92.  
  93. function drawHorrizontalBar(topLeftX, topLeftY, width,
  94.         height, progress, fillColor,
  95.         drawBorder, borderColor)
  96.    
  97.     checkParams(topLeftX, topLeftY, width,
  98.         height, progress, fillColor,
  99.         drawBorder, borderColor)
  100.    
  101.     local oldBG = term.getBackgroundColor()
  102.     local pxToFill
  103.     local border
  104.     -- clear area
  105.     paintutils.drawFilledBox(topLeftX, topLeftY,
  106.             topLeftX+width-1, topLeftY+height-1,
  107.             oldBG)
  108.     -- draw border
  109.     if drawBorder then
  110.         border = 1
  111.         pxToFill = myRound((width - 2) * (progress / 100))
  112.         paintutils.drawBox(topLeftX, topLeftY,
  113.                 topLeftX+width-1, topLeftY+height-1,
  114.                 borderColor)
  115.     else
  116.         border = 0
  117.         pxToFill = myRound(width * (progress / 100))
  118.     end
  119.     -- draw progress bar
  120.     paintutils.drawFilledBox(topLeftX+border, topLeftY+border,
  121.             topLeftX+border+pxToFill-1, topLeftY+height-1-border,
  122.             fillColor)
  123.     term.setBackgroundColor(oldBG)
  124.     term.setCursorPos(1, topLeftY+height+1)
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement