-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbutton.lua
87 lines (79 loc) · 2.37 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
local Button = {
position = lovr.math.newVec3(0,0,0),
size = lovr.math.newVec3(0.4, 0.2, 0.1),
onPressed = function() end, -- button is pressed
onReleased = function() end, -- button is lifted
onActuate = function() end, -- button is lifted with cursor still inside
label = "Untitled",
fraction = 0.0,
isToggle = false,
font = lovr.graphics.newFont('assets/fonts/Inter.ttf', 32)
}
setmetatable(Button, {__index=letters.Node})
local Button_mt = {
__index = Button
}
function Button:new(o)
o = letters.Node.new(self, o)
setmetatable(o, Button_mt)
return o
end
function Button:draw()
local buttonPos = self.position + lovr.math.vec3(0,0,(1.1-self.fraction) * self.size.z)
-- draw the base of the button
lovr.graphics.setShader()
lovr.graphics.setColor(0.3, 0.3, 0.4, 1)
local x, y, z = self.position:unpack()
local w, h, d = self.size:unpack()
-- unpacks are deliberate to workaround bug in... .... luajit? lovr? lodr? I dunno
lovr.graphics.box('fill', x, y, z, w, h, d)
-- draw the actual key cap
lovr.graphics.setColor(0.5, 0.5, self.highlighted and 0.7 or 0.6, 1)
x, y, z = buttonPos:unpack()
w, h, d = lovr.math.vec3(self.size.x*0.8, self.size.y*0.8 , self.size.z):unpack()
lovr.graphics.box('fill', x, y, z, w, h, d)
local b = self.highlighted and 0.7 or 0.9
lovr.graphics.setColor(b, b, b, 1)
lovr.graphics.plane('fill', x, y, z+d/2+0.001, w, h)
-- draw the text
lovr.graphics.setShader()
lovr.graphics.setFont(self.font)
lovr.graphics.setColor(0.2, 0.2, 0.2, 1)
x, y, z = (buttonPos + lovr.math.vec3(0,0,self.size.z/2 + 0.002)):unpack()
lovr.graphics.print(self.label, x, y, z, 0.04)
end
function Button:highlight()
self.highlighted = true
end
function Button:dehighlight()
self.highlighted = false
end
function Button:select()
if self.isToggle == false then
self.selected = true
self.fraction = 1.0
self.onPressed(self)
end
end
function Button:deselect()
if self.isToggle == false then
self.selected = false
self.onReleased(self)
else
self.selected = not self.selected
if self.selected then
self.onPressed(self)
else
self.onReleased(self)
end
end
self.fraction = self.selected and 1.0 or 0.0
end
function Button:setSelected(newValue)
self.selected = newValue
self.fraction = self.selected and 1.0 or 0.0
end
function Button:actuate()
self.onActuate(self)
end
return Button