-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerchantPlus.lua
578 lines (494 loc) · 18.4 KB
/
MerchantPlus.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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
--
-- Merchant Plus
-- A Modern Scrollable UI for Merchants
--
-- Copyright 2023 - 2024 SimGuy
--
-- Use of this source code is governed by an MIT-style
-- license that can be found in the LICENSE file or at
-- https://opensource.org/licenses/MIT.
--
local ACR = LibStub("AceConfigRegistry-3.0")
local ACD = LibStub("AceConfigDialog-3.0")
local AddonName, Shared = ...
-- From Metadata.lua
local Metadata = Shared.Metadata
local Callbacks = Metadata.OptionCallbacks
-- From DataFunctions.lua
local Data = Shared.Data
-- From SortFunctions.lua
local Sort = Shared.Sort
-- Push us into shared object
local Addon = {}
Shared.Addon = Addon
-- Push a debugging function into the shared object
local function trace(...)
if Addon:GetOption("Trace") and ... then
print(...)
end
end
Shared.Trace = trace
local InitialWidth = nil
local SwitchOnOpen = false
local BuybackDirty = false
MerchantPlusFrameMixin = {}
-- Re-anchor the Buyback and Currency elements to things that won't move around
function MerchantPlusFrameMixin:OnLoad()
MerchantExtraCurrencyInset:ClearAllPoints()
MerchantExtraCurrencyInset:SetPoint("RIGHT", MerchantMoneyInset, "LEFT", 5, 0)
MerchantExtraCurrencyInset:SetSize(166, 23)
MerchantExtraCurrencyBg:ClearAllPoints()
MerchantExtraCurrencyBg:SetPoint("RIGHT", MerchantMoneyBg, "LEFT", -4, 0)
MerchantExtraCurrencyBg:SetSize(159, 19)
end
MerchantPlusTabMixin = {}
-- Set up the Merchant Plus Tab
function MerchantPlusTabMixin:OnLoad()
PanelTemplates_SetNumTabs(MerchantPlusTabFrame, #MerchantPlusTabFrame.Tabs)
PanelTemplates_SetTab(MerchantPlusTabFrame, 0)
end
-- Handle the click on the tab
function MerchantPlusTabMixin:OnClick()
PanelTemplates_SetTab(MerchantPlusTabFrame, self:GetID())
end
-- Get an option for our own use (fake a request from Ace)
function Addon:GetOption(group, key)
local table = { group }
if key ~= nil then
table.type = "multiselect"
end
table.GetOption = Addon.GetAceOption
return table:GetOption(key)
end
-- Get an option for the AceConfigDialog
function Addon:GetAceOption(key)
local group = self[#self]
local value = false;
local settings = _G[AddonName]
if self.type == "multiselect" then
if settings and settings[group] ~= nil then
value = settings[group][key] or false
elseif Metadata.Defaults and Metadata.Defaults[group] ~= nil then
value = Metadata.Defaults[group][key] or false
end
else
if settings and settings[group] ~= nil then
value = settings[group]
elseif Metadata.Defaults and Metadata.Defaults[group] ~= nil then
value = Metadata.Defaults[group]
end
end
return value
end
-- Get all the options for a particular column
function Addon:GetColumnOptions(column)
local settings = _G[AddonName]
local options = {}
for key, value in pairs(Metadata.Defaults) do
local prefix = "Column_" .. column .. "_"
if string.find(key, '^' .. prefix) then
options[string.sub(key, #prefix+1)] = value
end
end
for key, value in pairs(settings) do
local prefix = "Column_" .. column .. "_"
if string.find(key, '^' .. prefix) then
options[string.sub(key, #prefix+1)] = value
end
end
return options
end
-- Set an option from the AceConfigDialog
function Addon:SetAceOption(value)
local key = self[#self]
if not key then return nil end
local settings = _G[AddonName]
if self.type == "multiselect" then
if settings then
if not settings[key] then
if Metadata.Defaults and Metadata.Defaults[key] then
settings[key] = Metadata.Defaults[key]
else
settings[key] = {}
end
end
if settings[key][value] == nil then
settings[key][value] = true
else
settings[key][value] = nil
end
end
else
if settings and settings[key] ~= value then
settings[key] = value
end
end
if Metadata.OptionCallbacks and Metadata.OptionCallbacks[key] then
local func = Metadata.OptionCallbacks[key]
func(key, value)
end
end
-- Actions related to changing the current frame tab
function Addon:SetTab(index)
-- We only want to act on MerchantFrame
if self == MerchantFrame then
trace("called: SetTab MerchantFrame", index)
-- Reset the filter back to Blizzard's default
ResetSetMerchantFilter()
MerchantFrame.FilterDropdown:Update()
-- If on tab 1 and Merchant Plus is requested, immediately switch
if index == 1 and SwitchOnOpen then
SwitchOnOpen = false
PanelTemplates_SetTab(MerchantPlusTabFrame, 1)
-- Otherwise deselect our tab
else
if PanelTemplates_GetSelectedTab(MerchantPlusTabFrame) == 1 then
PanelTemplates_SetTab(MerchantPlusTabFrame, 0)
end
end
elseif self == MerchantPlusTabFrame then
trace("called: SetTab MerchantPlusTabFrame", index)
-- Update MerchantPlusFrame when its tab is selected
if index == 1 then
-- We're on the Buyback tab, but we need the merchant to be on
-- tab 1 so that the native click handlers work properly
if PanelTemplates_GetSelectedTab(MerchantFrame) == 2 then
SwitchOnOpen = true
-- This taints MerchantFrame.selectedTab, but there's no
-- other way to force the Frame to think it's on the
-- Merchant tab if transitioning from the Buyback tab.
--
-- The taint clears itself if the user transitions to
-- the Merchant tab manually at any time.
--
-- This should be okay because nothing ever copies this
-- value except into lastTab and that value seems unused.
PanelTemplates_SetTab(MerchantFrame, 1)
end
-- Deselect the Merchant tab, since we should always be
-- transitioning from it, but we don't want to change the
-- internal value of selectedTab if we don't have to.
PanelTemplates_DeselectTab(MerchantFrameTab1)
Addon:UpdateFrame()
end
end
end
-- This gets called any time that our tab becomes focused or any time MerchantFrame_Update()
-- gets called. We want to know if Blizzard starts messing with things from other tabs.
function Addon:UpdateFrame()
-- Don't do anything if the MerchantFrame isn't visible
if not MerchantFrame:IsShown() then
return
end
-- Check if our tab is selected
local show = PanelTemplates_GetSelectedTab(MerchantPlusTabFrame) == 1
-- Check if we are on the Buyback tab
local buyback = PanelTemplates_GetSelectedTab(MerchantFrame) == 2
-- We should have saved the width, but if not use 336 which has been standard for a while
local width = show and Addon:GetOption("WindowWidth") or InitialWidth or 336
trace("called: UpdateFrame; show", show)
-- Set the width of the frame wider or back to the default
MerchantFrame:SetWidth(width)
-- Hide or show MerchantItem buttons
for i = 1, BUYBACK_ITEMS_PER_PAGE do
local button = _G["MerchantItem"..i]
button:SetShown(not show and (i <= MERCHANT_ITEMS_PER_PAGE or buyback))
end
-- Hide or show the filtering dropdown
MerchantFrame.FilterDropdown:SetShown(not show)
-- Set up the frame if our tab is selected
if show then
-- Set the portrait and name of the frame
MerchantFrame:SetTitle(UnitName("npc"))
MerchantFrame:SetPortraitToUnit("npc")
-- Hide all the buttons from the merchant page
MerchantPageText:Hide()
MerchantPrevPageButton:Hide()
MerchantNextPageButton:Hide()
-- Hide the Buyback background
BuybackBG:Hide()
-- Update the state of repair buttons
MerchantFrame_UpdateRepairButtons()
-- Update the state of the Sell All Junk button
if C_MerchantFrame.IsSellAllJunkEnabled and C_MerchantFrame.IsSellAllJunkEnabled() then
-- The button is anchored weirdly unless the Repair Buttons are shown.
if not CanMerchantRepair() then
MerchantSellAllJunkButton:ClearAllPoints()
MerchantSellAllJunkButton:SetPoint("BOTTOM", MerchantFrame, "BOTTOMLEFT", 170, 33)
end
local hasJunkItems = C_MerchantFrame.GetNumJunkItems() > 0;
MerchantSellAllJunkButton.Icon:SetDesaturated(not hasJunkItems);
MerchantSellAllJunkButton:SetEnabled(hasJunkItems);
MerchantSellAllJunkButton:Show()
end
-- Reanchor and show the Buyback button
-- Reanchoring works around an issue with ElvUI when switching from the Buyback tab
MerchantBuyBackItem:ClearAllPoints()
MerchantBuyBackItem:SetPoint("BOTTOM", MerchantFrame, "BOTTOMLEFT", 263.5, 33)
MerchantBuyBackItem:Show()
-- Update the Buyback button if something happened while we weren't looking
if BuybackDirty then
Addon:UpdateBuyback()
end
-- Show the frame backgrounds related to the repair and buyback
MerchantFrameBottomLeftBorder:Show()
end
-- Show or hide our own frame now that everything is set up
MerchantPlusFrame:SetShown(show)
end
-- If a buyback happens when we are on the buyback tab, flag that we need to update it next
-- time UpdateFrame() is called.
function Addon:HandleBuyback()
local buybackTab = PanelTemplates_GetSelectedTab(MerchantFrame) == 2
trace("called: HandleBuyback", buybackTab)
if buybackTab then
BuybackDirty = true
end
end
-- Blizzard doesn't put this functionality in a separate function so we have to duplicate it here.
-- MerchantFrame_Update() would take care of this when the inventory changes, but only when it
-- thinks the Merchant tab is active, meaning if you buy back from the Buyback tab it will not
-- update.
--
-- This taints the ItemButton and elements, but we will attempt to clear taint when the window
-- closes in hopes that it doesn't spread.
--
-- To minimize how often this gets called, we will only call it if a buyback happens while on
-- the buyback tab.
function Addon:UpdateBuyback()
trace("called: UpdateBuyback", BuybackDirty)
if not BuybackDirty then
return
end
local last = GetNumBuybackItems()
local name, texture, _, quantity, _, _, isBound = GetBuybackItemInfo(last)
if ( name ) then
trace("updated: UpdateBuyback", name)
MerchantBuyBackItemName:SetText(name)
SetItemButtonCount(MerchantBuyBackItemItemButton, quantity)
SetItemButtonTexture(MerchantBuyBackItemItemButton, texture)
MerchantFrameItem_UpdateQuality(MerchantBuyBackItem, GetBuybackItemLink(last), isBound)
else
trace("cleared: UpdateBuyback")
MerchantBuyBackItemName:SetText("")
SetItemButtonCount(MerchantBuyBackItemItemButton, nil)
SetItemButtonTexture(MerchantBuyBackItemItemButton, nil)
MerchantFrameItem_UpdateQuality(MerchantBuyBackItem, nil)
-- Hide the tooltip upon sale
if GameTooltip:IsOwned(MerchantBuyBackItemItemButton) then
GameTooltip:Hide()
end
end
MerchantBuyBackItem:Show()
BuybackDirty = false
end
-- A callback for when UpdateTableBuilderLayout is called to define the columns that are shown
function Addon:SetTableLayout()
trace("called: SetTableLayout")
Data.Functions = {}
for _, key in ipairs(Metadata.ColumnSort) do
local col = Metadata.Columns[key]
local enabled = Addon:GetOption("Columns", key)
local required = col.required
trace("column status:", key, enabled)
if enabled or required then
if col.datafunctions then
for _, func in ipairs(col.datafunctions) do
if type(func) == "function" then
tInsertUnique(Data.Functions, func)
end
end
end
local options = Addon:GetColumnOptions(key)
MerchantPlusItemList:AddColumn(key, col.name, col.celltype, col.fixed,
col.width, col.padding[1], col.padding[2], col, options)
end
end
end
-- Update the displayed columns when they change
function Addon:UpdateColumns()
MerchantPlusItemList:UpdateTableBuilderLayout()
end
-- Update the saved sort when saving is toggled
function Addon:UpdateSort()
trace("called: Options_Sort_Update")
local settings = _G[AddonName]
local save = Addon:GetOption('SortRemember')
if save then
local order, state = MerchantPlusItemList:GetSortOrder()
if settings then
settings["SortOrder"] = order or ""
settings["SortState"] = state or 0
end
else
if settings then
settings["SortOrder"] = nil
settings["SortState"] = nil
end
end
end
-- Handle any events that are needed
function Addon:HandleEvent(event, target)
if event == "MERCHANT_SHOW" then
trace("called: MERCHANT_SHOW")
-- Store the width of the frame when it first opened so we can restore it
if not InitialWidth then
InitialWidth = MerchantFrame:GetWidth()
end
-- If the user wants our tab to show by default, flag that for
-- the next tab switch event
if Addon:GetOption("TabDefault") then
SwitchOnOpen = true
end
-- Restore the saved sort when opening the vendor if configured
if Addon:GetOption("SortRemember") then
local order = Addon:GetOption("SortOrder")
local state = Addon:GetOption("SortState")
MerchantPlusItemList:SetSortOrder(order, state)
else
MerchantPlusItemList:SetSortOrder("")
end
end
-- This generally means the merchant's contents changed
if event == "MERCHANT_UPDATE" then
trace("called: MERCHANT_UPDATE")
MerchantPlusItemList:RefreshScrollFrame()
end
-- I haven't yet found a case where we need to do anything as this is
-- usually followed by MERCHANT_UPDATE
if event == "MERCHANT_FILTER_ITEM_UPDATE" then
trace("called: MERCHANT_FILTER_ITEM_UPDATE")
end
if event == "MERCHANT_CLOSED" then
trace("called: MERCHANT_CLOSED")
-- Hide the MerchantPlus frame so we don't call OnShow before
-- MerchantFrame sets itself up.
MerchantPlusFrame:Hide()
PanelTemplates_SetTab(MerchantPlusTabFrame, 0)
-- Close any confirmation dialogs if we are closed
StaticPopup_Hide("CONFIRM_PURCHASE_NONREFUNDABLE_ITEM")
StaticPopup_Hide("CONFIRM_PURCHASE_TOKEN_ITEM")
StaticPopup_Hide("CONFIRM_HIGH_COST_ITEM")
StaticPopup_Hide("CONFIRM_PURCHASE_ITEM_DELAYED")
Addon:ClearMerchantTaint()
end
-- If player's inventory changed, the items available on the vendor might change
if event == "UNIT_INVENTORY_CHANGED" then
trace("called: UNIT_INVENTORY_CHANGED")
MerchantPlusItemList:RefreshScrollFrame()
end
if event == "ADDON_LOADED" and target == AddonName then
if not _G[AddonName] then
_G[AddonName] = {}
end
trace("called: ADDON_LOADED")
-- Don't register options unless they're defined.
if Metadata.Options then
Metadata.Options.get = Addon.GetAceOption
Metadata.Options.set = Addon.SetAceOption
ACR:RegisterOptionsTable(AddonName, Metadata.Options)
ACD:AddToBlizOptions(AddonName, Metadata.FriendlyName)
end
MerchantPlusItemList.SetTableLayout = Addon.SetTableLayout
MerchantPlusItemList.SortCallback = Addon.UpdateSort
MerchantPlusItemList.GetDataCount = Data.GetMerchantCount
MerchantPlusItemList.GetData = Data.UpdateMerchant
MerchantPlusItemList.Sort = Sort.Sort
-- Detect ElvUI and register callback to apply the skin
if ElvUI then
Addon.ElvUI = unpack(ElvUI)
Addon.ElvUISkin = Addon.ElvUI:GetModule('Skins')
Addon.ElvUISkin:AddCallbackForAddon('MerchantPlus', 'Merchant Plus', Addon.ElvUILoad)
end
end
end
-- Calling native functions may taint variables in secure frames, but we
-- can nil them to hopefully ensure the taint doesn't propagate.
function Addon:ClearTaint(n)
local t = _G[n]
trace("called: ClearTaint", n)
for k in pairs(t) do
-- Don't clear the selectedTab automatically, because it breaks stuff
if not (n == 'MerchantFrame' and k == 'selectedTab') then
Addon:ClearSingleTaint(n, k)
end
end
end
-- Provide a way to clear a single value's taint if needed
function Addon:ClearSingleTaint(n, k)
local t = _G[n]
local secure, addon = issecurevariable(t, k)
if secure == false and addon == AddonName then
t[k] = nil
local fixed = issecurevariable(t, k)
trace("tainted:", n, addon, k, "| cleared:", fixed)
end
end
-- Clear all the taint we can when the window is closed
function Addon:ClearMerchantTaint()
Addon:ClearTaint("MerchantFrame")
Addon:ClearTaint("MerchantBuyBackItem")
Addon:ClearTaint("MerchantBuyBackItemItemButton")
end
-- Clear taint specific to purchasing items
function Addon:ClearPurchaseTaint()
Addon:ClearTaint("MerchantFrame")
end
-- When the Merchant Frame opens, we need to reskin any headers that may have
-- been created since we last checked
function Addon:ElvUIHeaders()
trace("called: ElvUIHeaders")
for _, header in next, { MerchantPlusItemList.HeaderContainer:GetChildren() } do
if not header.IsSkinned then
header:DisableDrawLayer('BACKGROUND')
header:CreateBackdrop('Transparent')
header.IsSkinned = true
end
end
end
-- When loading, if ElvUI is skinning the Merchant Frame, we'll try to adopt
-- the same style
function Addon:ElvUILoad()
local E = Addon.ElvUI
local S = Addon.ElvUISkin
trace("called: ElvUILoad")
-- Only skin us if the Merchant Frame is skinned
if not (E.private.skins.blizzard.enable and E.private.skins.blizzard.merchant) then return end
trace("skinning: ElvUI")
S:HandleTab(MerchantPlusTab)
MerchantPlusTab:ClearAllPoints()
MerchantPlusTab:Point('TOPLEFT', MerchantFrameTab2, 'TOPRIGHT', -5, 0)
MerchantPlusFrame:StripTextures()
MerchantPlusItemList:StripTextures()
S:HandleTrimScrollBar(MerchantPlusItemList.ScrollBar)
MerchantPlusItemList.ScrollBar:ClearAllPoints()
MerchantPlusItemList.ScrollBar:Point('TOPRIGHT', MerchantPlusItemList, -6, -16)
MerchantPlusItemList.ScrollBar:Point('BOTTOMRIGHT', MerchantPlusItemList, -6, 16)
MerchantPlusItemList.ScrollBox:SetTemplate('Transparent')
MerchantPlusItemList.NineSlice:SetTemplate('Transparent')
MerchantPlusItemList.NineSlice:SetInside(MerchantPlusItemList)
MerchantPlusItemList:SetTemplate('Transparent')
hooksecurefunc(MerchantPlusItemList, "RefreshScrollFrame", Addon.ElvUIHeaders)
end
-- These are init steps specific to this addon
function Addon:Init()
hooksecurefunc("PanelTemplates_SetTab", Addon.SetTab)
hooksecurefunc("MerchantFrame_Update", Addon.UpdateFrame)
-- Buyback while on the Buyback tab
hooksecurefunc("BuybackItem", Addon.HandleBuyback)
-- Attempt to clear tainted table data from the MerchantFrame
-- after buying something
hooksecurefunc("BuyMerchantItem", Addon.ClearPurchaseTaint)
Addon.Events = CreateFrame("Frame")
Addon.Events:RegisterEvent("ADDON_LOADED")
Addon.Events:RegisterEvent("MERCHANT_SHOW")
Addon.Events:RegisterEvent("MERCHANT_UPDATE")
Addon.Events:RegisterEvent("MERCHANT_FILTER_ITEM_UPDATE")
Addon.Events:RegisterEvent("MERCHANT_CLOSED")
Addon.Events:RegisterEvent("UNIT_INVENTORY_CHANGED")
Addon.Events:SetScript("OnEvent", Addon.HandleEvent)
Callbacks.SortRemember = Addon.UpdateSort
Callbacks.Columns = Addon.UpdateColumns
end
Addon:Init()
Data:Init()