summaryrefslogtreecommitdiff
path: root/client/msgbox.lua
blob: 720bfab0ef39dc60e3310ffa7d52e3e6616c6c1e (plain)
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
local font = love.graphics.getFont()


local msgs = {}

local function add(str)
	table.insert(msgs, 1, {text=love.graphics.newText(font,str),time=10})
end

local margin_bottom = 70

local function draw()
	if #msgs == 0 then return end

	local W,H = love.graphics.getDimensions()
	local maxw = 0
	local sumh = 0
	for _,msg in ipairs(msgs) do
		local text = msg.text
		maxw = math.max(maxw,text:getWidth())
		sumh = sumh + text:getHeight()
	end
	love.graphics.setColor(0,0,0,0.8)
	love.graphics.rectangle("fill",0,H-sumh-margin_bottom,maxw,sumh)

	love.graphics.setColor(1,1,1)
	local cury = H
	for _,msg in ipairs(msgs) do
		local text = msg.text
		local h = text:getHeight()
		love.graphics.draw(text,0,cury-h-margin_bottom)
		cury = cury - h
	end
end

local function update(dt)
	for ix = #msgs,1,-1 do
		local m = msgs[ix]
		m.time = m.time - dt
		if m.time <= 0 then
			msgs[ix] = nil
		end
	end
end




return {
	draw=draw,
	update=update,
	add=add,
}