/
timer.lua
executable file
·294 lines (223 loc) · 6.21 KB
/
timer.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
--[[
timer.lua
Displays countdowns on widgets
--]]
local Addon = ...
local Timer = OmniCC:New('Timer')
local timers = {}
local IconSize = 36
local Padding = 0
local Day, Hour, Minute = 86400, 3600, 60
local Dayish, Hourish, Minuteish, Soonish = 3600 * 23.5, 60 * 59.5, 59.5, 5.5
local HalfDayish, HalfHourish, HalfMinuteish = Day/2 + 0.5, Hour/2 + 0.5, Minute/2 + 0.5
local floor, min, type = floor, min, type
local round = function(x) return floor(x + 0.5) end
local OmniCC, GetTime = OmniCC, GetTime
--[[ Constructor ]]--
function Timer:New(cooldown)
local timer = Timer:Bind(CreateFrame('Frame', nil, cooldown:GetParent()))
timer:SetFrameLevel(cooldown:GetFrameLevel() + 5)
timer:Hide()
timer.text = timer:CreateFontString(nil, 'OVERLAY')
timer.cooldown = cooldown
timer:SetPoint('CENTER', cooldown)
timer:UpdateFontSize(cooldown:GetSize())
timers[cooldown] = timer
return timer
end
function Timer:Get(cooldown)
return timers[cooldown]
end
--[[ Controls ]]--
function Timer:Start(start, duration)
self.start = start
self.visible = self.cooldown:IsVisible()
self.duration = duration
self.textStyle = nil
self.enabled = true
self:UpdateShown()
end
function Timer:Stop()
self.start, self.duration, self.enabled, self.visible, self.textStyle = nil
self:CancelUpdate()
self:Hide()
end
--[[ Update Schedules ]]--
function Timer:ScheduleUpdate(delay)
local engine = OmniCC:GetUpdateEngine()
local updater = engine:Get(self)
updater:ScheduleUpdate(delay)
end
function Timer:CancelUpdate()
local engine = OmniCC:GetUpdateEngine()
local updater = engine:GetActive(self)
if updater then
updater:CancelUpdate()
end
end
--[[ Redraw ]]--
function Timer:UpdateFontSize(width, height)
self.abRatio = round(width) / IconSize
self:SetSize(width, height)
self:UpdateTextPosition()
if self.enabled and self.visible then
self:UpdateText(true)
end
end
function Timer:UpdateText(forceStyleUpdate)
if self.start > GetTime() then
return self:ScheduleUpdate(self.start - GetTime())
end
local remain = self:GetRemain()
if remain > 0 then
local overallScale = self.abRatio * (self:GetEffectiveScale()/UIParent:GetScale())
if overallScale < self:GetSettings().minSize then
self.text:Hide()
self:ScheduleUpdate(1)
else
local style = self:GetTextStyle(remain)
if (style ~= self.textStyle) or forceStyleUpdate then
self.textStyle = style
self:UpdateTextStyle()
end
if self.text:GetFont() then
self.text:SetFormattedText(self:GetTimeText(remain))
self.text:Show()
end
self:ScheduleUpdate(self:GetNextUpdate(remain))
end
else
if self.duration >= self:GetSettings().minEffectDuration then
OmniCC:TriggerEffect(self.cooldown)
end
self:Stop()
end
end
function Timer:UpdateTextStyle()
local sets = self:GetSettings()
local font, size, outline = sets.fontFace, sets.fontSize, sets.fontOutline
local style = sets.styles[self.textStyle]
if sets.scaleText then
size = size * style.scale * (self.abRatio or 1)
else
size = size * style.scale
end
if size > 0 then
if not self.text:SetFont(font, size, outline) then
self.text:SetFont(STANDARD_TEXT_FONT, size, outline)
end
end
self.text:SetTextColor(style.r, style.g, style.b, style.a)
end
function Timer:UpdateTextPosition()
local sets = self:GetSettings()
local abRatio = self.abRatio or 1
local text = self.text
text:ClearAllPoints()
text:SetPoint(sets.anchor, sets.xOff * abRatio, sets.yOff * abRatio)
end
function Timer:UpdateShown()
if self:ShouldShow() then
self:Show()
self:UpdateText()
else
self:Hide()
end
end
function Timer:UpdateOpacity()
self.cooldown:SetAlpha(self:GetSettings().spiralOpacity)
end
--[[ Accessors ]]--
function Timer:GetRemain()
return self.duration - (GetTime() - self.start)
end
function Timer:GetTextStyle(remain)
if remain < Soonish then
return 'soon'
elseif remain < Minuteish then
return 'seconds'
elseif remain < Hourish then
return 'minutes'
else
return 'hours'
end
end
function Timer:GetNextUpdate(remain)
local sets = self:GetSettings()
if remain < (sets.tenthsDuration + 0.5) then
return 0.1
elseif remain < Minuteish then
return remain - round(remain) + 0.51
elseif remain < sets.mmSSDuration then
return remain - round(remain) + 0.51
elseif remain < Hourish then
local minutes = round(remain/Minute)
if minutes > 1 then
return remain - (minutes*Minute - HalfMinuteish)
end
return remain - Minuteish + 0.01
elseif remain < Dayish then
local hours = round(remain/Hour)
if hours > 1 then
return remain - (hours*Hour - HalfHourish)
end
return remain - Hourish + 0.01
else
local days = round(remain/Day)
if days > 1 then
return remain - (days*Day - HalfDayish)
end
return remain - Dayish + 0.01
end
end
function Timer:GetTimeText(remain)
local sets = self:GetSettings()
if remain < sets.tenthsDuration then
return '%.1f', remain
elseif remain < Minuteish then
local seconds = round(remain)
return seconds ~= 0 and seconds or ''
elseif remain < sets.mmSSDuration then
local seconds = round(remain)
return '%d:%02d', seconds/Minute, seconds%Minute
elseif remain < Hourish then
return '%dm', round(remain/Minute)
elseif remain < Dayish then
return '%dh', round(remain/Hour)
else
return '%dd', round(remain/Day)
end
end
function Timer:ShouldShow()
if not (self.enabled and self.visible) or self.cooldown.noCooldownCount then
return false
end
local sets = self:GetSettings()
if self.duration < sets.minDuration then
return false
end
return sets.enabled
end
--[[ Static Functions ]]--
function Timer:ForAll(f, ...)
if type(f) == 'string' then
f = self[f]
end
for _, timer in pairs(timers) do
f(timer, ...)
end
end
function Timer:ForAllShown(f, ...)
if type(f) == 'string' then
f = self[f]
end
for _, timer in pairs(timers) do
if timer:IsShown() then
f(timer, ...)
end
end
end
--[[ Settings ]]--
function Timer:GetSettings()
return OmniCC:GetGroupSettingsFor(self.cooldown)
end