This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MoneyToast.lua
351 lines (293 loc) · 9.9 KB
/
MoneyToast.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
local _, addon = ...
---@cast addon MoneyToastAddon
local defaultConfig = {
stayVisible = false,
widgets = {
currencies = {
2778, -- Bronze (MoP: Remix)
},
gold = true,
}
}
local loaded = false
local animationGroupIdCounter = 0
local updateVariables = function()
if not MONEYTOAST_VARIABLES then
MONEYTOAST_VARIABLES = defaultConfig
return
end
if not MONEYTOAST_VARIABLES.widgets then
MONEYTOAST_VARIABLES.widgets = defaultConfig.widgets
end
end
---@param widgetType string
---@param dataId integer | nil
local getWidgetName = function(widgetType, dataId)
return "MoneyToast_Widget_" .. widgetType .. (dataId or "")
end
---@param widgetType string
---@param dataId integer | nil
local createWidget = function(self, widgetType, dataId)
local frameName = getWidgetName(widgetType, dataId)
local frame = CreateFrame("Frame", frameName, _G["MoneyToast"], "MoneyToast_Widget_Template")
---@cast frame WidgetFrame
local iconFileId
if widgetType == "Gold" then
iconFileId = "Interface\\Icons\\INV_Misc_Coin_02"
elseif widgetType == "Currency" and dataId ~= nil then
local currencyInfo = C_CurrencyInfo.GetCurrencyInfo(dataId)
iconFileId = currencyInfo.iconFileID
end
---@diagnostic disable-next-line: undefined-field
frame.Balance.Icon:SetTexture(iconFileId)
frame:SetScript("OnUpdate", function(frame, elapsed) addon.Animations:OnUpdate(elapsed) end)
animationGroupIdCounter = animationGroupIdCounter + 1
local widgetInfo = {
type = widgetType,
dataId = dataId,
visible = false,
balance = 0,
balanceDelta = 0,
earnedThisSession = 0,
animationGroupId = animationGroupIdCounter
}
self.widgets[frameName] = frame
self.widgetInfo[frameName] = widgetInfo
end
---@param self CoreModule
local initWidgets = function(self)
if MONEYTOAST_VARIABLES.widgets.gold then
self:CreateWidget("Gold")
end
for _, currency in ipairs(MONEYTOAST_VARIABLES.widgets.currencies) do
self:CreateWidget("Currency", currency)
end
self:RegisterAnimations();
end
---@param currencyType integer
local isCurrencyTracked = function(currencyType)
for _, currencyTracked in ipairs(MONEYTOAST_VARIABLES.widgets.currencies) do
if currencyTracked == currencyType then
return true
end
end
return false
end
---@param event WowEvent
local onEvent = function(_, event, ...)
if event == "VARIABLES_LOADED" then
updateVariables()
addon.Core:InitWidgets();
loaded = true;
elseif event == "PLAYER_ENTERING_WORLD" then
addon.Core:UpdateWidgetsSilently();
elseif event == "PLAYER_MONEY" then
addon.Core:UpdateWithAnimation("Gold");
elseif event == "CURRENCY_DISPLAY_UPDATE" then
if not loaded then
return
end
local currencyType = ...
if not currencyType then
return
end
if isCurrencyTracked(currencyType) then
addon.Core:UpdateWithAnimation("Currency", currencyType)
end
end
end
---@param frame WidgetFrame
local hideOrShowWidget = function(frame)
if MONEYTOAST_VARIABLES.stayVisible then
frame:Show()
frame:SetAlpha(1)
else
frame:Hide()
frame:SetAlpha(0)
end
end
---@param frame WidgetFrame
local resetWidget = function(frame)
frame:ClearAllPoints()
frame:SetPoint("TOP", 0, -32)
end
---@param self CoreModule
local resetWidgets = function(self)
for _, frame in pairs(self.widgets) do
resetWidget(frame)
end
end
---@param self CoreModule
local hideOrShowWidgets = function(self)
for _, frame in pairs(self.widgets) do
hideOrShowWidget(frame)
end
end
---@param self CoreModule
local registerAnimations = function(self)
for frameName, frame in pairs(self.widgets) do
local widgetInfo = self.widgetInfo[frameName]
addon.Animations:RegisterAnimation("Balance", widgetInfo.animationGroupId, {
frame = frame,
widgetInfo = widgetInfo,
})
addon.Animations:RegisterAnimation("FrameFadeIn", widgetInfo.animationGroupId, {
frame = frame,
widgetInfo = widgetInfo,
})
addon.Animations:RegisterAnimation("FrameFadeOut", widgetInfo.animationGroupId, {
frame = frame,
})
end
end
---@param self CoreModule
local onLoad = function(self)
SlashCmdList["MONEYTOAST"] = function(msg)
local _, _, cmd, args = string.find(msg, "%s?(%w+)%s?(.*)");
if cmd == "fade" then
if args == "on" or args == "true" or args == "1" then
MONEYTOAST_VARIABLES.stayVisible = true;
elseif args == "off" or args == "false" or args == "0" then
MONEYTOAST_VARIABLES.stayVisible = false;
end
self:HideOrShowWidgets();
elseif cmd == "reset" then
self:ResetWidgets()
else
self:CommandHelp()
end
end
SLASH_MONEYTOAST1 = "/moneytoast"
end
-- commands
local commandHelp = function()
print("MoneyToast Commands:")
print("/moneytoast fade on - Always show the widgets so they can be moved.")
print("/moneytoast fade off - Re-enable the fade animations so that they disappear automatically.")
print("/moneytoast reset - Resets the position of the notification frames.")
end
-- local functions
---@param widgetInfo WidgetInfo
---@param newBalance number
local setCurrentBalance = function(widgetInfo, newBalance)
if widgetInfo.balance ~= 0 then
local delta = newBalance - widgetInfo.balance
widgetInfo.earnedThisSession = widgetInfo.earnedThisSession + delta
end
widgetInfo.balance = newBalance;
end
---@param self CoreModule
---@param widgetName string
local setDefaultBalance = function(self, widgetName)
local frame = self.widgets[widgetName]
local widgetInfo = self.widgetInfo[widgetName]
local label;
if widgetInfo.type == "Gold" then
label = MT_CURRENT_BALANCE
elseif widgetInfo.type == "Currency" then
local currencyInfo = C_CurrencyInfo.GetCurrencyInfo(widgetInfo.dataId)
label = currencyInfo.name
end
frame.Balance.Label:SetText(label)
end
---@param widgetInfo WidgetInfo
local getBalanceForWidget = function(widgetInfo)
if widgetInfo.type == "Gold" then
return GetMoney()
elseif widgetInfo.type == "Currency" then
local currencyInfo = C_CurrencyInfo.GetCurrencyInfo(widgetInfo.dataId)
return currencyInfo.quantity
end
error("No balance found for this widget.")
end
---@param widgetInfo WidgetInfo
local getBalanceLabel = function(widgetInfo)
if widgetInfo.type == "Gold" then
return widgetInfo.balanceDelta > 0
and string.format(MT_MONEY_RECEIVED, addon.Utilities.GetMoneyStringPadded(abs(widgetInfo.balanceDelta), true))
or string.format(MT_MONEY_SPENT, addon.Utilities.GetMoneyStringPadded(abs(widgetInfo.balanceDelta), true))
elseif widgetInfo.type == "Currency" then
return widgetInfo.balanceDelta > 0
and string.format(MT_MONEY_RECEIVED, FormatLargeNumber(abs(widgetInfo.balanceDelta)))
or string.format(MT_MONEY_SPENT, FormatLargeNumber(abs(widgetInfo.balanceDelta)))
end
end
---@param self CoreModule
---@param widgetName string
---@param animated boolean
local update = function(self, widgetName, animated)
local widgetInfo = self.widgetInfo[widgetName]
local frame = self.widgets[widgetName]
hideOrShowWidget(frame)
self:SetDefaultBalance(widgetName)
local newBalance = getBalanceForWidget(widgetInfo)
local delta = widgetInfo.balance > 0 and newBalance - widgetInfo.balance or 0
setCurrentBalance(widgetInfo, newBalance)
if not animated then
addon.Animations:SetAnimationValue("Balance", widgetInfo.animationGroupId, widgetInfo.balance)
return;
end
frame:Show()
addon.Animations:CancelAnimation("FrameFadeOut", widgetInfo.animationGroupId)
if not widgetInfo.visible then
widgetInfo.balanceDelta = abs(delta)
addon.Animations:PlayAnimation("FrameFadeIn", widgetInfo.animationGroupId)
else
widgetInfo.balanceDelta = widgetInfo.balanceDelta + abs(delta)
addon.Animations:SkipAnimation("FrameFadeIn", widgetInfo.animationGroupId)
end
local label = getBalanceLabel(widgetInfo)
frame.Balance.Label:SetText(label);
end
---@param self CoreModule
local updateWidgets = function(self, withAnimation)
for widgetName in pairs(self.widgets) do
self:Update(widgetName, withAnimation)
end
end
---@param self CoreModule
local updateWidgetsSilently = function(self)
self:UpdateWidgets(false);
end
---@param self CoreModule
local updateWidgetsWithAnimation = function(self)
self:UpdateWidgets(true);
end
---@param self CoreModule
local updateWithAnimation = function(self, widgetType, dataId)
local widgetName = getWidgetName(widgetType, dataId)
local widget = self.widgets[widgetName]
if not widget then
error("Can not find widget of type " .. widgetType)
end
local animated = true
self:Update(widgetName, animated)
end
---@class CoreModule
local coreModule = {
widgets = {}, ---@type table<string, WidgetFrame>
widgetInfo = {}, ---@type table<string, WidgetInfo>
CommandHelp = commandHelp,
CreateWidget = createWidget,
HideOrShowWidgets = hideOrShowWidgets,
InitWidgets = initWidgets,
OnEvent = onEvent,
OnLoad = onLoad,
ResetWidgets = resetWidgets,
RegisterAnimations = registerAnimations,
SetDefaultBalance = setDefaultBalance,
Update = update,
UpdateWidgets = updateWidgets,
UpdateWidgetsSilently = updateWidgetsSilently,
UpdateWidgetsWithAnimation = updateWidgetsWithAnimation,
UpdateWithAnimation = updateWithAnimation,
}
addon.Core = coreModule
addon.Core:OnLoad()
local mainFrame = CreateFrame("Frame", "MoneyToast")
mainFrame:RegisterEvent("ADDON_LOADED");
mainFrame:RegisterEvent("PLAYER_MONEY");
mainFrame:RegisterEvent("PLAYER_ENTERING_WORLD");
mainFrame:RegisterEvent("VARIABLES_LOADED");
mainFrame:RegisterEvent("CURRENCY_DISPLAY_UPDATE");
mainFrame:SetScript("OnEvent", addon.Core.OnEvent)