-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbutton.lua
More file actions
69 lines (40 loc) · 940 Bytes
/
button.lua
File metadata and controls
69 lines (40 loc) · 940 Bytes
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
require("love")
require("common")
local gr = love.graphics
Button = {}
local Button_mt = {
__index = Button,
}
function Button.new(title, x, y, w, h)
local o = setmetatable({}, Button_mt)
o.title = title
o.x = x
o.y = y
o.w = w
o.h = h
o.bgColor = { 0.5, 0.1, 0. }
return o
end
function Button:draw()
local prevColor = { gr.getColor() }
local prevFont = gr.getFont()
love.graphics.setColor(self.bgColor)
love.graphics.rectangle("fill", self.x, self.y, self.w, self.h)
if self.font then
gr.setFont(self.font)
end
gr.setColor(prevColor)
gr.setFont(prevFont)
end
function Button:update(_)
local mx, my = love.mouse.getPosition()
if pointInRect(mx, my, self.x, self.y, self.w, self.h) then
print("hovered")
end
end
function Button:mouseReleased(_, _, _tn)
if self.onMouseReleased then
self:onMouseReleased()
end
end
return Button