-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.lua
More file actions
124 lines (76 loc) · 2.46 KB
/
button.lua
File metadata and controls
124 lines (76 loc) · 2.46 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
--Vykresli Button. Parametry:
-- X - X hodnota pozice
-- Y - Y hodnota pozice
-- W - sirka buttonu
-- H - vyska
-- text - text co bude v buttonu
-- action - funkce co se zavola po kliknuti
-- color - barva pozadi
-- hover - barva pri mouse hoveru
-- fontcolor - barva fontu
-- name - button name (unikatni pro kazdy button
font = love.graphics.newFont("fonts/bebasneueregular.ttf", 30)
local Button = {}
local index = 1
local buttons = {}
function Button.new(x,y,w,h,text,color,fontcolor,hover,action,name)
buttons[index] = {}
buttons[index][1] = w
buttons[index][2] = h
buttons[index][3] = x
buttons[index][4] = y
buttons[index][5] = action
buttons[index][6] = name
if color == nil then
color = "ffffff"
end
if fontcolor == nil then
fontcolor = "000000"
end
local newx, newy = love.mouse.getPosition()
if newx >= buttons[index][3] and newx <= buttons[index][3] + buttons[index][1] and newy >= buttons[index][4] and newy <= buttons[index][4] + buttons[index][2] then
color = hover
Button.draw(x,y,w,h,text,color,fontcolor)
else Button.draw(x,y,w,h,text,color,fontcolor)
end
checkindex(name)
end
function Button.update()
end
function hex(hex)
local redColor,greenColor,blueColor=hex:match('(..)(..)(..)')
redColor, greenColor, blueColor = tonumber(redColor, 16)/255, tonumber(greenColor, 16)/255, tonumber(blueColor, 16)/255
redColor, greenColor, blueColor = math.floor(redColor*100)/100, math.floor(greenColor*100)/100, math.floor(blueColor*100)/100
return redColor, greenColor, blueColor
end
function checkindex(name)
local buttoncheck = true
for i = 1, (index - 1) do
if buttons[i][6] == name then
buttoncheck = false
end
end
if buttoncheck then
index = index + 1
end
end
function Button.draw(x,y,w,h,text,color,fontcolor)
love.graphics.setColor(hex(color))
love.graphics.rectangle("fill", x, y, w, h )
love.graphics.setFont(font)
love.graphics.setColor(hex(fontcolor))
love.graphics.printf(text,x, y + h/2 - font:getHeight() / 2 , w,"center")
end
function love.mousepressed( x, y, button)
for i = 1, (index-1) do
if x >= buttons[i][3] and x <= buttons[i][3] + buttons[i][1] and y >= buttons[i][4] and y <= buttons[i][4] + buttons[i][2] then
--print(index)
Button.action(i)
end
end
end
function Button.action(i)
print(index)
return buttons[i][5]() or nil
end
return Button