-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutt.lua
112 lines (104 loc) · 2.48 KB
/
butt.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
local butt = {}
function butt:load()
butt.table = {}
return butt
end
function butt:basicButt()
return {
txt = "",
aligne = "none",
x = love.window.getWidth() / 2 - (75 / 2),
y = 200,
background = {color = {r = 255, g = 255, b = 255, a = 255}},
txtColor = {r = 0, g = 0, b = 0, a = 255},
width = 150,
height = 50,
callback = nil,
paddingTop = 10,
paddingRight = 0,
paddingBottom = 0,
paddingLeft = 0,
hover = {
background = {color = {r = 255, g = 255, b = 255, a = 170}},
txtColor = {r = 255, g = 255, b = 255, a = 255}
}
}
end
function butt.new(buttons, param)
local newBt = butt:basicButt()
if param ~= nil then
for index, value in pairs(param) do
newBt[index] = value
end
end
table.insert(buttons, newBt)
return buttons
end
function butt.mousepressed(buttons, x, y, bt)
if not bt == 'l' then
return 0
end
for index, button in ipairs(buttons) do
if love.mouse.getX() >= button.x
and love.mouse.getX() <= button.x + button.width
and love.mouse.getY() >= button.y
and love.mouse.getY() <= button.y + button.height then
button.callback(button)
end
end
end
function butt.draw(buttons)
love.graphics.setBackgroundColor(42,42,42)
love.graphics.setFont(env.littleFont)
for index, bt in ipairs(buttons) do
-- Text aligne
if bt.aligne == "center" then
bt.x = love.window.getWidth() / 2 - (bt.width / 2)
elseif bt.aligne == "right" then
bt.x = love.window.getWidth() - bt.width
elseif bt.aligne == "left" then
bt.x = 0
end
-- Background & font color
if love.mouse.getX() <= bt.x
or love.mouse.getX() >= bt.x + bt.width
or love.mouse.getY() <= bt.y
or love.mouse.getY() >= bt.y + bt.height
then
love.graphics.setColor(
bt.background.color.r,
bt.background.color.g,
bt.background.color.b,
bt.background.color.a
)
love.graphics.rectangle("fill", bt.x, bt.y, bt.width, bt.height);
love.graphics.setColor(
bt.txtColor.r,
bt.txtColor.g,
bt.txtColor.b,
bt.txtColor.a
)
else -- Mouse is hover
love.graphics.setColor(
bt.hover.background.color.r,
bt.hover.background.color.g,
bt.hover.background.color.b,
bt.hover.background.color.a
)
love.graphics.rectangle("fill", bt.x, bt.y, bt.width, bt.height);
love.graphics.setColor(
bt.hover.txtColor.r,
bt.hover.txtColor.g,
bt.hover.txtColor.b,
bt.hover.txtColor.a
)
end
-- Txt
love.graphics.print(
bt.txt,
bt.x + bt.paddingLeft,
bt.y + bt.paddingTop
)
end
end
return butt