Skip to content

Commit

Permalink
Reworked new item detection and change detection. (#667)
Browse files Browse the repository at this point in the history
* Reworked new item detection and change detection.

* Fixed a bug where force stacked items may not have updated correctly.

* Fixed a bug where bank bags or inventory items would sometimes glow for no reason.
  • Loading branch information
Cidan authored Aug 27, 2022
1 parent 43fd5e5 commit df5f499
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 16 deletions.
23 changes: 21 additions & 2 deletions modules/NewItemTracking.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function mod:OnInitialize()
glowScale = 1.5,
glowColor = { 0.3, 1, 0.3, 0.7 },
ignoreJunk = false,
highlightChangedItems = false,
},
})

Expand All @@ -82,6 +83,7 @@ function mod:OnEnable()

self:RegisterMessage('AdiBags_UpdateButton', 'UpdateButton')
self:RegisterMessage('AdiBags_AddNewItem', 'AddNewItem')
self:RegisterMessage('AdiBags_ButtonProtoRelease', 'StopButtonGlow')
self:RegisterEvent('BAG_NEW_ITEMS_UPDATED')
end

Expand Down Expand Up @@ -122,19 +124,29 @@ function mod:OnBagFrameCreated(bag)
end

function mod:UpdateButton(event, button)
if addon.BAG_IDS.BANK[button.bag] then return end
if addon.BAG_IDS.BANK[button.bag] then
self:StopButtonGlow(event, button)
return
end
local isNew = self:IsNew(button.bag, button.slot, button.itemLink)
self:ShowLegacyGlow(button, isNew and mod.db.profile.highlight == "legacy")
self:ShowBlizzardGlow(button, isNew and mod.db.profile.highlight == "blizzard")
self:UpdateModuleButton()
end

function mod:StopButtonGlow(event, button)
self:ShowLegacyGlow(button, false)
self:ShowBlizzardGlow(button, false)
end

function mod:UpdateModuleButton()
self.button:SetEnabled(next(newItems) or self.container.ToSortSection:IsShown())
end

function mod:AddNewItem(event, link)
newItems[link] = true
if self.db.profile.highlightChangedItems then
newItems[link] = true
end
end

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -215,6 +227,12 @@ function mod:GetOptions()
end,
width = 'double',
},
highlightChangedItems = {
name = L['Highlight items that have changed'],
type = 'toggle',
order = 50,
width = 'double'
}
}, addon:GetOptionHandler(self)
end

Expand All @@ -223,6 +241,7 @@ end
--------------------------------------------------------------------------------

function mod:ShowBlizzardGlow(button, enable)
if not button.NewItemTexture then return end
if enable then
local _, _, _, quality = GetContainerItemInfo(button.bag, button.slot)
if quality and NEW_ITEM_ATLAS_BY_QUALITY[quality] then
Expand Down
68 changes: 55 additions & 13 deletions widgets/ContainerFrame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ local GetContainerNumFreeSlots = _G.GetContainerNumFreeSlots
local GetContainerNumSlots = _G.GetContainerNumSlots
local GetCursorInfo = _G.GetCursorInfo
local GetItemInfo = _G.GetItemInfo
local GetItemGUID = _G.C_Item.GetItemGUID
local GetMerchantItemLink = _G.GetMerchantItemLink
local ipairs = _G.ipairs
local max = _G.max
Expand Down Expand Up @@ -121,6 +122,8 @@ function containerProto:OnCreate(name, isBank, bagObject)
self.changed = {}
self.sameChanged = {}

self.itemGUIDtoItem = {}

local ids
for bagId in pairs(BAG_IDS[isBank and "BANK" or "BAGS"]) do
self.content[bagId] = { size = 0 }
Expand Down Expand Up @@ -613,6 +616,14 @@ function containerProto:UpdateContent(bag)
for slot = 1, newSize do
local itemId = GetContainerItemID(bag, slot)
local link = GetContainerItemLink(bag, slot)
local guid = ""
local itemLocation = ItemLocation:CreateFromBagAndSlot(bag, slot)

if addon.isRetail then
if itemLocation and itemLocation:IsValid() then
guid = GetItemGUID(itemLocation)
end
end
if not itemId or (link and addon.IsValidItemLink(link)) then
local slotData = content[slot]
if not slotData then
Expand Down Expand Up @@ -643,31 +654,58 @@ function containerProto:UpdateContent(bag)
link, count = false, 0
end

