Advertisement
khanggaroo

InterruptBar.lua

Mar 20th, 2013
1,711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.26 KB | None | 0 0
  1. ----------------------------------------------------
  2. -- Interrupt Bar by Kollektiv
  3. ----------------------------------------------------
  4.  
  5. -- Add new abilities here. Order is determined as shown.
  6. local abilities = {
  7.     {spellid = 2139,   duration = 20},   -- Counterspell
  8.     {spellid = 15487,  duration = 45},   -- Silence
  9.     {spellid = 34490,  duration = 20},   -- Silencing Shot
  10.     {spellid = 19647,  duration = 24},   -- Spell Lock
  11.     {spellid = 57994,  duration = 12},   -- Wind Shear
  12.     {spellid = 6552,   duration = 15},   -- Pummel
  13.     {spellid = 47528,  duration = 15},   -- Mind Freeze
  14.     {spellid = 1766,   duration = 15},   -- Kick
  15.     {spellid = 80965,  duration = 15},   -- Skull Bash
  16.     {spellid = 96231,  duration = 15},   -- Rebuke
  17.     {spellid = 116705, duration = 15},   -- Spear Hand Strike
  18.     {spellid = 78675,  duration = 60},   -- Solar Beam
  19. }
  20.  
  21. -----------------------------------------------------
  22. -----------------------------------------------------
  23.  
  24. InterruptBarDB = InterruptBarDB or { scale = 1, hidden = false, lock = false, }
  25.  
  26. for _,ability in ipairs(abilities) do
  27.     local _,_,spellicon = GetSpellInfo(ability.spellid)
  28.     ability.icon = spellicon
  29. end
  30.  
  31. local SPELLLOCK = 19647
  32. local OPTICALBLAST = 115782
  33.  
  34. local order
  35. local frame
  36. local bar
  37. local btns = {}
  38.  
  39. local band = bit.band
  40. local GetTime = GetTime
  41. local ipairs = ipairs
  42. local pairs = pairs
  43. local select = select
  44. local floor = floor
  45. local ceil = math.ceil
  46. local band = bit.band
  47. local GetSpellInfo = GetSpellInfo
  48.  
  49. local function InterruptBar_OnUpdate(self)
  50.     local cooldown = self.start + self.duration - GetTime()
  51.     if cooldown <= 0 then
  52.         self.deactivate()
  53.     else
  54.         self.settimeleft(ceil(cooldown))
  55.     end
  56. end
  57.  
  58. local function InterruptBar_CreateIcon(ability)
  59.     local btn = CreateFrame("Frame",nil,bar)
  60.     btn:SetWidth(30)
  61.     btn:SetHeight(30)
  62.     btn:SetFrameStrata("LOW")
  63.  
  64.     local cd = CreateFrame("Cooldown",nil,btn)
  65.     cd.noomnicc = true
  66.     cd.noCooldownCount = true
  67.     cd:SetAllPoints(true)
  68.     cd:SetFrameStrata("MEDIUM")
  69.     cd:Hide()
  70.  
  71.     local texture = btn:CreateTexture(nil,"BACKGROUND")
  72.     texture:SetAllPoints(true)
  73.     texture:SetTexture(ability.icon)
  74.     texture:SetTexCoord(0.07,0.9,0.07,0.90)
  75.  
  76.     local text = cd:CreateFontString(nil,"ARTWORK")
  77.     text:SetFont(STANDARD_TEXT_FONT,18,"OUTLINE")
  78.     text:SetTextColor(1,1,0,1)
  79.     text:SetPoint("LEFT",btn,"LEFT",2,0)
  80.  
  81.     btn.texture = texture
  82.     btn.text = text
  83.     btn.duration = ability.duration
  84.     btn.cd = cd
  85.  
  86.     btn.activate = function()
  87.         if btn.active then return end
  88.         if InterruptBarDB.hidden then btn:Show() end
  89.         btn.start = GetTime()
  90.         btn.cd:Show()
  91.         btn.cd:SetCooldown(GetTime()-0.40,btn.duration)
  92.         btn.start = GetTime()
  93.         btn.settimeleft(btn.duration)
  94.         btn:SetScript("OnUpdate", InterruptBar_OnUpdate)
  95.         btn.active = true
  96.     end
  97.  
  98.     btn.deactivate = function()
  99.         if InterruptBarDB.hidden then btn:Hide() end
  100.         btn.text:SetText("")
  101.         btn.cd:Hide()
  102.         btn:SetScript("OnUpdate", nil)
  103.         btn.active = false
  104.     end
  105.  
  106.     btn.settimeleft = function(timeleft)
  107.         if timeleft < 10 then
  108.             if timeleft <= 0.5 then
  109.                 btn.text:SetText("")
  110.             else
  111.                 btn.text:SetFormattedText(" %d",timeleft)
  112.             end
  113.         else
  114.             btn.text:SetFormattedText("%d",timeleft)
  115.         end
  116.         if timeleft < 6 then
  117.             btn.text:SetTextColor(1,0,0,1)
  118.         else
  119.             btn.text:SetTextColor(1,1,0,1)
  120.         end
  121.         if timeleft > 60 then
  122.             btn.text:SetFont(STANDARD_TEXT_FONT,14,"OUTLINE")
  123.         else
  124.             btn.text:SetFont(STANDARD_TEXT_FONT,18,"OUTLINE")
  125.         end
  126.     end
  127.  
  128.     return btn
  129. end
  130.  
  131. local function InterruptBar_AddIcons()
  132.         local y = -45
  133.         for _,ability in ipairs(abilities) do
  134.                 local btn = InterruptBar_CreateIcon(ability)
  135.                 btn:SetPoint("CENTER", bar, "CENTER", 0, y)
  136.                 btns[ability.spellid] = btn
  137.                 y = y + 30
  138.         end
  139. end
  140.  
  141. local function InterruptBar_SavePosition()
  142.     local point, _, relativePoint, xOfs, yOfs = bar:GetPoint()
  143.     if not InterruptBarDB.Position then
  144.         InterruptBarDB.Position = {}
  145.     end
  146.     InterruptBarDB.Position.point = point
  147.     InterruptBarDB.Position.relativePoint = relativePoint
  148.     InterruptBarDB.Position.xOfs = xOfs
  149.     InterruptBarDB.Position.yOfs = yOfs
  150. end
  151.  
  152. local function InterruptBar_LoadPosition()
  153.     if InterruptBarDB.Position then
  154.         bar:SetPoint(InterruptBarDB.Position.point,UIParent,InterruptBarDB.Position.relativePoint,InterruptBarDB.Position.xOfs,InterruptBarDB.Position.yOfs)
  155.     else
  156.         bar:SetPoint("CENTER", UIParent, "CENTER")
  157.     end
  158. end
  159.  
  160. local function InterruptBar_UpdateBar()
  161.     bar:SetScale(InterruptBarDB.scale)
  162.     if InterruptBarDB.hidden then
  163.         for _,btn in pairs(btns) do btn:Hide() end
  164.     else
  165.         for _,btn in pairs(btns) do btn:Show() end
  166.     end
  167.     if InterruptBarDB.lock then
  168.         bar:EnableMouse(false)
  169.     else
  170.         bar:EnableMouse(true)
  171.     end
  172. end
  173.  
  174. local function InterruptBar_CreateBar()
  175.     bar = CreateFrame("Frame", nil, UIParent)
  176.     bar:SetMovable(true)
  177.     bar:SetWidth(120)
  178.     bar:SetHeight(30)
  179.     bar:SetClampedToScreen(true)
  180.     bar:SetScript("OnMouseDown",function(self,button) if button == "LeftButton" then self:StartMoving() end end)
  181.     bar:SetScript("OnMouseUp",function(self,button) if button == "LeftButton" then self:StopMovingOrSizing() InterruptBar_SavePosition() end end)
  182.     bar:Show()
  183.    
  184.     InterruptBar_AddIcons()
  185.     InterruptBar_UpdateBar()
  186.     InterruptBar_LoadPosition()
  187. end
  188.  
  189. local function InterruptBar_COMBAT_LOG_EVENT_UNFILTERED(_, eventtype, _, _, srcName, srcFlags, _, _, dstName, dstFlags, _, spellid)
  190.     if band(srcFlags, 0x00000040) == 0x00000040 and eventtype == "SPELL_CAST_SUCCESS" then
  191.         -- Redirect Optical Blast to Spell Lock
  192.         if spellid == OPTICALBLAST and btns[SPELLLOCK] then
  193.             spellid = SPELLLOCK
  194.         end
  195.         local btn = btns[spellid]
  196.         if btn then btn.activate() end
  197.     end
  198. end
  199.  
  200. local function InterruptBar_ResetAllTimers()
  201.     for _,btn in pairs(btns) do
  202.         btn.deactivate()
  203.     end
  204. end
  205.  
  206. local function InterruptBar_PLAYER_ENTERING_WORLD(self)
  207.     InterruptBar_ResetAllTimers()
  208. end
  209.  
  210. local function InterruptBar_Reset()
  211.     InterruptBarDB = { scale = 1, hidden = false, lock = false }
  212.     InterruptBar_UpdateBar()
  213.     InterruptBar_LoadPosition()
  214. end
  215.  
  216. local function InterruptBar_Test()
  217.     for _,btn in pairs(btns) do
  218.         btn.activate()
  219.     end
  220. end
  221.  
  222. local cmdfuncs = {
  223.     scale = function(v) InterruptBarDB.scale = v; InterruptBar_UpdateBar() end,
  224.     hidden = function() InterruptBarDB.hidden = not InterruptBarDB.hidden; InterruptBar_UpdateBar() end,
  225.     lock = function() InterruptBarDB.lock = not InterruptBarDB.lock; InterruptBar_UpdateBar() end,
  226.     reset = function() InterruptBar_Reset() end,
  227.     test = function() InterruptBar_Test() end,
  228. }
  229.  
  230. local cmdtbl = {}
  231. function InterruptBar_Command(cmd)
  232.     for k in ipairs(cmdtbl) do
  233.         cmdtbl[k] = nil
  234.     end
  235.     for v in gmatch(cmd, "[^ ]+") do
  236.     tinsert(cmdtbl, v)
  237.   end
  238.   local cb = cmdfuncs[cmdtbl[1]]
  239.   if cb then
  240.     local s = tonumber(cmdtbl[2])
  241.     cb(s)
  242.   else
  243.     ChatFrame1:AddMessage("InterruptBar Options | /ib <option>",0,1,0)     
  244.     ChatFrame1:AddMessage("-- scale <number> | value: " .. InterruptBarDB.scale,0,1,0)
  245.     ChatFrame1:AddMessage("-- hidden (toggle) | value: " .. tostring(InterruptBarDB.hidden),0,1,0)
  246.     ChatFrame1:AddMessage("-- lock (toggle) | value: " .. tostring(InterruptBarDB.lock),0,1,0)
  247.     ChatFrame1:AddMessage("-- test (execute)",0,1,0)
  248.     ChatFrame1:AddMessage("-- reset (execute)",0,1,0)
  249.   end
  250. end
  251.  
  252. local function InterruptBar_OnLoad(self)
  253.     self:RegisterEvent("PLAYER_ENTERING_WORLD")
  254.     self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
  255.     if not InterruptBarDB.scale then InterruptBarDB.scale = 1 end
  256.     if not InterruptBarDB.hidden then InterruptBarDB.hidden = false end
  257.     if not InterruptBarDB.lock then InterruptBarDB.lock = false end
  258.     InterruptBar_CreateBar()
  259.    
  260.     SlashCmdList["InterruptBar"] = InterruptBar_Command
  261.     SLASH_InterruptBar1 = "/ib"
  262.    
  263.     ChatFrame1:AddMessage("Interrupt Bar by Kollektiv. Type /ib for options.",0,1,0)
  264. end
  265.  
  266. local eventhandler = {
  267.     ["VARIABLES_LOADED"] = function(self) InterruptBar_OnLoad(self) end,
  268.     ["PLAYER_ENTERING_WORLD"] = function(self) InterruptBar_PLAYER_ENTERING_WORLD(self) end,
  269.     ["COMBAT_LOG_EVENT_UNFILTERED"] = function(self,...) InterruptBar_COMBAT_LOG_EVENT_UNFILTERED(...) end,
  270. }
  271.  
  272. local function InterruptBar_OnEvent(self,event,...)
  273.     eventhandler[event](self,...)
  274. end
  275.  
  276. frame = CreateFrame("Frame",nil,UIParent)
  277. frame:SetScript("OnEvent",InterruptBar_OnEvent)
  278. frame:RegisterEvent("VARIABLES_LOADED")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement