Guest User

test

a guest
Dec 4th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. os = require("os")
  2. term = require("term")
  3. component = require("component")
  4.  
  5. gpu = component.gpu
  6.  
  7. function throwError(errorMessage)
  8.   gpu.setResolution(gpu.maxResolution())
  9.   gpu.setForeground(0xff0000)
  10.   gpu.setBackground(0x000000)
  11.   term.clear()
  12.   print("The program ran into a serious error during execution.")
  13.   print(errorMessage)
  14.   gpu.setForeground(0xffffff)
  15.   os.exit()
  16. end
  17.  
  18. function addRect(x1,y1,x2,y2,color)
  19.   temp = gpu.getBackground()
  20.   gpu.setBackground(color)
  21.   gpu.fill(x1,y1,x2-x1+1,y2-y1+1," ")
  22.   gpu.setBackground(temp)
  23. end
  24.  
  25. function addProgressBar(x1,y1,x2,y2,sideWidth,sideHeight,borderColor,filledBarColor,emptyBarColor,textColor,headerText,filledPercentage,isSideways,isInverted)
  26.   isSideways = isSideways or false
  27.   isInverted = isInverted or false
  28.   if type(x1)~="number" then throwError("addProgressBar() argument 1 (x1) type int expected, "..type(x1).." found") end
  29.   if type(x2)~="number" then throwError("addProgressBar() argument 3 (x2) type int expected, "..type(x2).." found") end
  30.   if type(y1)~="number" then throwError("addProgressBar() argument 2 (y1) type int expected, "..type(y1).." found") end
  31.   if type(y2)~="number" then throwError("addProgressBar() argument 4 (y2) type int expected, "..type(y2).." found") end
  32.   if type(sideWidth)~="number" then throwError("addProgressBar() argument 5 (sideWidth) type int expected, "..type(sideWidth).." found") end
  33.   if type(sideHeight)~="number" then throwError("addProgressBar() argument 6 (sideHeight) type int expected, "..type(sideHeight).." found") end
  34.   if type(filledPercentage)~="number" then throwError("addProgressBar() argument 11 (filledPercentage) type float expected, "..type(filledPercentage).." found") end
  35.  
  36.   if isSideways == false then
  37.     addRect(x1,y1,x2,y2,borderColor)
  38.     addRect(x1+sideWidth,y1+sideHeight,x2-sideWidth,y2-sideHeight,emptyBarColor)
  39.     barSize = (y2-y1-2*sideHeight)
  40.     pixelsFilled = math.floor(filledPercentage*barSize+0.5)
  41.     if isInverted == false then
  42.       addRect(x1+sideWidth,y2-sideHeight-pixelsFilled,x2-sideWidth,y2-sideHeight,filledBarColor)
  43.     elseif isInverted == true then
  44.       addRect(x1+sideWidth,y1+sideHeight,x2-sideWidth,y1+sideHeight+pixelsFilled,filledBarColor)
  45.     else
  46.       throwError("addProgressBar() argument 13 (isInverted) type boolean expected, "..type(isInverted).." found")
  47.     end
  48.   elseif isSideways == true then
  49.     addRect(x1,y1,x2,y2,borderColor)
  50.     addRect(x1+sideWidth,y1+sideHeight,x2-sideWidth,y2-sideHeight,emptyBarColor)
  51.     barSize = (x2-x1-2*sideWidth)
  52.     pixelsFilled = math.floor(filledPercentage*barSize+0.5)
  53.     if isInverted == false then
  54.       addRect(x1+sideWidth,y1+sideHeight,x1+sideWidth+pixelsFilled,y2-sideHeight,filledBarColor)
  55.     elseif isInverted == true then
  56.       addRect(x2-sideWidth-pixelsFilled,y1+sideHeight,x2-sideWidth,y2-sideHeight,filledBarColor)
  57.     else
  58.       throwError("addProgressBar() argument 13 (isInverted) type boolean expected, "..type(isInverted).." found")
  59.     end
  60.   else
  61.     throwError("addProgressBar() argument 12 (isSideways) type boolean expected, "..type(isSideways).." found")
  62.   end
  63.   if sideHeight>0 then
  64.     col = {f = gpu.getForeground(),b = gpu.getBackground()}
  65.     centerX = math.ceil((x1+x2)/2)
  66.     centerY = math.floor(y1+sideHeight/2)
  67.     gpu.setForeground(textColor)
  68.     gpu.setBackground(borderColor)
  69.     gpu.set(centerX-(string.len(tostring(headerText))/2),centerY,tostring(headerText))
  70.     gpu.setForeground(col.f)
  71.     gpu.setBackground(col.b)
  72.     centerX = nil
  73.     centerY = nil
  74.     col = nil
  75.   end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment