-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.lua
More file actions
107 lines (92 loc) · 3.55 KB
/
Copy pathbutton.lua
File metadata and controls
107 lines (92 loc) · 3.55 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
require 'widget'
Button = Widget:new()
Button.__index = Button
function Button:new(label)
local button = {
label = label,
font = nil,
color = { 1, 1, 1, 1 },
pressedColor = { 1, 1, 1, 1 },
roundness = 0,
roundnessRadius = 0,
labelY = 0,
isPressed = false,
onPressedHanders = {},
onReleasedHanders = {}
}
setmetatable(button, self)
return button
end
function Button:setLabel(label)
self.label = label
end
function Button:setFont(font)
self.font = font
end
function Button:setColor(color)
self.color = color
end
function Button:setPressedColor(color)
self.pressedColor = color
end
function Button:setRoundness(roundness)
if roundness < 0 then roundness = 0 end
if roundness > 1 then roundness = 1 end
self.roundness = roundness
end
function Button:onMousePressed(x, y)
if not Widget.onMousePressed(self, x, y) then return end
self.isPressed = love.mouse.isDown(1)
if self.isPressed then
for k, v in pairs(self.onPressedHanders) do
v()
end
end
end
function Button:onMouseReleased(x, y)
self.isPressed = false
if not Widget.onMousePressed(self, x, y) then return end
for k, v in pairs(self.onReleasedHanders) do
v()
end
end
function Button:addOnPressedHandler(handler)
self.onPressedHanders[handler] = handler
end
function Button:removedOnPressedHandler(handler)
self.onPressedHanders[handler] = nil
end
function Button:addOnReleasedHandler(handler)
self.onReleasedHanders[handler] = handler
end
function Button:removedOnReleasedHandler(handler)
self.onReleasedHanders[handler] = nil
end
function Button:setSize(size)
Widget.setSize(self, size)
self.roundnessRadius = size.height * 0.5 * self.roundness
self.labelY = (size.height - self.font:getHeight()) / 2
end
function Button:draw()
love.graphics.setColor(self.isPressed and self.pressedColor or self.color)
if self.roundness == 0 then
love.graphics.rectangle('line', self.position.x, self.position.y, self.size.width, self.size.height)
else
love.graphics.arc('line', 'open', self.position.x + self.roundnessRadius, self.position.y + self.roundnessRadius,
self.roundnessRadius, -math.pi, -math.pi / 2, 8)
love.graphics.arc('line', 'open', self.position.x + self.size.width - self.roundnessRadius, self.position.y + self.roundnessRadius,
self.roundnessRadius, -math.pi / 2, 0, 8)
love.graphics.arc('line', 'open', self.position.x + self.size.width - self.roundnessRadius, self.position.y + self.size.height - self.roundnessRadius,
self.roundnessRadius, 0, math.pi / 2, 8)
love.graphics.arc('line', 'open', self.position.x + self.roundnessRadius, self.position.y + self.size.height - self.roundnessRadius,
self.roundnessRadius, math.pi / 2, math.pi, 8)
love.graphics.line(self.position.x + self.roundnessRadius, self.position.y, self.position.x + self.size.width - self.roundnessRadius, self.position.y)
love.graphics.line(self.position.x + self.roundnessRadius, self.position.y + self.size.height,
self.position.x + self.size.width - self.roundnessRadius, self.position.y + self.size.height)
love.graphics.line(self.position.x, self.position.y + self.roundnessRadius, self.position.x, self.position.y + self.size.height - self.roundnessRadius)
love.graphics.line(self.position.x + self.size.width, self.position.y + self.roundnessRadius,
self.position.x + self.size.width, self.position.y + self.size.height - self.roundnessRadius)
end
love.graphics.setFont(self.font)
love.graphics.printf(self.label, self.position.x, self.position.y + self.labelY, self.size.width, 'center')
end