local addonName = ...
local INSPECT_DELAY = 40
local iconKey = addonName .. "Icon"
local CheckInteractDistance, next = CheckInteractDistance, next
local CanInspect, ClearInspectPlayer, NotifyInspect = CanInspect, ClearInspectPlayer, NotifyInspect
local GetInspectSpecialization = GetInspectSpecialization
local GetArenaOpponentSpec, GetNumArenaOppenents = GetArenaOpponentSpec, GetNumArenaOppenents
local GetNamePlateForUnit = C_NamePlate.GetNamePlateForUnit
local UnitClassBase, UnitExists, UnitGUID = UnitClassBase, UnitExists, UnitGUID
local UnitIsEnemy, UnitIsFriend, UnitIsPlayer = UnitIsEnemy, UnitIsFriend, UnitIsPlayer
local iconData, inspectList, isAtlas, namePlateByGUID, reinspectList = { }, { }, { }, { }, { }
local arenaSpec, numArenaOpponents = { }
local SetInspectDelay
local frame = CreateFrame("Frame")
local function InspectList_OnUpdate(self)
local namePlate, unit = next(inspectList)
if namePlate then
inspectList[namePlate], reinspectList[namePlate] = nil, unit
if CheckInteractDistance(unit, 1) then
SetInspectDelay()
NotifyInspect(unit)
end
elseif next(reinspectList) then
inspectList, reinspectList = reinspectList, inspectList
else
self:Hide()
end
end
do
local delayCount = 0
local function InspectDelay_OnUpdate(self)
delayCount = delayCount + 1
if delayCount >= INSPECT_DELAY then
delayCount = 0
self:SetScript("OnUpdate", InspectList_OnUpdate)
end
end
function SetInspectDelay(reset)
delayCount = 0
if reset then
frame:SetScript("OnUpdate", InspectList_OnUpdate)
else
frame:SetScript("OnUpdate", InspectDelay_OnUpdate)
end
end
end
local function UpdateNamePlateIcon(namePlate, unit, texture)
local icon = namePlate[iconKey]
if UnitIsPlayer(unit) then
if not texture then
local specID = GetInspectSpecialization(unit)
texture = (specID or 0) > 0 and iconData[specID]
end
local usingSpec = texture and true or nil
if not texture then
local _, classID = UnitClassBase(unit)
texture = iconData[classID]
end
if texture then
if not icon then
icon = namePlate:CreateTexture(nil, "OVERLAY")
namePlate[iconKey] = icon
end
local iconStyle = UnitIsFriend("player", unit) and "friend" or "enemy"
if icon.style ~= iconStyle then
icon.style = iconStyle
icon:ClearAllPoints()
if iconStyle == "friend" then
icon:SetPoint('CENTER', 0, 29.46)
icon:SetSize(24, 24)
else
icon:SetPoint('RIGHT', 10, -5)
icon:SetSize(20, 20)
end
end
icon[isAtlas[texture] and "SetAtlas" or "SetTexture"](icon, texture)
icon:Show()
return usingSpec
end
end
if icon then
icon:Hide()
end
end
frame:SetScript("OnEvent", function(self, event, arg1)
if event == "INSPECT_READY" then
local namePlate = namePlateByGUID[arg1]
if namePlate and UpdateNamePlateIcon(namePlate, namePlate.namePlateUnitToken) then
inspectList[namePlate], reinspectList[namePlate] = nil, nil
end
ClearInspectPlayer()
SetInspectDelay(true)
elseif event == "NAME_PLATE_UNIT_ADDED" or event == "PLAYER_SPECIALIZATION_CHANGED" then
local namePlate = GetNamePlateForUnit(arg1)
if namePlate then
if numArenaOpponents and UnitIsEnemy("player", arg1) then
for index = 1, numArenaOpponents do
if UnitIsUnit("arena" .. index, arg1) then
UpdateNamePlateIcon(namePlate, arg1, iconData[arenaSpec[index]])
return
end
end
end
if not UpdateNamePlateIcon(namePlate, arg1) and CanInspect(arg1) then
local guid = UnitGUID(arg1)
if guid then
namePlateByGUID[guid], inspectList[namePlate] = namePlate, arg1
self:Show()
end
end
end
elseif event == "NAME_PLATE_UNIT_REMOVED" then
local namePlate = GetNamePlateForUnit(arg1)
if namePlate then
inspectList[namePlate], reinspectList[namePlate] = nil, nil
local guid = UnitGUID(arg1)
if guid then
namePlateByGUID[guid] = nil
end
if namePlate[iconKey] then
namePlate[iconKey]:Hide()
end
end
elseif event == "PLAYER_ENTERING_WORLD" then
wipe(arenaSpec)
wipe(inspectList)
wipe(reinspectList)
numArenaOpponents = nil
elseif event == "ARENA_PREP_OPPONENT_SPECIALIZATIONS" then
wipe(arenaSpec)
numArenaOpponents = GetNumArenaOpponents()
if numArenaOpponents > 0 then
for index = 1, numArenaOpponents do
local specID = GetArenaOpponentSpec(index)
arenaSpec[index] = (specID or 0) > 0 and specID or nil
local namePlate = GetNamePlateForUnit("arena" .. index)
if namePlate then
inspectList[namePlate], reinspectList[namePlate] = nil, nil
UpdateNamePlateIcon(namePlate, namePlate.namePlateUnitToken, iconData[specID])
end
end
else
numArenaOpponents = nil
end
end
end)
frame:SetScript("OnUpdate", InspectList_OnUpdate)
frame:RegisterEvent("ARENA_PREP_OPPONENT_SPECIALIZATIONS")
frame:RegisterEvent("INSPECT_READY")
frame:RegisterEvent("NAME_PLATE_UNIT_ADDED")
frame:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
frame:RegisterEvent("PLAYER_ENTERING_WORLD")
frame:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")
do
local GetClassAtlas, GetClassInfo = GetClassAtlas, GetClassInfo
local GetNumSpecializationsForClassID = GetNumSpecializationsForClassID
local GetSpecializationInfoForClassID = GetSpecializationInfoForClassID
for index = 1, GetNumClasses() do
local _, class, classID = GetClassInfo(index)
local atlas = GetClassAtlas(class)
iconData[classID], isAtlas[atlas] = atlas, true
for specNum = 1, GetNumSpecializationsForClassID(classID) do
local specID, name, description, icon = GetSpecializationInfoForClassID(classID, specNum)
iconData[specID] = icon
end
end
end