-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathAPI.lua
More file actions
532 lines (481 loc) · 17.7 KB
/
Copy pathAPI.lua
File metadata and controls
532 lines (481 loc) · 17.7 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
local addonTbl
do
local _
_, addonTbl = ...
end
local API = {}
addonTbl.API = API
local type, next, error = type, next, error
local function CopyTable(settingsTable)
local copy = {}
for key, value in next, settingsTable do
if type(value) == "table" then
copy[key] = CopyTable(value)
else
copy[key] = value
end
end
return copy
end
--------------------------------------------------------------------------------
-- Addons creating bars
--
-- Allows addons to show a custom bar to the user
function API.CreateBarFromAddon(addonName, barText, barIcon, barTime)
if type(addonName) ~= "string" or #addonName < 3 then error("Invalid addon name for bar creation.") end
if type(barText) ~= "string" or #barText < 3 then error("Invalid text for bar creation.") end
local iconType = type(barIcon)
if iconType ~= "string" and iconType ~= "number" then error("Invalid icon for bar creation.") end
if type(barTime) ~= "number" then error("Invalid bar time for bar creation.") end
local L = API:GetLocale("BigWigs")
addonTbl.loaderPublic.Print(L.showAddonBar:format(addonName, barText))
addonTbl.LoadAndEnableCore()
addonTbl.loaderPublic:SendMessage("BigWigs_StartBar", nil, nil, barText, barTime, barIcon)
addonTbl.loaderPublic:SendMessage("BigWigs_Timer", nil, nil, barTime, barTime, barText, 0, barIcon, false, true)
end
-- Allows addons to send custom bars to the group
function API.SendBarToGroup(addonName, barText, barTime)
if type(addonName) ~= "string" or #addonName < 3 then error("Invalid addon name for bar creation.") end
if type(barText) ~= "string" or #barText < 3 then error("Invalid text for bar creation.") end
if type(barTime) ~= "number" or barTime < 3 then error("Invalid bar time for bar creation.") end
addonTbl.LoadAndEnableCore()
local bars = BigWigs:GetPlugin("Bars", true)
if bars then
bars:SendCustomBarToGroup(barText, barTime)
end
end
--------------------------------------------------------------------------------
-- Bar Styles
--
do
local currentAPIVersion = 1
local errorWrongAPI = "The bar style API version is now %d; the bar style %q needs to be updated for this version of BigWigs."
local errorAlreadyExist = "Trying to register %q as a bar styler, but it already exists."
local function noop() end
local barStyles = {}
-- For more on bar styles, visit: https://github.com/BigWigsMods/BigWigs/wiki/Custom-Bar-Styles
function API:RegisterBarStyle(key, styleData)
if type(key) ~= "string" then error("Bar style must be a string.") end
if type(styleData) ~= "table" then error("Bar style data must be a table.") end
if type(styleData.version) ~= "number" then error("Bar style version must be a number.") end
if type(styleData.apiVersion) ~= "number" then error("Bar style apiVersion must be a number.") end
if type(styleData.GetStyleName) ~= "function" then error("Bar style GetStyleName must be a function.") end
if type(styleData:GetStyleName()) ~= "string" then error("Bar style GetStyleName() return must be a string.") end
if styleData.apiVersion ~= currentAPIVersion then error(errorWrongAPI:format(currentAPIVersion, key)) end
if barStyles[key] and barStyles[key].version == styleData.version then error(errorAlreadyExist:format(key)) end
if not barStyles[key] or barStyles[key].version < styleData.version then
if not styleData.ApplyStyle then styleData.ApplyStyle = noop end
if not styleData.BarStopped then styleData.BarStopped = noop end
if not styleData.GetSpacing then styleData.GetSpacing = noop end
barStyles[key] = styleData
end
end
function API:GetBarStyle(key)
if type(key) ~= "string" then error("Bar style must be a string.") end
local style = barStyles[key]
if style then
return style
end
end
function API:GetBarStyleList()
local list = {}
for k, v in next, barStyles do
list[k] = v:GetStyleName()
end
return list
end
end
--------------------------------------------------------------------------------
-- Configuration
--
do
local list = {
["PrivateAuras"] = true,
}
function API.OpenConfigToPanel(panel)
if list[panel] then
addonTbl.LoadCoreAndOptions()
if BigWigsOptions then
BigWigsOptions:Close()
BigWigsOptions:Open(panel)
end
end
end
end
--------------------------------------------------------------------------------
-- Countdown
--
do
local voices = {}
function API:RegisterCountdown(id, name, data)
if not data then data, name = name, id end
if type(id) ~= "string" then error("Countdown name must be a string.") end
if type(data) ~= "table" or #data < 5 or #data > 10 then error("Countdown data must be an indexed table with 5-10 entries.") end
if voices[id] then error(("Countdown %q already registered."):format(id)) end
voices[id] = { name = name }
for i = 1, #data do
voices[id][i] = data[i]
end
end
function API:GetCountdownList()
local list = {}
for k, v in next, voices do
list[k] = v.name
end
return list
end
function API:HasCountdown(id)
return voices[id] and true
end
function API:GetCountdownSound(id, index)
return voices[id] and voices[id][index]
end
end
--------------------------------------------------------------------------------
-- Locale
--
do
local tbl = {}
local myRegion = GetLocale()
function API:NewLocale(locale, region)
if region == "enUS" or region == myRegion then
if not tbl[locale] then
tbl[locale] = {}
end
return tbl[locale]
end
end
function API:GetLocale(locale)
if tbl[locale] then
return tbl[locale]
end
end
end
do
local localeTable = {}
function API.GetBossModuleLocale(moduleName)
if localeTable[moduleName] then
return CopyTable(localeTable[moduleName])
end
end
function API.SetBossModuleLocale(moduleName, moduleLocaleTable)
if API.IsLocale("enUS") then error("This function is for non-default locales only.") return end
if type(moduleName) ~= "string" then error("Module name must be a string.") return end
if type(moduleLocaleTable) ~= "table" then error("Locale must be a table.") return end
if localeTable[moduleName] then error(("Locale table for module %q already exists."):format(moduleName)) return end
localeTable[moduleName] = moduleLocaleTable
end
end
do
local currentLocale = GetLocale()
function API.IsLocale(localeName)
return localeName == currentLocale
end
end
--------------------------------------------------------------------------------
-- Profile import/export
--
-- A custom profile name and callback function is completely optional
-- You can optionally supply a callback function that will return false if the user declined the profile import, and true if the user accepted
function API.RegisterProfile(addonName, profileString, optionalCustomProfileName, optionalCallbackFunction)
if type(addonName) ~= "string" or #addonName < 3 then error("Invalid addon name for profile import.") return end
if type(profileString) ~= "string" or #profileString < 3 then error("Invalid profile string for profile import.") return end
if optionalCustomProfileName and (type(optionalCustomProfileName) ~= "string" or #optionalCustomProfileName < 3) then error("Invalid custom profile name for the string you want to import.") return end
if optionalCallbackFunction and type(optionalCallbackFunction) ~= "function" then error("Invalid custom callback function for the string you want to import.") return end
addonTbl.LoadCoreAndOptions()
if not BigWigsOptions.VerifyAddOnProfileString(profileString) then error("Invalid profile string for profile import.") return end
BigWigsOptions.SaveImportStringDataFromAddOn(addonName, profileString, optionalCustomProfileName, optionalCallbackFunction)
end
-- Input the name of YOUR addon, i.e. the addon making the profile request
function API.RequestProfile(addonName)
if type(addonName) ~= "string" or #addonName < 3 then error("Invalid addon name for profile request.") return end
local L = API:GetLocale("BigWigs")
addonTbl.loaderPublic.Print(L.requestAddonProfile:format(addonName))
addonTbl.LoadCoreAndOptions()
return BigWigsOptions.RequestProfile(addonName)
end
-- Fetch the name of the current profile
function API.GetProfileName()
local name = addonTbl.loaderPublic.db:GetCurrentProfile()
return name
end
do
local popup = CreateFrame("Frame", nil, UIParent)
popup:Hide()
popup:SetPoint("CENTER", UIParent, "CENTER")
popup:SetSize(320, 72)
popup:EnableMouse(true) -- Do not allow click-through on the frame
popup:SetFrameStrata("TOOLTIP")
popup:SetFrameLevel(120) -- Lots of room to draw under it
popup:SetFixedFrameStrata(true)
popup:SetFixedFrameLevel(true)
local border = CreateFrame("Frame", nil, popup, "DialogBorderOpaqueTemplate")
border:SetAllPoints(popup)
local textFrame = popup:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
textFrame:SetSize(290, 0)
textFrame:SetPoint("TOP", 0, -16)
textFrame:SetText("BigWigs")
local function newButton(newText)
local button = CreateFrame("Button", nil, popup)
button:SetSize(128, 21)
button:SetNormalFontObject(GameFontNormal)
button:SetHighlightFontObject(GameFontHighlight)
button:SetNormalTexture(130763) -- "Interface\\Buttons\\UI-DialogBox-Button-Up"
button:GetNormalTexture():SetTexCoord(0.0, 1.0, 0.0, 0.71875)
button:SetPushedTexture(130761) -- "Interface\\Buttons\\UI-DialogBox-Button-Down"
button:GetPushedTexture():SetTexCoord(0.0, 1.0, 0.0, 0.71875)
button:SetHighlightTexture(130762) -- "Interface\\Buttons\\UI-DialogBox-Button-Highlight"
button:GetHighlightTexture():SetTexCoord(0.0, 1.0, 0.0, 0.71875)
button:SetText(newText)
return button
end
local acceptButton = newButton(ACCEPT)
acceptButton:SetPoint("BOTTOMRIGHT", popup, "BOTTOM", -6, 16)
local cancelButton = newButton(CANCEL)
cancelButton:SetPoint("LEFT", acceptButton, "RIGHT", 13, 0)
popup:SetScript("OnKeyDown", function(_, key)
if key == "ESCAPE" then
cancelButton:Click()
end
end)
-- Input the name of YOUR addon, i.e. the addon making the swap request
-- If the profile you're trying to swap to doesn't exist, this function will return false, it will return true if the profile was found and the popup was displayed to the user
-- You can optionally supply a callback function that will return false if the user declined the profile swap, and true if the user accepted
function API.SwapProfile(addonName, profileName, optionalCallbackFunction)
if type(addonName) ~= "string" or #addonName < 3 then error("Invalid addon name for profile import.") return end
if type(profileName) ~= "string" or #profileName < 3 then error("Invalid profile name for profile import.") return end
if optionalCallbackFunction and type(optionalCallbackFunction) ~= "function" then error("Invalid custom callback function for the profile you want to swap to.") return end
if profileName == API.GetProfileName() then error("You cannot swap to the same profile.") return end
local profileList = {}
addonTbl.loaderPublic.db:GetProfiles(profileList)
for i = 1, #profileList do
local name = profileList[i]
if profileName == name then
popup:Show()
textFrame:SetText(API:GetLocale("BigWigs").confirm_profile_swap:format(addonName, profileName))
local height = 61 + textFrame:GetHeight()
popup:SetHeight(height)
acceptButton:ClearAllPoints()
acceptButton:SetPoint("BOTTOMRIGHT", popup, "BOTTOM", -6, 16)
acceptButton:SetScript("OnClick", function()
popup:Hide()
acceptButton:SetScript("OnClick", nil)
cancelButton:SetScript("OnClick", nil)
addonTbl.loaderPublic.db:SetProfile(profileName)
if optionalCallbackFunction then
optionalCallbackFunction(true)
end
end)
cancelButton:SetScript("OnClick", function()
popup:Hide()
cancelButton:SetScript("OnClick", nil)
acceptButton:SetScript("OnClick", nil)
if optionalCallbackFunction then
optionalCallbackFunction(false)
end
end)
return true
end
end
return false -- The profile name you provided doesn't exist
end
end
--------------------------------------------------------------------------------
-- Slash commands
--
do
local slashTable = {}
local slashNoUpdatesTable = {}
local strsub = string.sub
-- Registers a slash command
function API.RegisterSlashCommand(rawSlashName, slashFunc, noUpdates)
local slashName = strsub(rawSlashName, 2)
if not slashTable[slashName] then
_G["SLASH_"..slashName.."1"] = rawSlashName
SlashCmdList[slashName] = function(text)
local func = slashTable[slashName]
func(text)
if slashTable[slashName] ~= func then -- Did this slash command load an addon that changed what the slash command does?
slashTable[slashName](text) -- Then call the new function also
end
end
end
if not slashNoUpdatesTable[slashName] then
slashTable[slashName] = slashFunc
if noUpdates then
slashNoUpdatesTable[slashName] = true
end
end
end
end
--------------------------------------------------------------------------------
-- Specialization (LibSpecialization)
--
do
local listID, listRole, listPosition = {}, {}, {}
do
local LibSpec = LibStub("LibSpecialization", true)
if LibSpec then
local function addToTable(specID, role, position, playerName)
listID[playerName] = specID
listRole[playerName] = role
listPosition[playerName] = position
end
local lsTable = {}
LibSpec.RegisterGroup(lsTable, addToTable)
LibSpec.RegisterGuild(lsTable, addToTable)
end
end
function API.GetSpecializationID(playerName)
return listID[playerName]
end
function API.GetSpecializationRole(playerName)
return listRole[playerName]
end
function API.GetSpecializationPosition(playerName)
return listPosition[playerName]
end
end
--------------------------------------------------------------------------------
-- Spell renames
--
do
local tbl = {}
-- This function provides external addons with the spell renames that we use in our modules
function API.GetSpellRename(spellId)
return tbl[spellId]
end
function API.SetSpellRename(spellId, text)
if type(spellId) ~= "number" then error("Invalid spell ID for spell rename.") end
if type(text) ~= "string" or #text < 3 then error("Invalid spell text for spell rename.") end
tbl[spellId] = text
end
end
--------------------------------------------------------------------------------
-- Tools/Plugins option tables
--
do -- Tools
local tbl = {}
-- Get all AceGUI option tables under the "Tools" category
function API.GetToolOptions()
return CopyTable(tbl)
end
-- Register an AceGUI options table for a module under the "Tools" category
function API.RegisterToolOptions(key, settingsTable)
if type(key) ~= "string" then error("The key needs to be a string.") end
if type(settingsTable) ~= "table" then error("The settings table needs to be a table.") end
tbl[key] = settingsTable
end
end
do -- Plugins
local tbl = {}
-- Get all AceGUI option tables under the "Tools" category
function API.GetPluginOptions()
return CopyTable(tbl)
end
-- Register an AceGUI options table for a module under the "Tools" category
function API.RegisterPluginOptions(key, settingsTable)
if type(key) ~= "string" then error("The key needs to be a string.") end
if type(settingsTable) ~= "table" then error("The settings table needs to be a table.") end
tbl[key] = settingsTable
end
end
--------------------------------------------------------------------------------
-- Tooltip
--
do
local bwTooltip = CreateFrame("GameTooltip", "BigWigsTooltip", UIParent, "GameTooltipTemplate")
function API.GetTooltip()
return bwTooltip
end
end
--------------------------------------------------------------------------------
-- Utility
--
do
local floor = math.floor
function API.SecondsToTime(seconds, noFloat)
local L = API:GetLocale("BigWigs")
if seconds > 60 then
local min = floor(seconds/60)
local sec = seconds % 60
return L.shortMinutesAndSeconds:format(min, sec)
elseif seconds < 10 and not noFloat then
local sec = floor(seconds * 10) / 10 -- Turn 9.965 into 9.9 not 10
return L.shortSubTenSeconds:format(sec)
else
return L.shortSecondsOnly:format(seconds)
end
end
end
--------------------------------------------------------------------------------
-- Validation
--
do
local validFramePoints = {
["TOPLEFT"] = true, ["TOPRIGHT"] = true, ["BOTTOMLEFT"] = true, ["BOTTOMRIGHT"] = true,
["TOP"] = true, ["BOTTOM"] = true, ["LEFT"] = true, ["RIGHT"] = true, ["CENTER"] = true,
}
function API.IsValidFramePoint(point)
return validFramePoints[point]
end
function API.GetFramePointList()
local list = {}
local L = API:GetLocale("BigWigs")
for k in next, validFramePoints do
list[k] = L[k]
end
return list
end
end
do
local pcall = pcall
local dummy = UIParent:CreateFontString()
dummy:Hide()
local IsKnownFile = C_UIFileAsset and C_UIFileAsset.IsKnownFile -- XXX [Mainline:✓ MoP:✗ Wrath:✗ TBC:✗ Vanilla:✗]
function API.IsValidMediaPath(mediaPath)
if IsKnownFile then
local result = IsKnownFile(mediaPath)
return result
else
local result = pcall(dummy.SetFont, dummy, mediaPath, 10)
return result
end
end
end
--------------------------------------------------------------------------------
-- Versions
--
do
-- Returns the BigWigs version as a number
function API.GetVersion()
return addonTbl.version, addonTbl.guildVersion
end
-- Returns the BigWigs version hash from Git as a string
function API.GetVersionHash()
return addonTbl.versionHash
end
end
--------------------------------------------------------------------------------
-- Voice
--
do
local addons = {}
function API.RegisterVoicePack(pack)
if type(pack) ~= "string" then error("Voice pack name must be a string.") return end
if not addons[pack] then
addons[pack] = true
else
error(("Voice pack %s already registered."):format(pack))
end
end
function API.HasVoicePack()
if next(addons) then
return true
end
end
end
-------------------------------------------------------------------------------
-- Global
--
BigWigsAPI = setmetatable({}, { __index = API, __newindex = function() end, __metatable = false })