local graphics = love.graphics local newImage = graphics.newImage local newFont = graphics.newFont local timer = love.timer local color = require("util.color") local log = require("log") local notification = { messages = {}, font = newFont("fonts/melee-bold.otf", 12), padding = 8, } local dummy = function(height, fade) end function notification.add(height, length, fade, draw) height = height or 16 length = length or 3 fade = fade or 0.75 local now = timer.getTime() table.insert(notification.messages, { time = now, length = length, fade = fade, finish = now + length + fade, height = height, draw = draw or dummy, lerp = {}, }) end function notification.update(x, y) local now = timer.getTime() local prevh = 0 for k, message in pairs(notification.messages) do if (message.finish >= now) then x = x * 0.5 + (message.lerp.x or x) * 0.5 y = y * 0.5 + (message.lerp.y or y) * 0.5 y = y + prevh/2 + notification.padding/2 message.lerp.x = x message.lerp.y = y prevh = message.height end end for k, message in pairs(notification.messages) do if (message.finish >= now) then return end end notification.messages = {} end function notification.draw() local now = timer.getTime() for k, message in pairs(notification.messages) do local fade = 1 if now >= message.time + message.length then fade = 1-((now - message.time - message.length)/message.fade) end local x, y = message.lerp.x, message.lerp.y if x and y then graphics.push("transform") graphics.translate(x, y) graphics.setFont(notification.font) graphics.setColor(255, 255, 255, 255 * fade) local succ, ret = xpcall(message.draw, debug.traceback, message.height, fade) if not succ then log.error("notification error: %s", ret) end graphics.pop() end end end function notification.color(color, text, ...) if select("#", ...) > 0 then text = string.format(text, ...) end notification.add(14, 5, 0.75, function(height, fade) graphics.setColor(0, 0, 0, 255 * fade) graphics.textOutline(text, 1, 1, 1) graphics.setColor(color.r, color.g, color.b, 255 * fade) graphics.print(text, 0, 0) end) end function notification.error(text, ...) love.window.requestAttention() notification.color(color(241, 73, 82), text, ...) end function notification.warning(text, ...) notification.color(color(255, 242, 0), text, ...) end function notification.info(text, ...) notification.color(color(79, 202, 255), text, ...) end function notification.message(text, ...) notification.color(color_white, text, ...) end function notification.coloredMessage(...) local args = {...} notification.add(14, 5, 0.75, function(height, fade) graphics.setColor(0, 0, 0, 255 * fade) graphics.textOutline(args, 1, 1, 1) graphics.setColor(255, 255, 255, 255 * fade) graphics.print(args, 0, 0) end) end return notification