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