button.lua 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. -- BUTTON
  2. goo.button = class('goo button', goo.object)
  3. function goo.button:initialize( parent )
  4. super.initialize(self,parent)
  5. self.text = "button"
  6. self.borderStyle = 'line'
  7. self.backgroundColor = {0,0,0,255}
  8. self.borderColor = {255,255,255,255}
  9. self.textColor = {255,255,255,255}
  10. self.spacing = 5
  11. self.border = true
  12. self.background = true
  13. end
  14. function goo.button:draw()
  15. if self.background then
  16. self:setColor( self.backgroundColor )
  17. love.graphics.rectangle( 'fill', 0, 0, self.w , self.h )
  18. end
  19. if self.border then
  20. love.graphics.setLine( 1, 'rough' )
  21. self:setColor( self.borderColor )
  22. love.graphics.rectangle( 'line', 0, 0, self.w+2, self.h )
  23. end
  24. self:setColor( self.textColor )
  25. love.graphics.setFont( self.style.textFont )
  26. local fontW,fontH = self.style.textFont:getWidth(self.text or ''), self.style.textFont:getHeight()
  27. local ypos = ((self.h - fontH)/2)+(fontH*0.8)
  28. local xpos = ((self.w - fontW)/2)
  29. love.graphics.print( self.text, xpos, ypos )
  30. end
  31. function goo.button:enterHover()
  32. self.backgroundColor = self.style.backgroundColorHover
  33. self.borderColor = self.style.borderColorHover
  34. self.textColor = self.style.textColorHover
  35. end
  36. function goo.button:exitHover()
  37. self.backgroundColor = self.style.backgroundColor
  38. self.borderColor = self.style.borderColor
  39. self.textColor = self.style.textColor
  40. end
  41. function goo.button:mousepressed(x,y,button)
  42. if self.onClick then self:onClick(button) end
  43. self:updateBounds( 'children', self.updateBounds )
  44. end
  45. function goo.button:setText( text )
  46. self.text = text or ''
  47. end
  48. function goo.button:sizeToText( padding )
  49. local padding = padding or 5
  50. local _font = self.style.textFont or love.graphics.getFont()
  51. self.w = _font:getWidth(self.text or '') + (padding*2)
  52. self.h = _font:getHeight() + (padding*2)
  53. self:updateBounds()
  54. end
  55. goo.button:getterSetter('border')
  56. goo.button:getterSetter('background')
  57. return goo.button