button.lua 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. local button = goo.newobject( 'button' )
  2. button.backgroundState = {
  3. ['off'] = 'backgroundColor',
  4. ['over'] = 'backgroundColorHover',
  5. ['click'] = 'backgroundColorClick'
  6. }
  7. button.textState = {
  8. ['off'] = 'textColor',
  9. ['over'] = 'textColorHover',
  10. ['click'] = 'textColorHover'
  11. }
  12. function button:init()
  13. self:setbounds( 10, 10, 50, 50 )
  14. self.text = "Click me please."
  15. end
  16. function button:update(dt)
  17. self.base.update( self )
  18. end
  19. function button:setcolor( type, state )
  20. local colorpack = goo.skin[ self.name ][ button[ type ][ state ] ]
  21. if colorpack then love.graphics.setColor( unpack(colorpack) ) end
  22. end
  23. function button:draw()
  24. local font = self:getskinvar( 'textFont' )
  25. local fontH = self:getskinvar( 'textFont' ):getHeight()
  26. love.graphics.setFont( font )
  27. self:setcolor( 'backgroundState', self.hoverstate )
  28. love.graphics.rectangle( 'fill', 0, 0, self.w, self.h )
  29. self:setcolor( 'textState', self.hoverstate )
  30. love.graphics.printf( self.text, 0, (self.h/2)-fontH, self.w, 'center' )
  31. end
  32. function button:sizetotext( padding )
  33. local padding = padding or 5
  34. local font = self:getskinvar( 'textFont' )
  35. self.w = font:getWidth( self.text ) + ( padding*5 )
  36. self.h = font:getHeight() + ( padding*5 )
  37. self:updatebounds()
  38. end
  39. function button:mousepressed(x,y,button)
  40. self.hoverstate = 'click'
  41. self:onClick( x, y, button )
  42. end
  43. function button:onClick() end