-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextbutton.lua
More file actions
119 lines (71 loc) · 2.33 KB
/
textbutton.lua
File metadata and controls
119 lines (71 loc) · 2.33 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
--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 (hex)
-- fontcolor - barva fontu (hex)
-- hover - barva pri hoveru (hex)
local Textfont = love.graphics.newFont("fonts/bebasneueregular.ttf", 60)
local TextButton = {}
local index = 1
local buttons = {}
function TextButton.new(x,y,text,color,hover,action,name,state)
love.graphics.setFont(Textfont)
buttons[index] = {}
buttons[index][1] = Textfont:getWidth(text)
buttons[index][2] = Textfont:getHeight()
buttons[index][3] = x
buttons[index][4] = y
buttons[index][5] = action
buttons[index][6] = name
buttons[index][7] = state
if color == nil then
color = "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
TextButton.draw(x,y,text,color)
else TextButton.draw(x,y,text,color)
end
checkindex(name)
end
function TextButton.update()
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 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 TextButton.draw(x,y,text,color)
love.graphics.setColor(hex(color))
love.graphics.print(text,x,y)
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)
TextButton.action(i)
end
end
end
function TextButton.action(i)
--print(index)
return buttons[i][5]() or nil
end
return TextButton