Guest User

Untitled

a guest
Jun 6th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 44.04 KB | None | 0 0
  1. local UI, C = select(2, ...):unpack()
  2. local np = UI:NewModule("np")
  3.  
  4. local minIconSize = 16
  5. local maxIconSize = 80
  6. local minTextSize = 6
  7. local maxTextSize = 20
  8. local defaultBuffShow           = 3
  9. local defaultDebuffShow         = 2
  10. local saveNameToGUID            = true
  11. local watchUnitIDAuras = true
  12. local abovePlayers      = true
  13. local aboveNPC      = true
  14. local aboveFriendly     = true
  15. local aboveNeutral      = true
  16. local aboveHostile      = true
  17. local barAnchorPoint        = "BOTTOM"
  18. local plateAnchorPoint      = "TOP"
  19. local Spacing = 0
  20. local barOffsetX        = 0
  21. local barOffsetY        = 8
  22. local iconsPerBar       = 6
  23. local barGrowth     = 1
  24. local numBars       = 2
  25. local iconSize      = 24
  26. local biggerSelfSpells      = true
  27. local showCooldown      = true
  28. local shrinkBar     = true
  29. local cooldownSize      = 10
  30. local stackSize     = 10
  31. local showCooldownTexture       = true
  32. local blinkTimeleft     = 0.2 --20%
  33. local showTotems = true
  34.  
  35. local scanDelay     = 1
  36. local updateDelay   = 1
  37.  
  38. np.nameplates = {}
  39. np.GUIDs = {}
  40. np.names = {}
  41. np.onShowHooks = {}
  42. np.onHideHooks = {}
  43. np.plateGUIDs = {}
  44. np.isOnScreen = {}
  45. np.isOnUpdating = {}
  46.  
  47. local function IsNamePlateFrame(frame)
  48.     if frame:GetName() then return false end
  49.     if frame:GetID() ~= 0 then return false end
  50.     if frame:GetObjectType() ~= "Frame" then return false end
  51.     if frame:GetNumChildren() == 0 then
  52.         return false
  53.     end
  54.     if frame:GetNumRegions() == 0 then
  55.         return false
  56.     end
  57.     return true
  58. end
  59.  
  60. local function ScanWorldFrameChildren(n, ...)
  61.     for i = 1, n do
  62.         local frame = select(i, ...)
  63.         if frame:IsShown() and IsNamePlateFrame(frame) then
  64.             if not np.nameplates[frame] then
  65.                 np:NameplateFirstLoad(frame)
  66.                 np:SetupNameplate(frame)                    
  67.             end
  68.         end    
  69.     end
  70. end
  71.  
  72. local lastChildren = 0
  73. local function FindNameplates()
  74.     local curChildren = WorldFrame:GetNumChildren()
  75.     if curChildren ~= lastChildren then
  76.         lastChildren = curChildren
  77.         ScanWorldFrameChildren(curChildren, WorldFrame:GetChildren())
  78.     end
  79. end
  80.  
  81. local function HideMouseoverRegion(frame)
  82.     local region = select(6 ,frame:GetRegions())
  83.     if region and region.Hide then
  84.         region:Hide()
  85.     end
  86. end
  87.  
  88. local function RecycleNameplate(frame)
  89.     np:RecycleNameplate(frame)
  90.     if np.plateGUIDs[frame] then
  91.         np.GUIDs[np.plateGUIDs[frame]] = false
  92.     end
  93.     local plateName = np:GetName(frame)
  94.     if plateName and np.names[plateName] then
  95.         np.names[plateName] = false
  96.     end
  97.     np.plateGUIDs[frame] = false
  98. end
  99.  
  100. local function FoundPlateGUID(frame, GUID, unitID)
  101.     np.plateGUIDs[frame] = GUID
  102.     np.GUIDs[GUID] = frame
  103.     np:FoundGUID(frame, GUID, unitID)
  104. end
  105.  
  106. local function GetMouseoverGUID(frame)
  107.     local unitID = "mouseover"
  108.     if UnitExists(unitID) then
  109.         FoundPlateGUID(frame, UnitGUID(unitID), unitID)
  110.     end
  111. end
  112.  
  113. local function FindPlateWithRaidIcon(iconNum)
  114.     for frame in pairs(np.nameplates) do
  115.         if np:IsMarked(frame) and np:GetRaidIcon(frame) == iconNum then
  116.             return frame
  117.         end
  118.     end
  119.     return nil
  120. end
  121.  
  122. local function CheckRaidIconOnUnit(unitID, frame, raidNum, from)
  123.     local targetID = unitID.."target"
  124.     local targetIndex
  125.     if UnitExists(targetID) and not UnitIsUnit("target", targetID) then
  126.         targetIndex = GetRaidTargetIndex(targetID)
  127.         if targetIndex and targetIndex == raidNum then
  128.             FoundPlateGUID(frame, UnitGUID(targetID), targetID)
  129.             return true
  130.         end
  131.     end
  132.     return false
  133. end
  134.  
  135. local function FindGUIDByRaidIcon(frame, raidNum, from)
  136.     local group, num = "", 0
  137.     if GetNumRaidMembers() > 1 then
  138.         group, num = "raid", GetNumRaidMembers()
  139.     elseif GetNumPartyMembers() > 0 then
  140.         group, num = "party", GetNumPartyMembers()
  141.     else
  142.         return
  143.     end
  144.     local unitID
  145.     for i = 1, num do
  146.         unitID = group..i;
  147.         if CheckRaidIconOnUnit(unitID, frame, raidNum, from) then
  148.             return
  149.         end
  150.         if UnitExists(unitID.."pet") then
  151.             if CheckRaidIconOnUnit(unitID.."pet", frame, raidNum, from) then
  152.                 return
  153.             end
  154.         end
  155.     end
  156. end
  157.  
  158. local function UpdateNameplateInfo(frame)
  159.     if np:IsMouseover(frame) and not UnitExists("mouseover") then
  160.         HideMouseoverRegion(frame)
  161.     end
  162.     if not np.plateGUIDs[frame] then
  163.         if np:IsMouseover(frame) then
  164.             GetMouseoverGUID(frame)
  165.         elseif np:IsMarked(frame) then
  166.             local raidNum = np:GetRaidIcon(frame)
  167.             if raidNum and raidNum > 0 then
  168.                 FindGUIDByRaidIcon(frame, raidNum, "UpdateNameplateInfo")
  169.             end
  170.         end
  171.     end
  172.     frame.lnpLastUpdate = 0
  173. end
  174.  
  175. local function CheckUnitIDForMatchingHP(unitID, frameName, current, max)
  176.     local targetID = unitID.."target"
  177.     if UnitName(targetID) == frameName then
  178.         local health = UnitHealth(targetID)
  179.         local maxHealth = UnitHealthMax(targetID)
  180.         if health == current and maxHealth == max then
  181.             return true
  182.         end
  183.     end
  184.     return false
  185. end
  186.  
  187. function np:NewNameplateCheckHP(frame)
  188.     local bar = select(1, frame:GetChildren())
  189.     if bar and bar.GetValue then
  190.         local _, max = bar:GetMinMaxValues()
  191.         local current = bar:GetValue()
  192.         np.prevHealth[frame] = current
  193.         if current > 0 and current ~= max then
  194.             local group, num = "", 0
  195.             if GetNumRaidMembers() > 1 then
  196.                 group, num = "raid", GetNumRaidMembers()
  197.             elseif GetNumPartyMembers() > 0 then
  198.                 group, num = "party", GetNumPartyMembers()
  199.             else
  200.                 return
  201.             end
  202.             local possibleUnits = {}
  203.             local frameName = self:GetName(frame)
  204.             local unitID, targetID, targetIndex
  205.             for i = 1, num do
  206.                 unitID = group..i;
  207.                 if CheckUnitIDForMatchingHP(unitID, frameName, current, max) then
  208.                     table.insert(possibleUnits, #possibleUnits+1, unitID.."target")
  209.                 end
  210.                 if UnitExists(unitID.."pet") then
  211.                     if CheckUnitIDForMatchingHP(unitID.."pet", frameName, current, max) then
  212.                         table.insert(possibleUnits, #possibleUnits+1, unitID.."pettarget")
  213.                     end
  214.                 end
  215.             end
  216.             if #possibleUnits == 1 then
  217.                 FoundPlateGUID(frame, UnitGUID(possibleUnits[1]), possibleUnits[1])
  218.                 return true
  219.             end
  220.         end
  221.     end
  222. end
  223.  
  224. np.prevHealth = np.prevHealth or {}
  225.  
  226. function np.OnNameplateShow(frame, ...)
  227.     np:SetupNameplate(frame)
  228.     np:NewNameplateCheckHP(frame)
  229. end
  230. local function ourOnShow(...) return np.OnNameplateShow(...) end
  231.  
  232. function np.OnNameplateHide(frame, ...)
  233.     np.isOnScreen[frame] = false
  234.     np.isOnUpdating[frame] = false
  235.     RecycleNameplate(frame)
  236. end
  237. local function ourOnHide(...) return np.OnNameplateHide(...) end
  238.  
  239. function np.OnNameplateUpdate(frame, elapsed, ...)
  240.     np.isOnUpdating[frame] = true
  241.     if frame.lnpCheckForTarget then
  242.         frame.lnpCheckForTarget = false
  243.         if not np.plateGUIDs[frame] and frame:IsShown() and ((frame:GetAlpha() == 1) and UnitExists("target")) then
  244.             FoundPlateGUID(frame, UnitGUID("target"), "target")
  245.         end
  246.     end
  247.     frame.lnpLastUpdate = (frame.lnpLastUpdate or 0) + elapsed
  248.     if frame.lnpLastUpdate > updateDelay then
  249.         UpdateNameplateInfo(frame)
  250.     end
  251. end
  252.  
  253. local testing = false
  254. function np:HookNameplate(frame)
  255.     if frame:HasScript("OnHide") and not self.onHideHooks[frame] then
  256.         self.onHideHooks[frame] = true
  257.         frame:HookScript("OnHide", ourOnHide)
  258.     end
  259.     if frame:HasScript("OnShow") and not self.onShowHooks[frame] then
  260.         self.onShowHooks[frame] = true
  261.         frame:HookScript("OnShow", ourOnShow)
  262.     end
  263. end
  264.  
  265. function np:NameplateFirstLoad(frame)
  266.     self:HookNameplate(frame)
  267. end
  268.  
  269. function np:SetupNameplate(frame)
  270.     self.isOnScreen[frame] = true
  271.     local plateName = self:GetName(frame)
  272.     self.nameplates[frame] = plateName
  273.     self.names[plateName] = frame
  274.     np:NewNameplate(frame)
  275.     frame.lnpCheckForTarget = true
  276.     UpdateNameplateInfo(frame)
  277. end
  278.  
  279. local function CheckForTargetGUID()
  280.     local unitID = "target"
  281.     local GUID
  282.     for frame in pairs(np.nameplates) do
  283.         if np:IsTarget(frame) then
  284.             np.targeted = frame
  285.             if not np.plateGUIDs[frame] then
  286.                 FoundPlateGUID(frame, UnitGUID(unitID), unitID)
  287.             end
  288.             np:TargetNameplate(frame)
  289.             return
  290.         end
  291.     end
  292. end
  293.  
  294. local function OnUpdate(frame, elapsed)
  295.     if frame.checkTarget then
  296.         frame.checkTarget = false
  297.         CheckForTargetGUID()
  298.     end
  299.     FindNameplates()
  300.     frame.lastUpdate = frame.lastUpdate + elapsed
  301.     if frame.lastUpdate >= scanDelay then
  302.         frame.lastUpdate = 0
  303.         for frame, value in pairs(np.isOnScreen) do
  304.             if (value == true and not frame:IsShown()) then
  305.                 np.onHideHooks[frame] = false
  306.                 np.isOnScreen[frame] = false
  307.                 np:HookNameplate(frame)
  308.                 np.OnNameplateHide(frame)
  309.                
  310.             elseif (value == false and frame:IsShown()) then
  311.                 np.onShowHooks[frame] = false
  312.                 np.isOnScreen[frame] = false
  313.                 np:HookNameplate(frame)
  314.                 np:SetupNameplate(frame, true)
  315.                
  316.             end
  317.         end
  318.     end
  319. end
  320.  
  321. local function OnEvent(frame, event, ...)
  322.     if event == "UPDATE_MOUSEOVER_UNIT" then
  323.         if GetMouseFocus():GetName() == "WorldFrame" then
  324.             local i = 0
  325.             local mouseoverPlate
  326.             for frame in pairs(np.nameplates) do
  327.                 if frame:IsShown() and np:IsMouseover(frame) then
  328.                     i = i + 1
  329.                     mouseoverPlate = frame
  330.                 end
  331.             end
  332.             if i == 1 then
  333.                 if not np.plateGUIDs[mouseoverPlate] then
  334.                     GetMouseoverGUID(mouseoverPlate)
  335.                 end
  336.                 np:MouseoverNameplate(mouseoverPlate)
  337.             end
  338.         end
  339.     elseif event == "PLAYER_TARGET_CHANGED" then
  340.         if UnitExists("target") then
  341.             frame.checkTarget = true
  342.         else
  343.             np.targeted = nil
  344.         end
  345.     elseif event == "UNIT_TARGET" then
  346.         local unitID = ...
  347.         local targetID = unitID.."target"
  348.         if UnitExists(targetID) and not UnitIsUnit("player", unitID) and UnitInRange(unitID) then
  349.             local targetGUID = UnitGUID(targetID)
  350.             local iconNum = GetRaidTargetIndex(targetID)
  351.             if iconNum and iconNum > 0 then
  352.                 local foundPlate = FindPlateWithRaidIcon(iconNum)
  353.                 if foundPlate and not np.plateGUIDs[foundPlate] then
  354.                     FoundPlateGUID(foundPlate, targetGUID, targetID)
  355.                 end
  356.             end
  357.             if np.GUIDs[targetGUID] and np.GUIDs[targetGUID]:IsShown() then
  358.                 return
  359.             end
  360.             local health = UnitHealth(targetID)
  361.             local maxHealth = UnitHealthMax(targetID)
  362.             if health > 0 and health ~= maxHealth then
  363.                 local foundPlate = np:GetNameplateByUnit(targetID)
  364.                 if foundPlate and not np.plateGUIDs[foundPlate] then
  365.                     local name = UnitName(targetID)
  366.                     if name == np:GetName(foundPlate) then
  367.                         FoundPlateGUID(foundPlate, targetGUID, targetID)
  368.                     end
  369.                 end
  370.             end
  371.         end
  372.     elseif event == "RAID_TARGET_UPDATE" then
  373.         for frame in pairs(np.nameplates) do
  374.             if frame:IsShown() and not np.plateGUIDs[frame] and np:IsMarked(frame) then
  375.                 local raidNum = np:GetRaidIcon(frame)
  376.                 if raidNum and raidNum > 0 then
  377.                     FindGUIDByRaidIcon(frame, raidNum, event)
  378.                 end
  379.             end
  380.         end
  381.     end
  382. end
  383.  
  384. np.frame = np.frame or CreateFrame("Frame")
  385.  
  386. np.frame.lastUpdate = 0
  387. np.frame.lastHPCheck = 0
  388. np.frame.checkTarget = false
  389. np.frame:SetScript("OnUpdate", OnUpdate)
  390. np.frame:SetScript("OnEvent", OnEvent)
  391. np.frame:RegisterEvent('UPDATE_MOUSEOVER_UNIT')
  392. np.frame:RegisterEvent('PLAYER_TARGET_CHANGED')
  393. np.frame:RegisterEvent('UNIT_TARGET')
  394. np.frame:RegisterEvent('RAID_TARGET_UPDATE')
  395. np.frame:RegisterEvent('PLAYER_ENTERING_WORLD')
  396.  
  397. local raidIconTexCoord = {
  398.     [0]     = {
  399.         [0]     = 1, --star
  400.         [0.25]  = 5, --moon
  401.     },
  402.     [0.25]  = {
  403.         [0]     = 2, --circle
  404.         [0.25]  = 6, --square
  405.     },
  406.     [0.5]   = {
  407.         [0]     = 3, --star
  408.         [0.25]  = 7, --cross
  409.     },
  410.     [0.75]  = {
  411.         [0]     = 4, --star
  412.         [0.25]  = 8, --skull
  413.     },
  414. }
  415.  
  416. local function reactionByColor(red, green, blue, a)                                                                                                        
  417.     if red < .01 and blue < .01 and green > .99 then return "FRIENDLY", "NPC"
  418.     elseif red < .01 and blue > .99 and green < .01 then return "FRIENDLY", "PLAYER"
  419.     elseif red > .99 and blue < .01 and green > .99 then return "NEUTRAL", "NPC"
  420.     elseif red > .99 and blue < .01 and green < .01 then return "HOSTILE", "NPC"
  421.     else return "HOSTILE", "PLAYER" end
  422. end
  423.  
  424. local colorToClass = {}
  425. local function pctToInt(number) return math.floor((100*number) + 0.5) end
  426. for classname, color in pairs(RAID_CLASS_COLORS) do
  427.     colorToClass["C"..pctToInt(color.r)+pctToInt(color.g)+pctToInt(color.b)] = classname
  428. end
  429.  
  430. local function threatByColor( region )
  431.     if not region:IsShown() then return "LOW" end
  432.     local redCan, greenCan, blueCan, alphaCan = region:GetVertexColor()
  433.     if greenCan > .7 then return "MEDIUM" end
  434.     if redCan > .7 then return "HIGH" end
  435. end
  436.  
  437. local function combatByColor(r, g, b, a)
  438.     return (r > .5 and g < .5)
  439. end
  440.  
  441. local function GetHealthBarColor(frame)
  442.     local bar = select(1, frame:GetChildren())
  443.     if bar and bar.GetStatusBarColor then
  444.         return bar:GetStatusBarColor()
  445.     end
  446.     return nil
  447. end
  448.  
  449. local function RemoveHexColor(inputString)
  450.     local find = inputString:find("|c")
  451.     if find then
  452.         inputString = inputString:sub(find+10)
  453.     end
  454.     inputString = inputString:gsub("|r", "")
  455.     return inputString
  456. end
  457.  
  458. function np:GetName(frame)
  459.     local nameRegion = select(7, frame:GetRegions())
  460.     if nameRegion and nameRegion.GetText then
  461.         return RemoveHexColor( nameRegion:GetText() )
  462.     end
  463.     return nil
  464. end
  465.  
  466. function np:GetScale(frame)
  467.     return frame:GetScale()
  468. end
  469.  
  470. function np:GetVisibleFrame(frame)
  471.     return frame
  472. end
  473.  
  474. function np:GetReaction(frame)
  475.     local r,g,b = GetHealthBarColor(frame)
  476.     if r then
  477.         return reactionByColor(r, g, b )
  478.     end
  479.     return nil
  480. end
  481.  
  482. function np:GetType(frame)
  483.     local r, g, b = GetHealthBarColor(frame)
  484.     if r then
  485.         return select(2, reactionByColor( r, g, b ) )
  486.     end
  487.     return nil
  488. end
  489.  
  490. function np:IsTarget(frame)
  491.     return frame:IsShown() and frame:GetAlpha() == 1 and UnitExists("target") or false
  492. end
  493.  
  494. function np:GetRaidIcon(frame)
  495.     local region = select(10 ,frame:GetRegions())
  496.     if region and region.IsShown and region:IsShown() and region.GetTexCoord then
  497.         local ULx,ULy = region:GetTexCoord() --,LLx,LLy,URx,URy,LRx,LRy
  498.         if ULx and ULy then
  499.             return raidIconTexCoord[ULx] and raidIconTexCoord[ULx][ULy] or 0
  500.         end
  501.     end
  502.     return nil
  503. end
  504.  
  505. function np:IsMouseover(frame)
  506.     local region = select(6 ,frame:GetRegions())
  507.     if region and region.IsShown then
  508.         return region:IsShown() and true or false
  509.     end
  510.     return nil
  511. end
  512.  
  513. function np:IsMarked(frame)
  514.     local region = select(10 ,frame:GetRegions())
  515.     if region and region.IsShown then
  516.         return region:IsShown() and true or false
  517.     end
  518.     return nil
  519. end
  520.  
  521. function np:GetGUID(frame)
  522.     return self.plateGUIDs[frame]
  523. end
  524.  
  525. function np:GetTargetNameplate()
  526.     if self.targeted and self.targeted:IsShown() then
  527.         return self.targeted
  528.     end
  529. end
  530.  
  531. function np:GetNameplateByGUID(GUID)
  532.     if self.GUIDs[GUID] and self.GUIDs[GUID]:IsShown() then
  533.         return self.GUIDs[GUID]
  534.     end
  535. end
  536.  
  537. function np:GetNameplateByName(name)
  538.     if self.names[name] and self.names[name]:IsShown() then
  539.         return self.names[name]
  540.     end
  541. end
  542.  
  543. function np:GetNameplateByUnit(unitID)
  544.  
  545.     if unitID=="player" then return end
  546.     for frame in pairs(np.nameplates) do
  547.         if frame:IsShown() then
  548.             local name = UI:utf8sub(select(7, frame:GetRegions()):GetText(), 13, false)
  549.             local cur = frame.healthBar:GetValue()
  550.             local max = select(2, frame.healthBar:GetMinMaxValues())
  551.             if UnitHealth(unitID)>1 and name and name==UnitName(unitID) and cur==UnitHealth(unitID) and max==UnitHealthMax(unitID) then
  552.                 return frame
  553.             end
  554.         end
  555.     end
  556. end
  557.  
  558. local function Round(num, zeros)
  559.     return math.floor( num * 10 ^ (zeros or 0) + 0.5 ) / 10 ^ (zeros or 0)
  560. end
  561.  
  562. local function GetFullName(unitID)
  563.     local name = GetUnitName(unitID, true)
  564.     name = name:gsub(" - ","")
  565.     return name
  566. end
  567.  
  568. local totemList = {
  569.     2484,--Earthbind Totem
  570.     8143,--Tremor Totem
  571.     8177,--Grounding Totem
  572.     8512,--Windfury Totem
  573.     6495,--Sentry Totem
  574.     8170,--Cleansing Totem
  575.     3738,--Wrath of Air Totem
  576.     2062,--Earth Elemental Totem
  577.     2894,--Fire Elemental Totem
  578.     58734,--Magma Totem
  579.     58582,--Stoneclaw Totem
  580.     58753,--Stoneskin Totem
  581.     58739,--Fire Resistance Totem
  582.     58656,--Flametongue Totem
  583.     58745,--Frost Resistance Totem
  584.     58757,--Healing Stream Totem
  585.     58774,--Mana Spring Totem
  586.     58749,--Nature Resistance Totem
  587.     58704,--Searing Totem
  588.     58643,--Strength of Earth Totem
  589.     57722,--Totem of Wrath
  590. }
  591.  
  592. local defaultSpells1 = {
  593.     118, --Polymorph
  594.     51514, --Hex
  595.     710, --Banish
  596.     6358, --Seduction
  597.     6770, --Sap
  598.     605, --Mind Control
  599.     33786, --Cyclone
  600.     5782, --Fear
  601.     5484, --Howl of Terror
  602.     6789, --Death Coil
  603.     45438, --Ice Block
  604.     642, --Divine Shield
  605.     8122, --Psychic Scream
  606.     339, --Entangling Roots
  607.     23335, -- Silverwing Flag (alliance WSG flag)
  608.     23333, -- Warsong Flag (horde WSG flag)
  609.     34976, -- Netherstorm Flag (EotS flag)
  610.     2094, --Blind
  611.     33206, --Pain Suppression (priest)
  612.     29166, --Innervate (druid)
  613.     47585, --Dispersion (priest)
  614.     19386, --Wyvern Sting (hunter)
  615. }
  616.  
  617. local defaultSpells2 = {
  618.     15487, --Silence (priest)
  619.     10060, --Power Infusion (priest)
  620.     2825, --Bloodlust
  621.     5246, --Intimidating Shout (warrior)
  622.     31224, --Cloak of Shadows (rogue)
  623.     498, --Divine Protection
  624.     47476, --Strangulate (warlock)
  625.     31884, --Avenging Wrath (pally)
  626.     37587, --Bestial Wrath (hunter)
  627.     12472, --Icy Veins (mage)
  628.     49039, --Lichborne (DK)
  629.     48792, --Icebound Fortitude (DK)
  630.     5277, --Evasion (rogue)
  631.     53563, --Beacon of Light (pally)
  632.     22812, --Barkskin (druid)
  633.     67867, --Trampled (ToC arena spell when you run over someone)
  634.     1499, --Freezing Trap
  635.     2637, --Hibernate
  636.     64044, --Psychic Horror
  637.     19503, --Scatter Shot (hunter)
  638.     34490, --Silencing Shot (hunter)
  639.     10278, --Hand of Protection (pally)
  640.     10326, --Turn Evil (pally)
  641.     44572, --Deep Freeze (mage)
  642.     20066, --Repentance (pally)
  643.     46968, --Shockwave (warrior)
  644.     46924, --Bladestorm (warrior)
  645.     16689, --Nature's Grasp (Druid)
  646.     53601, --блядощит ΠΏΠ΅Π»Π°Π΄ΠΈΠ½Π°
  647. }
  648.  
  649. local myClass = select(2, UnitClass("player"))
  650. if myClass == "DRUID" or myClass == "ROGUE" then
  651.     table.insert(defaultSpells2, #defaultSpells2+1, 132)
  652.     table.insert(defaultSpells2, #defaultSpells2+1, 16882)
  653.     table.insert(defaultSpells2, #defaultSpells2+1, 6512)
  654. end
  655.  
  656. local regEvents = {
  657.     "PLAYER_TARGET_CHANGED",
  658.     "UPDATE_MOUSEOVER_UNIT",
  659.     "UNIT_AURA",
  660.     "UNIT_TARGET",
  661. }
  662.  
  663. local spellOpts = {}
  664. local ignoreDefaultSpell = {}
  665.  
  666. local buffFrames = {}
  667. local guidBuffs = {}
  668. local nametoGUIDs = {}
  669. local buffBars = {}
  670.  
  671. local totems = {}
  672. local name, texture
  673. for i=1,table.getn(totemList) do
  674.     name, _, texture = GetSpellInfo(totemList[i])
  675.     totems[name] = texture
  676. end
  677.  
  678. local spellName
  679. for i=1, table.getn(defaultSpells1) do
  680.     spellName = GetSpellInfo(defaultSpells1[i])
  681.     if spellName then
  682.         spellOpts[spellName] = {
  683.             iconSize = 80, --max size
  684.             cooldownSize = 18,--almost max.
  685.             show = 1, --always show spell
  686.             stackSize = 18,
  687.         }
  688.     end
  689. end
  690.  
  691. for i=1, table.getn(defaultSpells2) do
  692.     spellName = GetSpellInfo(defaultSpells2[i])
  693.     if spellName then
  694.         spellOpts[spellName] = {
  695.             iconSize = 40, --mid size
  696.             cooldownSize = 14,
  697.             show = 1, --always show spell
  698.             stackSize = 14,
  699.         }
  700.     end
  701. end
  702.  
  703. local function HidePlateSpells(plate)
  704.     if buffFrames[plate] then
  705.         for i=1, table.getn(buffFrames[plate]) do
  706.             buffFrames[plate][i]:Hide()
  707.         end
  708.     end
  709. end
  710.  
  711. local function isTotem(name)
  712.     return totems[name]
  713. end
  714.  
  715. local function ShouldAddBuffs(plate)
  716.     local plateName =  np:GetName(plate) or "UNKNOWN"
  717.     if showTotems == false and isTotem(plateName) then
  718.         return false
  719.     end
  720.     local plateType = np:GetType(plate)
  721.     if (abovePlayers == true and plateType == "PLAYER") or (aboveNPC == true and plateType == "NPC") then
  722.  
  723.         local plateReaction = np:GetReaction(plate)
  724.         if aboveFriendly == true and plateReaction == "FRIENDLY" then
  725.             return true
  726.         elseif aboveNeutral == true and plateReaction == "NEUTRAL" then
  727.             return true
  728.         elseif aboveHostile == true and plateReaction == "HOSTILE" then
  729.             return true
  730.         end
  731.     end
  732.     return false
  733. end
  734.  
  735. local function RemoveOldSpells(GUID)
  736.     for i=(numBars * iconsPerBar), 1, -1 do
  737.         if guidBuffs[GUID] and guidBuffs[GUID][i] then
  738.             if guidBuffs[GUID][i].expirationTime and guidBuffs[GUID][i].expirationTime > 0 and GetTime() > guidBuffs[GUID][i].expirationTime then
  739.                 table.remove(guidBuffs[GUID], i)
  740.             end
  741.         end
  742.     end
  743. end
  744.  
  745. local function CreateBarFrame(parentFrame, realPlate)
  746.     local f = CreateFrame("frame", nil, parentFrame)
  747.     f.realPlate = realPlate
  748.     f:SetFrameStrata("BACKGROUND")
  749.     f:SetFrameLevel(parentFrame:GetFrameLevel())
  750.     f:SetWidth(1)
  751.     f:SetHeight(1)
  752.     f.barBG = f:CreateTexture(nil,"LOW")
  753.     f.barBG:SetAllPoints(true)
  754.     f.barBG:SetTexture(1,1,1,0.3)
  755.     if showBarBackground == true then
  756.         f.barBG:Show()
  757.     else
  758.         f.barBG:Hide()
  759.     end
  760.     f:Show()
  761.     return f
  762. end
  763.  
  764. local function BuildPlateBars(plate, visibleFrame) 
  765.     buffBars[plate] = buffBars[plate] or {}
  766.     if not buffBars[plate][1] then
  767.         buffBars[plate][1] = CreateBarFrame(visibleFrame, plate)
  768.     end
  769.     buffBars[plate][1]:ClearAllPoints()
  770.     buffBars[plate][1]:SetPoint(barAnchorPoint, visibleFrame, plateAnchorPoint, barOffsetX, barOffsetY)
  771.     buffBars[plate][1]:SetParent(visibleFrame)
  772.     local barPoint      = barAnchorPoint
  773.     local parentPoint   = plateAnchorPoint
  774.     if barGrowth == 1 then
  775.         barPoint = string.gsub(barPoint, "TOP", "BOTTOM");
  776.         parentPoint = string.gsub(parentPoint, "BOTTOM", "TOP");
  777.     else
  778.         barPoint = string.gsub(barPoint, "BOTTOM,", "TOP");
  779.         parentPoint = string.gsub(parentPoint, "TOP", "BOTTOM");
  780.     end
  781.     if numBars > 1 then
  782.         for r=2, numBars do
  783.             if not buffBars[plate][r] then
  784.                 buffBars[plate][r] = CreateBarFrame(visibleFrame, plate)
  785.             end
  786.             buffBars[plate][r]:ClearAllPoints()
  787.             buffBars[plate][r]:SetPoint(barPoint, buffBars[plate][r-1], parentPoint, 0, 0)
  788.             buffBars[plate][r]:SetParent(visibleFrame)
  789.         end
  790.     end
  791. end
  792.  
  793. local function UpdateBuffCDSize(buffFrame, size)
  794.     buffFrame.cd:SetFont("Fonts\\FRIZQT__.TTF", size, "NORMAL")
  795.     buffFrame.cdbg:SetHeight(buffFrame.cd:GetStringHeight())
  796. end
  797.  
  798. local function UpdateBuffSize(frame, size)     
  799.     frame.icon:SetWidth(size)
  800.     frame.icon:SetHeight(size)
  801.     frame:SetWidth(size)
  802.     if showCooldown == true then
  803.         frame:SetHeight(size + frame.cd:GetStringHeight())
  804.     else
  805.         frame:SetHeight(size)
  806.     end
  807. end
  808.  
  809. local function SetStackSize(buffFrame, size)
  810.     buffFrame.stack:SetFont("Fonts\\FRIZQT__.TTF", size, "OUTLINE")
  811. end
  812.  
  813. local function iconOnShow(self)
  814.     self:SetAlpha(1)
  815.     self.cdbg:Hide()
  816.     self.cd:Hide()
  817.     self.cdtexture:Hide()
  818.     self.stack:Hide()
  819.     if showCooldown == true and self.expirationTime > 0 then
  820.         self.cdbg:Show()
  821.         self.cd:Show()
  822.  
  823.         if showCooldownTexture == true then
  824.             self.cdtexture:SetCooldown(self.startTime, self.duration);
  825.             self.cdtexture:Show()
  826.         end
  827.     end
  828.     local iconSize = iconSize
  829.     local cooldownSize = cooldownSize
  830.     local stackSize = stackSize
  831.     local spellName = self.spellName or "X"
  832.     local spellOpts = np:HaveSpellOpts(spellName)
  833.     if spellOpts then
  834.         iconSize = spellOpts.iconSize or iconSize
  835.         cooldownSize = spellOpts.cooldownSize or cooldownSize
  836.         stackSize = spellOpts.stackSize or stackSize
  837.     end
  838.     UpdateBuffCDSize(self, cooldownSize)
  839.     if self.stackCount and self.stackCount > 1 then
  840.         self.stack:SetText(self.stackCount)
  841.         self.stack:Show()
  842.         SetStackSize(self, stackSize)
  843.     end
  844.     if self.isDebuff then
  845.         local colour = DebuffTypeColor[self.debuffType or ""]
  846.         if colour then
  847.             self.icon:SetBackdropBorderColor(colour.r, colour.g, colour.b)
  848.         end
  849.     end
  850.     if self.playerCast and biggerSelfSpells == true then
  851.         UpdateBuffSize(self, iconSize * 1.2)
  852.     else
  853.         UpdateBuffSize(self, iconSize)
  854.     end
  855. end
  856.  
  857. local function iconOnUpdate(self, elapsed)
  858.     self.lastUpdate = self.lastUpdate + elapsed
  859.     if self.lastUpdate > 0.1 then
  860.         self.lastUpdate = 0
  861.         if self.expirationTime > 0 then
  862.             local rawTimeLeft = self.expirationTime - GetTime()
  863.             local timeLeft
  864.             if rawTimeLeft < 10 then
  865.                 timeLeft = Round(rawTimeLeft, 1)
  866.             else
  867.                 timeLeft = Round(rawTimeLeft)
  868.             end
  869.             if showCooldown == true then
  870.                 self.cd:SetText(timeLeft)
  871.                 self.cdbg:SetWidth(self.cd:GetStringWidth())
  872.             end
  873.             if (timeLeft / self.duration ) < blinkTimeleft and timeLeft < 60 then
  874.                 local f = GetTime() % 1
  875.                 if f > 0.5 then
  876.                     f = 1 - f
  877.                 end
  878.                 self:SetAlpha(f * 3)
  879.             end
  880.             if GetTime() > self.expirationTime then
  881.                 self:Hide()
  882.                 local GUID = np:GetGUID(self.realPlate)
  883.                 if GUID then
  884.                     RemoveOldSpells(GUID)
  885.                     np:AddBuffsToPlate(self.realPlate, GUID)
  886.                 else
  887.                     local plateName =  np:GetName(self.realPlate)
  888.                     if plateName and nametoGUIDs[plateName] then
  889.                         RemoveOldSpells(nametoGUIDs[plateName])
  890.                         np:AddBuffsToPlate(self.realPlate, nametoGUIDs[plateName])
  891.                     end
  892.                 end
  893.             end
  894.         end
  895.     end
  896. end
  897.  
  898. function np:AddBuffsToPlate(plate, GUID)   
  899.     if not buffFrames[plate] or not buffFrames[plate][numBars]  then
  900.         np:BuildBuffFrame(plate)
  901.     end
  902.     local t, f
  903.     if guidBuffs[GUID] then
  904.         table.sort(guidBuffs[GUID], function(a,b)
  905.             if(a and b) then
  906.                 if a.playerCast ~= b.playerCast then
  907.                     return (a.playerCast or 0) > (b.playerCast or 0)
  908.                 elseif a.expirationTime == b.expirationTime then
  909.                     return a.name  < b.name
  910.                 else
  911.                     return (a.expirationTime or 0) < (b.expirationTime or 0)
  912.                 end
  913.             end
  914.         end)
  915.         for i=1, numBars * iconsPerBar do
  916.             if buffFrames[plate][i] then
  917.                 if guidBuffs[GUID][i] then
  918.                     buffFrames[plate][i].spellName      = guidBuffs[GUID][i].name or ""
  919.                     buffFrames[plate][i].expirationTime = guidBuffs[GUID][i].expirationTime or 0
  920.                     buffFrames[plate][i].duration       = guidBuffs[GUID][i].duration or 1
  921.                     buffFrames[plate][i].startTime      = guidBuffs[GUID][i].startTime or GetTime()
  922.                     buffFrames[plate][i].stackCount = guidBuffs[GUID][i].stackCount or 0
  923.                     buffFrames[plate][i].isDebuff   = guidBuffs[GUID][i].isDebuff
  924.                     buffFrames[plate][i].debuffType = guidBuffs[GUID][i].debuffType
  925.                     buffFrames[plate][i].playerCast = guidBuffs[GUID][i].playerCast
  926.                     buffFrames[plate][i].texture:SetTexture("Interface\\Icons\\"..guidBuffs[GUID][i].icon)
  927.                     buffFrames[plate][i]:Show()
  928.                     iconOnShow(buffFrames[plate][i])
  929.                     iconOnUpdate(buffFrames[plate][i], 1)
  930.                 else
  931.                     buffFrames[plate][i]:Hide()
  932.                 end
  933.             end
  934.         end
  935.         UpdateAllBarSizes(plate)
  936.     end
  937. end
  938.  
  939. local function iconOnHide(self)
  940.     self.stack:Hide()
  941.     self.cdbg:Hide()
  942.     self.cd:Hide()
  943.     self.cdtexture:Hide()
  944.     self:SetAlpha(1)
  945.    
  946.     UpdateBuffSize(self, iconSize)
  947. end
  948.  
  949. local function CreateBuffFrame(parentFrame, realPlate)
  950.     local f = CreateFrame("Frame", nil, parentFrame)
  951.     f.realPlate = realPlate
  952.     f:SetFrameStrata("LOW")
  953.     f.icon = CreateFrame("Frame", nil, f)
  954.     f.icon:SetPoint("TOP", f)
  955.     f.texture = f.icon:CreateTexture(nil, "BORDER")
  956.     f.texture:SetPoint("TOPLEFT", 2,-2)
  957.     f.texture:SetPoint("BOTTOMRIGHT", -2,2)
  958.     f.texture:SetTexCoord(.07,.93,.07,.93)
  959.     local cd = f:CreateFontString(nil, "ARTWORK", "ChatFontNormal")
  960.     cd:SetText("0")
  961.     cd:SetPoint("TOP", f.icon, "BOTTOM")
  962.     f.cd = cd
  963.     f.cdbg = f:CreateTexture(nil,"BACKGROUND")
  964.     f.cdbg:SetTexture(0,0,0,.75)
  965.     f.cdbg:SetPoint("CENTER", cd)
  966.     f.cdtexture = CreateFrame("Cooldown", nil, f.icon, "CooldownFrameTemplate")
  967.     f.cdtexture:SetReverse(true)
  968.     f.cdtexture:SetDrawEdge(false)
  969.     f.cdtexture:SetAllPoints(true)
  970.     f.stack = f.icon:CreateFontString(nil, "OVERLAY", "ChatFontNormal")
  971.     f.stack:SetText("0")
  972.     f.stack:SetPoint("BOTTOMRIGHT", f.icon, "BOTTOMRIGHT", -1, 3)
  973.     f.lastUpdate = 0
  974.     f.expirationTime = 0
  975.     f:SetScript("OnShow", iconOnShow)
  976.     f:SetScript("OnHide", iconOnHide)
  977.     f:SetScript("OnUpdate", iconOnUpdate)
  978.     f.stackCount = 0
  979.     f.cdbg:Hide()
  980.     f.cd:Hide()
  981.     f.cdtexture:Hide()
  982.     f.stack:Hide()
  983.     return f
  984. end
  985.  
  986. function np:BuildBuffFrame(plate, reset, onlyOne)
  987.     local visibleFrame = plate
  988.     if not buffBars[plate] then
  989.         BuildPlateBars(plate, visibleFrame)
  990.     end
  991.     if not buffBars[plate][numBars] then
  992.         BuildPlateBars(plate, visibleFrame)
  993.     end
  994.     buffFrames[plate] = buffFrames[plate] or {}
  995.     if reset then
  996.         for i=1, table.getn(buffFrames[plate]) do
  997.             buffFrames[plate][i]:Hide()
  998.         end
  999.     end
  1000.     local total = 1
  1001.     if not buffFrames[plate][total] then
  1002.         buffFrames[plate][total] = CreateBuffFrame(buffBars[plate][1], plate)
  1003.     end
  1004.     buffFrames[plate][total]:SetParent(buffBars[plate][1])
  1005.     buffFrames[plate][total]:ClearAllPoints()
  1006.     buffFrames[plate][total]:SetPoint("BOTTOMLEFT", buffBars[plate][1])
  1007.     if onlyOne then
  1008.         return
  1009.     end
  1010.     local prevFrame = buffFrames[plate][total]
  1011.     for i=2, iconsPerBar do
  1012.         total = total + 1
  1013.         if not buffFrames[plate][total] then
  1014.             buffFrames[plate][total] = CreateBuffFrame(buffBars[plate][1], plate)
  1015.         end
  1016.         buffFrames[plate][total]:SetParent(buffBars[plate][1])
  1017.         buffFrames[plate][total]:ClearAllPoints()
  1018.         buffFrames[plate][total]:SetPoint("BOTTOMLEFT", prevFrame, "BOTTOMRIGHT", Spacing, 0)
  1019.         prevFrame = buffFrames[plate][total]
  1020.     end
  1021.     if numBars > 1 then
  1022.         for r=2, numBars do
  1023.             for i=1, iconsPerBar do
  1024.                 total = total + 1
  1025.                 if not buffFrames[plate][total] then
  1026.                     buffFrames[plate][total] = CreateBuffFrame(buffBars[plate][r], plate)
  1027.                 end
  1028.                 buffFrames[plate][total]:SetParent(buffBars[plate][r])
  1029.                 buffFrames[plate][total]:ClearAllPoints()
  1030.                 if i == 1 then
  1031.                     buffFrames[plate][total]:SetPoint("BOTTOMLEFT", buffBars[plate][r])
  1032.                 else
  1033.                     buffFrames[plate][total]:SetPoint("BOTTOMLEFT", prevFrame, "BOTTOMRIGHT")
  1034.                 end
  1035.                 prevFrame = buffFrames[plate][total]
  1036.             end
  1037.         end
  1038.     end
  1039. end
  1040.  
  1041. local function GetBarChildrenSize(n, ...)
  1042.     local frame
  1043.     local totalWidth = 1
  1044.     local totalHeight = 1
  1045.     if n > iconsPerBar then
  1046.         n = iconsPerBar
  1047.     end
  1048.     for i = 1, n do
  1049.         frame = select(i, ...)
  1050.         if shrinkBar == true then
  1051.             if frame:IsShown() then
  1052.                 totalWidth = totalWidth + frame:GetWidth()
  1053.                
  1054.                 if frame:GetHeight() > totalHeight then
  1055.                     totalHeight = frame:GetHeight()
  1056.                 end
  1057.             end
  1058.         else
  1059.             totalWidth = totalWidth + frame:GetWidth()
  1060.            
  1061.             if frame:GetHeight() > totalHeight then
  1062.                 totalHeight = frame:GetHeight()
  1063.             end
  1064.         end
  1065.     end
  1066.     return totalWidth, totalHeight
  1067. end
  1068.  
  1069. local function UpdateBarSize(barFrame)                                 
  1070.     if barFrame:GetNumChildren() == 0 then
  1071.         return
  1072.     end
  1073.     local totalWidth, totalHeight = GetBarChildrenSize(barFrame:GetNumChildren(), barFrame:GetChildren())
  1074.     barFrame:SetWidth(totalWidth)
  1075.     barFrame:SetHeight(totalHeight)
  1076. end
  1077.  
  1078. function UpdateAllBarSizes(plate)
  1079.     for r=1, numBars  do
  1080.         UpdateBarSize(buffBars[plate][r])
  1081.     end
  1082. end
  1083.  
  1084. local function AddOurStuffToPlate(plate)
  1085.     local GUID = np:GetGUID(plate)
  1086.     if GUID then
  1087.         RemoveOldSpells(GUID)
  1088.         np:AddBuffsToPlate(plate, GUID)
  1089.         return
  1090.     end
  1091.     local plateName =  np:GetName(plate) or "UNKNOWN"
  1092.     if saveNameToGUID == true and nametoGUIDs[plateName] and (np:GetType(plate) == "PLAYER") then
  1093.         RemoveOldSpells(nametoGUIDs[plateName])
  1094.         np:AddBuffsToPlate(plate, nametoGUIDs[plateName])
  1095.     end
  1096. end
  1097.  
  1098. function np:HaveSpellOpts(spellName)
  1099.     if not ignoreDefaultSpell[spellName] and spellOpts[spellName] then
  1100.         return spellOpts[spellName]
  1101.     end
  1102.     return false
  1103. end
  1104.  
  1105. function np:CollectUnitInfo(unitID)
  1106.     local GUID = UnitGUID(unitID)
  1107.     local name = UnitName(unitID)
  1108.     if saveNameToGUID == true and UnitIsPlayer(unitID) or UnitClassification(unitID) == "worldboss" then
  1109.         nametoGUIDs[name] = GUID
  1110.     end
  1111.     if watchUnitIDAuras == true then
  1112.         guidBuffs[GUID] = guidBuffs[GUID] or {}
  1113.         for i=table.getn(guidBuffs[GUID]), 1, -1 do
  1114.             table.remove(guidBuffs[GUID], i)
  1115.         end
  1116.         local i = 1;
  1117.         local name, rank, icon, count, dispelType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId
  1118.         while UnitBuff(unitID, i) do
  1119.             name, rank, icon, count, dispelType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId = UnitBuff(unitID, i)
  1120.             for _, _, shortIcon in icon:gmatch("(.+)\\(.+)\\(.+)") do
  1121.                 icon = shortIcon
  1122.                 break
  1123.             end
  1124.             local spellOpts = self:HaveSpellOpts(name)
  1125.             if spellOpts and spellOpts.show then
  1126.                 if spellOpts.show == 1 or (spellOpts.show == 2 and unitCaster and unitCaster == "player") then
  1127.                     table.insert(guidBuffs[GUID], {
  1128.                         name = name,
  1129.                         icon = icon,
  1130.                         expirationTime = expirationTime,
  1131.                         startTime = expirationTime - duration,
  1132.                         duration = duration,
  1133.                         playerCast = unitCaster and unitCaster == "player" and 1,
  1134.                         stackCount = count,
  1135.                         sID = spellId,
  1136.                         caster = unitCaster and  GetFullName(unitCaster),
  1137.                     })
  1138.                 end
  1139.             else
  1140.                 if defaultBuffShow == 1 or (defaultBuffShow == 2 and unitCaster and unitCaster == "player") then
  1141.                     table.insert(guidBuffs[GUID], {
  1142.                         name = name,
  1143.                         icon = icon,
  1144.                         expirationTime = expirationTime,
  1145.                         startTime = expirationTime - duration,
  1146.                         duration = duration,
  1147.                         playerCast = unitCaster and unitCaster == "player" and 1,
  1148.                         stackCount = count,
  1149.                         sID = spellId,
  1150.                         caster = unitCaster and  GetFullName(unitCaster),
  1151.                     })
  1152.                 end
  1153.             end
  1154.             i=i + 1
  1155.         end
  1156.         i = 1;
  1157.         local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId
  1158.         while UnitDebuff(unitID, i) do
  1159.             name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId = UnitDebuff(unitID, i)
  1160.             for _, _, shortIcon in icon:gmatch("(.+)\\(.+)\\(.+)") do
  1161.                 icon = shortIcon
  1162.                 break
  1163.             end
  1164.             local spellOpts = self:HaveSpellOpts(name)
  1165.             if spellOpts and spellOpts.show then
  1166.                 if spellOpts.show == 1 or (spellOpts.show == 2 and unitCaster and unitCaster == "player") then
  1167.                     table.insert(guidBuffs[GUID], {
  1168.                         name = name,
  1169.                         icon = icon,
  1170.                         expirationTime = expirationTime,
  1171.                         startTime = expirationTime - duration,
  1172.                         duration = duration,
  1173.                         playerCast = unitCaster and unitCaster == "player" and 1,
  1174.                         stackCount = count,
  1175.                         debuffType = debuffType,
  1176.                         isDebuff    = true,
  1177.                         sID = spellId,
  1178.                         caster = unitCaster and  GetFullName(unitCaster),
  1179.                     })
  1180.                 end
  1181.             else
  1182.                 if defaultDebuffShow == 1 or (defaultDebuffShow == 2 and unitCaster and unitCaster == "player") then
  1183.                     table.insert(guidBuffs[GUID], {
  1184.                         name = name,
  1185.                         icon = icon,
  1186.                         expirationTime = expirationTime,
  1187.                         startTime = expirationTime - duration,
  1188.                         duration = duration,
  1189.                         playerCast = unitCaster and unitCaster == "player" and 1,
  1190.                         stackCount = count,
  1191.                         debuffType = debuffType,
  1192.                         isDebuff    = true,
  1193.                         sID = spellId,
  1194.                         caster = unitCaster and  GetFullName(unitCaster),
  1195.                     })
  1196.                 end
  1197.             end
  1198.             i=i + 1
  1199.         end
  1200.     end
  1201.     if not self:UpdatePlateByGUID(GUID) and (UnitIsPlayer(unitID) or UnitClassification(unitID) == "worldboss") then
  1202.         self:UpdatePlateByName(name)
  1203.     end
  1204. end
  1205.  
  1206. function np:PLAYER_TARGET_CHANGED()
  1207.     if UnitExists("target") then
  1208.         self:CollectUnitInfo("target")
  1209.     end
  1210. end
  1211.  
  1212. function np:UNIT_TARGET(unitID)
  1213.     if not UnitIsUnit(unitID, "player") and UnitExists(unitID.."target") then
  1214.         self:CollectUnitInfo(unitID.."target")
  1215.     end
  1216. end
  1217.  
  1218. function np:UPDATE_MOUSEOVER_UNIT()
  1219.     if UnitExists("mouseover") then
  1220.         self:CollectUnitInfo("mouseover")
  1221.     end
  1222. end
  1223.  
  1224. function np:UNIT_AURA(unitID)
  1225. if unitID=="player" then return end
  1226.     if UnitExists(unitID) then
  1227.         self:CollectUnitInfo(unitID)
  1228.     end
  1229. end
  1230.  
  1231. function np:UpdatePlateByGUID(GUID)
  1232.     local plate = np:GetNameplateByGUID(GUID)
  1233.     if plate then
  1234.         if ShouldAddBuffs(plate) == true then
  1235.            
  1236.             np:AddBuffsToPlate(plate, GUID)
  1237.             return true
  1238.         end
  1239.     end
  1240.     return false
  1241. end
  1242.  
  1243. function np:UpdatePlateByName(name)
  1244.     local GUID = nametoGUIDs[name]
  1245.     if GUID then
  1246.         local plate = np:GetNameplateByName(name)
  1247.         if plate then
  1248.             if ShouldAddBuffs(plate) == true then
  1249.                 np:AddBuffsToPlate(plate, GUID)
  1250.             end
  1251.             return true
  1252.         end
  1253.     end
  1254. end
  1255.  
  1256. local function PlateOnShow(frame)
  1257.     local name = frame.name
  1258.         name:SetText(UI:utf8sub(frame.nameOld:GetText(),15, true))
  1259.    
  1260.     frame.healthBar:ClearAllPoints()
  1261.     frame.healthBar:SetPoint("TOPLEFT", frame, 15, -15)
  1262.     frame.healthBar:SetPoint("BOTTOMRIGHT", frame, -15, 15)
  1263.        
  1264.     frame.highlight:ClearAllPoints()
  1265.     frame.highlight:SetAllPoints(frame.healthBar)
  1266.     frame.highlight:Hide()
  1267. end
  1268.  
  1269. local function PlateOnEvent(self, event, unit, spell, _, castid)
  1270.  
  1271.     if unit=="player" or unit==unit:match("raid%d") or unit==unit:match("raidpet%d") then return end
  1272.     local unitName = UI:utf8sub(select(7, self:GetRegions()):GetText(), 13, false)
  1273.     local curHP = self.healthBar:GetValue()
  1274.     local maxHP = select(2, self.healthBar:GetMinMaxValues())
  1275.    
  1276.     if unitName~=UnitName(unit) or  curHP~=UnitHealth(unit) or maxHP~=UnitHealthMax(unit) then
  1277.         return
  1278.     end
  1279.    
  1280.     local castbar = self.Castbar
  1281.     if event=="UNIT_SPELLCAST_START" then
  1282.         local castbar = self.Castbar;
  1283.         local name, _, text, texture, startTime, endTime, isTradeSkill, castid, interrupt = UnitCastingInfo(unit)
  1284.         if(not name) then castbar:Hide(); return end
  1285.  
  1286.         castbar.duration = GetTime() - (startTime/1000);
  1287.         castbar.max = (endTime - startTime) / 1000;
  1288.        
  1289.         if(mergeTradeskill and isTradeSkill and UnitIsUnit(unit, "player")) then
  1290.             castbar.duration = castbar.duration + (castbar.max * tradeskillCurrent);
  1291.             castbar.max = castbar.max * tradeskillTotal;
  1292.            
  1293.             if(unit == "player") then
  1294.                 tradeskillCurrent = tradeskillCurrent + 1;
  1295.             end
  1296.             castbar:SetValue(castbar.duration);
  1297.         else
  1298.             castbar:SetValue(0);
  1299.         end
  1300.        
  1301.         castbar:SetMinMaxValues(0, castbar.max);
  1302.         castbar:SetAlpha(1.0);
  1303.         castbar.holdTime = 0;
  1304.         castbar.casting = 1;
  1305.         castbar.castid = castid;
  1306.         castbar.delay = 0;
  1307.         castbar.channeling = nil;
  1308.         castbar.fadeOut = nil;
  1309.         castbar.interrupt = interrupt;
  1310.         castbar.isTradeSkill = isTradeSkill;
  1311.        
  1312.         if(castbar.Icon) then
  1313.             castbar.Icon:SetTexture(texture);
  1314.         end
  1315.        
  1316.         castbar:SetStatusBarColor(1,.8,0)
  1317.         castbar:Show();
  1318.     elseif event=="UNIT_SPELLCAST_FAILED" then
  1319.         local castbar = self.Castbar;
  1320.         if(castbar.castid ~= castid) then return; end
  1321.         castbar:SetValue(castbar.max);
  1322.         castbar.casting = nil;
  1323.         castbar.channeling = nil;
  1324.         castbar.interrupt = nil;
  1325.         castbar.fadeOut = 1;
  1326.         castbar.holdTime = GetTime() + CASTING_BAR_HOLD_TIME;
  1327.         castbar:SetStatusBarColor(1,.2,.2)
  1328.     elseif event=="UNIT_SPELLCAST_INTERRUPTED" then
  1329.         local castbar = self.Castbar;
  1330.         if(castbar.castid ~= castid) then
  1331.             return;
  1332.         end
  1333.         castbar:SetValue(castbar.max);
  1334.         castbar.casting = nil;
  1335.         castbar.channeling = nil;
  1336.         castbar.fadeOut = 1;
  1337.         castbar.holdTime = GetTime() + CASTING_BAR_HOLD_TIME;
  1338.         castbar:SetStatusBarColor(1,.2,.2)
  1339.     elseif event=="UNIT_SPELLCAST_DELAYED" then
  1340.         local castbar = self.Castbar;
  1341.         if(castbar:IsShown()) then
  1342.             local name, _, text, texture, startTime, endTime, isTradeSkill = UnitCastingInfo(unit);
  1343.             if(not name) then return; end
  1344.            
  1345.             local duration = GetTime() - (startTime / 1000);
  1346.             if(duration < 0) then duration = 0; end
  1347.            
  1348.             castbar.delay = castbar.delay + castbar.duration - duration;
  1349.             castbar.duration = duration;
  1350.            
  1351.             castbar:SetValue(duration);
  1352.            
  1353.             if(not castbar.casting) then
  1354.                 castbar.casting = 1;
  1355.                 castbar.channeling = nil;
  1356.                 castbar.fadeOut = 0;
  1357.             end
  1358.         end
  1359.     elseif event=="UNIT_SPELLCAST_STOP" then
  1360.         local castbar = self.Castbar;
  1361.         if(castbar.castid == castid and castbar.casting and (not castbar.fadeOut)) then
  1362.             if(mergeTradeskill and UnitIsUnit(unit, "player")) then
  1363.                 if(tradeskillCurrent == tradeskillTotal) then
  1364.                     mergeTradeskill = false;
  1365.                 end
  1366.             else
  1367.                 castbar:SetValue(castbar.max);
  1368.                 castbar.casting = nil;
  1369.                 castbar.interrupt = nil;
  1370.                 castbar.fadeOut = 1;
  1371.                 castbar.holdTime = 0;
  1372.             end
  1373.         end
  1374.     elseif event=="UNIT_SPELLCAST_CHANNEL_START" then
  1375.         local castbar = self.Castbar;
  1376.         local name, _, text, texture, startTime, endTime, isTradeSkill, notInterruptible = UnitChannelInfo(unit);
  1377.         if(not name) then return; end
  1378.        
  1379.         castbar.duration = ((endTime / 1000) - GetTime());
  1380.         castbar.max = (endTime - startTime) / 1000;
  1381.         castbar.delay = 0;
  1382.         castbar:SetMinMaxValues(0, castbar.max);
  1383.         castbar:SetValue(castbar.duration);
  1384.        
  1385.         if(castbar.Icon) then
  1386.             castbar.Icon:SetTexture(texture);
  1387.         end
  1388.        
  1389.         castbar:SetAlpha(1.0);
  1390.         castbar.holdTime = 0;
  1391.         castbar.casting = nil;
  1392.         castbar.channeling = 1;
  1393.         castbar.interrupt = notInterruptible;
  1394.         castbar.fadeOut = nil;
  1395.         castbar:SetStatusBarColor(.4,1,0)
  1396.         castbar:Show();
  1397.     elseif event=="UNIT_SPELLCAST_CHANNEL_UPDATE" then
  1398.         local castbar = self.Castbar;
  1399.         if(castbar:IsShown()) then
  1400.             local name, _, text, texture, startTime, endTime, isTradeSkill = UnitChannelInfo(unit);
  1401.             if(not name) then return; end
  1402.             local duration = ((endTime / 1000) - GetTime());
  1403.             castbar.delay = castbar.delay + castbar.duration - duration;
  1404.             castbar.duration = duration;
  1405.             castbar.max = (endTime - startTime) / 1000;
  1406.            
  1407.             castbar:SetMinMaxValues(0, castbar.max);
  1408.             castbar:SetValue(duration);
  1409.         end
  1410.     elseif event=="UNIT_SPELLCAST_CHANNEL_STOP" then
  1411.         local castbar = self.Castbar;
  1412.         if(castbar:IsShown() or castbar.channeling)  then
  1413.             castbar:SetValue(castbar.max);
  1414.             castbar.channeling = nil;
  1415.             castbar.interrupt = nil;
  1416.             castbar.fadeOut = 1;
  1417.             castbar.holdTime = 0;
  1418.             castbar:SetStatusBarColor(1,.2,.2)
  1419.         end
  1420.     end
  1421. end
  1422.  
  1423. local function PlateCastbarOnUpdate(self, elapsed)
  1424.     if not self then return end
  1425.     if(self.casting) then
  1426.         local duration = self.duration + elapsed;
  1427.         if(duration >= self.max) then
  1428.             self:SetValue(self.max)
  1429.             self.holdTime = 0
  1430.             self.fadeOut = 1
  1431.             self.casting = nil
  1432.             return
  1433.         end
  1434.  
  1435.         self.duration = duration
  1436.         self:SetValue(duration)
  1437.     elseif(self.channeling) then
  1438.         local duration = self.duration - elapsed;
  1439.         if(duration <= 0) then
  1440.             self.fadeOut = 1
  1441.             self.channeling = nil
  1442.             self.holdTime = 0
  1443.             return
  1444.         end
  1445.  
  1446.         self.duration = duration
  1447.         self:SetValue(duration)
  1448.     elseif(GetTime() < self.holdTime) then
  1449.         return
  1450.     elseif(self.fadeOut) then
  1451.         local alpha = self:GetAlpha() - CASTING_BAR_ALPHA_STEP;
  1452.         if (alpha > 0.05) then
  1453.             self:SetAlpha(alpha);
  1454.         else
  1455.             self.fadeOut = nil;
  1456.             self:Hide();
  1457.         end
  1458.     end
  1459. end
  1460.  
  1461. local function StylePlates(frame)
  1462. if frame.done then return end
  1463.    
  1464.     frame.healthBar, frame.castBar = frame:GetChildren()
  1465.     local healthBar, castBar = frame.healthBar, frame.castBar
  1466.     local glow, overlay, castbarOverlay, shielded, spellIcon, highlight, nameText, levelText, bossIcon, raidIcon, stateIcon = frame:GetRegions()
  1467.    
  1468.     frame.nameOld = nameText
  1469.     local name = frame:CreateFontString(nil, "ARTWORK", "ChatFontSmall")
  1470.         name:SetPoint("TOPLEFT", 15,0)
  1471.     frame.name = name
  1472.    
  1473.     healthBar:SetStatusBarTexture("Interface\\AddOns\\DreamsUI\\media\\st")
  1474.     healthBar:ClearAllPoints()
  1475.     healthBar:SetPoint("TOPLEFT", frame, 15, -15)
  1476.     healthBar:SetPoint("BOTTOMRIGHT", frame, -15, 15)
  1477.    
  1478.     local castbar = CreateFrame("statusbar", nil, frame)
  1479.         castbar:SetScale(UIParent:GetScale())
  1480.         castbar:SetStatusBarTexture("Interface\\AddOns\\DreamsUI\\media\\st")
  1481.         castbar:SetPoint("TOPLEFT",frame, "BOTTOMLEFT", 28, -10)
  1482.         castbar:SetPoint("BOTTOMRIGHT",frame, 0, -20)
  1483.         castbar.tex = castbar:CreateTexture(nil, "BACKGROUND")
  1484.         castbar.tex:SetTexture(0,0,0,0.75)
  1485.         castbar.tex:SetAllPoints()
  1486.        
  1487.     local icon = castbar:CreateTexture(nil, "ARTWORK")
  1488.         icon:SetSize(25,25)
  1489.         icon:SetTexCoord(.08, .92, .08, .92)
  1490.         icon:SetPoint("RIGHT", castbar, "LEFT", -3, 0)
  1491.        
  1492.         castbar.Icon = icon
  1493.         frame.Castbar = castbar
  1494.         frame.Castbar:Hide()
  1495.    
  1496.    
  1497.     levelText:SetFont(ChatFontNormal:GetFont())
  1498.     levelText:SetPoint("TOPRIGHT", -15,0)
  1499.    
  1500.     bossIcon:SetPoint("TOPRIGHT", -15,0)
  1501.    
  1502.     stateIcon:ClearAllPoints()
  1503.     stateIcon:SetPoint("RIGHT",frame, 15, 0)
  1504.    
  1505.     highlight:SetTexture(.3,.3,.3)
  1506.     highlight:ClearAllPoints()
  1507.     highlight:SetAllPoints(healthBar)
  1508.    
  1509.     frame.highlight = highlight
  1510.     frame.glow = glow
  1511.    
  1512.     overlay:SetTexture(nil)
  1513.     glow:SetTexture(nil)
  1514.     nameText:Hide()
  1515.    
  1516.     PlateOnShow(frame)
  1517.     frame:SetScript("OnUpdate", PlateOnUpdate)
  1518.     frame:RegisterEvent("UNIT_SPELLCAST_START")
  1519.     frame:RegisterEvent("UNIT_SPELLCAST_FAILED")
  1520.     frame:RegisterEvent("UNIT_SPELLCAST_STOP")
  1521.     frame:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
  1522.     frame:RegisterEvent("UNIT_SPELLCAST_DELAYED")
  1523.     frame:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START")
  1524.     frame:RegisterEvent("UNIT_SPELLCAST_CHANNEL_UPDATE")
  1525.     frame:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP")
  1526.     frame:SetScript("OnEvent", PlateOnEvent)
  1527.     frame.Castbar:SetScript("OnUpdate", PlateCastbarOnUpdate)
  1528.     frame.done = true
  1529. end
  1530.  
  1531. function np:NewNameplate(plate)
  1532.     StylePlates(plate)
  1533.     PlateOnShow(plate)
  1534.     plate.Castbar:Hide()
  1535.     if ShouldAddBuffs(plate) == true then
  1536.         AddOurStuffToPlate(plate)
  1537.     end
  1538. end
  1539.  
  1540. function np:FoundGUID(plate, GUID, unitID)
  1541.     if ShouldAddBuffs(plate) == true then
  1542.         if not guidBuffs[GUID] then
  1543.             self:CollectUnitInfo(unitID)
  1544.         end
  1545.         RemoveOldSpells(GUID)
  1546.         np:AddBuffsToPlate(plate, GUID)
  1547.     end
  1548. end
  1549.  
  1550. function np:MouseoverNameplate(mouseoverPlate)
  1551.    
  1552. end
  1553.  
  1554. function np:TargetNameplate(frame)
  1555.  
  1556. end
  1557.  
  1558. function np:RecycleNameplate(plate)
  1559.     HidePlateSpells(plate)
  1560. end
  1561.  
  1562. function np:Init()
  1563.     for i, event in pairs(regEvents) do
  1564.         self:RegisterEvent(event)
  1565.     end
  1566.    
  1567.     for plate in pairs(buffBars) do
  1568.         for i=1, table.getn(buffBars[plate]) do
  1569.             buffBars[plate][i]:Show()
  1570.         end
  1571.     end
  1572. end
Advertisement
Add Comment
Please, Sign In to add comment