if slotData.link ~= link or slotData.texture ~= texture then
if slotData.guid ~= guid or slotData.texture ~= texture then
local prevSlotId = slotData.slotId
local prevLink = slotData.link
local prevGUID = slotData.guid
local prevTexture = slotData.texture
local prevEquipSlot = slotData.equipSlot
-- If links only differ in character level that's the same item
local sameItem = addon.IsSameLinkButLevel(slotData.link, link)
local sameItem
-- Use the new guid system to detect if an item is actually the same.
if addon.isRetail then
sameItem = prevGUID == guid
else
sameItem = addon.IsSameLinkButLevel(slotData.link, link)
end

slotData.count = count
slotData.link = link
slotData.itemId = itemId
slotData.guid = guid
slotData.itemLocation = itemLocation
slotData.name, slotData.quality, slotData.iLevel, slotData.reqLevel, slotData.class, slotData.subclass, slotData.equipSlot, slotData.texture, slotData.vendorPrice = name, quality, iLevel, reqLevel, class, subclass, equipSlot, texture, vendorPrice
slotData.maxStack = maxStack or (link and 1 or 0)

if sameItem then
-- Items that are the same item but have mutated are marked as "new" to make them more visble.
-- However, only things with a new texture are marked as new, i.e. wrapped items.
if prevTexture ~= texture then
changed[slotData.slotId] = slotData
--addon:SendMessage('AdiBags_AddNewItem', slotData.link)
-- If this item is in the inventory, in the same slot, and has been indexed before,
-- i.e. not the first time the bag was opened, and the texture has changed, this means
-- the item has updated in some material way (i.e. wrapped in wrapping paper, unwrapped),
-- and it must be marked as new.
--
-- This new method only works on retail, as Blizzard did not backport GUID's to classic
-- game modes.
if addon.isRetail then
local context = self.itemGUIDtoItem[guid]
if context and context:IsValid() and prevTexture ~= slotData.texture then
sameChanged[slotData.slotId] = slotData
addon:SendMessage('AdiBags_AddNewItem', slotData.link)
else
-- Otherwise, just a normal change, i.e. enchanted, gem, etc.
changed[slotData.slotId] = slotData
end
else
changed[slotData.slotId] = slotData
end
else
removed[prevSlotId] = prevLink
added[slotData.slotId] = slotData
-- Remove the old item, and add the new item to the
-- item index.
if addon.isRetail then
if prevGUID then
self.itemGUIDtoItem[prevGUID] = nil
end
self.itemGUIDtoItem[guid] = slotData.itemLocation
end
end
elseif slotData.count ~= count then
slotData.count = count
Expand Down Expand Up @@ -843,10 +881,14 @@ function containerProto:UpdateButtons()
for slotId in pairs(changed) do
buttons[slotId]:FullUpdate()
end

for slotId, slotData in pairs(sameChanged) do
self:DispatchItem(slotData)
buttons[slotId]:FullUpdate()

if next(sameChanged) then
self:SendMessage('AdiBags_PreFilter', self)
for slotId, slotData in pairs(sameChanged) do
self:DispatchItem(slotData)
buttons[slotId]:FullUpdate()
end
self:SendMessage('AdiBags_PostFilter', self)
end

self:SendMessage('AdiBags_PostContentUpdate', self, added, removed, changed)
Expand Down
11 changes: 10 additions & 1 deletion widgets/ItemButton.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function buttonProto:OnRelease()
self.texture = nil
self.bagFamily = nil
self.stack = nil
addon:SendMessage('AdiBags_ButtonProtoRelease', self)
end

function buttonProto:ToString()
Expand Down Expand Up @@ -469,6 +470,7 @@ function stackProto:OnRelease()
self:SetSection(nil)
self.key = nil
self.container = nil
addon:SendMessage('AdiBags_ButtonProtoRelease', self)
wipe(self.slots)
end

Expand Down Expand Up @@ -598,7 +600,14 @@ function stackProto:Update()
end
end

stackProto.FullUpdate = stackProto.Update
function stackProto:FullUpdate()
if not self:CanUpdate() then return end
self:UpdateVisibleSlot()
self:UpdateCount()
if self.button then
self.button:FullUpdate()
end
end

function stackProto:UpdateCount()
local count = 0
Expand Down

0 comments on commit df5f499

Please sign in to comment.