ui_button.lua 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. local l_gfx = love.graphics
  2. -- The Button class definition
  3. -- highlights when mouse over it
  4. -- does stuff if clicked
  5. -- has caption
  6. -- somewhat cute
  7. Button = {}
  8. Button.__index = Button
  9. Button.ident = "ui_button"
  10. Button.caption = "Button"
  11. Button.name = "Button"
  12. Button.showBorder = false
  13. function Button:new(name)
  14. local self = {}
  15. setmetatable(self,Button)
  16. if name ~= nil then self.name = name end
  17. return self
  18. end
  19. setmetatable(Button,{__index = UIElement}) -- inherits from UIElement class, inherits its input methods and stuff
  20. function Button:draw()
  21. local cr,cg,cb,ca = love.graphics.getColor()
  22. if self:isMouseOver() == true and self.active == true then
  23. l_gfx.setColor(self.colorHighlight)
  24. else
  25. l_gfx.setColor(self.colorFill)
  26. end
  27. l_gfx.rectangle("fill",self.x,self.y,self.w,self.h)
  28. if self.showBorder == true then
  29. l_gfx.setColor(self.colorLine)
  30. l_gfx.rectangle("line",self.x,self.y,self.w,self.h)
  31. end
  32. if self.active == true then
  33. l_gfx.setColor(self.colorFont)
  34. else
  35. l_gfx.setColor(self.colorDisabledFill)
  36. end
  37. l_gfx.printf(self.caption,self.x,self.y+(self.h/2-7),self.w,"center")
  38. l_gfx.setColor(cr,cg,cb,ca)
  39. end