wow-addons/QuestCraftHighlight/QuestCraftHighlight.lua

300 lines
9.5 KiB
Lua
Raw Normal View History

-- Quest Craft Highlight: marks recipes whose output a quest in your log needs.
-- Lowercased item name -> { need = total across quests, quests = { { title, level, need } } }.
local questItems = {}
local dirty = true
local incomplete = false
local lastNumQuests
-- Recipe rows seen so far -> the recipeInfo each is currently showing.
local rows = {}
-- Output items whose name was not loaded yet when a row asked for it.
local pendingItems = {}
local hooked = false
-- "Linen Cloth: 3/5" or "3/5 Linen Cloth (Optional)" -> "Linen Cloth"
local function ObjectiveItemName(text)
local name = text:match("^%d+/%d+%s+(.+)$") or text:match("^(.-):?%s*%d+/%d+")
if name then
name = name:gsub("%s+%(Optional%)$", "")
if name ~= "" then
return name
end
end
end
local function ReadEntries()
local entries = {}
for i = 1, (C_QuestLog.GetNumQuestLogEntries()) do
local info = C_QuestLog.GetInfo(i)
if info then
entries[#entries + 1] = info
end
end
return entries
end
-- Returns the titles of collapsed headers whose quests are not being listed.
-- Those have to be opened to be read, then closed again.
local function ExpandHiddenHeaders(entries)
local reopened
for i = #entries, 1, -1 do
local info, nextInfo = entries[i], entries[i + 1]
if info.isHeader and info.isCollapsed and (not nextInfo or nextInfo.isHeader) then
reopened = reopened or {}
reopened[info.title] = true
ExpandQuestHeader(i)
end
end
return reopened
end
local function CollapseHeaders(titles)
local entries = ReadEntries()
for i = #entries, 1, -1 do
local info = entries[i]
if info.isHeader and titles[info.title] then
CollapseQuestHeader(i)
end
end
end
local function QuestLevel(info)
if info.level and info.level > 0 then
return info.level
end
return C_QuestLog.GetQuestDifficultyLevel and C_QuestLog.GetQuestDifficultyLevel(info.questID) or nil
end
local function Rebuild()
local entries = ReadEntries()
local reopened = ExpandHiddenHeaders(entries)
if reopened then
entries = ReadEntries()
end
local new = {}
incomplete = false
for _, info in ipairs(entries) do
if not info.isHeader and not info.isHidden and info.questID and info.questID > 0 then
local objectives = C_QuestLog.GetQuestObjectives(info.questID)
if not objectives then
incomplete = true
else
for _, objective in ipairs(objectives) do
if objective.type == "item" then
if not objective.text or objective.text == "" then
incomplete = true
else
local name = ObjectiveItemName(objective.text)
if name then
local key = name:lower()
local item = new[key] or { need = 0, quests = {} }
new[key] = item
item.need = item.need + (objective.numRequired or 0)
table.insert(item.quests, {
title = info.title,
level = QuestLevel(info),
need = objective.numRequired,
})
end
end
end
end
end
end
end
if reopened then
CollapseHeaders(reopened)
end
questItems = new
dirty = false
lastNumQuests = select(2, C_QuestLog.GetNumQuestLogEntries())
end
local function FindMatch(recipeInfo)
local schematic = C_TradeSkillUI.GetRecipeSchematic(recipeInfo.recipeID, false)
local itemID = schematic and schematic.outputItemID
if not itemID then
return nil
end
local name = C_Item.GetItemNameByID(itemID)
if not name then
-- The recipe name usually equals the item name, so match on that for
-- now and re-check once the real name has loaded.
pendingItems[itemID] = true
C_Item.RequestLoadItemDataByID(itemID)
name = recipeInfo.name
end
if not name then
return nil
end
if dirty then
Rebuild()
end
return questItems[name:lower()], itemID
end
-- Same arithmetic as the row's own Init, with room kept for our text.
local function FitLabel(row, reserved)
local countWidth = row.Count:IsShown() and row.Count:GetStringWidth() or 0
local lockedWidth = row.LockedIcon:IsShown() and row.LockedIcon:GetWidth() or 0
local width = row:GetWidth() - (lockedWidth + countWidth + 10 + row.SkillUps:GetWidth() + reserved)
row.Label:SetWidth(row:GetWidth())
row.Label:SetWidth(math.min(width, row.Label:GetStringWidth()))
end
local function Decorate(row, recipeInfo)
rows[row] = recipeInfo
local match, itemID = FindMatch(recipeInfo)
if not match then
if row.qchMatch then
row.qchMatch = nil
row.qchTint:Hide()
row.qchText:Hide()
FitLabel(row, 0)
end
return
end
if not row.qchTint then
row.qchTint = row:CreateTexture(nil, "BACKGROUND")
row.qchTint:SetAllPoints()
row.qchTint:SetColorTexture(1, 0.82, 0, 0.18)
row.qchText = row:CreateFontString(nil, "OVERLAY", "GameFontNormal_NoShadow")
end
local have = C_Item.GetItemCount(itemID) or 0
local done = have >= match.need
row.qchMatch = match
row.qchHave = have
row.qchTint:SetShown(not done)
row.qchText:SetText("! " .. have .. " / " .. match.need)
if done then
row.qchText:SetTextColor(0.25, 1, 0.25)
else
row.qchText:SetTextColor(1, 0.82, 0)
end
local lockedWidth = row.LockedIcon:IsShown() and row.LockedIcon:GetWidth() or 0
row.qchText:ClearAllPoints()
row.qchText:SetPoint("RIGHT", row, "RIGHT", -(lockedWidth + 4), 0)
row.qchText:Show()
FitLabel(row, row.qchText:GetStringWidth() + 8)
end
local function ShowQuestLines(row)
local match = row.qchMatch
if not match then
return
end
-- The row already owns the tooltip when its name is truncated.
if not GameTooltip:IsOwned(row.Label) then
GameTooltip:SetOwner(row, "ANCHOR_RIGHT")
end
for _, quest in ipairs(match.quests) do
local left = "Quest: "
if quest.level then
left = left .. "[" .. quest.level .. "] "
end
left = left .. quest.title
local color = quest.level and GetQuestDifficultyColor and GetQuestDifficultyColor(quest.level)
local r, g, b = 1, 0.82, 0
if color then
r, g, b = color.r, color.g, color.b
end
GameTooltip:AddDoubleLine(left, row.qchHave .. " / " .. (quest.need or "?"), r, g, b, 1, 1, 1)
end
GameTooltip:Show()
end
-- Rows copy the mixin when they are created, which happens after this hook.
local function HookRecipeList()
if hooked or not ProfessionsRecipeListRecipeMixin then
return
end
hooked = true
hooksecurefunc(ProfessionsRecipeListRecipeMixin, "Init", function(row, node)
local recipeInfo = node:GetData().recipeInfo
if recipeInfo then
Decorate(row, recipeInfo)
end
end)
hooksecurefunc(ProfessionsRecipeListRecipeMixin, "OnEnter", ShowQuestLines)
end
local refreshQueued = false
local function QueueRefresh()
if refreshQueued then
return
end
refreshQueued = true
C_Timer.After(0, function()
refreshQueued = false
for row, recipeInfo in pairs(rows) do
if row:IsVisible() then
Decorate(row, recipeInfo)
end
end
end)
end
HookRecipeList()
local frame = CreateFrame("Frame")
for _, event in ipairs({
"ADDON_LOADED", "PLAYER_ENTERING_WORLD", "QUEST_LOG_UPDATE", "QUEST_ACCEPTED", "QUEST_REMOVED",
"QUEST_TURNED_IN", "BAG_UPDATE_DELAYED", "ITEM_DATA_LOAD_RESULT",
}) do
if not C_EventUtils or C_EventUtils.IsEventValid(event) then
frame:RegisterEvent(event)
end
end
frame:SetScript("OnEvent", function(_, event, arg1)
if event == "ADDON_LOADED" then
if arg1 == "Blizzard_ProfessionsTemplates" then
HookRecipeList()
end
elseif event == "ITEM_DATA_LOAD_RESULT" then
if pendingItems[arg1] then
pendingItems[arg1] = nil
QueueRefresh()
end
elseif event == "BAG_UPDATE_DELAYED" then
QueueRefresh()
elseif event == "QUEST_LOG_UPDATE" then
-- Also fires when headers are toggled, including by Rebuild itself,
-- so only react if the quests themselves changed.
if incomplete or select(2, C_QuestLog.GetNumQuestLogEntries()) ~= lastNumQuests then
dirty = true
QueueRefresh()
end
else
dirty = true
QueueRefresh()
end
end)
-- /qch prints what the addon currently knows.
SLASH_QUESTCRAFTHIGHLIGHT1 = "/qch"
SlashCmdList.QUESTCRAFTHIGHLIGHT = function()
Rebuild()
local print = function(text) DEFAULT_CHAT_FRAME:AddMessage("|cffffd100QuestCraftHighlight:|r " .. text) end
local rowCount = 0
for _ in pairs(rows) do
rowCount = rowCount + 1
end
print("hooked=" .. tostring(hooked) .. " rows=" .. rowCount)
for name, item in pairs(questItems) do
print(name .. " -> need " .. item.need .. ", first quest " .. item.quests[1].title)
end
end