Guest User

core.lua

a guest
Jul 21st, 2016
109
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local Opts = nibRunes_Cfg
  2.  
  3. local nRD = CreateFrame("Frame")
  4. local EventsRegistered
  5.  
  6. -- Rune Data
  7. local RUNETYPE_BLOOD = 1
  8. local RUNETYPE_UNHOLY = 2
  9. local RUNETYPE_FROST = 3
  10. local RUNETYPE_DEATH = 4
  11.  
  12. local gcdNextDuration = 1
  13. local gcdEnd = 0
  14.  
  15. local HR, HS = false, false
  16.  
  17. -- Combat Fader
  18. local CombatFader = CreateFrame("Frame")
  19. CombatFader.Status = ""
  20.  
  21. local FadeTime = 0.25
  22.  
  23. local RuneFull = {
  24.     [1] = true,
  25.     [2] = true,
  26.     [3] = true,
  27.     [4] = true,
  28.     [5] = true,
  29.     [6] = true,
  30. }
  31.  
  32. local RunesAreReady = true
  33.  
  34. ---- COMBAT FADER
  35. -- Fade frame
  36. function CombatFader.FadeIt(Frame, NewOpacity)
  37.     local CurrentOpacity = Frame:GetAlpha();
  38.     if NewOpacity > CurrentOpacity then
  39.         UIFrameFadeIn(Frame, FadeTime, CurrentOpacity, NewOpacity);
  40.     elseif NewOpacity < CurrentOpacity then
  41.         UIFrameFadeOut(Frame, FadeTime, CurrentOpacity, NewOpacity);
  42.     end
  43. end
  44.  
  45. -- Determine new opacity values for frames
  46. function CombatFader.FadeFrames()
  47.     local NewOpacity
  48.  
  49.     -- Retrieve Element's opacity/visibility for current status
  50.     NewOpacity = 1
  51.     if CombatFader.Status == "INCOMBAT" then
  52.         NewOpacity = Opts.combatfader.opacity.incombat
  53.     elseif CombatFader.Status == "HARMTARGET" then
  54.         NewOpacity = Opts.combatfader.opacity.harmtarget
  55.     elseif CombatFader.Status == "HURT" then
  56.         NewOpacity = Opts.combatfader.opacity.hurt
  57.     elseif CombatFader.Status == "OUTOFCOMBAT" then
  58.         NewOpacity = Opts.combatfader.opacity.outofcombat
  59.     end
  60.     CombatFader.FadeIt(nRD.Frames.Parent, NewOpacity)
  61. end
  62.  
  63. -- Update current status
  64. function CombatFader.UpdateStatus()
  65.     local OldStatus = CombatFader.Status
  66.     if UnitAffectingCombat("player") then
  67.         CombatFader.Status = "INCOMBAT";                -- InCombat - Priority 1
  68.     elseif UnitExists("target") and UnitCanAttack("player", "target") then
  69.         CombatFader.Status = "HARMTARGET";          -- HarmTarget - Priority 2
  70.     elseif not RunesAreReady then
  71.         CombatFader.Status = "HURT";                    -- Not Full - Priority 4
  72.     else
  73.         CombatFader.Status = "OUTOFCOMBAT";         -- OutOfCombat - Priority 5
  74.     end
  75.     if CombatFader.Status ~= OldStatus then CombatFader.FadeFrames() end       
  76. end
  77.  
  78. function CombatFader.PLAYER_ENTERING_WORLD()
  79.     CombatFader.Status = nil
  80.     CombatFader.UpdateStatus()
  81.     CombatFader.FadeFrames()
  82. end
  83.  
  84. function CombatFader.UpdateRuneStatus()
  85.     if Opts.combatfader.enabled then
  86.         if ( RuneFull[1] and RuneFull[2] and RuneFull[3] and RuneFull[4] and RuneFull[5] and RuneFull[6] ) then
  87.             RunesAreReady = true
  88.         else
  89.             RunesAreReady = false
  90.         end
  91.         CombatFader.UpdateStatus()
  92.         CombatFader.FadeFrames()
  93.     end
  94. end
  95.  
  96. function CombatFader.OptionsRefresh()
  97.     CombatFader.Status = nil
  98.     CombatFader.UpdateStatus()
  99. end
  100.  
  101. function CombatFader.UpdateEnabled()
  102.     if Opts.combatfader.enabled then
  103.         CombatFader:RegisterEvent("PLAYER_ENTERING_WORLD")
  104.         CombatFader:RegisterEvent("PLAYER_TARGET_CHANGED")
  105.         CombatFader:RegisterEvent("PLAYER_REGEN_ENABLED")
  106.         CombatFader:RegisterEvent("PLAYER_REGEN_DISABLED")
  107.         CombatFader:SetScript("OnEvent", CombatFader.UpdateStatus)
  108.        
  109.         CombatFader.Status = nil
  110.         CombatFader.UpdateRuneStatus()
  111.     else
  112.         CombatFader:UnregisterEvent("PLAYER_ENTERING_WORLD")
  113.         CombatFader:UnregisterEvent("PLAYER_TARGET_CHANGED")
  114.         CombatFader:UnregisterEvent("PLAYER_REGEN_ENABLED")
  115.         CombatFader:UnregisterEvent("PLAYER_REGEN_DISABLED")
  116.        
  117.         nRD.Frames.Parent:SetAlpha(1)
  118.     end
  119. end
  120.  
  121. ---- RUNES
  122.  
  123. local runes = {}
  124. for i = 1, 6 do
  125.     runes[i] = {}
  126. end
  127.  
  128. local runeOrder = {
  129.     [1] = 1,
  130.     [2] = 2,
  131.     [3] = 3,
  132.     [4] = 4,
  133.     [5] = 5,
  134.     [6] = 6,
  135. }
  136.  
  137.  
  138. local tsort = table.sort
  139.  
  140. local function orderRunes()
  141.     for i = 1, 6 do
  142.         runeOrder[i] = i
  143.     end
  144.     tsort(runeOrder, function(a,b)
  145.         local vala = runes[a].ready and 0 or runes[a].start
  146.         local valb = runes[b].ready and 0 or runes[b].start
  147.         return vala == valb and (a < b) or (vala < valb)
  148.     end)
  149.  
  150.     return runeOrder
  151.  
  152. end
  153.  
  154.  
  155. function nRD.OnUpdate()
  156.     local time = GetTime()
  157.    
  158.     if time > nRD.LastTime + 0.04 then
  159.         -- Update 25 times a second
  160.         -- Update Rune Bars
  161.  
  162.         -- Presort runes
  163.  
  164.         for rune = 1, 6 do
  165.             local start, duration, runeReady = GetRuneCooldown(rune)
  166.             runes[rune].start = start
  167.             runes[rune].runeReady = runeReady
  168.         end
  169.         local newOrder = orderRunes()
  170.  
  171.  
  172.         local RuneBar
  173.         local start, duration, runeReady
  174.         for rune = 1, 6 do
  175.  
  176.             start, duration, runeReady = GetRuneCooldown(newOrder[rune])
  177.             RuneBar = nRD.Frames.RuneBars[rune]
  178.  
  179.             if RuneBar ~= nil then
  180.                 if runeReady or UnitIsDead("player") or UnitIsGhost("player") then
  181.                     if ( Opts.combatfader.enabled and (not RuneFull[rune]) and (not RunesAreReady) ) then
  182.                         RuneFull[rune] = runeReady
  183.                         CombatFader.UpdateRuneStatus()
  184.                     end
  185.                    
  186.                     local BGSize = Opts.runes.size.height + Opts.appearance.borderwidth * 2
  187.                     if HR then RuneBar.StatusBarBG:SetWidth(BGSize) else RuneBar.StatusBarBG:SetHeight(BGSize) end
  188.                     RuneBar.BottomStatusBar:SetValue(1)
  189.                     RuneBar.TopStatusBar:SetValue(1)
  190.                 else
  191.                     if ( Opts.combatfader.enabled and (RuneFull[rune] or RunesAreReady) ) then
  192.                         RuneFull[rune] = runeReady
  193.                         CombatFader.UpdateRuneStatus()
  194.                     end
  195.                    
  196.                     local BGSize = ((Opts.runes.size.height) * ((time - start) / duration)) + (Opts.appearance.borderwidth * 2)
  197.                     if HR then RuneBar.StatusBarBG:SetWidth(BGSize) else RuneBar.StatusBarBG:SetHeight(BGSize) end
  198.                     RuneBar.BottomStatusBar:SetValue((time - start) / duration)
  199.                     RuneBar.TopStatusBar:SetValue(math.max((time - (start + duration - gcdNextDuration)) / gcdNextDuration, 0.0))
  200.                 end
  201.             end
  202.         end
  203.  
  204.         nRD.LastTime = time
  205.     end
  206. end
  207.  
  208. function nRD.RuneTextureUpdate(rune)
  209.     RuneBar = nRD.Frames.RuneBars[rune]
  210.     if not RuneBar then return end
  211.    
  212.     local RuneType = 4
  213.     if RuneType then
  214.         RuneBar.BottomStatusBar.bg:SetColorTexture(Opts.runes.colors.bright[RuneType].r * Opts.runes.colors.dimfactor, Opts.runes.colors.bright[RuneType].g * Opts.runes.colors.dimfactor, Opts.runes.colors.bright[RuneType].b * Opts.runes.colors.dimfactor)
  215.         RuneBar.TopStatusBar.bg:SetColorTexture(Opts.runes.colors.bright[RuneType].r, Opts.runes.colors.bright[RuneType].g, Opts.runes.colors.bright[RuneType].b)
  216.     end
  217. end
  218.  
  219. function nRD.UpdateRuneTextures()
  220.     for rune = 1, 6 do
  221.         nRD.RuneTextureUpdate(rune)
  222.     end
  223. end
  224.  
  225. local function Rune_TypeUpdate(event, rune)
  226.     if not rune or tonumber(rune) ~= rune or rune < 1 or rune > 6 then
  227.         return
  228.     end
  229.  
  230.     -- Update Rune colors
  231.     nRD.RuneTextureUpdate(rune, select(3, GetRuneCooldown(rune)))
  232. end
  233.  
  234. local function Rune_PlayerEnteringWorld()
  235.     -- Update rune colors
  236.     nRD.UpdateRuneTextures()
  237. end
  238.  
  239. local function RuneEvents(self, event, ...)
  240.     if event == "PLAYER_ENTERING_WORLD" then
  241.         Rune_PlayerEnteringWorld()
  242.     elseif event == "RUNE_TYPE_UPDATE" then
  243.         Rune_TypeUpdate(event, ...)
  244.     end
  245. end
  246.  
  247. function nRD.SetupEvents()
  248.     if EventsRegistered then return end
  249.  
  250.     nRD.Frames.Parent:RegisterEvent("RUNE_TYPE_UPDATE")
  251.     nRD.Frames.Parent:RegisterEvent("PLAYER_ENTERING_WORLD")
  252.     nRD.Frames.Parent:SetScript("OnEvent", RuneEvents)
  253.    
  254.     -- Enable OnUpdate handler
  255.     nRD.LastTime = 0
  256.     nRD.Frames.Main:SetScript("OnUpdate", nRD.OnUpdate)
  257.    
  258.     EventsRegistered = true
  259. end
  260.  
  261. -- Settings Update
  262. function nRD.UpdateSettings()
  263.     local PF = _G[Opts.position.parent] or UIParent
  264.     nRD.Frames.Parent:SetParent(PF)
  265.     nRD.Frames.Parent:SetPoint(Opts.position.anchor, PF, Opts.position.anchor, Opts.position.x, Opts.position.y)
  266.     nRD.Frames.Parent:SetFrameStrata(Opts.framelevel.strata)
  267.     nRD.Frames.Parent:SetFrameLevel(Opts.framelevel.level)
  268.    
  269.     if HR and HS then
  270.         nRD.Frames.Parent:SetWidth(Opts.runes.size.height * 6 + Opts.runes.size.padding * 7)
  271.         nRD.Frames.Parent:SetHeight(Opts.runes.size.width + Opts.runes.size.padding * 2)
  272.     elseif HR and not HS then
  273.         nRD.Frames.Parent:SetWidth(Opts.runes.size.height + Opts.runes.size.padding * 2)
  274.         nRD.Frames.Parent:SetHeight(Opts.runes.size.width * 6 + Opts.runes.size.padding * 7)
  275.     else
  276.         nRD.Frames.Parent:SetHeight(Opts.runes.size.height + Opts.runes.size.padding * 2)
  277.         nRD.Frames.Parent:SetWidth(Opts.runes.size.width * 6 + Opts.runes.size.padding * 7)
  278.     end
  279.    
  280.     nRD.Frames.Main:SetAllPoints(nRD.Frames.Parent)
  281.     nRD.Frames.Main:SetAlpha(Opts.appearance.opacity)
  282.    
  283.     local RuneBar
  284.     for i = 1, 6 do
  285.         local CurRune = Opts.runes.order[i]
  286.         RuneBar = nRD.Frames.RuneBars[i]
  287.  
  288.         -- Create Rune Bar
  289.         RuneBar.frame:SetFrameStrata(Opts.framelevel.strata)
  290.         RuneBar.frame:SetFrameLevel(Opts.framelevel.level + 1)
  291.         if HR and HS then
  292.             RuneBar.frame:SetWidth(Opts.runes.size.height)
  293.             RuneBar.frame:SetHeight(Opts.runes.size.width)
  294.             RuneBar.frame:SetPoint("TOPLEFT", nRD.Frames.Main, "TOPLEFT", Opts.runes.size.padding + (CurRune - 1) * (Opts.runes.size.height + Opts.runes.size.padding), -Opts.runes.size.padding)
  295.         elseif HR and not HS then
  296.             RuneBar.frame:SetWidth(Opts.runes.size.height)
  297.             RuneBar.frame:SetHeight(Opts.runes.size.width)
  298.             RuneBar.frame:SetPoint("TOPLEFT", nRD.Frames.Main, "TOPLEFT", -Opts.runes.size.padding, Opts.runes.size.padding + (CurRune - 1) * (Opts.runes.size.width + Opts.runes.size.padding))
  299.         else
  300.             RuneBar.frame:SetHeight(Opts.runes.size.height)
  301.             RuneBar.frame:SetWidth(Opts.runes.size.width)
  302.             RuneBar.frame:SetPoint("TOPLEFT", nRD.Frames.Main, "TOPLEFT", Opts.runes.size.padding + (CurRune - 1) * (Opts.runes.size.width + Opts.runes.size.padding), -Opts.runes.size.padding)
  303.         end
  304.        
  305.         -- Status Bar BG (Border)
  306.         if HR then
  307.             RuneBar.StatusBarBG:SetPoint("LEFT", RuneBar.frame, "LEFT", -Opts.appearance.borderwidth, 0)
  308.         else
  309.             RuneBar.StatusBarBG:SetPoint("BOTTOM", RuneBar.frame, "BOTTOM", 0, -Opts.appearance.borderwidth)
  310.         end
  311.         RuneBar.StatusBarBG:SetHeight(RuneBar.frame:GetHeight() + Opts.appearance.borderwidth * 2)
  312.         RuneBar.StatusBarBG:SetWidth(RuneBar.frame:GetWidth() + Opts.appearance.borderwidth * 2)
  313.         RuneBar.StatusBarBG:SetColorTexture(0, 0, 0, 1)
  314.  
  315.         -- Bottom Status Bar
  316.         RuneBar.BottomStatusBar:SetFrameStrata(Opts.framelevel.strata)
  317.         RuneBar.BottomStatusBar:SetFrameLevel(RuneBar.frame:GetFrameLevel() + 1)
  318.         RuneBar.BottomStatusBar.bg:SetColorTexture(Opts.runes.colors.bright[RUNETYPE_BLOOD].r * Opts.runes.colors.dimfactor, Opts.runes.colors.bright[RUNETYPE_BLOOD].g * Opts.runes.colors.dimfactor, Opts.runes.colors.bright[RUNETYPE_BLOOD].b * Opts.runes.colors.dimfactor)
  319.  
  320.         -- Top Status Bar
  321.         RuneBar.TopStatusBar:SetFrameStrata(Opts.framelevel.strata)
  322.         RuneBar.TopStatusBar:SetFrameLevel(RuneBar.BottomStatusBar:GetFrameLevel() + 1)
  323.         RuneBar.TopStatusBar.bg:SetColorTexture(Opts.runes.colors.bright[RUNETYPE_BLOOD].r, Opts.runes.colors.bright[RUNETYPE_BLOOD].g, Opts.runes.colors.bright[RUNETYPE_BLOOD].b)
  324.     end
  325.    
  326.     nRD.UpdateRuneTextures()
  327. end
  328.  
  329. -- Frame Creation
  330. function nRD.CreateFrames()
  331.     if nRD.Frames then return end
  332.    
  333.     nRD.Frames = {}
  334.    
  335.     -- Parent frame
  336.     nRD.Frames.Parent = CreateFrame("Frame", "nibRunes_RuneDisplay", UIParent)
  337.    
  338.     -- Create main frame
  339.     nRD.Frames.Main = CreateFrame("Frame", nil, nRD.Frames.Parent)
  340.     nRD.Frames.Main:SetParent(nRD.Frames.Parent)
  341.    
  342.     -- Rune Bars
  343.     nRD.Frames.RuneBars = {}
  344.     local RuneBar
  345.     local SBO
  346.     if HR then SBO = "HORIZONTAL" else SBO = "VERTICAL" end
  347.     for i = 1, 6 do
  348.         nRD.Frames.RuneBars[i] = {}
  349.         RuneBar = nRD.Frames.RuneBars[i]
  350.  
  351.         -- Create Rune Bar
  352.         RuneBar.frame = CreateFrame("Frame", nil, nRD.Frames.Main)
  353.        
  354.         -- Status Bar BG (Border)
  355.         RuneBar.StatusBarBG = RuneBar.frame:CreateTexture()
  356.  
  357.         -- Bottom Status Bar
  358.         RuneBar.BottomStatusBar = CreateFrame("StatusBar", nil, RuneBar.frame)
  359.         RuneBar.BottomStatusBar:SetOrientation(SBO)
  360.         RuneBar.BottomStatusBar:SetMinMaxValues(0, 1)
  361.         RuneBar.BottomStatusBar:SetValue(1)
  362.         RuneBar.BottomStatusBar:SetAllPoints(RuneBar.frame)
  363.  
  364.         RuneBar.BottomStatusBar.bg = RuneBar.BottomStatusBar:CreateTexture()
  365.         RuneBar.BottomStatusBar.bg:SetAllPoints()
  366.         RuneBar.BottomStatusBar.bg:SetColorTexture(Opts.runes.colors.bright[RUNETYPE_BLOOD].r * Opts.runes.colors.dimfactor, Opts.runes.colors.bright[RUNETYPE_BLOOD].g * Opts.runes.colors.dimfactor, Opts.runes.colors.bright[RUNETYPE_BLOOD].b * Opts.runes.colors.dimfactor)
  367.         RuneBar.BottomStatusBar:SetStatusBarTexture(RuneBar.BottomStatusBar.bg)
  368.  
  369.         -- Top Status Bar
  370.         RuneBar.TopStatusBar = CreateFrame("StatusBar", nil, RuneBar.frame)
  371.         RuneBar.TopStatusBar:SetOrientation(SBO)
  372.         RuneBar.TopStatusBar:SetMinMaxValues(0, 1)
  373.         RuneBar.TopStatusBar:SetValue(1)
  374.         RuneBar.TopStatusBar:SetAllPoints(RuneBar.frame)
  375.  
  376.         RuneBar.TopStatusBar.bg = RuneBar.TopStatusBar:CreateTexture()
  377.         RuneBar.TopStatusBar.bg:SetAllPoints()
  378.         RuneBar.TopStatusBar.bg:SetColorTexture(Opts.runes.colors.bright[RUNETYPE_BLOOD].r, Opts.runes.colors.bright[RUNETYPE_BLOOD].g, Opts.runes.colors.bright[RUNETYPE_BLOOD].b)
  379.         RuneBar.TopStatusBar:SetStatusBarTexture(RuneBar.TopStatusBar.bg)
  380.     end
  381. end
  382.  
  383. ---- CORE
  384. function nRD.RefreshMod()
  385.     nRD.UpdateSettings()
  386.     CombatFader.UpdateEnabled()
  387. end
  388.  
  389. ----
  390. function nRD.Enable()
  391.     -- Refresh
  392.     nRD.RefreshMod()
  393.    
  394.     -- Setup Events
  395.     nRD.SetupEvents()
  396.    
  397.     -- Disable default rune frame
  398.     if Opts.hideblizzard then
  399.         RuneFrame:UnregisterAllEvents()
  400.         RuneFrame:Hide()
  401.         RuneFrame.Show = function() end
  402.     end
  403.    
  404.     -- Show RuneDisplay
  405.     nRD.Frames.Parent:Show()
  406. end
  407.  
  408. function nRD.PLAYER_LOGIN()
  409.     if not (select(2, UnitClass("player")) == "DEATHKNIGHT") then return end
  410.  
  411.     if Opts.horizontalrunes then HR = true end
  412.     if Opts.horizontalstacked then HS = true end
  413.    
  414.     nRD.CreateFrames()
  415.     nRD.Enable()
  416. end
  417.  
  418. local function EventHandler(self, event, ...)
  419.     if event == "PLAYER_LOGIN" then
  420.         nRD.PLAYER_LOGIN()
  421.     end
  422. end
  423. nRD:RegisterEvent("PLAYER_LOGIN")
  424. nRD:RegisterEvent("UNIT_HEALTH")
  425. nRD:SetScript("OnEvent", EventHandler)
RAW Paste Data