View Single Post
03-01-21, 07:27 AM   #2
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
I don't pvp so I didn't get to test the enemy nameplate part but here is a start:
Lua Code:
  1. local addonName = ...
  2.  
  3. local iconKey = addonName .. "Icon"
  4.  
  5. local GetNamePlateForUnit = C_NamePlate.GetNamePlateForUnit
  6.  
  7. local isInArena = false
  8.  
  9. local iconTexture = {
  10.     ["DEATHKNIGHT"] = 135771,
  11.     ["DEMONHUNTER"] = 236415,
  12.     ["DRUID"] = 625999,
  13.     ["HUNTER"] = 626000,
  14.     ["MAGE"] = 626001,
  15.     ["MONK"] = 626002,
  16.     ["PALADIN"] = 626003,
  17.     ["PRIEST"] = 626004,
  18.     ["ROGUE"] = 626005,
  19.     ["SHAMAN"] = 626006,
  20.     ["WARLOCK"] = 626007,
  21.     ["WARRIOR"] = 626008
  22. }
  23.  
  24. local function UpdateNamePlateIcon(namePlate, unit)
  25.     local icon = namePlate[iconKey]
  26.     if UnitIsPlayer(unit) then
  27.         local texture
  28.         local specID = GetInspectSpecialization(unit)
  29.         if specID then
  30.             local _, _, _, icon = GetSpecializationInfoByID(specID)
  31.             texture = icon
  32.         end
  33.         if not texture then
  34.             local _, class = UnitClass(unit)
  35.             texture = iconTexture[class]
  36.         end
  37.         if texture then
  38.             if not icon then
  39.                 icon = namePlate:CreateTexture(nil, "OVERLAY")
  40.                 namePlate[iconKey] = icon
  41.             end
  42.             local iconStyle = UnitIsFriend("player", unit) and "friend" or "enemy"
  43.             if icon.style ~= iconStyle then
  44.                 icon.style = iconStyle
  45.                 icon:ClearAllPoints()
  46.                 if iconStyle == "friend" then
  47.                     icon:SetPoint('CENTER', 0, 29.46)
  48.                     icon:SetSize(24, 24)
  49.                 else
  50.                     icon:SetPoint('RIGHT', 10, -5)
  51.                     icon:SetSize(20, 20)
  52.                 end
  53.             end
  54.             icon:SetTexture(texture)
  55.             icon:Show()
  56.             return
  57.         end
  58.     end
  59.     if icon then
  60.         icon:Hide()
  61.     end
  62. end
  63.  
  64. local function UpdateAllNamePlateIcons()
  65.     local nameplates = C_NamePlate.GetNamePlates()
  66.     for index = 1, #nameplates do
  67.         UpdateNamePlateIcon(nameplates[index], nameplates[index].namePlateUnitToken)
  68.     end
  69. end
  70.  
  71. local frame = CreateFrame("Frame")
  72.  
  73. frame:SetScript("OnEvent", function(self, event, unit)
  74.     if event == "NAME_PLATE_UNIT_ADDED" then
  75.         local namePlate = GetNamePlateForUnit(unit)
  76.         UpdateNamePlateIcon(namePlate, unit)
  77.     elseif event == "NAME_PLATE_UNIT_REMOVED" then
  78.         local namePlate = GetNamePlateForUnit(unit)
  79.         if namePlate[iconKey] then
  80.             namePlate[iconKey]:Hide()
  81.         end
  82.     elseif event == "PLAYER_ENTERING_WORLD" then
  83.         if isInArena ~= C_PvP.IsArena() then
  84.             isInArena = not isInArena
  85.             UpdateAllNamePlateIcons()
  86.         end
  87.     elseif event == "ARENA_PREP_OPPONENT_SPECIALIZATIONS" then
  88.         UpdateAllNamePlateIcons()
  89.     end
  90. end)
  91.  
  92. frame:RegisterEvent("ARENA_PREP_OPPONENT_SPECIALIZATIONS")
  93. frame:RegisterEvent("NAME_PLATE_UNIT_ADDED")
  94. frame:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
  95. frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  Reply With Quote