-
Notifications
You must be signed in to change notification settings - Fork 3
/
Item.lua
457 lines (385 loc) · 11.6 KB
/
Item.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
local _, Inventorian = ...
local L = LibStub("AceLocale-3.0"):GetLocale("Inventorian")
local ItemCache = LibStub("LibItemCache-1.1-Inventorian")
local ItemSearch = LibStub("LibItemSearch-Inventorian-1.0")
local InventorianItemMixin = {}
Inventorian.Item = {}
Inventorian.Item.count = 0
Inventorian.Item.pool = nil
function Inventorian.Item:Create()
if not self.pool then
self:CreateItemPool()
end
local item = next(self.pool)
if item then
self.pool[item] = nil
else
self.count = self.count + 1
item = CreateFrame("ItemButton", ("InventorianItemButton%d"):format(self.count), nil, "ContainerFrameItemButtonTemplate")
item = self:WrapItemButton(item)
end
return item
end
function Inventorian.Item:WrapItemButton(item)
item = Mixin(item, InventorianItemMixin)
item:UnregisterAllEvents()
-- replace scripts
item:SetScript("OnEvent", item.OnEvent)
item:SetScript("OnEnter", item.OnEnter)
item:SetScript("OnLeave", item.OnLeave)
item:SetScript("OnShow", item.OnShow)
-- elements
local name = item:GetName()
item.IconQuestTexture = _G[name .. "IconQuestTexture"]
item.Cooldown = _G[name .. "Cooldown"]
-- adjust ther normal texture to be less "obvious" on empty buttons
item:GetNormalTexture():SetVertexColor(1,1,1,0.66)
-- cleanup state
item:Reset()
return item
end
local MAX_CONTAINER_ITEMS = 36
function Inventorian.Item:CreateItemPool()
self.pool = {}
for c = 1, NUM_CONTAINER_FRAMES do
for i = 1, MAX_CONTAINER_ITEMS do
local item = _G[("ContainerFrame%dItem%d"):format(c, i)]
if item then
item:SetID(0)
item:ClearAllPoints()
item = self:WrapItemButton(item)
self.pool[item] = true
end
end
end
end
function InventorianItemMixin:Free()
self:Hide()
self:SetParent(nil)
self:SetID(0)
self:ClearAllPoints()
self:UnlockHighlight()
Inventorian.Item.pool[self] = true
end
function InventorianItemMixin:Set(container, bag, slot)
self.container = container
self.bag = bag
self.slot = slot
self:SetParent(self:GetBagContainer(container, bag))
self:SetID(slot)
if self:IsVisible() then
self:Update()
else
self:Show()
end
end
function InventorianItemMixin:OnShow()
if not self:GetParent() then return end
self:Update()
end
-----------------------------------------------------------------------
-- Button style setters
function InventorianItemMixin:Update()
if not self:IsVisible() then
return
end
local icon, count, locked, quality, readable, lootable, link, noValue, itemID, isBound = self:GetInfo()
self:SetItem(link)
self:SetTexture(icon)
self:SetCount(count)
self:SetLocked(locked)
self:SetReadable(readable)
self:UpdateCooldown()
self:UpdateBorder(quality, noValue, isBound)
self:UpdateSearch(self.container.searchText)
if GameTooltip:IsOwned(self) then
if not self:GetItem() then
self:OnLeave()
end
self:UpdateTooltip()
end
end
function InventorianItemMixin:SetItem(itemLink)
self.item = itemLink
self.itemLink = itemLink
end
function InventorianItemMixin:SetTexture(icon)
SetItemButtonTexture(self, icon)
if icon then
self.icon:SetAlpha(1)
else
self.icon:SetAlpha(0.8)
end
end
function InventorianItemMixin:SetCount(count)
SetItemButtonCount(self, count)
end
function InventorianItemMixin:SetLocked(locked)
SetItemButtonDesaturated(self, locked)
end
function InventorianItemMixin:UpdateLocked()
self:SetLocked(self:IsLocked())
end
-- returns true if the slot is locked, and false otherwise
function InventorianItemMixin:IsLocked()
return select(3, self:GetInfo())
end
function InventorianItemMixin:SetReadable(readable)
self.readable = readable
end
function InventorianItemMixin:UpdateCooldown()
if self:GetItem() and not self:IsCached() then
local start, duration, enable = C_Container.GetContainerItemCooldown(self.bag, self.slot)
CooldownFrame_Set(self.Cooldown, start, duration, enable)
if duration > 0 and enable == 0 then
SetItemButtonTextureVertexColor(self, 0.4, 0.4, 0.4)
else
SetItemButtonTextureVertexColor(self, 1, 1, 1)
end
else
SetItemButtonTextureVertexColor(self, 1, 1, 1)
self.Cooldown:Hide()
end
end
function InventorianItemMixin:SetBorderColor(r, g, b)
self.IconBorder:SetVertexColor(r, g, b)
self.IconBorder:Show()
end
function InventorianItemMixin:HideBorder()
self.NewItemTexture:Hide()
self.IconQuestTexture:Hide()
self.BattlepayItemTexture:Hide()
self.IconBorder:Hide()
self.JunkIcon:Hide()
ClearItemButtonOverlay(self)
if self.flashAnim:IsPlaying() or self.newitemglowAnim:IsPlaying() then
self.flashAnim:Stop()
self.newitemglowAnim:Stop()
end
end
function InventorianItemMixin:UpdateBorder(quality, noValue, isBound)
local item = self:GetItem()
self:HideBorder()
if item then
local isQuestItem, questId, isActive = self:GetQuestInfo()
if questId and not isActive then
self.IconQuestTexture:SetTexture(TEXTURE_ITEM_QUEST_BANG)
self.IconQuestTexture:Show()
elseif questId or isQuestItem then
self.IconQuestTexture:SetTexture(TEXTURE_ITEM_QUEST_BORDER)
self.IconQuestTexture:Show()
end
local isNewItem, isBattlePayItem = self:IsNew()
if isNewItem then
if isBattlePayItem then
self.BattlepayItemTexture:Show()
else
if quality and NEW_ITEM_ATLAS_BY_QUALITY[quality] then
self.NewItemTexture:SetAtlas(NEW_ITEM_ATLAS_BY_QUALITY[quality])
else
self.NewItemTexture:SetAtlas("bags-glow-white")
end
self.NewItemTexture:Show()
end
if not self.flashAnim:IsPlaying() and not self.newitemglowAnim:IsPlaying() then
self.flashAnim:Play()
self.newitemglowAnim:Play()
end
end
SetItemButtonQuality(self, quality, item, false, isBound)
self.JunkIcon:SetShown(quality == Enum.ItemQuality.Poor and not noValue and MerchantFrame:IsShown())
end
end
function InventorianItemMixin:UpdateSearch(text)
local found = false
if text and self:GetItem() then
found = ItemSearch:Find(self:GetItem(), text)
end
if not text or found then
self:SetMatchesSearch(found or nil)
local isNewItem = self:IsNew()
if isNewItem and not self.newitemglowAnim:IsPlaying() then
self.newitemglowAnim:Play()
end
else
self:SetMatchesSearch(false)
if self.flashAnim:IsPlaying() or self.newitemglowAnim:IsPlaying() then
self.flashAnim:Stop()
self.newitemglowAnim:Stop()
end
end
end
function InventorianItemMixin:UpdateItemContextOverlay()
ItemButtonMixin.UpdateItemContextOverlay(self)
-- update anchoring for the color texture to cover the borders
if self.ItemContextOverlay:GetTexture() == nil then
self.ItemContextOverlay:ClearAllPoints()
self.ItemContextOverlay:SetSize(39, 39)
self.ItemContextOverlay:SetPoint("CENTER")
end
end
function InventorianItemMixin:Highlight(enable)
if enable then
self:LockHighlight()
else
self:UnlockHighlight()
end
end
function InventorianItemMixin:OnEvent(event, ...)
if event == "ITEM_DATA_LOAD_RESULT" then
local id = (...)
if id == self.itemID then
self:UnregisterEvent("ITEM_DATA_LOAD_RESULT")
self:Update()
end
end
end
function InventorianItemMixin:OnEnter()
if self:IsCached() then
self.cacheOverlay = self.cacheOverlay or self:CreateCacheOverlay()
self.cacheOverlay:Show()
self.cacheOverlay:GetScript("OnEnter")(self.cacheOverlay)
else
if self:IsBank() or self:IsReagentBank() then
if self:GetItem() then
local id = self:IsBank() and BankButtonIDToInvSlotID(self:GetID()) or ReagentBankButtonIDToInvSlotID(self:GetID())
GameTooltip:SetOwner(self, "ANCHOR_NONE")
ContainerFrameItemButton_CalculateItemTooltipAnchors(self, GameTooltip)
GameTooltip:SetInventoryItem("player", id)
GameTooltip:Show()
CursorUpdate(self)
end
else
ContainerFrameItemButtonMixin.OnEnter(self)
end
end
end
function InventorianItemMixin:OnLeave()
GameTooltip:Hide()
BattlePetTooltip:Hide()
ResetCursor()
end
InventorianItemMixin.UpdateTooltip = InventorianItemMixin.OnEnter
-----------------------------------------------------------------------
-- Utility
local function CacheOverlay_OnEnter(self)
local parent = self:GetParent()
if parent:GetItem() then
GameTooltip:SetOwner(parent, "ANCHOR_NONE")
ContainerFrameItemButton_CalculateItemTooltipAnchors(parent, GameTooltip)
GameTooltip:SetHyperlink(parent:GetItem())
GameTooltip:Show()
end
parent:LockHighlight()
CursorUpdate(parent)
end
local function CacheOverlay_OnLeave(self)
self:GetParent():OnLeave()
self:Hide()
end
local function CacheOverlay_OnHide(self)
self:GetParent():UnlockHighlight()
end
local function CacheOverlay_OnClick(self)
local item = self:GetParent():GetItem()
if item then
HandleModifiedItemClick(item)
end
end
function InventorianItemMixin:CreateCacheOverlay()
local overlay = CreateFrame("Button", nil, self)
overlay:RegisterForClicks("anyUp")
overlay:EnableMouse(true)
overlay:SetAllPoints(self)
overlay.UpdateTooltip = CacheOverlay_OnEnter
overlay:SetScript("OnClick", CacheOverlay_OnClick)
overlay:SetScript("OnEnter", CacheOverlay_OnEnter)
overlay:SetScript("OnLeave", CacheOverlay_OnLeave)
overlay:SetScript("OnHide", CacheOverlay_OnHide)
return overlay
end
function InventorianItemMixin:GetBagContainer(container, bag)
local bagContainers = container.bagContainers
-- use a metatable to create the new bag wrappers on demand
if not bagContainers then
bagContainers = setmetatable({}, {
__index = function(t, k)
local f = CreateFrame("Frame", nil, container)
f:SetID(k)
t[k] = f
return f
end
})
container.bagContainers = bagContainers
end
return bagContainers[bag]
end
-----------------------------------------------------------------------
-- Various information getters
function InventorianItemMixin:IsCached()
return self.container:GetParent():IsCached()
end
function InventorianItemMixin:GetBag()
return self.bag
end
function InventorianItemMixin:GetInfo()
local player = self.container:GetParent():GetPlayerName()
local icon, count, locked, quality, readable, lootable, link, _, noValue, itemID, isBound
if self:IsCached() then
icon, count, locked, quality, readable, lootable, link = ItemCache:GetItemInfo(player, self.bag, self.slot)
if link then
itemID = C_Item.GetItemInfoInstant(link)
end
else
-- LibItemCache doesn't provide noValue or itemID, so fallback to base API
local info = C_Container.GetContainerItemInfo(self.bag, self.slot)
if info then
icon = info.iconFileID
count = info.stackCount
locked = info.isLocked
quality = info.quality
readable = info.isReadable
lootable = info.hasLoot
link = info.hyperlink
noValue = info.hasNoValue
itemID = info.itemID
isBound = info.isBound
end
if link and (not quality or quality < 0) then
quality = C_Item.GetItemQualityByID(link)
end
end
if not icon and (itemID or link) then
self.itemID = itemID or C_Item.GetItemInfoInstant(link)
if self:IsCached() then
self:RegisterEvent("ITEM_DATA_LOAD_RESULT")
C_Item.RequestLoadItemDataByID(self.itemID)
else
local location = ItemLocation:CreateFromBagAndSlot(self.bag, self.slot)
if C_Item.DoesItemExist(location) and not C_Item.IsItemDataCached(location) then
self:RegisterEvent("ITEM_DATA_LOAD_RESULT")
C_Item.RequestLoadItemData(location)
end
end
end
return icon, count, locked, quality, readable, lootable, link, noValue, itemID, isBound
end
function InventorianItemMixin:GetQuestInfo()
if not self:IsCached() then
local info = C_Container.GetContainerItemQuestInfo(self.bag, self.slot)
if info then
return info.isQuestItem, info.questID, info.isActive
end
end
end
function InventorianItemMixin:IsNew()
if not self:IsCached() then
return C_NewItems.IsNewItem(self.bag, self.slot), C_Container.IsBattlePayItem(self.bag, self.slot)
end
end
function InventorianItemMixin:IsBank()
return self.bag == BANK_CONTAINER
end
function InventorianItemMixin:IsReagentBank()
return self.bag == REAGENTBANK_CONTAINER
end