-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotify.lua
More file actions
91 lines (75 loc) · 2.38 KB
/
notify.lua
File metadata and controls
91 lines (75 loc) · 2.38 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
local FONT_SIZE = 16
local DEFAULT_NOTIFY_TIME = 3.5
local RECTANGLE_BORDER_RADIUS = 10
local PADDING = 4
function Client:startNotify()
self.notifyMessage = ""
self.notifyTime = 0
self.notifyError = false
end
function Client:updateNotify(dt)
if self.notifyTime > 0 then
self.notifyTime = self.notifyTime - dt
if self.notifyTime <= 0 then
self:clearNotify()
end
end
end
local font
local lastDpiScale
function Client:drawNotify()
if self.notifyTime <= 0 then
return
end
local dpiScale = love.graphics.getDPIScale()
if dpiScale ~= lastDpiScale then
font = love.graphics.newFont(dpiScale * FONT_SIZE)
lastDpiScale = dpiScale
end
love.graphics.push("all")
love.graphics.setFont(font)
local windowWidth, windowHeight = love.graphics.getDimensions()
local rectangleBorderRadius = dpiScale * RECTANGLE_BORDER_RADIUS
local padding = dpiScale * PADDING
local textLimit = 0.75 * windowWidth - 2 * rectangleBorderRadius - 2 * padding
local textWidth, wrappedText = font:getWrap(self.notifyMessage, textLimit)
local textHeight = #wrappedText * font:getLineHeight() * font:getHeight()
love.graphics.origin()
if self.notifyError then
love.graphics.setColor(0.5, 0, 0, 0.7)
else
love.graphics.setColor(0, 0, 0, 0.7)
end
love.graphics.rectangle(
"fill",
0.5 * (windowWidth - textWidth - 2 * rectangleBorderRadius - 2 * padding),
-rectangleBorderRadius - padding,
textWidth + 2 * rectangleBorderRadius + 2 * padding,
textHeight + 2 * rectangleBorderRadius + 2 * padding,
rectangleBorderRadius
)
love.graphics.setColor(1, 1, 1)
for i = 1, #wrappedText do
local stripped = wrappedText[i]:gsub("^ *", ""):gsub(" *$", "")
love.graphics.print(
stripped,
0.5 * (windowWidth - font:getWidth(stripped)),
padding + (i - 1) * font:getLineHeight() * font:getHeight()
)
end
love.graphics.pop()
end
function Client:notify(message, time, isError)
self.notifyMessage = message
self.notifyTime = time or DEFAULT_NOTIFY_TIME
if isError ~= nil then
self.notifyError = isError
else
self.notifyError = false
end
end
function Client:clearNotify()
self.notifyMessage = ""
self.notifyTime = 0
self.notifyError = false
end