-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.lua
85 lines (72 loc) · 1.85 KB
/
message.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
local fontpool = require('fontpool')
local color = require('hug.color')
local textrender = require('textrender')
local message = {}
local mt = { __index = message }
local defaultFont = fontpool:get(18)
local setmetatable = setmetatable
local graphics = love.graphics
local setColor, setFont = graphics.setColor, graphics.setFont
local rectangle = graphics.rectangle
local mathex = require('hug.extensions.math')
local lerp = mathex.lerp
function message.new(text, duration, x, y)
local instance = {
-- now set below
--text = text or '',
duration = duration or 0,
--color = color.new(255, 255, 255),
x = x or 0,
y = y or 0,
dx = x or 0,
dy = y or 0,
w = 0,
h = 0,
font = defaultFont
}
message.setText(instance, text or '')
message.setColor(instance, color.fromrgba(1, 1, 1))
return setmetatable(instance, mt)
end
function message:update(dt)
self.duration = self.duration - dt
self.x = lerp(self.x, self.dx, dt * 10)
self.y = lerp(self.y, self.dy, dt * 10)
end
function message:draw()
setColor(self.backcolor)
-- inflate rectangle 2px on each side
rectangle('fill', self.x - 2, self.y - 2, self.w + 4, self.h + 4)
setColor(self.forecolor)
setFont(self.font)
textrender.print(self.text, self.x, self.y)
end
function message:visible()
return self.duration > 0
end
function message:setDestination(x, y)
self.dx = x
self.dy = y
end
function message:getWidth()
return self.font:getWidth(self.text)
end
function message:getHeight()
return self.font:getHeight()
end
function message:setText(text)
self.w = self.font:getWidth(text)
self.h = self.font:getHeight()
self.text = text
end
function message:setColor(fg, bg)
self.forecolor = fg
if bg ~= nil then
self.backcolor = bg
else
self.backcolor = -self.forecolor
end
-- add an alpha channel
self.backcolor[4] = 0.5
end
return message