-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.lua
72 lines (59 loc) · 1.84 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
60
61
62
63
64
65
66
67
68
69
70
71
72
Button = {}
Button.new = function (x, y, w, h)
local self = {}
self.x = x
self.y = y
self.w = w
self.h = h
self.disabled = false
self.hit = false
self.hover = 0
self.title = ""
self.text_scale_x = 1
self.text_scale_y = 1
self.text_color = {r = 0, g = 0, b = 0}
self.callback = function ()
end
self.onMousePress = function (x, y)
if not self.disabled and x > self.x and x < self.x + self.w and y > self.y and y < self.y + self.h then
self.hit = true
end
end
self.onMouseRelease = function (x, y)
if not self.disabled and x > self.x and x < self.x + self.w and y > self.y and y < self.y + self.h then
self.hit = false
self.callback()
end
end
self.update = function (dt)
self.hover = math.max(self.hover - 2 * dt, 0)
local x, y = love.mouse.getPosition()
if x > self.x and x < self.x + self.w and y > self.y and y < self.y + self.h then
self.hover = 1
end
end
self.draw = function ()
if self.disabled then
love.graphics.setColor(128, 128, 128)
love.graphics.rectangle("fill", x, y, w, h)
love.graphics.setColor(self.text_color.r * 0.5, self.text_color.g * 0.5, self.text_color.b * 0.5)
else
if self.hit then
love.graphics.setColor(128, 128, 128)
love.graphics.rectangle("fill", x, y, w, h)
love.graphics.setColor(204, 204, 204)
love.graphics.rectangle("fill", x+1, y+1, w-2, h-2)
else
local b = self.hover * 0.2 + 0.8
love.graphics.setColor(160 * b, 160 * b, 160 * b)
love.graphics.rectangle("fill", x, y, w, h)
love.graphics.setColor(255 * b, 255 * b, 255 * b)
love.graphics.rectangle("fill", x+1, y+1, w-2, h-2)
end
love.graphics.setColor(self.text_color.r, self.text_color.g, self.text_color.b)
end
love.graphics.printf(self.title, x, y + h * 0.5 - 8 * self.text_scale_y, w, "center", 0, self.text_scale_x, self.text_scale_y)
end
return self
end
return Button