fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosAllow notification calls to have formatted text. Safely parse our config file incase it ever gets corrupted for whatever reason. Revert our writing of a new config if it's out of date.. (Was getting a corrupted config file during testing) Don't even bother parsing very old config values anymore. If someone is upgrading that far, I want them to look at the new settings available to them.8baa412

v2.1.1a
2.8 KiB130 linesraw
1local graphics = love.graphics
2local newImage = graphics.newImage
3local newFont = graphics.newFont
4local timer = love.timer
5local color = require("util.color")
6
7local log = require("log")
8
9local notification = {
10	messages = {},
11	font = newFont("fonts/melee-bold.otf", 12),
12	padding = 8,
13}
14
15local dummy = function(height, fade) end
16
17function notification.add(height, length, fade, draw)
18	height = height or 16
19	length = length or 3
20	fade = fade or 0.75
21
22	local now = timer.getTime()
23
24	table.insert(notification.messages, {
25		time = now,
26		length = length,
27		fade = fade,
28		finish = now + length + fade,
29
30		height = height,
31
32		draw = draw or dummy,
33
34		lerp = {},
35	})
36end
37
38function notification.update(x, y)
39	local now = timer.getTime()
40
41	local prevh = 0
42
43	for k, message in pairs(notification.messages) do
44		if (message.finish >= now) then
45			x = x * 0.5 + (message.lerp.x or x) * 0.5
46			y = y * 0.5 + (message.lerp.y or y) * 0.5
47			y = y + prevh/2 + notification.padding/2
48
49			message.lerp.x = x
50			message.lerp.y = y
51
52			prevh = message.height
53		end
54	end
55	
56	for k, message in pairs(notification.messages) do
57		if (message.finish >= now) then
58			return
59		end
60	end
61	
62	notification.messages = {}
63end
64
65function notification.draw()
66	local now = timer.getTime()
67	for k, message in pairs(notification.messages) do
68
69		local fade = 1
70
71		if now >= message.time + message.length then
72			fade = 1-((now - message.time - message.length)/message.fade)
73		end
74
75		local x, y = message.lerp.x, message.lerp.y
76
77		if x and y then
78			graphics.push("transform")
79			graphics.translate(x, y)
80			graphics.setFont(notification.font)
81			graphics.setColor(255, 255, 255, 255 * fade)
82			local succ, ret = xpcall(message.draw, debug.traceback, message.height, fade)
83			if not succ then
84				log.error("notification error: %s", ret)
85			end
86			graphics.pop()
87		end
88	end
89end
90
91function notification.color(color, text, ...)
92	if select("#", ...) > 0 then
93		text = string.format(text, ...)
94	end
95	notification.add(14, 5, 0.75, function(height, fade)
96		graphics.setColor(0, 0, 0, 255 * fade)
97		graphics.textOutline(text, 1, 1, 1)
98		graphics.setColor(color.r, color.g, color.b, 255 * fade)
99		graphics.print(text, 0, 0)
100	end)
101end
102
103function notification.error(text, ...)
104	love.window.requestAttention()
105	notification.color(color(241, 73, 82), text, ...)
106end
107
108function notification.warning(text, ...)
109	notification.color(color(255, 242, 0), text, ...)
110end
111
112function notification.info(text, ...)
113	notification.color(color(79, 202, 255), text, ...)
114end
115
116function notification.message(text, ...)
117	notification.color(color_white, text, ...)
118end
119
120function notification.coloredMessage(...)
121	local args = {...}
122	notification.add(14, 5, 0.75, function(height, fade)
123		graphics.setColor(0, 0, 0, 255 * fade)
124		graphics.textOutline(args, 1, 1, 1)
125		graphics.setColor(255, 255, 255, 255 * fade)
126		graphics.print(args, 0, 0)
127	end)
128end
129
130return notification