-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
249 lines (198 loc) · 6.64 KB
/
main.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
local AddonName, AddonTable = ...
local Addon = LibStub('AceAddon-3.0'):NewAddon(AddonTable, AddonName)
local DB_NAME = AddonName .. 'DB'
local DB_SCHEMA_VERSION = 1
local cooldowns = {}
-- handle floating point distance
local function equalish(v1, v2, precision)
if v1 == v2 then
return true
end
if v1 == nil or v2 == nil then
return false
end
local factor = math.pow(10, precision);
return math.floor(v1 * factor + 0.5) == math.floor(v2 * factor + 0.5)
end
local function sanitizeModRate(value)
value = tonumber(value) or 0
if value <= 0 then
value = 1
end
return value
end
local function getFontStringFromRegions(...)
for i = 1, select("#", ...) do
local region = select(i, ...)
if region:GetObjectType() == "FontString" then
return region
end
end
end
local function updateText(cooldown)
local cd = cooldowns[cooldown]
-- no text, quit early
local fs = cd.fontString
if not fs and fs:GetText() ~= "" then
return
end
local theme, sleep = Addon:GetTheme(cd)
if not theme then
return
end
local tFontHeight = theme.fontScale * cd.fontHeight
local fontName, fontHeight, fontFlags = fs:GetFont()
if not (theme.fontName == fontName and fontHeight == tFontHeight and theme.fontFlags == fontFlags) then
if not fs:SetFont(theme.fontName, tFontHeight, theme.fontFlags) then
fs:SetFont(STANDARD_TEXT_FONT, tFontHeight, theme.fontFlags)
end
end
fs:SetTextColor(theme.r, theme.g, theme.b, theme.a)
local shadow = theme.shadow
fs:SetShadowColor(shadow.r, shadow.g, shadow.b, shadow.a)
fs:SetShadowOffset(shadow.x, shadow.y)
-- schedule the next update, if needed
-- C_Timer.After has an upper limit, so we don't bother updating the timer
-- once it reaches the day threshold. (You probably shouldn't play WoW for 24 hours straight,
-- so this is acceptable to me)
if sleep and sleep > 0 and sleep < 86400 then
C_Timer.After(sleep, cd.update)
end
end
-- setup initial data when we first see a cooldown
setmetatable(cooldowns, {
__index = function(t, k)
local fs = getFontStringFromRegions(k:GetRegions())
local _, fontHeight = fs:GetFont()
local v = {
update = function() updateText(k) end,
fontString = fs,
fontHeight = fontHeight,
endTime = 0
}
t[k] = v
return v
end
})
-- hooks
local function setTimer(cooldown, start, duration, modRate)
-- skip zero duration cooldowns
if start <= 0 or duration <= 0 or modRate <= 0 then
return
end
local endTime = start + duration
-- both the wow api and addons (especially auras) have a habit of resetting
-- cooldowns every time there's an update to an aura we chack and do nothing
-- if there's an exact start/duration match
local cd = cooldowns[cooldown]
if not (equalish(endTime, cd.endTime, 3) and cd.modRate == modRate) then
cd.endTime = endTime
cd.modRate = modRate
updateText(cooldown)
end
end
function Addon:OnInitialize()
-- initialize db
local db = LibStub('AceDB-3.0'):New(DB_NAME, self:GetDBDefaults(), DEFAULT)
db.RegisterCallback(self, 'OnProfileChanged', 'OnProfileChanged')
db.RegisterCallback(self, 'OnProfileCopied', 'OnProfileChanged')
db.RegisterCallback(self, 'OnProfileReset', 'OnProfileChanged')
self.db = db
-- setup hooks
local cooldown_mt = getmetatable(ActionButton1Cooldown).__index
hooksecurefunc(cooldown_mt, 'SetCooldown', function(cd, start, duration, modRate)
if cd:IsForbidden() then
return
end
start = tonumber(start) or 0
duration = tonumber(duration) or 0
modRate = sanitizeModRate(modRate)
setTimer(cd, start, duration, modRate)
end)
hooksecurefunc(cooldown_mt, 'SetCooldownDuration', function(cd, duration, modRate)
if cd:IsForbidden() then
return
end
local start = GetTime()
duration = tonumber(duration) or 0
modRate = sanitizeModRate(modRate)
setTimer(cd, start, duration, modRate)
end)
end
function Addon:UpgradeDB()
local dbVersion = self.db.global.dbVersion
if dbVersion ~= DB_SCHEMA_VERSION then
self.db.global.dbVersion = DB_SCHEMA_VERSION
end
local addonVersion = C_AddOns.GetAddOnMetadata(AddonName, 'Version')
if self.db.global.addonVersion ~= addonVersion then
self.db.global.addonVersion = addonVersion
end
end
function Addon:OnProfileChanged()
self:Refresh()
end
function Addon:GetDBDefaults()
return {
profile = {
themes = {
['**'] = {
-- what font to use (an actual path)
fontName = STANDARD_TEXT_FONT,
-- NONE | OUTLINE | THICKOUTLINE | MONOCHROME
fontFlags = 'OUTLINE',
fontScale = 1,
-- text color
r = 1,
g = 1,
b = 1,
a = 1,
-- shadow color and offset
shadow = {
-- color
r = 1,
g = 1,
b = 1,
a = 0,
-- offset
x = 0,
y = 0
}
},
soon = {r = 1, g = 0, b = 0, fontScale = 1.5},
seconds = {r = 1, g = 1, b = 0},
minutes = {r = 1, g = 1, b = 1},
hours = {r = .7, g = .7, b = .7, fontScale = .75}
},
-- rules, in eval order
rules = {
{duration = 5, theme = "soon"},
{duration = 60, theme = "seconds"},
{duration = 3600, theme = "minutes"},
{duration = math.huge, theme = "hours"}
}
}
}
end
function Addon:GetTheme(cooldown)
local remain = (cooldown.endTime - GetTime()) / cooldown.modRate
if remain <= 0 then
return
end
local nextRule
for _, rule in ipairs(self.db.profile.rules) do
if remain <= rule.duration then
if nextRule then
return self.db.profile.themes[rule.theme], (remain - nextRule.duration) * cooldown.modRate
end
return self.db.profile.themes[rule.theme], 0
else
nextRule = rule
end
end
end
function Addon:Refresh()
for cooldown in pairs(cooldowns) do
updateText(cooldown)
end
end