ui_label.lua 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. local l_gfx = love.graphics
  2. -- Your simple label element
  3. -- can wrap words
  4. -- can align words
  5. Label = {}
  6. Label.__index = Label
  7. Label.ident = "ui_label"
  8. Label.caption = "Label"
  9. Label.align = "center"
  10. Label.wrap = true
  11. Label.name = "Label"
  12. function Label:new(name)
  13. local self = {}
  14. setmetatable(self,Label)
  15. if name ~= nil then self.name = name end
  16. return self
  17. end
  18. setmetatable(Label,{__index = UIElement})
  19. function Label:draw()
  20. local cr,cg,cb,ca = love.graphics.getColor()
  21. l_gfx.setColor(self.colorFont)
  22. if self.wrap == true then
  23. l_gfx.printf(self.caption,self.x,self.y,self.w,self.align)
  24. else
  25. l_gfx.print(self.caption,self.x,self.y)
  26. end
  27. l_gfx.setColor(cr,cg,cb,ca)
  28. end
  29. -- This label is updateable
  30. -- useful for displaying varying information, e.g. FPS:
  31. --[[
  32. local fpslabel = RefreshingLabel:new("L_FPS")
  33. fpslabel:update(dt)
  34. fpslabel.caption = "FPS:"..love.timer.getFPS()
  35. end
  36. ]]
  37. RefreshingLabel = {}
  38. RefreshingLabel.__index = RefreshingLabel
  39. RefreshingLabel.ident = "ui_refreshinglabel"
  40. RefreshingLabel.name = "RefreshingLabel"
  41. RefreshingLabel.updateable = true
  42. function RefreshingLabel:new(name)
  43. local self = {}
  44. setmetatable(self,RefreshingLabel)
  45. if name ~= nil then self.name = name end
  46. return self
  47. end
  48. setmetatable(RefreshingLabel,{__index = Label})
  49. TextBox = {}
  50. TextBox.__index = TextBox
  51. TextBox.name = "TextBox"
  52. TextBox.ident = "ui_textbox"
  53. TextBox.showBorder = true
  54. TextBox.fill = true
  55. function TextBox:new(name)
  56. local self = setmetatable({},TextBox)
  57. self.name = name or self.name
  58. return self
  59. end
  60. setmetatable(TextBox,{__index = Label})
  61. function TextBox:alignBox()
  62. --local w,l = l_gfx.getFont():getWrap(self.caption,self.w)
  63. --self.h = l_gfx.getFont():getHeight()*l
  64. end
  65. function TextBox:draw()
  66. local cr,cg,cb,ca = love.graphics.getColor()
  67. if self.showBorder == true then
  68. l_gfx.setColor(self.colorLine)
  69. l_gfx.rectangle("line",self.x,self.y,self.w,self.h)
  70. end
  71. if self.fill == true then
  72. l_gfx.setColor(self.colorFill)
  73. l_gfx.rectangle("fill",self.x,self.y,self.w,self.h)
  74. end
  75. l_gfx.setColor(self.colorFont)
  76. l_gfx.printf(self.caption,self.x,self.y,self.w,self.align)
  77. l_gfx.setColor(cr,cg,cb,ca)
  78. end