-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlovelyToasts.lua
149 lines (122 loc) · 4.4 KB
/
lovelyToasts.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
--[[
Author: Lucy van Sandwijk
Website: https://loucee.dev
GitHub: https://github.com/Loucee/Lovely-Toasts
]]
local yOffset = 15
local lovelyToasts = {
canvasSize = { },
style = {
font = love.graphics.newFont(16),
textColor = { 1, 1, 1, 1},
backgroundColor = { 0, 0, 0, 0.5 },
paddingLR = 25,
paddingTB = 10
},
options = {
tapToDismiss = false,
queueEnabled = false,
animationDuration = 0.3
}
}
_toasts = { }
--------------------------------------------------------------------------------
function lovelyToasts.update(dt)
if #_toasts > 0 then
local current = _toasts[1]
-- Update toast time
current._timePassed = current._timePassed + dt
-- Update alpha and y offset
if (current._timePassed <= lovelyToasts.options.animationDuration) then
current._alpha = math.min(100, current._alpha + (100 / lovelyToasts.options.animationDuration * dt))
elseif (current._timePassed >= current.duration - lovelyToasts.options.animationDuration) then
current._alpha = math.max(0, current._alpha - (100 / lovelyToasts.options.animationDuration * dt))
end
current._yOffset = math.max(0, current._yOffset - (yOffset / lovelyToasts.options.animationDuration * dt))
-- Remove toast when duration ended
if (current._timePassed >= current.duration) then
table.remove(_toasts, 1)
end
end
end
function lovelyToasts.draw()
if #_toasts > 0 then
local screenW = lovelyToasts.canvasSize[1] or love.graphics.getWidth()
local current = _toasts[1]
-- Store current font and color to restore later
local r, g, b, a = love.graphics.getColor()
local font = love.graphics.getFont()
local textWidth = lovelyToasts.style.font:getWidth(current.text)
local textHeight = lovelyToasts.style.font:getHeight()
local textX = (screenW / 2) - (textWidth / 2)
local textY = lovelyToasts._yForPosition(current.position) - (textHeight / 2) + current._yOffset
-- Draw toast background
local bgR, bgG, bgB, bgA = unpack(lovelyToasts.style.backgroundColor)
love.graphics.setColor(bgR, bgG, bgB, (bgA or 0.5) * (current._alpha / 100))
love.graphics.rectangle("fill",
textX - lovelyToasts.style.paddingLR,
textY - lovelyToasts.style.paddingTB,
textWidth + (lovelyToasts.style.paddingLR * 2),
textHeight + (lovelyToasts.style.paddingTB * 2),
10, 10, 1000
)
-- Draw toast title
local tR, tG, tB, tA = unpack(lovelyToasts.style.textColor)
love.graphics.setColor(tR, tG, tB, (tA or 1) * (current._alpha / 100))
love.graphics.setFont(lovelyToasts.style.font)
love.graphics.print(current.text, textX, textY)
-- Restore color and font
love.graphics.setColor(r, g, b, a)
love.graphics.setFont(font)
end
end
function lovelyToasts.mousereleased(x, y, button)
if button == 1 then
lovelyToasts._dismissOnTouch(x, y)
end
end
function lovelyToasts.touchreleased(id, x, y, dx, dy, pressure)
lovelyToasts._dismissOnTouch(x, y)
end
--------------------------------------------------------------------------------
function lovelyToasts.show(text, duration, position)
local t = {
_timePassed = 0,
_alpha = 0,
_yOffset = yOffset,
text = text,
duration = (duration or 3) + (lovelyToasts.options.animationDuration * 2),
position = position or "bottom"
}
if (lovelyToasts.options.queueEnabled) then
table.insert(_toasts, t)
else
_toasts = { t }
end
end
--------------------------------------------------------------------------------
function lovelyToasts._yForPosition(pos)
local screenH = lovelyToasts.canvasSize[2] or love.graphics.getHeight()
if (pos == "bottom") then
return screenH * 0.8
elseif (pos == "top") then
return screenH * 0.2
elseif (pos == "middle") then
return screenH * 0.5
end
return pos
end
function lovelyToasts._dismissOnTouch(x, y)
if #_toasts > 0 and lovelyToasts.options.tapToDismiss then
local current = _toasts[1]
local toastWidth = lovelyToasts.style.font:getWidth(current.text) + (lovelyToasts.style.paddingLR * 2)
local toastHeight = lovelyToasts.style.font:getHeight() + (lovelyToasts.style.paddingTB * 2)
local toastX = (love.graphics.getWidth() / 2) - (toastWidth / 2) - lovelyToasts.style.paddingLR
local toastY = lovelyToasts._yForPosition(current.position) - (toastHeight / 2) - lovelyToasts.style.paddingTB
if (x > toastX) and (x < toastX + toastWidth) and (y > toastY) and (y < toastY + toastHeight) then
table.remove(_toasts, 1)
end
end
end
--------------------------------------------------------------------------------
return lovelyToasts