Advertisement
Guest User

Untitled

a guest
Feb 8th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.62 KB | None | 0 0
  1. local TEXTURE = 'Interface\\AddOns\\tCastBars\\Texture'
  2. local FONT = 'Interface\\AddOns\\tCastBars\\Font.ttf'
  3. local BACKDROP = {
  4.     bgFile = TEXTURE,
  5.     insets = {top = -0.8, bottom = -0.8, left = -0.8, right = -0.8}
  6. }
  7.  
  8. local castbars = {
  9.     player = true,
  10.     target = true,
  11.     focus = true,
  12.     pet = true,
  13. }
  14.  
  15. local CastingBarFinishSpell = function(self) --this is called at the end of casts or channels
  16.     self.barTexture:SetColorTexture(0.1, 0.9, 0.1) --set it green to show completion (to be honest you don't see this color change)
  17.     self.fadeOut = 1 --we want to fade the frame out now
  18.     self.casting = nil --we're no longer casting
  19.     self.channeling = nil --we're no longer channeling
  20. end
  21.  
  22. local function unitSpellcastStart(frame, event, unit) --this function gets called whenever a UNIT_SPELLCAST_START event fires for player, focus, pet, or target
  23.     local name, text, texture, startTime, endTime, _, _, notInterruptible, spellID = UnitCastingInfo(unit)
  24.     if not name then
  25.         frame:Hide()
  26.         return
  27.     end
  28.  
  29.     if frame.lag then
  30.         frame.lag:Show()
  31.     end
  32.  
  33.     frame.value = GetTime() - (startTime / 1000)
  34.     frame.maxValue = (endTime - startTime) / 1000
  35.     frame.bar:SetMinMaxValues(0, frame.maxValue)
  36.     frame.bar:SetValue(frame.value)
  37.     frame.bar.text:SetText(text)
  38.     frame.icon:SetTexture(texture)
  39.     frame:SetAlpha(1)
  40.     frame.holdTime = 0 --this is like a time to fade after a cast finishes that is checked in our OnUpdate
  41.     frame.casting = 1
  42.     frame.spellID = spellID
  43.     frame.channeling = nil --we're not channeling, we're casting
  44.     frame.fadeOut = nil
  45.     if notInterruptible then --red if protected, green if interruptible
  46.         frame.barTexture:SetColorTexture(0.7, 0.1, 0.1)
  47.     else
  48.         frame.barTexture:SetColorTexture(0.1, 0.9, 0.1)
  49.     end
  50.  
  51.     if frame.showCastbar then
  52.         frame:Show()
  53.     end
  54. end
  55.  
  56. local function unitSpellcastStop(frame, event, unit, ...) --this function gets called whenever a UNIT_SPELLCAST_STOP or UNIT_SPELLCAST_CHANNELING_STOP fires for player, focus, pet, or target
  57.     local _, _, spellID = ...
  58.     if ((frame.casting and event == 'UNIT_SPELLCAST_STOP' and spellID == frame.spellID) or (frame.channeling and event == 'UNIT_SPELLCAST_CHANNEL_STOP')) then
  59.         frame.bar:SetValue(frame.maxValue)
  60.         if event == 'UNIT_SPELLCAST_STOP' then
  61.             frame.casting = nil
  62.             frame.barTexture:SetColorTexture(0.8, 0.8, 0) --yellow for a cast that was stopped by movement or escaping (NOT INTERRUPTED)
  63.         else
  64.             frame.channeling = nil
  65.         end
  66.  
  67.         frame.fadeOut = 1
  68.         frame.holdTime = 0
  69.     end
  70. end
  71.  
  72. local function unitSpellcastFailedInterrupted(frame, event, unit, ...) --this function gets called whenever a UNIT_SPELLCAST_FAILED or UNIT_SPELLCAST_INTERRUPTED fires for player, focus, pet or target
  73.     local _, _, spellID = ...
  74.     if (frame:IsShown() and frame.casting and spellID == frame.spellID and not frame.fadeOut) then
  75.         frame.bar:SetValue(frame.maxValue)
  76.         frame.barTexture:SetColorTexture(1, 0.4, 0) --orange for interrupted or failed
  77.         if event == 'UNIT_SPELLCAST_FAILED' then
  78.             frame.bar.text:SetText('FAILED')
  79.         else
  80.             frame.bar.text:SetText('INTERRUPTED')
  81.         end
  82.  
  83.         if frame.lag then
  84.             frame.lag:Hide()
  85.         end
  86.  
  87.         frame.casting = nil
  88.         frame.channeling = nil
  89.         frame.fadeOut = 1
  90.         frame.holdTime = GetTime()
  91.     end
  92. end
  93.  
  94. local function unitSpellcastDelayed(frame, event, unit) --this function gets called whenever a UNIT_SPELLCAST_DELAYED occurs for player, pet, target, or focus
  95.     if frame:IsShown() then
  96.         local name, _, _, startTime, endTime = UnitCastingInfo(unit)
  97.         if not name then
  98.             frame:Hide()
  99.             return
  100.         end
  101.  
  102.         frame.value = GetTime() - (startTime / 1000)
  103.         frame.maxValue = (endTime - startTime) / 1000
  104.         frame.bar:SetMinMaxValues(0, frame.maxValue)
  105.         if not frame.casting then
  106.             frame.casting = 1
  107.             frame.channeling = nil
  108.             frame.fadeOut = 0
  109.         end
  110.     end
  111. end
  112.  
  113. local function unitSpellcastChannelStart(frame, event, unit) --this function gets called whenever a UNIT_SPELLCAST_CHANNEL_START fires for player, target, focus, or pet
  114.     local name, text, texture, startTime, endTime, _, notInterruptible = UnitChannelInfo(unit)
  115.     if not name then
  116.         frame:Hide()
  117.         return
  118.     end
  119.  
  120.     if frame.lag then
  121.         frame.lag:Hide()
  122.     end
  123.  
  124.     frame.value = (endTime / 1000) - GetTime()
  125.     frame.maxValue = (endTime - startTime) / 1000
  126.     frame.bar:SetMinMaxValues(0, frame.maxValue)
  127.     if notInterruptible then --red  if protected, green for interruptible
  128.         frame.barTexture:SetColorTexture(0.7, 0.1, 0.1)
  129.     else
  130.         frame.barTexture:SetColorTexture(0.1, 0.9, 0.1)
  131.     end
  132.  
  133.     frame.bar:SetValue(frame.value)
  134.     frame.bar.text:SetText(text)
  135.     frame.icon:SetTexture(texture)
  136.     frame:SetAlpha(1)
  137.     frame.holdTime = 0
  138.     frame.casting = nil
  139.     frame.channeling = 1
  140.     frame.fadeOut = nil
  141.     if frame.showCastbar then
  142.         frame:Show()
  143.     end
  144. end
  145.  
  146. local function unitSpellcastChannelUpdate(frame, event, unit) --this function gets called whenever a UNIT_SPELLCAST_CHANNEL_UPDATE fires for player, target, focus, or pet
  147.     if frame:IsShown() then
  148.         local name, text, texture, startTime, endTime, _, notInterruptible = UnitChannelInfo(unit)
  149.         if not name then
  150.             frame:Hide()
  151.             return
  152.         end
  153.  
  154.         frame.value = (endTime / 1000) - GetTime()
  155.         frame.maxValue = (endTime - startTime) / 1000
  156.         frame.bar:SetMinMaxValues(0, frame.maxValue)
  157.         frame.bar:SetValue(frame.value)
  158.     end
  159. end
  160.  
  161. local MakeCastBarFunctions = function(frame, event) --this function gets called during our MakeCastBars function, during which, it will create functions for the individual units (player, target, focus, pet)
  162.     function frame:UNIT_SPELLCAST_START(event, unit)
  163.         unitSpellcastStart(frame, event, unit)
  164.     end
  165.  
  166.     function frame:UNIT_SPELLCAST_STOP(event, unit)
  167.         unitSpellcastStop(frame, event, unit)
  168.     end
  169.  
  170.     function frame:UNIT_SPELLCAST_CHANNEL_STOP(event, unit, ...)
  171.         unitSpellcastStop(frame, event, unit, ...)
  172.     end
  173.  
  174.     function frame:UNIT_SPELLCAST_FAILED(event, unit, ...)
  175.         unitSpellcastFailedInterrupted(frame, event, unit, ...)
  176.     end
  177.  
  178.     function frame:UNIT_SPELLCAST_INTERRUPTED(event, unit, ...)
  179.         unitSpellcastFailedInterrupted(frame, event, unit, ...)
  180.     end
  181.  
  182.     function frame:UNIT_SPELLCAST_DELAYED(event, unit)
  183.         unitSpellcastDelayed(frame, event, unit)
  184.     end
  185.  
  186.     function frame:UNIT_SPELLCAST_CHANNEL_START(event, unit)
  187.         unitSpellcastChannelStart(frame, event, unit)
  188.     end
  189.  
  190.     function frame:UNIT_SPELLCAST_CHANNEL_UPDATE(event, unit)
  191.         unitSpellcastChannelUpdate(frame, event, unit)
  192.     end
  193.  
  194.     function frame:UNIT_SPELLCAST_INTERRUPTIBLE()
  195.         frame.barTexture:SetColorTexture(0.1, 0.9, 0.1)
  196.     end
  197.  
  198.     function frame:UNIT_SPELLCAST_NOT_INTERRUPTIBLE()
  199.         frame.barTexture:SetColorTexture(0.7, 0.1, 0.1)
  200.     end
  201. end
  202.  
  203. local MakeCastBar = function(unit, enable) --this function will make castbars, place them, register their events, and make their functions for player, target, focus, and pet units
  204.     local frame = _G[unit..'tCastBar'] or CreateFrame('frame', unit..'tCastBar', UIParent)
  205.     frame:SetSize(228, 22)
  206.     frame:SetBackdrop(BACKDROP)
  207.     frame:SetBackdropColor(0, 0, 0)
  208.     frame:SetPoint('CENTER', UIParent, 'CENTER')
  209.  
  210.     if not frame.bar then
  211.         frame.bar = CreateFrame('StatusBar', nil, frame)
  212.         frame.bar:SetAllPoints()
  213.         frame.bar:SetMinMaxValues(0, 1)
  214.         frame.bar:SetValue(0)
  215.  
  216.         frame.barTexture = frame:CreateTexture(nil, 'BORDER')
  217.         frame.barTexture:SetTexture(TEXTURE)
  218.  
  219.         frame.bar:SetStatusBarTexture(frame.barTexture)
  220.  
  221.         frame.bar.bg = frame.bar:CreateTexture(nil, 'BACKGROUND')
  222.         frame.bar.bg:SetTexture(TEXTURE)
  223.         frame.bar.bg:SetColorTexture(0.2, 0.2, 0.2)
  224.         frame.bar.bg:SetAllPoints()
  225.  
  226.         frame.bar.text = frame.bar:CreateFontString(nil, 'OVERLAY')
  227.         frame.bar.text:SetFont(FONT, 12, 'OUTLINE')
  228.         frame.bar.text:SetJustifyH('CENTER')
  229.         frame.bar.text:SetJustifyV('MIDDLE')
  230.         frame.bar.text:SetPoint('CENTER')
  231.  
  232.         frame.bar.timer = frame.bar:CreateFontString(nil, 'OVERLAY')
  233.         frame.bar.timer:SetFont(FONT, 12, 'OUTLINE')
  234.         frame.bar.timer:SetJustifyH('RIGHT')
  235.         frame.bar.timer:SetJustifyV('MIDDLE')
  236.         frame.bar.timer:SetPoint('RIGHT', frame, 'RIGHT', -3, 0)
  237.  
  238.         frame.icon = frame:CreateTexture(nil, 'ARTWORK')
  239.         frame.icon:SetSize(22, 22)
  240.         frame.icon:SetPoint('TOPRIGHT', frame, 'TOPLEFT', -1, 1)
  241.         frame.icon:SetPoint('BOTTOMRIGHT', frame, 'BOTTOMLEFT', -1, -1)
  242.         frame.icon:SetTexCoord(.06, .94, .06, .94)
  243.     end
  244.  
  245.     if unit == 'player' then
  246.         frame.lag = _G[frame:GetName()..'Lag'] or frame.bar:CreateTexture(frame:GetName()..'Lag', 'BORDER')
  247.         frame.lag:SetPoint('TOPRIGHT') --this anchors the lag frame to the right of the cast bar (since we cast from left to right)
  248.         frame.lag:SetPoint('BOTTOMRIGHT')
  249.         frame.lag:SetTexture(TEXTURE)
  250.         frame.lag:SetColorTexture(1, 0.4, 0)
  251.         frame:SetPoint('CENTER', UIParent, 'CENTER', 11, -238)
  252.     elseif unit == 'pet' then
  253.         frame:SetPoint('CENTER', UIParent, 'CENTER', 11, -375)
  254.     elseif unit == 'target' then
  255.         frame:SetPoint('CENTER', UIParent, 'CENTER', 11, 100)
  256.     elseif unit == 'focus' then
  257.         frame:SetPoint('CENTER', UIParent, 'CENTER', 11, -150)
  258.     end
  259.  
  260.     frame.timerUpdate = 0.1 --this controls how many times per second our OnUpdate fires (1/frame.timerUpdate) = # of times updated in a second, lower = more = smoother appearance = more CPU usage
  261.  
  262.     frame:RegisterEvent('PLAYER_TARGET_CHANGED')
  263.     frame:RegisterEvent('PLAYER_FOCUS_CHANGED')
  264.     frame:RegisterUnitEvent('UNIT_SPELLCAST_START', unit)
  265.     frame:RegisterUnitEvent('UNIT_SPELLCAST_STOP', unit)
  266.     frame:RegisterUnitEvent('UNIT_SPELLCAST_FAILED', unit)
  267.     frame:RegisterUnitEvent('UNIT_SPELLCAST_INTERRUPTED', unit)
  268.     frame:RegisterUnitEvent('UNIT_SPELLCAST_DELAYED', unit)
  269.     frame:RegisterUnitEvent('UNIT_SPELLCAST_CHANNEL_START', unit)
  270.     frame:RegisterUnitEvent('UNIT_SPELLCAST_CHANNEL_UPDATE', unit)
  271.     frame:RegisterUnitEvent('UNIT_SPELLCAST_CHANNEL_STOP', unit)
  272.     frame:RegisterUnitEvent('UNIT_SPELLCAST_INTERRUPTIBLE', unit)
  273.     frame:RegisterUnitEvent('UNIT_SPELLCAST_NOT_INTERRUPTIBLE', unit)
  274.     frame:RegisterEvent('PLAYER_ENTERING_WORLD')
  275.  
  276.     frame.unit = unit
  277.     frame.casting = nil
  278.     frame.channeling = nil
  279.     frame.holdTime = 0
  280.     frame.showCastbar = castbars[unit]
  281.     frame.locked = true
  282.     frame:Hide()
  283.  
  284.     frame:SetScript('OnShow', function(self)
  285.         if not self.locked then return end
  286.  
  287.         if self.casting then
  288.             local _, _, _, startTime = UnitCastingInfo(self.unit)
  289.             if startTime then
  290.                 self.value = GetTime() - (startTime / 1000)
  291.             end
  292.         else
  293.             local _, _, _, _, endTime = UnitChannelInfo(self.unit)
  294.             if endTime then
  295.                 self.value = (endTime / 1000) - GetTime()
  296.             end
  297.         end
  298.     end)
  299.  
  300.     MakeCastBarFunctions(frame, event) --make our functions for the frame
  301.  
  302.     frame:SetScript('OnEvent', function(self, event, ...)
  303.         if not self.locked then return end
  304.  
  305.         local arg1 = ...
  306.         local unit = self.unit
  307.         if (event == 'PLAYER_ENTERING_WORLD' or event == 'PLAYER_TARGET_CHANGED' or event == 'PLAYER_FOCUS_CHANGED') then
  308.             local spellChannel = UnitChannelInfo(unit)
  309.             local spellName = UnitCastingInfo(unit)
  310.             if spellChannel then
  311.                 event = 'UNIT_SPELLCAST_CHANNEL_START'
  312.                 arg1 = unit
  313.             elseif spellName then
  314.                 event = 'UNIT_SPELLCAST_START'
  315.                 arg1 = unit
  316.             else
  317.                 CastingBarFinishSpell(self)
  318.             end
  319.         end
  320.  
  321.         if arg1 ~= unit then return end
  322.         if self[event] then --handle the event for our frame
  323.             self[event](frame, event, unit, ...)
  324.         end
  325.     end)
  326.  
  327.     frame:SetScript('OnUpdate', function(self, elapsed)
  328.         if not self.bar.timer then return end
  329.  
  330.         if (self.timerUpdate and self.timerUpdate < elapsed) then
  331.             if self.casting then
  332.                 self.bar.timer:SetText(format('%.1f', max(self.maxValue - self.value, 0)))
  333.             elseif self.channeling then
  334.                 self.bar.timer:SetText(format('%.1f', max(self.value, 0)))
  335.             else
  336.                 self.bar.timer:SetText('')
  337.             end
  338.  
  339.             self.timerUpdate = 0.1
  340.         else
  341.             self.timerUpdate = self.timerUpdate - elapsed
  342.         end
  343.  
  344.         if self.casting then
  345.             self.value = self.value + elapsed
  346.             if self.value >= self.maxValue then
  347.                 self.bar:SetValue(self.maxValue)
  348.                 CastingBarFinishSpell(self)
  349.                 return
  350.             end
  351.  
  352.             self.bar:SetValue(self.value)
  353.             if self.unit == 'player' then
  354.                 local down, up, lag = GetNetStats()
  355.                 local castingmin, castingmax = self.bar:GetMinMaxValues()
  356.                 local lagvalue = (lag / 1000) / (castingmax - castingmin)
  357.                 if lagvalue < 0 then
  358.                     lagvalue = 0
  359.                 elseif lagvalue > 1 then
  360.                     lagvalue = 1
  361.                 end
  362.  
  363.                 self.lag:ClearAllPoints()
  364.                 self.lag:SetPoint('RIGHT')
  365.                 self.lag:SetHeight(self.bar:GetHeight())
  366.                 self.lag:SetWidth(self.bar:GetWidth() * lagvalue)
  367.             end
  368.         elseif self.channeling then
  369.             self.value = self.value - elapsed
  370.             if self.value <= 0 then
  371.                 CastingBarFinishSpell(self)
  372.                 return
  373.             end
  374.  
  375.             self.bar:SetValue(self.value)
  376.         elseif GetTime() < self.holdTime then
  377.             return
  378.         elseif self.fadeOut then
  379.             self.fadeOut = nil
  380.             self:Hide()
  381.         end
  382.     end)
  383. end
  384.  
  385. local tCastBars = _G['tCastBars'] or CreateFrame('frame', 'tCastBars', UIParent)
  386. tCastBars:RegisterEvent('PLAYER_TALENT_UPDATE')
  387. tCastBars:RegisterEvent('PLAYER_ENTERING_WORLD')
  388. tCastBars:RegisterEvent('ADDON_LOADED')
  389. tCastBars:SetScript('OnEvent', function(self, event, arg1, ...)
  390.     if (event == 'PLAYER_ENTERING_WORLD' or event == 'PLAYER_TALENT_UPDATE') then
  391.         CastingBarFrame.showCastbar = false
  392.         CastingBarFrame:UnregisterAllEvents()
  393.         CastingBarFrame:SetScript('OnUpdate', function() end)
  394.  
  395.         TargetFrameSpellBar.showCastbar = false
  396.         TargetFrameSpellBar:UnregisterAllEvents()
  397.         TargetFrameSpellBar:SetScript('OnUpdate', function() end)
  398.  
  399.         FocusFrameSpellBar.showCastbar = false
  400.         FocusFrameSpellBar:UnregisterAllEvents()
  401.         FocusFrameSpellBar:SetScript('OnUpdate', function() end)
  402.  
  403.         PetCastingBarFrame.showCastbar = false
  404.         PetCastingBarFrame:UnregisterAllEvents()
  405.         PetCastingBarFrame:SetScript('OnUpdate', function() end)
  406.  
  407.         for unit, enable in pairs(castbars) do
  408.             if enable then
  409.                 MakeCastBar(unit, enable)
  410.             end
  411.         end
  412.     end
  413. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement