-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.lua
More file actions
519 lines (450 loc) · 15 KB
/
Copy pathCore.lua
File metadata and controls
519 lines (450 loc) · 15 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
-- Broker: BagFu
Broker_BagFu = LibStub("AceAddon-3.0"):NewAddon("Broker_BagFu", "AceEvent-3.0")
local Broker_BagFu, self = Broker_BagFu, Broker_BagFu
local LDB = LibStub:GetLibrary("LibDataBroker-1.1")
local L = LibStub("AceLocale-3.0"):GetLocale("Broker_BagFu")
local icon = LibStub("LibDBIcon-1.0")
local _G = _G
-- LDB data object
local dataobj = LDB:NewDataObject("Broker_BagFu", {
type = "data source",
text = '?/?',
icon = "Interface\\AddOns\\Broker_BagFu\\icon.tga",
})
-- Options database and defaults
local addonOptionsFrameName
local db
local defaults = {
profile = {
showDepletion = false,
includeAmmo = true,
includeProfession = true,
showTotal = true,
openBagsAtBank = false,
openBagsAtVendor = false,
showBagsInTooltip = true,
showColours = false,
minimap = {
hide = false,
},
},
}
-- Locations of these functions vary between WoW versions.
local GetAddOnMetadata
local GetBagName
local GetContainerNumFreeSlots
local GetContainerNumSlots
-- We need to know if we're in the Classic client at multiple points throughout
-- the addon to decide which version of a function to use.
local IsClassic
do
local is_retail = WOW_PROJECT_ID == WOW_PROJECT_MAINLINE
-- The inverse of the above should be true for Classic.
local is_classic = not is_retail
IsClassic = function()
return is_classic
end
end
-- Bag functions are in different locations depending on game version.
if IsClassic () then
GetAddOnMetadata = _G.GetAddOnMetadata
GetBagName = _G.GetBagName
GetContainerNumFreeSlots = _G.GetContainerNumFreeSlots
GetContainerNumSlots = _G.GetContainerNumSlots
else
GetAddOnMetadata = C_AddOns.GetAddOnMetadata
GetBagName = C_Container.GetBagName
GetContainerNumFreeSlots = C_Container.GetContainerNumFreeSlots
GetContainerNumSlots = C_Container.GetContainerNumSlots
end
-- Local functions and tables
local select = select
local CloseAllBags = CloseAllBags
local GetItemInfo = GetItemInfo
local GetItemInfoInstant = GetItemInfoInstant
local GetItemQualityColor = GetItemQualityColor
local IsShiftKeyDown = IsShiftKeyDown
local OpenAllBags = OpenAllBags
local ToggleBackpack = ToggleBackpack
-- Addon Metadata
local ADDON_TITLE = GetAddOnMetadata("Broker_BagFu", "Title")
local ADDON_NOTES = GetAddOnMetadata("Broker_BagFu", "Notes")
-- Constants
local NUM_BAG_SLOTS = NUM_BAG_SLOTS
local function GetOptions()
local options = {
type = "group",
name = ADDON_TITLE,
get = function(info)
return db[info[#info]]
end,
set = function(info, value)
db[info[#info]] = value
Broker_BagFu:BAG_UPDATE_DELAYED()
end,
args = {
bbdesc = {
type = "description",
order = 0,
name = ADDON_NOTES,
},
showColours = {
type = "toggle",
order = 50,
name = L["Use Colours"],
desc = L["Use colouring to show level of bag fullness"],
},
showBagsInTooltip = {
type = "toggle",
order = 75,
name = L["Bags in Tooltip"],
desc = L["Show all bags in the Broker: Bags tooltip"],
},
includeProfession = {
type = "toggle",
order = 200,
name = L["Profession Bags"],
desc = L["Include profession bags"],
},
showDepletion = {
type = "toggle",
order = 300,
name = L["Bag Depletion"],
desc = L["Show depletion of bags"],
},
showTotal = {
type = "toggle",
order = 400,
name = L["Bag Total"],
desc = L["Show total amount of space in bags"],
},
openBagsAtBank = {
type = "toggle",
order = 500,
name = L["Open Bags at Bank"],
desc = L["Open all of your bags when you're at the bank"],
set = function()
db.openBagsAtBank = not db.openBagsAtBank
Broker_BagFu:ToggleOpenAtBank()
end,
},
openBagsAtVendor = {
type = "toggle",
order = 600,
name = L["Open Bags at Vendor"],
desc = L["Open all of your bags when you're at a vendor"],
set = function()
db.openBagsAtVendor = not db.openBagsAtVendor
Broker_BagFu:ToggleOpenAtVendor()
end,
},
minimap = {
name = L["Minimap Icon"],
desc = L["Toggle minimap icon"],
type = "toggle",
get = function() return not db.minimap.hide end,
set = function()
db.minimap.hide = not db.minimap.hide
if db.minimap.hide then
icon:Hide("Broker_BagFu")
else
icon:Show("Broker_BagFu")
end
end,
},
},
}
if IsClassic() then
options.args.includeAmmo = {
type = "toggle",
order = "100",
name = L["Ammo Bags"],
desc = L["Include ammo bags"],
}
end
return options
end
local function OpenOptions()
if Settings and Settings.OpenToCategory then
Settings.OpenToCategory(addonOptionsFrameName)
else
InterfaceOptionsFrame_OpenToCategory(addonOptionsFrameName)
end
end
-- Ammo bags exist again in Classic
local function IsAmmoBag(bagType)
-- 4: Soul Bag
-- 2: Ammo Pouch
-- 1: Quiver
if bagType == 4 or bagType == 2 or bagType == 1 then
return true
end
return false
end
-- Handy function to tell if something is a profession bag.
local IsProfessionBag
do
local bagTypes = {
-- 1048576: Tackle Box
[0x100000] = true,
-- 65536: Cooking Bag
[0x10000] = true,
-- 1024: Mining Bag
[0x0400] = true,
-- 512: Gem Bag
[0x0200] = true,
-- 128: Engineering Bag
[0x0080] = true,
-- 64: Enchanting Bag
[0x0040] = true,
-- 32: Herb Bag
[0x0020] = true,
-- 16: Inscription Bag
[0x0010] = true,
-- 8: Leatherworking Bag
[0x0008] = true,
}
IsProfessionBag = function(bagType)
return bagTypes[bagType] or false
end
end
-- Get text colour for a bag based on percentage of fullness.
local function GetBagColour(percent)
local r, g, b
if percent < 0 then
r, g, b = 1, 0, 0
elseif percent <= 0.5 then
r, g, b = 1, percent * 2, 0
elseif percent >= 1 then
r, g, b = 0, 1, 0
else
r, g, b = 2 - percent * 2, 1, 0
end
return ("|cff%02x%02x%02x"):format(r * 255, g * 255, b * 255)
end
-- This function is responsible for getting the bag icons and quality.
-- It will cache what it finds instead of repeatedly asking the client.
local GetBagIconAndQuality
do
local bagIcon = {}
local bagQuality = {}
local BACKPACK_ICON = "Interface\\Icons\\INV_Misc_Bag_08:16"
local ITEM_QUALITY_COMMON
if IsClassic() then
ITEM_QUALITY_COMMON = LE_ITEM_QUALITY_COMMON
else
ITEM_QUALITY_COMMON = Enum.ItemQuality.Common
end
GetBagIconAndQuality = function(name)
-- This returns less than the usual GetItemInfo() but should always
-- get us the itemID at least.
local itemID = GetItemInfoInstant(name)
-- If we already cached a result, use that.
if bagQuality[itemID] then
local icon = bagIcon[itemID]
local quality = bagQuality[itemID]
return quality, icon
end
-- Otherwise query the client for more info
local item = Item:CreateFromItemID(itemID)
item:ContinueOnItemLoad(function()
local _,_,quality,_,_,_,_,_,_,icon = GetItemInfo(name)
bagIcon[itemID] = icon
bagQuality[itemID] = quality
end)
-- Return a default icon and quality if we fell through.
return ITEM_QUALITY_COMMON, BACKPACK_ICON
end
end
-- Shown on LDB dataobj mouseover.
function dataobj:OnTooltipShow()
-- Title, grab it from the TOC.
self:AddLine(ADDON_TITLE)
-- Show the bags in the tooltip, if needed
if db.showBagsInTooltip then
for i = 0, NUM_BAG_SLOTS do
local bagSize = GetContainerNumSlots(i)
if bagSize ~= nil and bagSize > 0 then
local name = GetBagName(i)
if name then
local quality, icon
if i == 0 then
icon = "Interface\\Icons\\INV_Misc_Bag_08:16"
quality = select(4, GetItemQualityColor(1))
else
quality, icon = GetBagIconAndQuality(name)
quality = select(4, GetItemQualityColor(quality))
icon = icon .. ":16"
end
local freeSlots, bagType = GetContainerNumFreeSlots(i)
local takenSlots = bagSize - freeSlots
local colour
if db.showColours then
local fillLevel
-- Ammo bags reverse the colour, it's good for them to
-- be full.
if IsAmmoBag(bagType) then
fillLevel = 1 - ((bagSize - takenSlots) / bagSize)
else
fillLevel = (bagSize - takenSlots) / bagSize
end
colour = GetBagColour(fillLevel)
name = ("|c%s%s|r"):format(quality, name)
end
if db.showDepletion then
takenSlots = bagSize - takenSlots
end
local textL, textR
textL = ("|T%s|t %s"):format(icon, name)
if db.showTotal then
textR = ("%s%d/%d%s"):format(
colour and colour or "",
takenSlots,
bagSize,
colour and "|r" or ""
)
else
textR = ("%s%d%s"):format(
colour and colour or "",
takenSlots,
colour and "|r" or ""
)
end
self:AddDoubleLine(textL, textR)
end
end
end
end
-- Hints!
self:AddLine("|cffffff00" .. L["Click|r to open your bags"])
self:AddLine("|cffffff00" .. L["Right-Click|r to open options menu"])
end
function dataobj:OnClick(button)
if button == "LeftButton" then
if not ContainerFrame1:IsShown() then
for i = 1, NUM_BAG_SLOTS do
local cf = _G["ContainerFrame" .. (i + 1)]
if cf:IsShown() then
cf:Hide()
end
end
ToggleBackpack()
if ContainerFrame1:IsShown() then
for i = 1, NUM_BAG_SLOTS do
local usable = true
local _, bagType = GetContainerNumFreeSlots(i)
if not db.includeAmmo and IsAmmoBag(bagType) then
usable = false
end
if not db.includeProfession and IsProfessionBag(bagType) then
usable = false
end
if usable or IsShiftKeyDown() then
ToggleBag(i)
end
end
end
else
for i = 0, NUM_BAG_SLOTS do
local cf = _G["ContainerFrame" .. (i + 1)]
if cf:IsShown() then
cf:Hide()
end
end
end
elseif button == "RightButton" then
OpenOptions()
end
end
function Broker_BagFu:OnInitialize()
local _
-- Saved Vars
self.db = LibStub("AceDB-3.0"):New("BrokerBagsDB", defaults, "Default")
db = self.db.profile
icon:Register("Broker_BagFu", dataobj, db.minimap)
-- Register the config
LibStub("AceConfigRegistry-3.0"):RegisterOptionsTable(
"Broker_BagFu",
GetOptions
)
_, addonOptionsFrameName = LibStub("AceConfigDialog-3.0"):AddToBlizOptions(
"Broker_BagFu",
ADDON_TITLE
)
end
function Broker_BagFu:OnEnable()
self:RegisterEvent("BAG_UPDATE_DELAYED")
self:ToggleOpenAtBank()
self:ToggleOpenAtVendor()
-- Force a BAG_UPDATE since it no longer seems to fire at PLAYER_LOGIN
-- since 5.0.4
self:BAG_UPDATE_DELAYED()
end
function Broker_BagFu:ToggleOpenAtBank()
if db.openBagsAtBank then
self:RegisterEvent("BANKFRAME_OPENED", function()
OpenAllBags(nil)
end)
else
self:UnregisterEvent("BANKFRAME_OPENED")
end
end
function Broker_BagFu:ToggleOpenAtVendor()
if db.openBagsAtVendor then
Broker_BagFu:RegisterEvent("MERCHANT_SHOW", function()
OpenAllBags(nil)
end)
Broker_BagFu:RegisterEvent("MERCHANT_CLOSED", function()
CloseAllBags(nil)
end)
else
Broker_BagFu:UnregisterEvent("MERCHANT_SHOW")
Broker_BagFu:UnregisterEvent("MERCHANT_CLOSED")
end
end
function Broker_BagFu:BAG_UPDATE_DELAYED()
local totalSlots = 0
local takenSlots = 0
for i = 0, NUM_BAG_SLOTS do
local usable = true
local freeSlots, bagType = GetContainerNumFreeSlots(i)
if i >= 1 then
if not db.includeAmmo and IsAmmoBag(bagType) then
usable = false
end
if not db.includeProfession and IsProfessionBag(bagType) then
usable = false
end
end
if usable then
local bagSize = GetContainerNumSlots(i)
if bagSize ~= nil and bagSize > 0 then
totalSlots = totalSlots + bagSize
takenSlots = takenSlots + (bagSize - freeSlots)
end
end
end
local colour
if db.showColours then
colour = GetBagColour((totalSlots - takenSlots) / totalSlots)
end
if db.showDepletion then
takenSlots = totalSlots - takenSlots
end
local displayText
if db.showTotal then
displayText = ("%s%d/%d%s"):format(
colour and colour or "",
takenSlots,
totalSlots,
colour and "|r" or ""
)
else
displayText = ("%s%d%s"):format(
colour and colour or "",
takenSlots,
colour and "|r" or ""
)
end
dataobj.text = displayText
end