annotate KBF.lua @ 10:7309cc67cac0

Hooray support for debuffs
author Chris Mellon <arkanes@gmai.com>
date Wed, 13 Oct 2010 09:27:34 -0500
parents ede17cc71b66
children 76650e326a57
rev   line source
arkanes@0 1 local _, kbf = ...
arkanes@0 2
arkanes@0 3 KBF = kbf -- make global for debugging
arkanes@0 4
arkanes@0 5 local kbf = LibStub("AceAddon-3.0"):NewAddon(kbf, "KBF", "AceEvent-3.0", "AceConsole-3.0")
arkanes@0 6
arkanes@0 7
arkanes@0 8 function kbf:OnInitialize()
arkanes@10 9 self.debuffFrames = {}
arkanes@0 10 self.anchor = self:CreateAnchorFrame()
arkanes@0 11 self.anchor:Show()
arkanes@0 12 self:RegisterEvent("UNIT_AURA")
arkanes@4 13 -- set up the countdown timer
arkanes@4 14 -- TODO: Fancy enable/disable based on whether you have any timed buffs.
arkanes@4 15 -- Not a big deal, how often do you care about that
arkanes@8 16 -- also TODO: Maybe should bucket OnUpdates somehow
arkanes@4 17 self.anchor:SetScript("OnUpdate", function() self:OnUpdate() end)
arkanes@7 18 self.dirty = true -- force an immediate scan on login
arkanes@8 19 --self:HideBlizzardBuffFrames()
arkanes@0 20 end
arkanes@1 21 -- naming convention
arkanes@1 22 -- "frame" is the secure aura button created by the group handler
arkanes@1 23 -- "bar" is the set of icon + status bars that we create to show the buff time
arkanes@0 24
arkanes@8 25 function kbf:HideBlizzardBuffFrames()
arkanes@8 26 local function HideBlizFrame(frame)
arkanes@8 27 if not frame then return end
arkanes@8 28 frame:UnregisterAllEvents()
arkanes@8 29 frame:SetScript("OnUpdate", nil)
arkanes@8 30 frame:Hide()
arkanes@8 31 frame.Show = function() end
arkanes@8 32 end
arkanes@8 33 HideBlizFrame(BuffFrame)
arkanes@8 34 HideBlizFrame(ConsolidatedBuffs)
arkanes@8 35 HideBlizFrame(TemporaryEnchantFrame)
arkanes@8 36
arkanes@8 37 end
arkanes@8 38
arkanes@4 39 function kbf:OnUpdate()
arkanes@10 40 local unit = self.secureFrame:GetAttribute("unit")
arkanes@10 41 local buffCount = 0
arkanes@4 42 for idx=1,99 do
arkanes@4 43 local frame = self.secureFrame:GetAttribute("child"..idx)
arkanes@10 44 if not (frame and frame:IsShown()) then break end
arkanes@10 45 buffCount = buffCount + 1
arkanes@6 46 if self.dirty then
arkanes@10 47 if self:BindBarToBuff(frame, unit) then break end
arkanes@9 48 end
arkanes@10 49 self:UpdateBarExpirationTime(frame)
arkanes@10 50 end
arkanes@10 51 -- temporary enchants
arkanes@10 52 -- TODO: The blizz secure aura header binds both temp enchants
arkanes@10 53 -- to the main hand. No support for cancelling weapon enchants
arkanes@10 54 -- until this gets fixed up
arkanes@10 55
arkanes@10 56 -- debuffs
arkanes@10 57 for idx=1,99 do
arkanes@10 58 local frame = self.debuffFrames[idx]
arkanes@10 59 if self.dirty then
arkanes@10 60 local name, rank, icon, stacks, debuffType, duration, expirationTime = UnitAura(unit, idx, "HARMFUL")
arkanes@10 61 if not name then
arkanes@10 62 -- out of debuffs, hide all the rest of them
arkanes@10 63 for jdx = idx, 99 do
arkanes@10 64 local bar = self.debuffFrames[jdx]
arkanes@10 65 if bar then bar:Hide() else break end
arkanes@10 66 end
arkanes@10 67 break
arkanes@10 68 end
arkanes@10 69 if not frame then
arkanes@10 70 frame = self:ConstructBar(nil, 1, 0, 0)
arkanes@10 71 self.debuffFrames[idx] = frame
arkanes@10 72 end
arkanes@10 73 self:SetBarAppearance(frame, name, icon, stacks, duration, expirationTime)
arkanes@10 74 frame:ClearAllPoints()
arkanes@10 75 -- position it under all the buffs, with a half-bar spacing
arkanes@10 76 frame:SetPoint("TOP", self.anchor, "BOTTOM", 0, (buffCount * -16) - 8)
arkanes@10 77 frame:Show()
arkanes@10 78 buffCount = buffCount + 1
arkanes@10 79 else
arkanes@10 80 -- not dirty, so no frame means we're done
arkanes@10 81 if not frame then break end
arkanes@6 82 end
arkanes@10 83 self:UpdateBarExpirationTime(frame)
arkanes@4 84 end
arkanes@6 85 self.dirty = nil
arkanes@4 86 end
arkanes@4 87
arkanes@0 88 function kbf:UNIT_AURA(event, unit)
arkanes@2 89 if unit ~= self.secureFrame:GetAttribute("unit") then return end
arkanes@6 90 self.dirty = true
arkanes@0 91 end
arkanes@0 92
arkanes@10 93 function kbf:UpdateBarExpirationTime(frame)
arkanes@10 94 if frame.expirationTime then
arkanes@10 95 local remaining = frame.expirationTime - GetTime()
arkanes@10 96 remaining = math.max(0, remaining)
arkanes@10 97 local perc = remaining / frame.duration
arkanes@10 98 frame.timertext:SetText(self:FormatTimeText(remaining))
arkanes@10 99 frame.statusbar:SetValue(remaining)
arkanes@10 100 end
arkanes@10 101 end
arkanes@10 102
arkanes@3 103 function kbf:BindBarToBuff(parentFrame, unit)
arkanes@3 104 local index = parentFrame:GetAttribute("index")
arkanes@3 105 local filter = parentFrame:GetAttribute("filter")
arkanes@10 106 local name, rank, icon, stacks, debuffType, duration, expirationTime,
arkanes@3 107 unitCaster, isStealable, shouldConsolidate, spellId = UnitAura(unit, index, filter)
arkanes@10 108 if not name then return end
arkanes@2 109 if not parentFrame.icon then
arkanes@2 110 self:ConstructBar(parentFrame)
arkanes@0 111 end
arkanes@10 112 self:SetBarAppearance(parentFrame, name, icon, stacks, duration, expirationTime)
arkanes@10 113 end
arkanes@10 114
arkanes@10 115 function kbf:SetBarAppearance(parentFrame, name, icon, stacks, duration, expirationTime)
arkanes@2 116 parentFrame.icon:SetNormalTexture(icon)
arkanes@0 117 if stacks and stacks > 0 then
arkanes@2 118 parentFrame.text:SetText(string.format("%s(%d)", name, stacks))
arkanes@0 119 else
arkanes@2 120 parentFrame.text:SetText(name)
arkanes@0 121 end
arkanes@4 122 parentFrame.timertext:SetText(self:FormatTimeText(duration))
arkanes@4 123 -- store duration information
arkanes@6 124 if duration and duration > 0 then
arkanes@4 125 parentFrame.expirationTime = expirationTime
arkanes@4 126 parentFrame.duration = duration
arkanes@4 127 parentFrame.statusbar:SetMinMaxValues(0, duration)
arkanes@4 128 else
arkanes@4 129 parentFrame.expirationTime = nil
arkanes@4 130 parentFrame.duration = 0
arkanes@4 131 parentFrame.statusbar:SetMinMaxValues(0,1)
arkanes@4 132 parentFrame.statusbar:SetValue(1)
arkanes@4 133 end
arkanes@4 134 end
arkanes@4 135
arkanes@4 136 function kbf:FormatTimeText(time)
arkanes@4 137 if not time or time == 0 then return "" end
arkanes@4 138 local timetext
arkanes@4 139 local h = floor(time/3600)
arkanes@4 140 local m = time - (h*3600)
arkanes@4 141 m = floor(m/60)
arkanes@4 142 local s = time - ((h*3600) + (m*60))
arkanes@4 143 if h > 0 then
arkanes@4 144 timetext = ("%d:%02d"):format(h, m)
arkanes@4 145 elseif m > 0 then
arkanes@4 146 timetext = string.format("%d:%02d", m, floor(s))
arkanes@4 147 elseif s < 10 then
arkanes@4 148 timetext = string.format("%1.1f", s)
arkanes@4 149 else
arkanes@4 150 timetext = string.format("%.0f", floor(s))
arkanes@4 151 end
arkanes@4 152 return timetext
arkanes@0 153 end
arkanes@0 154
arkanes@1 155 -- creates a icon + statusbar bar
arkanes@10 156 function kbf:ConstructBar(frame, r, g, b)
arkanes@0 157 local texture = "Interface\\TargetingFrame\\UI-StatusBar"
arkanes@1 158 -- Because of secureframe suckiness, these height & width numbers
arkanes@1 159 -- have to be consistent with the stuff in KBF.xml
arkanes@0 160 local height = 16
arkanes@1 161 local width = 200 -- this is the width *without* the icon
arkanes@0 162 local font, _ style = GameFontHighlight:GetFont()
arkanes@10 163 local r = r or 0
arkanes@10 164 local g = g or 1
arkanes@10 165 local b = b or 0
arkanes@10 166 local bgcolor = {r, g, b, 0.5}
arkanes@10 167 local color = {r, g, b, 1}
arkanes@0 168 local fontsize = 11
arkanes@0 169 local timertextwidth = fontsize * 3.6
arkanes@0 170 local textcolor = {1, 1, 1, 1}
arkanes@0 171 local timertextcolor = {1, 1, 1, 1}
arkanes@10 172 if not frame then
arkanes@10 173 frame = CreateFrame("Button", nil, UIParent) -- the "top level" frame that represents the bar as a whole
arkanes@10 174 frame:SetHeight(16)
arkanes@10 175 frame:SetWidth(200 + 16)
arkanes@10 176 end
arkanes@1 177 local bar = frame
arkanes@0 178 bar.icon = CreateFrame("Button", nil, bar) -- the icon
arkanes@0 179 bar.statusbarbg = CreateFrame("StatusBar", nil, bar) -- the bars background
arkanes@0 180 bar.statusbar = CreateFrame("StatusBar", nil, bar) -- and the bars foreground
arkanes@0 181 bar.text = bar.statusbar:CreateFontString(nil, "OVERLAY") -- the label text
arkanes@0 182 bar.timertext = bar.statusbar:CreateFontString(nil, "OVERLAY") -- and the timer text
arkanes@0 183
arkanes@0 184 -- the icon
arkanes@0 185 bar.icon:ClearAllPoints()
arkanes@0 186 bar.icon:SetPoint("LEFT", bar, "LEFT", 0, 0)
arkanes@0 187 -- icons are square
arkanes@0 188 bar.icon:SetWidth(height)
arkanes@0 189 bar.icon:SetHeight(height)
arkanes@2 190 --bar.icon:EnableMouse(false)
arkanes@0 191 -- the status bar background & foreground
arkanes@0 192 local function setupStatusBar(sb, color)
arkanes@0 193 sb:ClearAllPoints()
arkanes@0 194 sb:SetHeight(height)
arkanes@0 195 sb:SetWidth(width)
arkanes@0 196 -- offset the height of the frame on the x-axis for the icon.
arkanes@0 197 sb:SetPoint("TOPLEFT", bar, "TOPLEFT", height, 0)
arkanes@0 198 sb:SetStatusBarTexture(texture)
arkanes@0 199 sb:GetStatusBarTexture():SetVertTile(false)
arkanes@0 200 sb:GetStatusBarTexture():SetHorizTile(false)
arkanes@0 201 sb:SetStatusBarColor(unpack(color))
arkanes@0 202 sb:SetMinMaxValues(0,1)
arkanes@0 203 sb:SetValue(1)
arkanes@0 204 end
arkanes@0 205 setupStatusBar(bar.statusbarbg, bgcolor)
arkanes@0 206 setupStatusBar(bar.statusbar, color)
arkanes@0 207 bar.statusbarbg:SetFrameLevel(bar.statusbarbg:GetFrameLevel()-1) -- make sure the bg frame stays in the back
arkanes@0 208 -- timer text
arkanes@0 209 bar.timertext:SetFontObject(GameFontHighlight)
arkanes@0 210 bar.timertext:SetFont(GameFontHighlight:GetFont())
arkanes@0 211 bar.timertext:SetHeight(height)
arkanes@0 212 bar.timertext:SetWidth(timertextwidth)
arkanes@0 213 bar.timertext:SetPoint("LEFT", bar.statusbar, "LEFT", 0, 0)
arkanes@0 214 bar.timertext:SetJustifyH("RIGHT")
arkanes@0 215 bar.timertext:SetText("time")
arkanes@0 216 bar.timertext:SetTextColor(timertextcolor[1], timertextcolor[2], timertextcolor[3], timertextcolor[4])
arkanes@0 217
arkanes@0 218 -- and the label text
arkanes@0 219 bar.text:SetFontObject(GameFontHighlight)
arkanes@0 220 bar.text:SetFont(GameFontHighlight:GetFont())
arkanes@0 221 bar.text:SetHeight(height)
arkanes@0 222 bar.text:SetWidth((width - timertextwidth) *.9)
arkanes@0 223 bar.text:SetPoint("RIGHT", bar.statusbar, "RIGHT", 0, 0)
arkanes@0 224 bar.text:SetJustifyH("LEFT")
arkanes@0 225 bar.text:SetText("text")
arkanes@0 226 bar.text:SetTextColor(textcolor[1], textcolor[2], textcolor[3], textcolor[4])
arkanes@0 227
arkanes@0 228 bar:EnableMouse(true)
arkanes@0 229 return bar
arkanes@0 230 end
arkanes@0 231
arkanes@0 232 function kbf:CreateAnchorFrame()
arkanes@0 233 -- give it a name so it'll remember its position
arkanes@0 234 local anchor = CreateFrame("FRAME", "KBFAnchorFrame", UIParent)
arkanes@0 235 anchor:SetClampedToScreen(true)
arkanes@0 236 anchor:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",
arkanes@0 237 edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
arkanes@0 238 tile = true, tileSize = 16, edgeSize = 16,
arkanes@0 239 insets = { left = 4, right = 4, top = 4, bottom = 4 },
arkanes@0 240 })
arkanes@0 241 local text = anchor:CreateFontString(nil, "OVERLAY") -- the label text
arkanes@0 242 text:SetFontObject(GameFontHighlight)
arkanes@0 243 text:SetFont(GameFontHighlight:GetFont())
arkanes@0 244 text:SetPoint("TOPLEFT", anchor, "TOPLEFT", 0, 0)
arkanes@0 245 text:SetPoint("BOTTOMRIGHT", anchor, "BOTTOMRIGHT", 0, 0)
arkanes@0 246 text:SetText("KBF ANCHOR")
arkanes@10 247 anchor:SetWidth(200 + 16 + 8)
arkanes@10 248 anchor:SetHeight(16 + 8)
arkanes@0 249 -- movability
arkanes@0 250 anchor:EnableMouse(true)
arkanes@0 251 anchor:SetMovable(true)
arkanes@0 252 anchor:RegisterForDrag("LeftButton")
arkanes@0 253 anchor:SetScript("OnDragStart", anchor.StartMoving)
arkanes@0 254 anchor:SetScript("OnDragStop", anchor.StopMovingOrSizing)
arkanes@0 255 anchor:ClearAllPoints()
arkanes@0 256 anchor:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
arkanes@0 257 anchor:Hide()
arkanes@0 258
arkanes@0 259 local frame = CreateFrame("FRAME", "KBFBuffFrame", anchor, "SecureAuraHeaderTemplate")
arkanes@0 260 --local frame = anchor
arkanes@0 261 frame:SetAttribute("filter", "HELPFUL")
arkanes@0 262 frame:SetAttribute("template", "KBFSecureUnitAuraTemplate")
arkanes@3 263 frame:SetAttribute("point", "TOP")
arkanes@0 264 frame:SetAttribute("wrapAfter", 100) -- required due to bugs in secure header
arkanes@0 265 frame:SetAttribute("consolidateTo", nil)
arkanes@2 266 frame:SetAttribute("xOffset", 0)
arkanes@3 267 frame:SetAttribute("yOffset", -16)
arkanes@3 268 frame:SetAttribute("minWidth", 216)
arkanes@3 269 frame:SetAttribute("minHeight", 16)
arkanes@2 270 frame:SetAttribute("unit", "player") -- TODO: figure out the vehicle swapping stuff
arkanes@3 271 frame:SetAttribute("sortMethod", "NAME")
arkanes@3 272 frame:SetAttribute("sortOrder", "-")
arkanes@10 273 -- TODO: SecureAuraHeader doesn't correcltly implement the temp enchants
arkanes@10 274 --frame:SetAttribute("weaponTemplate", "KBFSecureUnitAuraTemplate")
arkanes@10 275 --frame:SetAttribute("includeWeapons", 1)
arkanes@3 276 frame:SetPoint("TOP", anchor, "BOTTOM", 0, 0)
arkanes@5 277 frame:Show() -- has to be shown, otherwise the child frames don't show
arkanes@0 278 self.secureFrame = frame
arkanes@0 279 return anchor
arkanes@0 280 end