Guest User

Untitled

a guest
Sep 15th, 2012
28
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local _, ns = ...
  2. local oUF = ns.oUF or oUF
  3. assert(oUF, 'oUF_AuraBars was unable to locate oUF install.')
  4. local function Round(number, decimalPlaces)
  5.  
  6.     if decimalPlaces and decimalPlaces > 0 then
  7.         local mult = 10^decimalPlaces
  8.         return math.floor(number * mult + .5) / mult
  9.     end
  10.    
  11.     return math.floor(num + .5)
  12. end
  13.  
  14. local function FormatTime(timeInSec)
  15.     local h = math.floor(timeInSec / 3600)
  16.     local m = math.floor((timeInSec - (3600 * h)) / 60)
  17.     local s = math.floor(timeInSec - ((3600 * h) + (60 * m)))
  18.    
  19.     if h > 0 then
  20.         return h .. ":" .. m .. " h"
  21.     elseif m > 0 then
  22.         return m .. " m"
  23.     else
  24.         return s .. " s"
  25.     end
  26.    
  27. end
  28.  
  29. local framebd = function(parent, anchor)
  30.     local frame = CreateFrame("Frame", nil, parent)
  31.     frame:SetFrameStrata("BACKGROUND")
  32.     frame:SetPoint("TOPLEFT", anchor, "TOPLEFT", -4, 4)
  33.     frame:SetPoint("BOTTOMRIGHT", anchor, "BOTTOMRIGHT", 4, -4)
  34.     frame:SetBackdrop({
  35.     edgeFile = "Interface\\AddOns\\oUF_Skaarj\\Media\\glowTex", edgeSize = 5,
  36.     bgFile = [=[Interface\ChatFrame\ChatFrameBackground]=],
  37.     insets = {left = 4, right = 4, top = 4, bottom = 4}})
  38.     frame:SetBackdropColor(0.8941176470588235, 0.9215686274509803, 0.8784313725490196,0.5)
  39.     frame:SetBackdropBorderColor(0, 0, 0, 0)
  40.     return frame
  41. end
  42.  
  43. local function UpdateTooltip(self)
  44.     GameTooltip:SetUnitAura(self.__unit, self:GetParent().aura.name, self:GetParent().aura.rank, self:GetParent().aura.filter)
  45. end
  46.  
  47. local function OnEnter(self)
  48.  
  49.     if(not self:IsVisible()) then return end
  50.    
  51.     GameTooltip:SetOwner(self, "ANCHOR_BOTTOMRIGHT")
  52.     self:UpdateTooltip()
  53. end
  54.  
  55. local function OnLeave(self)
  56.     GameTooltip:Hide()
  57. end
  58.  
  59. local function SetAnchors(self)
  60.     local bars = self.bars
  61.    
  62.     for index = 1, #bars do
  63.    
  64.         local frame = bars[index]
  65.         local anchor = frame.anchor
  66.         frame:SetWidth(198)
  67.         frame:ClearAllPoints()
  68.        
  69.         if self.down == true then
  70.        
  71.             if self == anchor then
  72.                 frame:SetPoint('TOPLEFT', anchor, 'BOTTOMLEFT', 15, 0)
  73.             else
  74.                 frame:SetPoint('TOPLEFT', anchor, 'BOTTOMLEFT', 0, 0)
  75.             end
  76.            
  77.         else
  78.        
  79.             if self == anchor then
  80.                 frame:SetPoint('BOTTOMLEFT', anchor, 'TOPLEFT', 15, 0)
  81.             else
  82.                 frame:SetPoint('BOTTOMLEFT', anchor, 'TOPLEFT', 0, 0)
  83.             end
  84.            
  85.         end
  86.        
  87.     end
  88.    
  89. end
  90.  
  91. local function CreateAuraBar(oUF, anchor)
  92.     local auraBarParent = oUF.BuffBars or oUF.DebuffBars
  93.     local frame = CreateFrame("Frame", nil, auraBarParent)
  94.     frame:SetHeight(15)
  95.     frame:SetWidth(198)
  96.     frame.anchor = anchor
  97.     local statusBar = CreateFrame("StatusBar", nil, frame)
  98.     statusBar:SetStatusBarTexture("Interface\\AddOns\\oUF_Skaarj\\Media\\texture")
  99.     statusBar:SetAlpha(.95)
  100.     statusBar.bd = framebd(statusBar, statusBar)
  101.     statusBar:SetAllPoints(frame)
  102.     frame.statusBar = statusBar
  103.    
  104.     if auraBarParent.down == true then
  105.    
  106.         if auraBarParent == anchor then
  107.             frame:SetPoint('TOPLEFT', anchor, 'BOTTOMLEFT', 0, 0)
  108.         else
  109.             frame:SetPoint('TOPLEFT', anchor, 'BOTTOMLEFT', 0, 0)
  110.         end
  111.        
  112.     else
  113.    
  114.         if auraBarParent == anchor then
  115.             frame:SetPoint('BOTTOMLEFT', anchor, 'TOPLEFT', 0, 0)
  116.         else
  117.             frame:SetPoint('BOTTOMLEFT', anchor, 'TOPLEFT', 0, 0)
  118.         end
  119.        
  120.     end
  121.    
  122.     statusBar.iconHolder = CreateFrame('Button', nil, statusBar)
  123.     statusBar.iconHolder:SetHeight(frame:GetHeight())
  124.     statusBar.iconHolder:SetWidth(frame:GetHeight())
  125.     statusBar.iconHolder:SetPoint('BOTTOMRIGHT', frame, 'BOTTOMLEFT', 0, 0)
  126.     statusBar.iconHolder.bd = framebd(statusBar.iconHolder, statusBar.iconHolder)
  127.     statusBar.iconHolder.__unit = oUF.unit
  128.     statusBar.iconHolder:SetScript('OnEnter', OnEnter)
  129.     statusBar.iconHolder:SetScript('OnLeave', OnLeave)
  130.     statusBar.iconHolder.UpdateTooltip = UpdateTooltip
  131.     statusBar.icon = statusBar.iconHolder:CreateTexture(nil, 'BACKGROUND')
  132.     statusBar.icon:SetTexCoord(.07, .93, .07, .93)
  133.     statusBar.icon:SetAllPoints()
  134.     statusBar.spelltime = statusBar:CreateFontString(nil, 'ARTWORK')
  135.     statusBar.spelltime:SetFont("Interface\\AddOns\\oUF_Skaarj\\Media\\pixel.ttf", 9, "Outlinemonochrome")
  136.     statusBar.spelltime:SetTextColor(1 ,1, 1)
  137.     statusBar.spelltime:SetJustifyH'RIGHT'
  138.     statusBar.spelltime:SetJustifyV'CENTER'
  139.     statusBar.spelltime:SetPoint'RIGHT'
  140.     statusBar.spellname = statusBar:CreateFontString(nil, 'ARTWORK')
  141.     statusBar.spellname:SetFont("Interface\\AddOns\\oUF_Skaarj\\Media\\pixel.ttf", 8, "Outlinemonochrome")
  142.     statusBar.spellname:SetTextColor(1, 1, 1)
  143.     statusBar.spellname:SetJustifyH'LEFT'
  144.     statusBar.spellname:SetJustifyV'CENTER'
  145.     statusBar.spellname:SetPoint('LEFT', 2, 0)
  146.     statusBar.spellname:SetPoint('RIGHT', statusBar.spelltime, 'LEFT')
  147.     return frame
  148. end
  149.  
  150. local function UpdateBars(auraBars)
  151.     local bars = auraBars.bars
  152.     local timenow = GetTime()
  153.    
  154.     for index = 1, #bars do
  155.    
  156.         local frame = bars[index]
  157.         local bar = frame.statusBar
  158.        
  159.         if not frame:IsVisible() then
  160.             break
  161.         end
  162.        
  163.         if bar.aura.noTime then --something weird here
  164.             bar.spelltime:SetText()
  165.         else
  166.             local timeleft = bar.aura.expirationTime - timenow
  167.             bar:SetValue(timeleft)
  168.             bar.spelltime:SetText(FormatTime(timeleft))
  169.         end
  170.        
  171.     end
  172.    
  173. end
  174.  
  175. local sort = function(a, b)
  176.     local compa, compb = a.noTime and math.huge or a.expirationTime, b.noTime and math.huge or b.expirationTime
  177.     return compa > compb
  178. end
  179.  
  180. local buffs = {}
  181. local function UpdateBuff(self, event, unit)
  182.    
  183.     if self.unit ~= unit then return end
  184.    
  185.     local BuffBars = self.BuffBars
  186.     local numBuffs = 0
  187.    
  188.     for i = 1, 40 do
  189.         local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellID = UnitBuff(unit, i)
  190.        
  191.         if not name then break end
  192.        
  193.         if BuffBars.filter(self, unit, name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellID) then
  194.             local t = buffs[i] or {}
  195.             t.name = name
  196.             t.rank = rank
  197.             t.icon = icon
  198.             t.count = count
  199.             t.debuffType = debuffType
  200.             t.duration = duration
  201.             t.expirationTime = expirationTime
  202.             t.unitCaster = unitCaster
  203.             t.isStealable = isStealable
  204.             t.noTime = (duration == 0 and expirationTime == 0)
  205.             t.filter = BuffBars.friendlyAuraType
  206.             t.shouldConsolidate = shouldConsolidate
  207.             t.spellID = spellID
  208.             buffs[i] = t
  209.             numBuffs = i
  210.         end
  211.        
  212.     end
  213.    
  214.     for i = numBuffs + 1, #buffs do
  215.         buffs[i] = nil
  216.     end
  217.    
  218.     if BuffBars.sort then
  219.         table.sort(buffs, type(BuffBars.sort) == 'function' and BuffBars.sort or nil) -- no need to pass the global "sort"
  220.     end
  221.    
  222.     local bars = BuffBars.bars
  223.    
  224.     for i = 1, #buffs do
  225.    
  226.         if BuffBars:GetWidth() == 0 then
  227.             break
  228.         end
  229.        
  230.         local frame = bars[i]
  231.        
  232.         if not frame then
  233.             frame = CreateAuraBar(self, i == 1 and BuffBars or bars[i - 1])
  234.             bars[i] = frame
  235.         end
  236.        
  237.         local bar = frame.statusBar
  238.         frame.i = i
  239.         local aura = buffs[i]
  240.         bar.aura = aura
  241.        
  242.         if aura.noTime then
  243.             bar:SetMinMaxValues(0, 1)
  244.             bar:SetValue(1)
  245.         else
  246.             if BuffBars.scaleTime then
  247.                 local maxvalue = math.min(BuffBars.scaleTime, bar.aura.duration)
  248.                 bar:SetMinMaxValues(0, maxvalue)
  249.                 bar:SetWidth((maxvalue/BuffBars.scaleTime)*((BuffBars.auraBarWidth or BuffBars:GetWidth())-(bar:GetHeight()+(BuffBars.gap or 0))))-- icon size + gap
  250.             else
  251.                 bar:SetMinMaxValues(0, aura.duration)
  252.             end
  253.             bar:SetValue(aura.expirationTime - GetTime())
  254.         end
  255.        
  256.         bar.icon:SetTexture(aura.icon)
  257.         bar.spellname:SetText(aura.count > 1 and string.format("%s [%d]", bar.aura.name, aura.count) or aura.name)
  258.         bar.spelltime:SetText(not bar.noTime and FormatTime(aura.expirationTime-GetTime()))
  259.         bar:SetStatusBarColor(.05, .05, .05)
  260.         frame:Show()
  261.     end
  262.    
  263.     for i = #buffs + 1, #bars do
  264.         bars[i]:Hide()
  265.     end
  266.    
  267. end
  268.  
  269. local debuffs = {}
  270. local function UpdateDebuff(self, event, unit)
  271.  
  272.     if self.unit ~= unit then return end
  273.    
  274.     local DebuffBars = self.DebuffBars
  275.     local numDebuffs = 0
  276.    
  277.     for i = 1, 40 do
  278.         local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellID = UnitDebuff(unit, i)
  279.        
  280.         if not name then break end
  281.        
  282.         if DebuffBars.filter(self, unit, name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellID) then
  283.             local t = debuffs[i] or {}
  284.             t.name = name
  285.             t.rank = rank
  286.             t.icon = icon
  287.             t.count = count
  288.             t.debuffType = debuffType
  289.             t.duration = duration
  290.             t.expirationTime = expirationTime
  291.             t.unitCaster = unitCaster
  292.             t.isStealable = isStealable
  293.             t.noTime = (duration == 0 and expirationTime == 0)
  294.             t.filter = DebuffBars.enemyAuraType
  295.             t.shouldConsolidate = shouldConsolidate
  296.             t.spellID = spellID
  297.             debuffs[i] = t
  298.             numDebuffs = i
  299.         end
  300.        
  301.     end
  302.    
  303.     for i = numDebuffs + 1, #debuffs do
  304.         debuffs[i] = nil
  305.     end
  306.    
  307.     if DebuffBars.sort then
  308.         table.sort(debuffs, type(DebuffBars.sort) == 'function' and DebuffBars.sort or nil)
  309.     end
  310.    
  311.     local bars = DebuffBars.bars
  312.    
  313.     for i = 1, #debuffs do
  314.    
  315.         if DebuffBars:GetWidth() == 0 then
  316.             break
  317.         end
  318.        
  319.         local frame = bars[i]
  320.        
  321.         if not frame then
  322.             frame = CreateAuraBar(self, i == 1 and DebuffBars or bars[i - 1])
  323.             bars[i] = frame
  324.         end
  325.        
  326.         local bar = frame.statusBar
  327.         frame.i = i
  328.         local aura = debuffs[i]
  329.         bar.aura = aura
  330.        
  331.         if aura.noTime then -- something weird here
  332.             bar:SetMinMaxValues(0, 1)
  333.             bar:SetValue(1)
  334.         else
  335.             if DebuffBars.scaleTime then
  336.                 local maxvalue = math.min(DebuffBars.scaleTime, bar.aura.duration)
  337.                 bar:SetMinMaxValues(0, maxvalue)
  338.                 bar:SetWidth((maxvalue/DebuffBars.scaleTime)*((DebuffBars.auraBarWidth or DebuffBars:GetWidth())-(bar:GetHeight()+(DebuffBars.gap or 0))))-- icon size + gap
  339.             else
  340.                 bar:SetMinMaxValues(0, aura.duration)
  341.             end
  342.             bar:SetValue(aura.expirationTime - GetTime())
  343.         end
  344.        
  345.         bar.icon:SetTexture(aura.icon)
  346.         bar.spellname:SetText(aura.count > 1 and string.format("%s [%d]", bar.aura.name, aura.count) or aura.name)
  347.         bar.spelltime:SetText(not bar.noTime and FormatTime(aura.expirationTime-GetTime()))
  348.         local debuffType = bar.aura.debuffType and bar.aura.debuffType or 'none'
  349.             r, g, b = DebuffTypeColor[debuffType].r, DebuffTypeColor[debuffType].g, DebuffTypeColor[debuffType].b
  350.            
  351.             if DebuffBars.debuffColor then
  352.                 r, g, b = unpack(DebuffBars.debuffColor)
  353.             end
  354.            
  355.         bar:SetStatusBarColor(r, g, b)
  356.         frame:Show()
  357.     end
  358.    
  359.     for i = #debuffs + 1, #bars do
  360.         bars[i]:Hide()
  361.     end
  362.    
  363. end
  364.  
  365. local function EnableBuff(self)
  366.  
  367.     if self.BuffBars then
  368.         self:RegisterEvent('UNIT_AURA', UpdateBuff)
  369.         self.BuffBars:SetHeight(15)
  370.         self.BuffBars.bars = self.BuffBars.bars or {}
  371.         self.BuffBars.SetAnchors = SetAnchors
  372.         self.BuffBars:SetScript('OnUpdate', UpdateBars)
  373.         return true
  374.     end
  375.    
  376. end
  377.  
  378. local function EnableDebuff(self)
  379.  
  380.     if self.DebuffBars then
  381.         self:RegisterEvent('UNIT_AURA', UpdateDebuff)
  382.         self.DebuffBars:SetHeight(15)
  383.         self.DebuffBars.bars = self.DebuffBars.bars or {}
  384.         self.DebuffBars.SetAnchors = SetAnchors
  385.         self.DebuffBars:SetScript('OnUpdate', UpdateBars)
  386.         return true
  387.     end
  388.    
  389. end
  390.  
  391. local function DisableBuff(self)
  392.     local auraFrame = self.BuffBars
  393.    
  394.     if auraFrame then
  395.         self:UnregisterEvent('UNIT_AURA', UpdateBuff)
  396.         auraFrame:SetScript('OnUpdate', nil)
  397.     end
  398.    
  399. end
  400.  
  401. local function DisableDebuff(self)
  402.     local auraFrame = self.DebuffBars
  403.    
  404.     if auraFrame then
  405.         self:UnregisterEvent('UNIT_AURA', UpdateDebuff)
  406.         auraFrame:SetScript('OnUpdate', nil)
  407.     end
  408.    
  409. end
  410.  
  411. oUF:AddElement('BuffBars', UpdateBuff, EnableBuff, DisableBuff)
  412. oUF:AddElement('DebuffBars', UpdateDebuff, EnableDebuff, DisableDebuff)
RAW Paste Data