From 9a47e69a50bd4f8471953a52d896c1b4fe73316e Mon Sep 17 00:00:00 2001 From: BenDol Date: Fri, 15 Jun 2018 00:03:55 +1200 Subject: [PATCH] unusable: bag module to indicate unusable items --- modules/bags.lua | 7 ++ modules/unusable.lua | 195 +++++++++++++++++++++++++++++++++++++++++++ pfUI.toc | 1 + 3 files changed, 203 insertions(+) create mode 100644 modules/unusable.lua diff --git a/modules/bags.lua b/modules/bags.lua index aa039972..5ad248ec 100644 --- a/modules/bags.lua +++ b/modules/bags.lua @@ -136,6 +136,9 @@ pfUI:RegisterModule("bags", function () if pfUI.bags[bag].slots[slot].frame:IsShown() then local _, _, locked, _ = GetContainerItemInfo(bag, slot) SetItemButtonDesaturated(pfUI.bags[bag].slots[slot].frame, locked, 0.5, 0.5, 0.5) + if pfUI.unusable then + pfUI.unusable:UpdateSlot(pfUI.bags[bag].slots[slot], bag, slot) + end end end end @@ -423,6 +426,10 @@ pfUI:RegisterModule("bags", function () end pfUI.bags[bag].slots[slot].frame:Show() + + if pfUI.unusable then + pfUI.unusable:UpdateSlot(pfUI.bags[bag].slots[slot], bag, slot) + end end function pfUI.bag:CreateBagSlots(frame) diff --git a/modules/unusable.lua b/modules/unusable.lua new file mode 100644 index 00000000..dd61c027 --- /dev/null +++ b/modules/unusable.lua @@ -0,0 +1,195 @@ +pfUI:RegisterModule("unusable", function () + pfUI.unusable = CreateFrame("Frame", "pfUnusable", UIParent) + pfUI.unusable.tooltip = CreateFrame("GameTooltip", "pfUnusableTooltip", UIParent, "GameTooltipTemplate") + + pfUI.unusable:UnregisterAllEvents() + pfUI.unusable:RegisterEvent("MAIL_INBOX_UPDATE") + pfUI.unusable:RegisterEvent("PLAYER_LEVEL_UP") + pfUI.unusable:RegisterEvent("SKILL_LINES_CHANGED") + + pfUI.unusable.unusables = {} + + pfUI.unusable:SetScript("OnEvent", function() + if event == "MAIL_INBOX_UPDATE" and MailFrame:IsVisible() then + local total = GetInboxNumItems() + for i = 1, INBOXITEMS_TO_DISPLAY do + local index = (((InboxFrame.pageNum - 1) * INBOXITEMS_TO_DISPLAY) + i) + local name, _, _, _, canUse = GetInboxItem(index) + local _, _, _, _, _, _, _, hasItem = GetInboxHeaderInfo(index) + + if hasItem and hasItem > 0 and name then + if not canUse or not pfUI.unusable:IsItemUsable(name, "MailBox", i) then + pfUI.unusable:MakeUnusable(_G["MailItem" .. i .. "ButtonIcon"]) + end + else + pfUI.unusable:MakeUsable(_G["MailItem" .. i .. "ButtonIcon"]) + end + end + elseif event == "PLAYER_LEVEL_UP" or event == "SKILL_LINES_CHANGED" then + for _, slot in pairs(pfUI.unusable.unusables) do + if slot and not pfUI.unusable:IsSlotItemUsable(slot) then + pfUI.unusable:MakeUnusable(slot) + end + end + end + end) + + function pfUI.unusable:UpdateSlot(frame, bag, slot) + if not pfUI.unusable:IsSlotItemUsable(frame, bag, slot) then + pfUI.unusable:MakeUnusable(frame) + end + end + + function pfUI.unusable:IsSlotItemUsable(slot, bag, index) + if not bag then bag = slot.bag end + if not index then index = slot.slot end + + local itemId, name = self:GetItemInfoFromLink(GetContainerItemLink(bag, index)) + return self:IsItemUsable(itemId, bag, index) + end + + function pfUI.unusable:IsItemUsable(item, bag, slot) + if item and (type(item) == "number" and item < 0) or item == "Unknown" then + return true + else + local name, itemLink, _, minLevel = GetItemInfo(item) + + if minLevel and minLevel > UnitLevel("player") then + return false + end + end + + self.tooltip:SetOwner(UIParent, "ANCHOR_NONE") + if bag == -1 then + self.tooltip:SetInventoryItem("player", self:GetBankFrameSlot(slot)) + elseif bag == "MailBox" then + self.tooltip:SetInboxItem(slot) + elseif bag == "Merchant" then + self.tooltip:SetMerchantItem(slot) + else + self.tooltip:SetBagItem(bag, slot) + end + + local result = true + for id = 0, 8 do + for _, v in pairs({"TextRight", "TextLeft"}) do + local widget = _G["pfUnusableTooltip"..v..id] + if widget and widget:GetText() then + local r, g, b, _ = widget:GetTextColor() + local rgb = r + g + b + if tostring(rgb) == "1.2509776437655" then + result = false + break -- item contains red text + end + end + if not result then break end + end + end + + self.tooltip:Hide() + return result + end + + function pfUI.unusable:MakeUnusable(slot) + if not slot then return end + local frame = slot.frame + + if type(frame) == "number" then + if MerchantFrame:IsVisible() then + local itemButton = _G["MerchantItem" .. frame .. "ItemButton"] + if itemButton then + SetItemButtonTextureVertexColor(itemButton, 0, 1, 0) + SetItemButtonNormalTextureVertexColor(itemButton, 0, 1, 0) + end + local merchantButton = _G["MerchantItem" .. frame] + if merchantButton then + SetItemButtonNameFrameVertexColor(merchantButton, 0, 1, 0) + SetItemButtonSlotVertexColor(merchantButton, 0, 1, 0) + end + end + elseif type(frame) == "table" or type(frame) == "userdata" then + if frame:IsObjectType("Texture") then + SetDesaturation(frame, nil) + frame:SetVertexColor(1, 0, 0) + SetDesaturation(frame, nil) + else + SetItemButtonTextureVertexColor(frame, 1, 0, 0) + + -- Hack: there is an issue with the bank frame overriding the VertexColor, + -- since we have no idea where this is happening, we will store the + -- original SetVertexColor function and override it, it will be restored + -- when we call MakeUsable(). + if not slot.oldVertexFunc and slot.bag and slot.bag < 0 then + slot.oldVertexFunc = _G[frame:GetName().."IconTexture"].SetVertexColor + _G[frame:GetName().."IconTexture"].SetVertexColor = function(this, r, g, b, a) + return slot.oldVertexFunc(this, 1, 0, 0) + end + end + end + else + return false + end + + self.unusables[slot] = slot + return true + end + + function pfUI.unusable:MakeUsable(slot) + if not slot then return end + local frame = slot.frame + + if type(frame) == "number" and MerchantFrame:IsVisible() then + local itemButton = _G["MerchantItem" .. frame .. "ItemButton"] + if itemButton then + SetItemButtonTextureVertexColor(itemButton, 1, 1, 1) + SetItemButtonNormalTextureVertexColor(itemButton, 1, 1, 1) + end + local merchantButton = _G["MerchantItem" .. frame] + if merchantButton then + SetItemButtonNameFrameVertexColor(merchantButton, 1, 1, 1) + SetItemButtonSlotVertexColor(merchantButton, 1, 1, 1) + end + elseif type(frame) == "table" or type(frame) == "userdata" then + if frame:IsObjectType("Texture") then + SetDesaturation(frame, nil) + frame:SetVertexColor(1, 1, 1) + SetDesaturation(frame, nil) + else + if slot.oldVertexFunc then + _G[frame:GetName().."IconTexture"] = slot.oldVertexFunc + slot.oldVertexFunc = nil + end + + SetItemButtonTextureVertexColor(frame, 1, 1, 1) + end + else + return false + end + + self.unusables[slot] = nil + return true + end + + function pfUI.unusable:GetItemInfoFromLink(link) + local id = -1 + local name = "Unknown" + if link ~= nil then + for i, n in string.gfind(link, "|c%x+|Hitem:(%d+):%d+:%d+:%d+|h%[(.-)%]|h|r") do + if i ~= nil then id = i end + if n ~= nil then name = n end + end + end + return id, name + end + + function pfUI.unusable:GetBankFrameSlot(slot) + for i = 1, NUM_BANKGENERIC_SLOTS, 1 do + local bankSlot = _G["BankFrameItem" .. i] + if bankSlot:GetID() == slot then + return i + 39 + end + end + return nil + end + +end) diff --git a/pfUI.toc b/pfUI.toc index 4b0a0b4b..1c5cd776 100644 --- a/pfUI.toc +++ b/pfUI.toc @@ -96,3 +96,4 @@ modules\tracking.lua modules\easteregg.lua modules\farmmode.lua modules\pettarget.lua +modules\unusable.lua