-
Notifications
You must be signed in to change notification settings - Fork 17
/
notice.lua
73 lines (58 loc) · 2 KB
/
notice.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
notice = {}
notice.red = {1, 0.5, 0.5}
notice.white = {1, 1, 1}
notice.notices = {}
notice.duration = 5 --seconds
notice.fadetime = 0.5
function notice.new(text, color, duration)
local duration = duration or notice.duration
local text = text or ""
local color = color or notice.white
table.insert(notice.notices, {text=text:lower(), color=color, life=duration, duration=duration})
end
function notice.update(dt)
for i = #notice.notices, 1, -1 do
local v = notice.notices[i]
v.life = v.life - dt
if v.life <= 0 then
table.remove(notice.notices, i)
end
end
end
function notice.draw()
local y = 0
for i = #notice.notices, 1, -1 do
local v = notice.notices[i]
--get width by finding longest line
local split = v.text:split("|")
local longest = #split[1]
for i = 2, #split do
if #split[i] > longest then
longest = #split[i]
end
end
local height = #split*10+3
actualy = notice.gety(y, v.life, height, v.duration)
local targetrect = {width*16 - longest*8-5, actualy, longest*8+5, height}
local scissor = {(width*16 - longest*8-5)*scale, y*scale, (longest*8+5)*scale, (actualy-y+height)*scale}
love.graphics.setScissor(unpack(scissor))
love.graphics.setColor(0, 0, 0, 0.8)
love.graphics.rectangle("fill", targetrect[1]*scale, targetrect[2]*scale, targetrect[3]*scale, targetrect[4]*scale)
love.graphics.setColor(1, 1, 1, 1)
drawrectangle(targetrect[1]+1, targetrect[2]+1, targetrect[3]-2, targetrect[4]-2)
love.graphics.setColor(v.color)
properprint(v.text, (targetrect[1]+2)*scale, (actualy+3)*scale)
y = actualy+height
love.graphics.setScissor()
end
love.graphics.setColor(1, 1, 1)
end
function notice.gety(y, life, height, duration)
if life > duration-notice.fadetime then
return y - height*((life-(duration-notice.fadetime))/notice.fadetime)^2
elseif life < notice.fadetime then
return y - height*((notice.fadetime-life)/notice.fadetime)^2
else
return y
end
end