-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.lua
75 lines (67 loc) · 1.77 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
-- Copyright 2019, Mansour Moufid <[email protected]>
local button = {}
local love = require('love')
love.graphics = require('love.graphics')
local function norm(x, y)
local a, b = unpack(x)
local c, d = unpack(y)
return math.sqrt((c - a) ^ 2 + (d - b) ^ 2)
end
local Button = {
label = function (self)
return 'button'
end,
toggle = function (self)
end,
resize = function (self, w, h)
end,
update = function (self, dt)
end,
mousepressed = function (self, x, y)
if norm({x, y}, self.center) > self.radius then
return
end
self.border = self.border / 2
end,
mousereleased = function (self, x, y)
if norm({x, y}, self.center) > self.radius then
return
end
self.border = self.border * 2
self:toggle()
end,
state = function ()
return false
end,
}
function Button:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
o.center = o.center or {0, 0}
o.radius = o.radius or 40
o.border = o.border or 2
o.colours = o.colours or {
fg = {0, 0, 0},
bg = {1, 1, 1},
}
return o
end
function Button:draw()
local bg, fg = self.colours.bg, self.colours.fg
if self.state() then
bg, fg = fg, bg
end
local x, y = unpack(self.center)
love.graphics.setColor(fg)
love.graphics.circle('fill', x, y, self.radius)
love.graphics.setColor(bg)
love.graphics.circle('fill', x, y, self.radius - self.border)
love.graphics.setColor(fg)
local font = love.graphics.getFont()
local t = love.graphics.newText(font, self:label())
local w, h = t:getDimensions()
love.graphics.print(self:label(), x - w / 2, y - h / 2)
end
button.Button = Button
return button