Guest User

Core.lua

a guest
Jul 21st, 2016
239
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.  
  61. CombatFader.FadeIt(nRD.Frames.Parent, NewOpacity)
  62. end
  63.  
  64. -- Update current status
  65. function CombatFader.UpdateStatus()
  66. local OldStatus = CombatFader.Status
  67. if UnitAffectingCombat("player") then
  68. CombatFader.Status = "INCOMBAT"; -- InCombat - Priority 1
  69. elseif UnitExists("target") and UnitCanAttack("player", "target") then
  70. CombatFader.Status = "HARMTARGET"; -- HarmTarget - Priority 2
  71. elseif not RunesAreReady then
  72. CombatFader.Status = "HURT"; -- Not Full - Priority 4
  73. else
  74. CombatFader.Status = "OUTOFCOMBAT"; -- OutOfCombat - Priority 5
  75. end
  76. if CombatFader.Status ~= OldStatus then CombatFader.FadeFrames() end
  77. end
  78.  
  79. function CombatFader.PLAYER_ENTERING_WORLD()
  80. CombatFader.Status = nil
  81. CombatFader.UpdateStatus()
  82. CombatFader.FadeFrames()
  83. end
  84.  
  85. function CombatFader.UpdateRuneStatus()
  86. if Opts.combatfader.enabled then
  87. if ( RuneFull[1] and RuneFull[2] and RuneFull[3] and RuneFull[4] and RuneFull[5] and RuneFull[6] ) then
  88. RunesAreReady = true
  89. else
  90. RunesAreReady = false
  91. end
  92. CombatFader.UpdateStatus()
  93. CombatFader.FadeFrames()
  94. end
  95. end
  96.  
  97. function CombatFader.OptionsRefresh()
  98. CombatFader.Status = nil
  99. CombatFader.UpdateStatus()
  100. end
  101.  
  102. function CombatFader.UpdateEnabled()
  103. if Opts.combatfader.enabled then
  104. CombatFader:RegisterEvent("PLAYER_ENTERING_WORLD")
  105. CombatFader:RegisterEvent("PLAYER_TARGET_CHANGED")
  106. CombatFader:RegisterEvent("PLAYER_REGEN_ENABLED")
  107. CombatFader:RegisterEvent("PLAYER_REGEN_DISABLED")
  108. CombatFader:SetScript("OnEvent", CombatFader.UpdateStatus)
  109.  
  110. CombatFader.Status = nil
  111. CombatFader.UpdateRuneStatus()
  112. else
  113. CombatFader:UnregisterEvent("PLAYER_ENTERING_WORLD")
  114. CombatFader:UnregisterEvent("PLAYER_TARGET_CHANGED")
  115. CombatFader:UnregisterEvent("PLAYER_REGEN_ENABLED")
  116. CombatFader:UnregisterEvent("PLAYER_REGEN_DISABLED")
  117.  
  118. nRD.Frames.Parent:SetAlpha(1)
  119. end
  120. end
  121.  
  122. ---- RUNES
  123. -- Events
  124. function nRD.OnUpdate()
  125. local time = GetTime()
  126.  
  127. if time > nRD.LastTime + 0.04 then -- Update 25 times a second
  128. -- Update Rune Bars
  129. local RuneBar
  130. local start, duration, runeReady
  131. for rune = 1, 6 do
  132. RuneBar = nRD.Frames.RuneBars[rune]
  133. start, duration, runeReady = GetRuneCooldown(rune)
  134. if RuneBar ~= nil then
  135. if runeReady or UnitIsDead("player") or UnitIsGhost("player") then
  136.  
  137. if ( Opts.combatfader.enabled and (not RuneFull[rune]) and (not RunesAreReady) ) then
  138. RuneFull[rune] = runeReady
  139. CombatFader.UpdateRuneStatus()
  140. end
  141.  
  142. local BGSize = Opts.runes.size.height + Opts.appearance.borderwidth * 2
  143. if HR then RuneBar.StatusBarBG:SetWidth(BGSize) else RuneBar.StatusBarBG:SetHeight(BGSize) end
  144. RuneBar.BottomStatusBar:SetValue(1)
  145. RuneBar.TopStatusBar:SetValue(1)
  146. else
  147.  
  148. if ( Opts.combatfader.enabled and (RuneFull[rune] or RunesAreReady) ) then
  149. RuneFull[rune] = runeReady
  150. CombatFader.UpdateRuneStatus()
  151.  
  152. end
  153.  
  154. local BGSize = math.max( ((Opts.runes.size.height) * ((time - start) / duration)) + (Opts.appearance.borderwidth * 2), 0)
  155.  
  156. if HR then RuneBar.StatusBarBG:SetWidth(BGSize) else RuneBar.StatusBarBG:SetHeight(BGSize) end
  157. RuneBar.BottomStatusBar:SetValue((time - start) / duration)
  158. RuneBar.TopStatusBar:SetValue(math.max((time - (start + duration - gcdNextDuration)) / gcdNextDuration, 0.0))
  159.  
  160. end
  161. end
  162. end
  163.  
  164. nRD.LastTime = time
  165. end
  166. end
  167.  
  168. function nRD.RuneTextureUpdate(rune)
  169. RuneBar = nRD.Frames.RuneBars[rune]
  170. if not RuneBar then return end
  171.  
  172. local RuneType = 3
  173. if RuneType then
  174. 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)
  175. RuneBar.TopStatusBar.bg:SetColorTexture(Opts.runes.colors.bright[RuneType].r, Opts.runes.colors.bright[RuneType].g, Opts.runes.colors.bright[RuneType].b)
  176. end
  177. end
  178.  
  179. function nRD.UpdateRuneTextures()
  180. for rune = 1, 6 do
  181. nRD.RuneTextureUpdate(rune)
  182. end
  183. end
  184.  
  185. local function Rune_TypeUpdate(event, rune)
  186. if not rune or tonumber(rune) ~= rune or rune < 1 or rune > 6 then
  187. return
  188. end
  189.  
  190. -- Update Rune colors
  191. nRD.RuneTextureUpdate(rune, select(3, GetRuneCooldown(rune)))
  192. end
  193.  
  194. local function Rune_PlayerEnteringWorld()
  195. -- Update rune colors
  196. nRD.UpdateRuneTextures()
  197. end
  198.  
  199. local function RuneEvents(self, event, ...)
  200. if event == "PLAYER_ENTERING_WORLD" then
  201. Rune_PlayerEnteringWorld()
  202. --elseif event == "RUNE_TYPE_UPDATE" then
  203. -- Rune_TypeUpdate(event, ...)
  204. end
  205. end
  206.  
  207. function nRD.SetupEvents()
  208. if EventsRegistered then return end
  209.  
  210. --nRD.Frames.Parent:RegisterEvent("RUNE_TYPE_UPDATE")
  211. nRD.Frames.Parent:RegisterEvent("PLAYER_ENTERING_WORLD")
  212. nRD.Frames.Parent:SetScript("OnEvent", RuneEvents)
  213.  
  214. -- Enable OnUpdate handler
  215. nRD.LastTime = 0
  216. nRD.Frames.Main:SetScript("OnUpdate", nRD.OnUpdate)
  217.  
  218. EventsRegistered = true
  219. end
  220.  
  221. -- Settings Update
  222. function nRD.UpdateSettings()
  223. local PF = _G[Opts.position.parent] or UIParent
  224. nRD.Frames.Parent:SetParent(PF)
  225. nRD.Frames.Parent:SetPoint(Opts.position.anchor, PF, Opts.position.anchor, Opts.position.x, Opts.position.y)
  226. nRD.Frames.Parent:SetFrameStrata(Opts.framelevel.strata)
  227. nRD.Frames.Parent:SetFrameLevel(Opts.framelevel.level)
  228.  
  229. if HR and HS then
  230. nRD.Frames.Parent:SetWidth(Opts.runes.size.height * 6 + Opts.runes.size.padding * 7)
  231. nRD.Frames.Parent:SetHeight(Opts.runes.size.width + Opts.runes.size.padding * 2)
  232. elseif HR and not HS then
  233. nRD.Frames.Parent:SetWidth(Opts.runes.size.height + Opts.runes.size.padding * 2)
  234. nRD.Frames.Parent:SetHeight(Opts.runes.size.width * 6 + Opts.runes.size.padding * 7)
  235. else
  236. nRD.Frames.Parent:SetHeight(Opts.runes.size.height + Opts.runes.size.padding * 2)
  237. nRD.Frames.Parent:SetWidth(Opts.runes.size.width * 6 + Opts.runes.size.padding * 7)
  238. end
  239.  
  240. nRD.Frames.Main:SetAllPoints(nRD.Frames.Parent)
  241. nRD.Frames.Main:SetAlpha(Opts.appearance.opacity)
  242.  
  243. local RuneBar
  244. for i = 1, 6 do
  245. local CurRune = Opts.runes.order[i]
  246. RuneBar = nRD.Frames.RuneBars[i]
  247.  
  248. -- Create Rune Bar
  249. RuneBar.frame:SetFrameStrata(Opts.framelevel.strata)
  250. RuneBar.frame:SetFrameLevel(Opts.framelevel.level + 1)
  251. if HR and HS then
  252. RuneBar.frame:SetWidth(Opts.runes.size.height)
  253. RuneBar.frame:SetHeight(Opts.runes.size.width)
  254. 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)
  255. elseif HR and not HS then
  256. RuneBar.frame:SetWidth(Opts.runes.size.height)
  257. RuneBar.frame:SetHeight(Opts.runes.size.width)
  258. 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))
  259. else
  260. RuneBar.frame:SetHeight(Opts.runes.size.height)
  261. RuneBar.frame:SetWidth(Opts.runes.size.width)
  262. 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)
  263. end
  264.  
  265. -- Status Bar BG (Border)
  266. if HR then
  267. RuneBar.StatusBarBG:SetPoint("LEFT", RuneBar.frame, "LEFT", -Opts.appearance.borderwidth, 0)
  268. else
  269. RuneBar.StatusBarBG:SetPoint("BOTTOM", RuneBar.frame, "BOTTOM", 0, -Opts.appearance.borderwidth)
  270. end
  271. RuneBar.StatusBarBG:SetHeight(RuneBar.frame:GetHeight() + Opts.appearance.borderwidth * 2)
  272. RuneBar.StatusBarBG:SetWidth(RuneBar.frame:GetWidth() + Opts.appearance.borderwidth * 2)
  273. RuneBar.StatusBarBG:SetColorTexture(0, 0, 0, 1)
  274.  
  275. -- Bottom Status Bar
  276. RuneBar.BottomStatusBar:SetFrameStrata(Opts.framelevel.strata)
  277. RuneBar.BottomStatusBar:SetFrameLevel(RuneBar.frame:GetFrameLevel() + 1)
  278. 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)
  279.  
  280. -- Top Status Bar
  281. RuneBar.TopStatusBar:SetFrameStrata(Opts.framelevel.strata)
  282. RuneBar.TopStatusBar:SetFrameLevel(RuneBar.BottomStatusBar:GetFrameLevel() + 1)
  283. 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)
  284. end
  285.  
  286. nRD.UpdateRuneTextures()
  287. end
  288.  
  289. -- Frame Creation
  290. function nRD.CreateFrames()
  291. if nRD.Frames then return end
  292.  
  293. nRD.Frames = {}
  294.  
  295. -- Parent frame
  296. nRD.Frames.Parent = CreateFrame("Frame", "nibRunes_RuneDisplay", UIParent)
  297.  
  298. -- Create main frame
  299. nRD.Frames.Main = CreateFrame("Frame", nil, nRD.Frames.Parent)
  300. nRD.Frames.Main:SetParent(nRD.Frames.Parent)
  301.  
  302. -- Rune Bars
  303. nRD.Frames.RuneBars = {}
  304. local RuneBar
  305. local SBO
  306. if HR then SBO = "HORIZONTAL" else SBO = "VERTICAL" end
  307. for i = 1, 6 do
  308. nRD.Frames.RuneBars[i] = {}
  309. RuneBar = nRD.Frames.RuneBars[i]
  310.  
  311. -- Create Rune Bar
  312. RuneBar.frame = CreateFrame("Frame", nil, nRD.Frames.Main)
  313.  
  314. -- Status Bar BG (Border)
  315. RuneBar.StatusBarBG = RuneBar.frame:CreateTexture()
  316.  
  317. -- Bottom Status Bar
  318. RuneBar.BottomStatusBar = CreateFrame("StatusBar", nil, RuneBar.frame)
  319. RuneBar.BottomStatusBar:SetOrientation(SBO)
  320. RuneBar.BottomStatusBar:SetMinMaxValues(0, 1)
  321. RuneBar.BottomStatusBar:SetValue(1)
  322. RuneBar.BottomStatusBar:SetAllPoints(RuneBar.frame)
  323.  
  324. RuneBar.BottomStatusBar.bg = RuneBar.BottomStatusBar:CreateTexture()
  325. RuneBar.BottomStatusBar.bg:SetAllPoints()
  326. 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)
  327. RuneBar.BottomStatusBar:SetStatusBarTexture(RuneBar.BottomStatusBar.bg)
  328.  
  329. -- Top Status Bar
  330. RuneBar.TopStatusBar = CreateFrame("StatusBar", nil, RuneBar.frame)
  331. RuneBar.TopStatusBar:SetOrientation(SBO)
  332. RuneBar.TopStatusBar:SetMinMaxValues(0, 1)
  333. RuneBar.TopStatusBar:SetValue(1)
  334. RuneBar.TopStatusBar:SetAllPoints(RuneBar.frame)
  335.  
  336. RuneBar.TopStatusBar.bg = RuneBar.TopStatusBar:CreateTexture()
  337. RuneBar.TopStatusBar.bg:SetAllPoints()
  338. 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)
  339. RuneBar.TopStatusBar:SetStatusBarTexture(RuneBar.TopStatusBar.bg)
  340. end
  341.  
  342. end
  343.  
  344. ---- CORE
  345. function nRD.RefreshMod()
  346. nRD.UpdateSettings()
  347. CombatFader.UpdateEnabled()
  348. end
  349.  
  350. ----
  351. function nRD.Enable()
  352. -- Refresh
  353. nRD.RefreshMod()
  354.  
  355. -- Setup Events
  356. nRD.SetupEvents()
  357.  
  358. -- Disable default rune frame
  359. if Opts.hideblizzard then
  360. RuneFrame:UnregisterAllEvents()
  361. RuneFrame:Hide()
  362. RuneFrame.Show = function() end
  363.  
  364. end
  365.  
  366. -- Show RuneDisplay
  367. nRD.Frames.Parent:Show()
  368. end
  369.  
  370. function nRD.PLAYER_LOGIN()
  371. if not (select(2, UnitClass("player")) == "DEATHKNIGHT") then return end
  372.  
  373. if Opts.horizontalrunes then HR = true end
  374. if Opts.horizontalstacked then HS = true end
  375.  
  376. nRD.CreateFrames()
  377. nRD.Enable()
  378. end
  379.  
  380. local function EventHandler(self, event, ...)
  381. if event == "PLAYER_LOGIN" then
  382. nRD.PLAYER_LOGIN()
  383. end
  384. end
  385. nRD:RegisterEvent("PLAYER_LOGIN")
  386. nRD:RegisterEvent("UNIT_HEALTH")
  387. nRD:SetScript("OnEvent", EventHandler)
RAW Paste Data