ui_progressbar.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. local l_gfx = love.graphics
  2. local format,min,floor = string.format,math.min,math.floor
  3. -- an updateable element which can show values, or can be updated manually without defining its update behavior
  4. ProgressBar = {}
  5. ProgressBar.__index = ProgressBar
  6. ProgressBar.ident = "ui_progressbar"
  7. ProgressBar.name = "ProgressBar"
  8. ProgressBar.caption = ""
  9. ProgressBar.displayVal = true
  10. ProgressBar.updateable = true
  11. ProgressBar.leftCaption = false
  12. ProgressBar.asPercentage = false
  13. ProgressBar.w = 128
  14. function ProgressBar:new()
  15. local self = {}
  16. setmetatable(self,ProgressBar)
  17. if name ~= nil then self.name = name end
  18. self.value = 0
  19. self.max = 100
  20. self.showCaption = true
  21. return self
  22. end
  23. setmetatable(ProgressBar,{__index = UIElement})
  24. function ProgressBar:draw()
  25. local cr,cg,cb,ca = love.graphics.getColor()
  26. l_gfx.setColor(self.colorFill)
  27. l_gfx.rectangle("fill",self.x,self.y,self.w,self.h)
  28. l_gfx.setColor(self.colorHighlight)
  29. local factor = min(1,self.value/self.max)
  30. l_gfx.rectangle("fill",self.x,self.y,self.w*factor,self.h)
  31. l_gfx.setColor(self.colorFont)
  32. if self.displayVal == true then
  33. if self.asPercentage == true then
  34. l_gfx.printf(floor(self.value/self.max*100).."%",self.x,self.y+(self.h/2-7),self.w,"center")
  35. else
  36. l_gfx.printf(self.value.."/"..self.max,self.x,self.y+(self.h/2-7),self.w,"center")
  37. end
  38. end
  39. if self.showCaption == true then
  40. if self.leftCaption == true then
  41. l_gfx.printf(self.caption,self.x-l_gfx:getFont():getWidth(self.caption),self.y+(self.h/2-7),self.w,"left")
  42. else
  43. l_gfx.printf(self.caption,self.x,self.y-16,self.w,"center")
  44. end
  45. end
  46. l_gfx.setColor(cr,cg,cb,ca)
  47. end