Advertisement
Pinkishu

bar

Jan 19th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.57 KB | None | 0 0
  1. bar = {}
  2.  
  3. function bar.new()
  4.     local newBar = {}
  5.     newBar.x = 0
  6.     newBar.y = 0
  7.     newBar.w = 0
  8.     newBar.h = 0
  9.     newBar.parent = nil
  10.     newBar.value = 0
  11.     newBar.highValue = 0
  12.     newBar.color = colors.white
  13.     setmetatable(newBar,{__index=bar})
  14.     return newBar
  15. end
  16.  
  17. function bar:setPos(x,y)
  18.     self.x = x
  19.     self.y = y
  20. end
  21.  
  22. function bar:getPos()
  23.     return self.x,self.y
  24. end
  25.  
  26. function bar:setParent(newParent)
  27.     self.parent = newParent
  28. end
  29.  
  30. function bar:getParent()
  31.     return self.parent
  32. end
  33.  
  34. function bar:setSize(w,h)
  35.     self.w = w
  36.     self.h = h
  37. end
  38.  
  39. function bar:getSize()
  40.     return self.w,self.h
  41. end
  42.  
  43. function bar:setValue(value)
  44.     self.value = value
  45. end
  46.  
  47. function bar:getValue()
  48.     return self.value
  49. end
  50.  
  51. function bar:setHighValue(hVal)
  52.     self.highValue = hVal
  53. end
  54.  
  55. function bar:getHighValue()
  56.     return self.highValue
  57. end
  58.  
  59. function bar:getColor()
  60.     if type(self.color) == "number" then return self.color
  61.     elseif type(self.color) == "table" then return self.color:getColor()
  62.     else return self:color() end
  63. end
  64.  
  65. function bar:setColor(newColor)
  66.     self.color = newColor
  67. end
  68.  
  69. function bar:draw()
  70.     local perc = self.value / self.highValue
  71.     local dH = math.floor(perc * self.h)
  72.     local monitor = self.parent:getMonitor()
  73.     local pW,pH = self.parent:getSize()
  74.     local pX,pY = self.parent:getPos()
  75.     monitor.setBackgroundColor(self:getColor())
  76.     for i=1,dH,1 do
  77.         monitor.setCursorPos(self.x+pX,self.y+pY+pH-i)
  78.         monitor.write(string.rep(" ",self.w))
  79.  
  80.     end
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement