Files

283 lines
13 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- The active player's known spells establish ownership. Source categories supply only membership.
local categories = {
{id=1006, label=MOUNTS or "Mounts", name="Mounts", icon=132261},
{id=1005, label=COMPANIONS or "Companions", name="Companions", icon=237579},
{id=1008, label=TOYS or "Toys", name="Toys", icon=237429},
}
local perPage = 8
local launcher, panel, rows, tabs, previous, following, pageText
local results, scan, activeCategory, page = nil, nil, 1, 1
local opened, bookVisible, dirty, retryAt = false, false, true, 0
local function public(v) return not issecretvalue or not issecretvalue(v) end
local function whole(v, low, high)
return public(v) and type(v) == "number" and v >= low and v <= high and v == math.floor(v)
end
local function enumerate()
local api, bankType = C_SpellBook, Enum and Enum.SpellBookSpellBank
if not api or not bankType or not Enum.SpellBookItemType or not WoWHybridCollectionSpells
or not api.GetNumSpellBookSkillLines or not api.GetSkillLineIndexByID or not api.GetSpellBookSkillLineInfo
or not api.GetSpellBookItemInfo or not api.IsSpellKnown then return end
local bank, spellType = bankType.Player, Enum.SpellBookItemType.Spell
if not whole(bank,0,255) or not whole(spellType,0,255) then return end
local count = api.GetNumSpellBookSkillLines()
if not whole(count,1,512) then return end
local represented, index, pending = {{},{},{}}, {}, {}
for id, group in pairs({[1006]=1,[777]=1,[1005]=2,[778]=2,[1008]=3}) do
local i = api.GetSkillLineIndexByID(id)
if not public(i) then return end
if whole(i,1,count) then index[i] = group end
end
-- A visible source category can be returned even before the ID lookup starts tracking it.
for i=1,count do
coroutine.yield()
local info = api.GetSpellBookSkillLineInfo(i)
if not public(info) or type(info) ~= "table" or not public(info.shouldHide)
or type(info.shouldHide) ~= "boolean" then return end
local collection
if not info.shouldHide then
if not public(info.name) or not public(info.iconID) then return end
for group, c in ipairs(categories) do
if info.iconID == c.icon and (index[i] == group or info.name == c.name or info.name == c.label) then
collection = group
end
end
if info.iconID == 132167 and (index[i] == 1 or info.name == categories[1].label or info.name == "Mounts") then collection = 1 end
if info.iconID == 0 and (info.name == categories[2].label or info.name == "Companions") then collection = 2 end
end
if collection and not info.shouldHide then
if not whole(info.itemIndexOffset,0,65535) or not whole(info.numSpellBookItems,0,65535)
or info.itemIndexOffset+info.numSpellBookItems>65535 then
pending[collection]=true
else
for slot=info.itemIndexOffset+1,info.itemIndexOffset+info.numSpellBookItems do
coroutine.yield()
local entry = api.GetSpellBookItemInfo(slot,bank)
if not public(entry) or type(entry) ~= "table" or not public(entry.itemType) then
pending[collection]=true
break
end
if entry.itemType == spellType then
if not public(entry.actionID) or not public(entry.spellID) then
pending[collection]=true
break
end
if whole(entry.actionID,1,2147483647) then represented[collection][entry.actionID] = true end
if whole(entry.spellID,1,2147483647) then represented[collection][entry.spellID] = true end
end
end
end
end
end
-- Defer only categories whose visible slots cannot yet be deduplicated.
local incomplete = next(pending) ~= nil
local lists = {{},{},{}}
for group,c in ipairs(categories) do
local source = WoWHybridCollectionSpells[c.id]
if type(source) ~= "table" then return end
for _,spell in ipairs(source) do
coroutine.yield()
if not pending[group] and not represented[group][spell] then
local known = api.IsSpellKnown(spell,bank)
if not public(known) or type(known) ~= "boolean" then
incomplete = true
elseif known then
lists[group][#lists[group]+1] = spell
represented[group][spell] = true
end
end
end
end
return lists, incomplete
end
local function countResults()
return results and (#results[1]+#results[2]+#results[3]) or 0
end
local function button(parent,text,w,h)
local b = CreateFrame("Button",nil,parent,"UIPanelButtonTemplate")
b:SetSize(w,h); b:SetText(text)
return b
end
local draw
local requested = {}
local function hideSpellTooltip(row)
if GameTooltip:IsOwned(row) then GameTooltip:Hide() end
end
local function showSpellTooltip(row)
if not row.spellID then
hideSpellTooltip(row)
return
end
GameTooltip:SetOwner(row,"ANCHOR_RIGHT")
GameTooltip:SetSpellByID(row.spellID)
end
local function pickUpSpell(row)
local spell = row:GetAttribute("spell1")
if not spell or InCombatLockdown() then return end
local known = C_SpellBook.IsSpellKnown(spell,Enum.SpellBookSpellBank.Player)
if public(known) and known == true then C_Spell.PickupSpell(spell) end
end
local function createPanel()
-- Cast buttons stay on an independent addon frame. The stock book has no protected descendants from this panel.
local guard = CreateFrame("Frame",nil,UIParent,"SecureHandlerStateTemplate")
guard:SetSize(1,1); guard:SetPoint("CENTER")
RegisterStateDriver(guard,"visibility","[combat] hide; show")
panel = CreateFrame("Frame",nil,guard)
panel:SetSize(366,394); panel:SetPoint("CENTER",UIParent,"CENTER",0,0)
panel:SetFrameStrata("DIALOG"); panel:SetClampedToScreen(true); panel:EnableMouse(true)
local bg = panel:CreateTexture(nil,"BACKGROUND")
bg:SetAllPoints(); bg:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Background")
local title = panel:CreateFontString(nil,"ARTWORK","GameFontNormalLarge")
title:SetPoint("TOPLEFT",17,-17); title:SetText(COLLECTIONS or "Collections")
local close = button(panel,CLOSE or "Close",68,22)
close:SetPoint("TOPRIGHT",-10,-12)
close:SetScript("OnClick",function() opened = false; panel:Hide() end)
tabs,rows = {},{}
for i,c in ipairs(categories) do
local tab = button(panel,c.label,111,23)
tab:SetPoint("TOPLEFT",12+(i-1)*115,-48)
tab:SetScript("OnClick",function() activeCategory,page=i,1;draw() end)
tabs[i]=tab
end
for i=1,perPage do
local row=CreateFrame("Button",nil,panel,"SecureActionButtonTemplate")
row:SetSize(332,31); row:SetPoint("TOPLEFT",18,-83-(i-1)*33)
row:RegisterForClicks("LeftButtonUp"); row:SetAttribute("useOnKeyDown",false)
row:RegisterForDrag("LeftButton")
row:SetScript("OnDragStart",pickUpSpell)
row:SetScript("OnEnter",showSpellTooltip)
row:SetScript("OnLeave",hideSpellTooltip)
row:SetScript("OnHide",hideSpellTooltip)
row.art=row:CreateTexture(nil,"ARTWORK")
row.art:SetSize(28,28); row.art:SetPoint("LEFT")
row.text=row:CreateFontString(nil,"ARTWORK","GameFontHighlight")
row.text:SetPoint("LEFT",37,0); row.text:SetWidth(289); row.text:SetJustifyH("LEFT"); row.text:SetWordWrap(false)
local highlight=row:CreateTexture(nil,"HIGHLIGHT")
highlight:SetAllPoints();highlight:SetColorTexture(1,.83,.45,.12)
rows[i]=row
end
previous=button(panel,PREVIOUS or "Previous",95,23)
previous:SetPoint("BOTTOMLEFT",16,12)
previous:SetScript("OnClick",function() page=math.max(1,page-1);draw() end)
following=button(panel,NEXT or "Next",95,23)
following:SetPoint("BOTTOMRIGHT",-16,12)
following:SetScript("OnClick",function() page=page+1;draw() end)
pageText=panel:CreateFontString(nil,"ARTWORK","GameFontNormal")
pageText:SetPoint("BOTTOM",0,18)
end
draw=function()
if InCombatLockdown() then return end
if not opened or not bookVisible or countResults()==0 then if panel then panel:Hide() end;return end
if not panel then createPanel() end
if #results[activeCategory]==0 then
for i,list in ipairs(results) do if #list>0 then activeCategory,page=i,1;break end end
end
for i,tab in ipairs(tabs) do tab:SetEnabled(#results[i]>0 and activeCategory~=i) end
local list=results[activeCategory]
local pages=math.max(1,math.ceil(#list/perPage))
page=math.min(page,pages)
for i,row in ipairs(rows) do
local spell=list[(page-1)*perPage+i]
row.spellID=nil
row:SetAttribute("type1",nil);row:SetAttribute("spell1",nil)
if spell then
local info=C_Spell and C_Spell.GetSpellInfo and C_Spell.GetSpellInfo(spell)
local known=C_SpellBook.IsSpellKnown(spell,Enum.SpellBookSpellBank.Player)
local passive
if C_Spell and C_Spell.IsSpellPassive then passive=C_Spell.IsSpellPassive(spell) end
local valid=public(info) and type(info)=="table" and public(info.spellID) and info.spellID==spell
and public(info.name) and type(info.name)=="string" and info.name~=""
local canCast=valid and public(known) and known==true and public(passive) and passive==false
if not public(known) or known~=true then
row:Hide()
else
if not valid and not requested[spell] and C_Spell and C_Spell.RequestLoadSpellData then
requested[spell] = true
C_Spell.RequestLoadSpellData(spell)
end
local name=valid and info.name or string.format("Spell %d",spell)
local detail=canCast and "" or (public(passive) and passive==true and " (Passive)" or " (Details unavailable)")
if valid then row.spellID=spell end
row.text:SetText(name..detail)
row.art:SetTexture(valid and public(info.iconID) and info.iconID or nil)
if canCast then row:SetAttribute("spell1",spell);row:SetAttribute("type1","spell") end
row:Show()
end
else row:Hide() end
if GameTooltip:IsOwned(row) then showSpellTooltip(row) end
end
pageText:SetText(page.." / "..pages)
previous:SetEnabled(page>1);following:SetEnabled(page<pages)
panel:Show()
end
local function reset(close)
scan,results,dirty,retryAt=nil,nil,true,0
if close then opened=false end
if not InCombatLockdown() then
if launcher then launcher:Hide() end
if panel then panel:Hide() end
end
end
local events=CreateFrame("Frame")
for _,event in ipairs({"PLAYER_ENTERING_WORLD","PLAYER_LEAVING_WORLD","SPELLS_CHANGED","SKILL_LINES_CHANGED",
"SPELL_DATA_LOAD_RESULT","PLAYER_REGEN_DISABLED","PLAYER_REGEN_ENABLED"}) do
events:RegisterEvent(event)
end
events:SetScript("OnEvent",function(_,event,spell,success)
if event=="SPELL_DATA_LOAD_RESULT" then
if public(spell) and requested[spell] and public(success) then
if success == false then
requested[spell] = nil
elseif success and opened and not InCombatLockdown() then
draw()
end
end
return
end
if scan and (event=="SPELLS_CHANGED" or event=="SKILL_LINES_CHANGED") then
-- Finish the bounded pass and reconcile changes in a following pass.
dirty=true
return
end
if event=="PLAYER_ENTERING_WORLD" then requested={} end
reset(event=="PLAYER_LEAVING_WORLD" or event=="PLAYER_ENTERING_WORLD" or event=="PLAYER_REGEN_DISABLED")
end)
events:SetScript("OnUpdate",function()
if InCombatLockdown() then return end
local book=PlayerSpellsFrame and PlayerSpellsFrame.SpellBookFrame
local visible=book and book:IsVisible()
visible=public(visible) and visible==true
if visible~=bookVisible then bookVisible=visible;reset(not visible) end
if not visible then return end
if not launcher then
launcher=button(book,COLLECTIONS or "Collections",118,23)
launcher:SetClampedToScreen(true)
launcher:SetPoint("TOPLEFT",book,"BOTTOMLEFT",12,-2)
launcher:Hide()
launcher:SetScript("OnClick",function() if not InCombatLockdown() then opened=not opened;draw() end end)
end
if not scan then
if not dirty or GetTime()<retryAt then return end
scan=coroutine.create(enumerate);dirty=false
end
-- Each resume performs at most one skill-line, slot, or known-spell iteration.
for step=1,16 do
local ok,lists,retry=coroutine.resume(scan)
if not ok or coroutine.status(scan)=="dead" then
scan=nil;results=ok and lists or nil
dirty=dirty or not results or retry;retryAt=GetTime()+2
launcher:SetShown(countResults()>0)
draw()
return
end
end
end)