alert.lua 825 B

1234567891011121314151617181920212223242526272829303132333435
  1. local Alert = class()
  2. local g = love.graphics
  3. function Alert:init()
  4. self.message = nil
  5. self.alpha = 0
  6. end
  7. function Alert:update()
  8. self.alpha = math.max(self.alpha - tickRate, 0)
  9. end
  10. function Alert:draw()
  11. if self.alpha < .001 then return end
  12. local u, v = ctx.u, ctx.v
  13. local str = self.message
  14. g.setFont('pixel', 8)
  15. local font = g.getFont()
  16. local w, h = font:getWidth(str), font:getHeight()
  17. g.setColor(0, 0, 0, math.min(self.alpha, 1) * 255)
  18. g.rectangleCenter('fill', .5 * u, .5 * v, w + 16, h + 16)
  19. g.setColor(255, 255, 255, math.min(self.alpha, 1) * 255)
  20. g.rectangleCenter('line', .5 * u, .5 * v, w + 16, h + 16, true, true, true)
  21. g.printCenter(str, .5 * u, .5 * v)
  22. end
  23. function Alert:show(message, alpha)
  24. alpha = alpha or 2
  25. self.alpha = alpha
  26. self.message = message
  27. end
  28. return Alert