Advertisement
Guest User

Untitled

a guest
Sep 14th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.21 KB | None | 0 0
  1. local bCastBars = _G['bCastBars'] or CreateFrame('frame', 'bCastBars', UIParent)
  2. local addonName, ns = ...
  3.  
  4. local BACKDROP = {
  5.     bgFile = [=[Interface\ChatFrame\ChatFrameBackground]=], insets = {top = -1, bottom = -1, left = -1, right = -1}
  6. }
  7.  
  8. local castbars = {
  9.     player = true,
  10.     target = true,
  11.     focus = true,
  12.     pet = true,
  13. }
  14.  
  15. local CastingBarFinishSpell = function(self)
  16.     self.bar:SetStatusBarColor(1,1,1)
  17.     self.fadeOut = 1
  18.     self.casting = nil
  19.     self.channeling = nil
  20. end
  21.  
  22. local MakeCastBar = function(unit, enable)
  23.     local frame = _G[unit.."bCastBar"] or CreateFrame("frame", unit.."bCastBar", UIParent)
  24.     frame:SetSize(250,20)
  25.     frame:SetBackdrop(BACKDROP)
  26.     frame:SetBackdropColor(0,0,0)
  27.     frame:SetPoint('CENTER', UIParent, 'CENTER')
  28.    
  29.     local bar = CreateFrame('StatusBar', nil, frame)
  30.     bar:SetStatusBarTexture([=[Interface\ChatFrame\ChatFrameBackground]=])
  31.     bar:SetSize(250,20)
  32.     bar:SetStatusBarColor(.2, .2, .2)
  33.     bar:SetAllPoints()
  34.    
  35.     local bg = bar:CreateTexture('castbarbgframe', 'BACKGROUND')
  36.     bg:SetTexture([=[Interface\ChatFrame\ChatFrameBackground]=])
  37.     bg:SetVertexColor(.2, .2, .2)
  38.     bg:SetAllPoints()
  39.    
  40.     local text = bar:CreateFontString(nil, 'OVERLAY')
  41.     text:SetFont([=[Interface\AddOns\oUF_P3lim\pixel.ttf]=], 11, 'OUTLINE')
  42.     text:SetPoint('CENTER', bar, 'CENTER')
  43.    
  44.     local timer = bar:CreateFontString(nil, 'OVERLAY')
  45.     timer:SetFont([=[Interface\AddOns\oUF_P3lim\pixel.ttf]=], 11, 'OUTLINE')
  46.     timer:SetJustifyH('RIGHT')
  47.     timer:SetPoint('RIGHT', bar, 'RIGHT', -3, 0)
  48.    
  49.     local icon = frame:CreateTexture(nil, 'ARTWORK')
  50.     icon:SetSize(22,22)
  51.     icon:SetPoint('TOPRIGHT', frame, 'TOPLEFT', -1, 1)
  52.     icon:SetPoint('BOTTOMRIGHT', frame, 'BOTTOMLEFT', -1, -1)
  53.     icon:SetTexCoord(.06, .94, .06, .94)
  54.    
  55.     frame.bar = bar
  56.     bar.timer = timer
  57.     bar.text = text
  58.     frame.icon = icon
  59.    
  60.     if unit == "player" then
  61.         frame.lag = _G[frame:GetName().."Lag"] or frame.bar:CreateTexture(frame:GetName().."Lag", "BORDER")
  62.         frame.lag:SetPoint("TOPRIGHT")
  63.         frame.lag:SetPoint("BOTTOMRIGHT")
  64.         frame.lag:SetTexture("Interface\\RAIDFRAME\\Raid-Bar-Hp-Fill", "BACKGROUND")
  65.         frame.lag:SetVertexColor(1, 0, 0)
  66.         frame.lag:SetBlendMode("ADD")
  67.     end
  68.    
  69.     frame.timerUpdate = .1
  70.    
  71.     frame:RegisterEvent("PLAYER_TARGET_CHANGED")
  72.     frame:RegisterEvent("UNIT_SPELLCAST_START")
  73.     frame:RegisterEvent("UNIT_SPELLCAST_STOP")
  74.     frame:RegisterEvent("UNIT_SPELLCAST_FAILED")
  75.     frame:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
  76.     frame:RegisterEvent("UNIT_SPELLCAST_DELAYED")
  77.     frame:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START")
  78.     frame:RegisterEvent("UNIT_SPELLCAST_CHANNEL_UPDATE")
  79.     frame:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP")
  80.     frame:RegisterEvent("UNIT_SPELLCAST_INTERRUPTIBLE")
  81.     frame:RegisterEvent("UNIT_SPELLCAST_NOT_INTERRUPTIBLE")
  82.     frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  83.    
  84.     frame.unit = unit
  85.     frame.casting = nil
  86.     frame.channeling = nil
  87.     frame.holdTime = 0
  88.     frame.showCastbar = castbars[unit]
  89.     frame.locked = true
  90.     frame:Hide()
  91.    
  92.     frame:SetScript("OnShow", function(self)
  93.         if not self.locked then return end
  94.         if self.casting then
  95.             local _, _, _, _, st = UnitCastingInfo(self.unit)
  96.             if st then
  97.                 self.value = (GetTime() - (st / 1000))
  98.             end
  99.         else
  100.             local _, _, _, _, _, et = UnitChannelInfo(self.unit)
  101.             if et then
  102.                 self.value = ((et / 1000) - GetTime())
  103.             end
  104.         end
  105.     end)
  106.    
  107.     frame:SetScript("OnEvent", function(self, event, ...)
  108.         local arg1 = ...
  109.         if not self.locked then return end
  110.         local unit = self.unit
  111.         if  event == "PLAYER_ENTERING_WORLD" or event == "PLAYER_TARGET_CHANGED" then
  112.             local spellChannel  = UnitChannelInfo(unit)
  113.             local spellName  = UnitCastingInfo(unit)
  114.             if  spellChannel then
  115.                 event = "UNIT_SPELLCAST_CHANNEL_START"
  116.                 arg1 = unit
  117.             elseif spellName then
  118.                 event = "UNIT_SPELLCAST_START"
  119.                 arg1 = unit
  120.             else
  121.                 CastingBarFinishSpell(self)
  122.             end
  123.         end
  124.  
  125.         if arg1 ~= unit then return end
  126.        
  127.         if event == "UNIT_SPELLCAST_START" then
  128.             local name, _, text, texture, startTime, endTime, _, castID, notInterruptible = UnitCastingInfo(unit)
  129.             if not name then
  130.                 self:Hide()
  131.                 return
  132.             end
  133.            
  134.             if self.lag then self.lag:Show() end
  135.             self.value = GetTime() - (startTime / 1000)
  136.             self.maxValue = (endTime - startTime) / 1000
  137.             self.bar:SetMinMaxValues(0, self.maxValue)
  138.             self.bar:SetValue(self.value)
  139.             self.bar.text:SetText(text)
  140.             self.icon:SetTexture(texture)
  141.             self:SetAlpha(1)
  142.             self.holdTime = 0
  143.             self.casting = 1
  144.             self.castID = castID
  145.             self.channeling = nil
  146.             self.fadeOut = nil
  147.             if notInterruptible then
  148.                 self.bar:SetStatusBarColor(.65, .1, .1)
  149.             else
  150.                 self.bar:SetStatusBarColor(0, .8, 0)
  151.             end
  152.             if self.showCastbar then self:Show() end
  153.         elseif event == "UNIT_SPELLCAST_STOP" or event == "UNIT_SPELLCAST_CHANNEL_STOP" then
  154.             if not self:IsVisible() then
  155.                 self:Hide()
  156.             end
  157.             if (self.casting and event == "UNIT_SPELLCAST_STOP" and select(4, ...) == self.castID) or (self.channeling and event == "UNIT_SPELLCAST_CHANNEL_STOP") then
  158.                 self.bar:SetValue(self.maxValue)
  159.                 if event == "UNIT_SPELLCAST_STOP" then
  160.                     self.casting = nil
  161.                     self.bar:SetStatusBarColor(.8, .8, 0)
  162.                 else
  163.                     self.channeling = nil
  164.                 end
  165.                 self.fadeOut = 1
  166.                 self.holdTime = 0
  167.             end
  168.         elseif event == "UNIT_SPELLCAST_FAILED" or event == "UNIT_SPELLCAST_INTERRUPTED" then
  169.             if self:IsShown() and (self.casting and select(4, ...) == self.castID) and not self.fadeOut then
  170.                 self.bar:SetValue(self.maxValue)
  171.                 self.bar:SetStatusBarColor(.8, .8, 0)
  172.                 if event == "UNIT_SPELLCAST_FAILED" then
  173.                     self.bar.text:SetText(FAILED)
  174.                 else
  175.                     self.bar.text:SetText(INTERRUPTED)
  176.                 end
  177.                 if self.lag then self.lag:Hide() end
  178.                 self.casting = nil
  179.                 self.channeling = nil
  180.                 self.fadeOut = 1
  181.                 self.holdTime = GetTime()
  182.             end
  183.         elseif event == "UNIT_SPELLCAST_DELAYED" then
  184.             if self:IsShown() then
  185.                 local name, _, text, texture, startTime, endTime = UnitCastingInfo(unit)
  186.                 if not name then
  187.                     self:Hide()
  188.                     return
  189.                 end
  190.                 self.value = (GetTime() - (startTime / 1000))
  191.                 self.maxValue = (endTime - startTime) / 1000
  192.                 self.bar:SetMinMaxValues(0, self.maxValue)
  193.                 if not self.casting then
  194.                     self.bar:SetStatusBarColor(0, .8, 0)
  195.                     self.casting = 1
  196.                     self.channeling = nil
  197.                     self.fadeOut = 0
  198.                 end
  199.             end
  200.         elseif event == "UNIT_SPELLCAST_CHANNEL_START" then
  201.             local name, _, text, texture, startTime, endTime, isTradeSkill, notInterruptible = UnitChannelInfo(unit)
  202.             if not name then
  203.                 self:Hide()
  204.                 return
  205.             end
  206.  
  207.             if self.lag then self.lag:Hide() end
  208.            
  209.             self.value = (endTime / 1000) - GetTime()
  210.             self.maxValue = (endTime - startTime) / 1000
  211.             self.bar:SetMinMaxValues(0, self.maxValue)
  212.             self.bar:SetValue(self.value)
  213.             self.bar.text:SetText(text)
  214.             self.icon:SetTexture(texture)
  215.             self:SetAlpha(1)
  216.             self.holdTime = 0
  217.             self.casting = nil
  218.             self.channeling = 1
  219.             self.fadeOut = nil
  220.            
  221.             if notInterruptible then
  222.                 self.bar:SetStatusBarColor(.65, .1, .1)
  223.             else
  224.                 self.bar:SetStatusBarColor(0, .8, 0)
  225.             end
  226.             if self.showCastbar then self:Show() end
  227.         elseif event == "UNIT_SPELLCAST_CHANNEL_UPDATE" then
  228.           if self:IsShown() then
  229.                 local name, _, text, texture, startTime, endTime, isTradeSkill, notInterruptible = UnitChannelInfo(unit)
  230.                 if not name then
  231.                     self:Hide()
  232.                     return
  233.                 end
  234.                 self.value = (endTime / 1000) - GetTime()
  235.                 self.maxValue = (endTime - startTime) / 1000
  236.                 self.bar:SetMinMaxValues(0, self.maxValue)
  237.                 self.bar:SetValue(self.value)
  238.             end
  239.         elseif event == "UNIT_SPELLCAST_INTERRUPTIBLE" then
  240.             self.bar:SetStatusBarColor(0, .8, 0)
  241.         elseif event == "UNIT_SPELLCAST_NOT_INTERRUPTIBLE" then
  242.             self.bar:SetStatusBarColor(.65, .1, .1)
  243.         end
  244.     end)
  245.    
  246.     frame:SetScript("OnUpdate", function(self, elapsed)
  247.    
  248.         if not self.bar.timer then return end
  249.         if self.timerUpdate and self.timerUpdate < elapsed then
  250.             if self.casting then
  251.                 self.bar.timer:SetText(format("%.1f", max(self.maxValue - self.value, 0)))
  252.             elseif self.channeling then
  253.                 self.bar.timer:SetText(format("%.1f", max(self.value, 0)))
  254.             else
  255.                 self.bar.timer:SetText("")
  256.             end
  257.             self.timerUpdate = .1
  258.         else
  259.             self.timerUpdate = self.timerUpdate - elapsed
  260.         end
  261.  
  262.         if self.casting then
  263.             self.value = self.value + elapsed
  264.             if self.value >= self.maxValue then
  265.                 self.bar:SetValue(self.maxValue)
  266.                 CastingBarFinishSpell(self)
  267.                 return
  268.             end
  269.             self.bar:SetValue(self.value)
  270.             if self.unit == "player" then
  271.                 local down, up, lag = GetNetStats()
  272.                 local castingmin, castingmax = self.bar:GetMinMaxValues()
  273.                 local lagvalue = ( lag / 1000 ) / ( castingmax - castingmin )
  274.                 if ( lagvalue < 0 ) then lagvalue = 0; elseif ( lagvalue > 1 ) then lagvalue = 1 end
  275.                 self.lag:ClearAllPoints()
  276.                 self.lag:SetPoint("RIGHT")
  277.                 self.lag:SetHeight(self.bar:GetHeight())
  278.                 self.lag:SetWidth(self.bar:GetWidth() * lagvalue)
  279.             end
  280.         elseif self.channeling then
  281.             self.value = self.value - elapsed
  282.             if self.value <= 0 then
  283.                 CastingBarFinishSpell(self)
  284.                 return
  285.             end
  286.             self.bar:SetValue(self.value)
  287.         elseif GetTime() < self.holdTime then
  288.             return
  289.         elseif self.fadeOut then
  290.             self.fadeOut = nil
  291.             self:Hide()
  292.         end
  293.     end)
  294. end
  295.  
  296. bCastBars:RegisterEvent('PLAYER_TALENT_UPDATE')
  297. bCastBars:RegisterEvent('PLAYER_ENTERING_WORLD')
  298. bCastBars:RegisterEvent('ADDON_LOADED')
  299. bCastBars:SetScript('OnEvent', function(self, event, arg1, ...)
  300.     if (event=='ADDON_LOADED' and arg1 == addonName) or event == 'PLAYER_ENTERING_WORLD' or event=='PLAYER_TALENT_UPDATE' then
  301.        
  302.         CastingBarFrame.showCastbar = false
  303.         CastingBarFrame:UnregisterAllEvents()
  304.         CastingBarFrame:SetScript("OnUpdate", function() end)
  305.  
  306.         TargetFrameSpellBar.showCastbar = false
  307.         TargetFrameSpellBar:UnregisterAllEvents()
  308.         TargetFrameSpellBar:SetScript("OnUpdate", function() end)
  309.  
  310.         FocusFrameSpellBar.showCastbar = false
  311.         FocusFrameSpellBar:UnregisterAllEvents()
  312.         FocusFrameSpellBar:SetScript("OnUpdate", function() end)
  313.  
  314.         PetCastingBarFrame.showCastbar = false
  315.         PetCastingBarFrame:UnregisterAllEvents()
  316.         PetCastingBarFrame:SetScript("OnUpdate", function() end)
  317.  
  318.         for unit, enable in pairs(castbars) do
  319.             if enable then
  320.                 MakeCastBar(unit, enable)
  321.             end
  322.         end
  323.     end
  324. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement