-
Notifications
You must be signed in to change notification settings - Fork 8
/
Notifications.lua
330 lines (272 loc) · 8.42 KB
/
Notifications.lua
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
local dbg = nil
local AbstractNotification = ZO_Object:Subclass()
AbstractNotification.NOTIFY = 0
AbstractNotification.COUNTDOWN = 1
function AbstractNotification:New(...)
local obj = ZO_Object.New(self)
obj:Init(...)
return obj
end
function AbstractNotification:Init(type, id, parent)
self.id = id
self.parent = parent
self.freeToUse = false
self.type = type
end
function AbstractNotification:GetType()
return self.type
end
function AbstractNotification:GetText()
return self.text
end
function AbstractNotification:GetId()
return self.id
end
function AbstractNotification:IsFreeToUse()
return self.freeToUse
end
function AbstractNotification:SetDisplayTime(displayTime)
self.displayTime = displayTime
self.endTime = GetGameTimeMilliseconds() + displayTime
end
function AbstractNotification:GetEndTime()
return self.endTime
end
function AbstractNotification:Stop()
EVENT_MANAGER:UnregisterForUpdate("RNNotification_" .. self.id)
-- dbg("Stopped: %d %s", self.id, self.text)
self:SetHidden(true)
self.freeToUse = true
end
function AbstractNotification:_runTimer(ms, f)
self.displayTime = math.floor((self.endTime - GetGameTimeMilliseconds())/ms + 0.5) * ms
if (f) then
f()
end
EVENT_MANAGER:RegisterForUpdate("RNNotification_" .. self.id, ms, function()
if (ms < 500) then
ms = 200
elseif (ms < 1000) then
ms = 500
end
self.displayTime = math.floor((self.endTime - GetGameTimeMilliseconds())/ms + 0.5) * ms
if (f) then
f()
end
if (self.displayTime <= 0) then
self:SetHidden(true)
self.freeToUse = true
--dbg("Hiding: "..self.id)
EVENT_MANAGER:UnregisterForUpdate("RNNotification_" .. self.id)
end
end)
end
function AbstractNotification:runTimer(ms, f)
local timer = self.endTime - GetGameTimeMilliseconds()
local mod = timer % ms
zo_callLater(function()
self:_runTimer(ms, f)
end, mod)
end
function AbstractNotification:SetAnchor(width, height)
self.ctrl:SetAnchor(TOP, self.parent, TOP, width, height)
end
function AbstractNotification:SetHidden(hidden)
self.ctrl:SetHidden(hidden)
end
function AbstractNotification:IsHidden()
return self.ctrl:IsHidden()
end
function AbstractNotification:GetPriority()
return 1
end
local Notification = AbstractNotification:Subclass()
function Notification:New(...)
local obj = AbstractNotification.New(self, AbstractNotification.NOTIFY, ...)
obj:Initialize(...)
return obj
end
function Notification:GetHeight()
return self.ctrl:GetTextHeight();
end
function Notification:SetText(str)
self.ctrl:SetText(str)
end
function Notification:SetScale(scale)
self.ctrl:SetScale(scale)
end
function Notification:Initialize(id, parent)
self.ctrl = WINDOW_MANAGER:CreateControl("RNotification"..id, self.parent, CT_LABEL)
self.fadeInAnimation = GetAnimationManager():CreateTimelineFromVirtual("CenterAnnounceFadeIn", self.ctrl)
self.ctrl:SetFont('ZoFontCenterScreenAnnounceSmall')
end
function Notification:Show(text)
self.text = text
self.fadeInAnimation:PlayFromStart()
self:SetHidden(false)
self:SetText(text)
self.freeToUse = false
self:runTimer(1000)
return self.id
end
local CountdownNotification = AbstractNotification:Subclass()
function CountdownNotification:New(...)
local obj = AbstractNotification.New(self, AbstractNotification.COUNTDOWN, ...)
obj:Initialize(...)
return obj
end
function CountdownNotification:Initialize(id, parent)
self.ctrl = WINDOW_MANAGER:CreateControl("RNotification"..id, self.parent)
self.ctrl.label = WINDOW_MANAGER:CreateControl("RNotification"..id.."Label", self.ctrl, CT_LABEL)
self.ctrl.label:SetAnchor(TOP, self.ctrl, LEFT, 0, 0)
self.ctrl.label:SetFont('ZoFontCenterScreenAnnounceSmall')
self.ctrl.counter = WINDOW_MANAGER:CreateControl("RNotification"..id.."Counter", self.ctrl, CT_LABEL)
self.ctrl.counter:SetAnchor(LEFT, self.ctrl.label, RIGHT, 20, 0)
self.ctrl.counter:SetFont('ZoFontCenterScreenAnnounceLarge')
self.fadeInAnimation = GetAnimationManager():CreateTimelineFromVirtual("CenterAnnounceFadeIn", self.ctrl)
self.countdownAnimation = GetAnimationManager():CreateTimelineFromVirtual("CenterAnnounceCountdownLoop", self.ctrl.counter)
end
function CountdownNotification:GetHeight()
return self.ctrl.label:GetTextHeight();
end
function CountdownNotification:SetText(str)
self.ctrl.label:SetText(str)
end
function CountdownNotification:SetScale(scale)
self.ctrl.label:SetScale(scale)
self.ctrl.counter:SetScale(scale * 1.3)
self.scale = scale;
local firstAnimation = self.countdownAnimation:GetAnimation(1)
local startScale = firstAnimation:GetStartScale()
local endScale = firstAnimation:GetEndScale()
firstAnimation:SetScaleValues(1.1 * self.scale, 1.5 * self.scale) -- TODO Get base scale from xml
local secondAnimation = self.countdownAnimation:GetAnimation(2)
local startScale = secondAnimation:GetStartScale()
local endScale = secondAnimation:GetEndScale()
secondAnimation:SetScaleValues(1.5 * self.scale, 1.1 * self.scale) -- TODO Get base scale from xml
end
function CountdownNotification:GetScale()
return self.scale and self.scale or 1;
end
local function OnCountdown(self, precise)
if (self.scale) then
self.ctrl.counter:SetScale(self.scale)
end
if (precise < 1000) then
txt = string.format("%0.1f", self.displayTime/1000)
else
self.countdownAnimation:PlayFromStart()
txt = self.displayTime/1000
end
if (self.displayTime <= 3000) then
txt = "|cff0000" .. txt .."|r"
end
self.ctrl.counter:SetText(txt)
end
function CountdownNotification:Show(text, precise)
self.text = text
if (precise == 1000) then
self.fadeInAnimation:PlayFromStart()
end
self:SetHidden(false)
self.freeToUse = false
self:SetText(text)
self.ctrl.counter:SetText("")
local txt
self:runTimer(precise == nil and 1000 or precise, function() OnCountdown(self, precise) end)
return self.id
end
local NotificationsPool = ZO_Object:Subclass()
function NotificationsPool:New(...)
local obj = ZO_Object.New(self)
obj:Initialize(...)
return obj
end
function NotificationsPool:Initialize(displayTime, parent)
self.pool = {}
self.displayTime = displayTime
if (parent == nil) then
self.parent = RaidNotifierUICenterAnnounce
else
self.parent = parent
end
dbg = RaidNotifier.dbg
-- self.bg = WINDOW_MANAGER:CreateControl(nil, self.parent, CT_BACKDROP)
-- self.bg:SetAnchorFill(self.parent)
-- self.bg:SetEdgeTexture(nil, 1, 1, 0.5, 0.5)
end
local pool = nil
function NotificationsPool.GetInstance(displayTime)
if (not pool) then
if (displayTime == nil) then
displayTime = 3000
end
pool = NotificationsPool:New(displayTime)
end
return pool
end
function NotificationsPool:SetScale(scale)
self.scale = scale;
end
-- 10 - 1s, 5 - 0.5s, 2 - 0.2s
function NotificationsPool:SetPrecise(precise)
self.precise = precise * 100
end
function NotificationsPool:Add(text, displayTime, isCountdown)
isCountdown = isCountdown and isCountdown or false
local notify = nil
for i = 1, #self.pool, 1 do
if (self.pool[i]:IsFreeToUse()) then
if ((self.pool[i]:GetType() == AbstractNotification.COUNTDOWN and isCountdown)
or (self.pool[i]:GetType() == AbstractNotification.NOTIFY and not isCountdown)) then
--dbg("Used already created notification "..self.pool[i]:GetId())
notify = self.pool[i]
break;
end
end
end
if (notify == nil) then
local id = #self.pool + 1
if (isCountdown == true) then
notify = CountdownNotification:New(id, self.parent)
else
notify = Notification:New(id, self.parent)
end
notify:SetText("X") -- we need anything to get text height
self.pool[id] = notify
--dbg("Created new notification "..id)
end
if (self.scale) then
notify:SetScale(self.scale)
end
notify:SetDisplayTime(displayTime and displayTime or self.displayTime)
table.sort(self.pool, function(a,b)
if (a:GetEndTime() < b:GetEndTime()) then
return true
-- elseif (a:GetEndTime() > b:GetEndTime()) then
-- return false
end
-- return a:GetLastUpdateTime() < b:GetLastUpdateTime()
return false
end)
local height = 0
for i = #self.pool, 1, -1 do
if (not self.pool[i]:IsHidden() or self.pool[i] == notify) then
self.pool[i]:SetAnchor(0, height)
height = height + self.pool[i]:GetHeight()
end
end
return notify:Show(text, self.precise)
end
function NotificationsPool:Stop(id)
if (id == nil or (id <= #self.pool and id > 0)) then
for i = 1, #self.pool, 1 do
if (self.pool[i]:GetId() == id or id == nil) then
self.pool[i]:Stop()
end
end
end
end
RaidNotifier = RaidNotifier or {}
RaidNotifier.Notification = Notification
RaidNotifier.NotificationsPool = NotificationsPool