Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- progbar: Progress bar API for ComputerCraft
- -- By: Stekeblad
- -- License: MIT
- -- Version: 1.1
- -- Updated: 13 July 2019
- function help()
- local oldFgColor = term.getTextColor()
- term.setTextColor(colors.lightGray)
- print("usage for draw vertical/horrizontal bar:")
- print(" topLeftX: a whole number")
- print(" topLeftY: a whole number")
- print(" width: a possitive whole number")
- print(" height: a possitive whole number")
- print(" progress: a number between 0 and 100, can have decimals")
- print(" fillColor: the color to use for the bar in the progressbar")
- print(" drawBorder: true/false, optional, use true if you want a border around the progress bar")
- print(" borderColor: the color to use for the border, only needed if border is true")
- term.setTextColor(oldFgColor)
- error("",4)
- end
- local function isWholeNumber(num)
- return num ~= nil and type(num) == "number" and num == math.ceil(num)
- end
- local function myRound(num)
- if num % 1 < 0.5 then
- return math.floor(num)
- else
- return math.ceil(num)
- end
- end
- local function checkParams(topLeftX, topLeftY, width,
- height, progress, fillColor,
- drawBorder, borderColor)
- if topLeftX == nil or not isWholeNumber(topLeftX) then
- help()
- elseif topLeftY == nil or not isWholeNumber(topLeftY) then
- help()
- elseif width == nil or not isWholeNumber(width) or width < 1 then
- help()
- elseif height == nil or not isWholeNumber(height) or height < 1 then
- help()
- elseif progress == nil or not type(progress) == "number"
- or progress < 0 or progress > 100 then
- help()
- elseif fillColor == nil then
- help()
- elseif drawBorder == true and borderColor == nil then
- help()
- end
- end
- function drawVerticalBar(topLeftX, topLeftY, width,
- height, progress, fillColor,
- drawBorder, borderColor)
- checkParams(topLeftX, topLeftY, width,
- height, progress, fillColor,
- drawBorder, borderColor)
- local oldBG = term.getBackgroundColor()
- local pxToFill
- local border
- -- clear area
- paintutils.drawFilledBox(topLeftX, topLeftY,
- topLeftX+width-1, topLeftY+height-1,
- oldBG)
- -- draw border
- if drawBorder then
- border = 1
- pxToFill = myRound((height - 2) * (progress / 100))
- paintutils.drawBox(topLeftX, topLeftY,
- topLeftX+width-1, topLeftY+height-1,
- borderColor)
- else
- border = 0
- pxToFill = myRound((height) * (progress / 100))
- end
- -- draw progress bar
- paintutils.drawFilledBox(topLeftX+border,
- topLeftY+(height-pxToFill)-border,
- topLeftX+width-1-border, topLeftY+height-1-border,
- fillColor)
- term.setBackgroundColor(oldBG)
- term.setCursorPos(1, topLeftY+height+1)
- end
- function drawHorrizontalBar(topLeftX, topLeftY, width,
- height, progress, fillColor,
- drawBorder, borderColor)
- checkParams(topLeftX, topLeftY, width,
- height, progress, fillColor,
- drawBorder, borderColor)
- local oldBG = term.getBackgroundColor()
- local pxToFill
- local border
- -- clear area
- paintutils.drawFilledBox(topLeftX, topLeftY,
- topLeftX+width-1, topLeftY+height-1,
- oldBG)
- -- draw border
- if drawBorder then
- border = 1
- pxToFill = myRound((width - 2) * (progress / 100))
- paintutils.drawBox(topLeftX, topLeftY,
- topLeftX+width-1, topLeftY+height-1,
- borderColor)
- else
- border = 0
- pxToFill = myRound(width * (progress / 100))
- end
- -- draw progress bar
- paintutils.drawFilledBox(topLeftX+border, topLeftY+border,
- topLeftX+border+pxToFill-1, topLeftY+height-1-border,
- fillColor)
- term.setBackgroundColor(oldBG)
- term.setCursorPos(1, topLeftY+height+1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement