-
Notifications
You must be signed in to change notification settings - Fork 5
/
menubutton.lua
75 lines (63 loc) · 2.17 KB
/
menubutton.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
menubutton = class:new()
function menubutton:init(x, y, text, func)
self.x = x
self.y = y
self.text = text
self.func = func
self.value = 0
self.active = true
self.hidden = false
self.xmargin = 20
self.textyplus = -5
self.height = menufont:getHeight()
self.width = menufont:getWidth( self.text )-3
end
function menubutton:update(dt)
local x, y = mymousegetPosition()
y = y - menuoffset
x = x - menuoffsetx
if self:gethighlight(x, y) then
self.value = self.value + ((1-self.value)*4*dt+0.01*dt)
if self.value > 1 then
self.value = 1
end
else
self.value = self.value - (self.value*4*dt+0.1*dt)
if self.value < 0 then
self.value = 0
end
end
end
function menubutton:draw()
--get foreground color
local r, g, b = 190, 206, 248
local tr, tg, tb = unpack(getrainbowcolor(math.mod(rainbowi+.5, 1)))
r = r + (tr-r)*(self.value*.7+.3)
g = g + (tg-g)*(self.value*.7+.3)
b = b + (tb-b)*(self.value*.7+.3)
love.graphics.setFont(menufont)
mygraphicssetScissor(self.x-self.width/2-self.xmargin+menuoffsetx, self.y-self.height/2+menuoffset, (self.width+self.xmargin*2)*self.value, self.height)
love.graphics.setColor(r, g, b, fadecolor*255)
love.graphics.rectangle("fill", self.x-self.width/2-self.xmargin, self.y-self.height/2, (self.width+self.xmargin*2)*self.value, self.height)
love.graphics.setColor(0, 0, 0, fadecolor*255)
love.graphics.print(self.text, self.x-self.width/2, self.y-self.height/2+self.textyplus)
mygraphicssetScissor(self.x-self.width/2-self.xmargin+(self.width+self.xmargin*2)*self.value+menuoffsetx, self.y-self.height/2+menuoffset, self.width+self.xmargin*2, self.height)
love.graphics.setColor(r, g, b, fadecolor*255)
love.graphics.print(self.text, self.x-self.width/2, self.y-self.height/2+self.textyplus)
mygraphicssetScissor()
end
function menubutton:mousepressed(x, y, button)
if self.active and button == lbutton then
if self:gethighlight(x, y) then
self:func()
return true
end
end
end
function menubutton:gethighlight(x, y)
if x >= self.x-self.width/2-self.xmargin-10 and x < self.x+self.width/2+self.xmargin+10 and
y >= self.y-self.height/2-10 and y < self.y+self.height/2+10 then
return true
end
return false
end