annotate KBF.lua @ 19:a6f5a0f2d429

correctly position debuffs when temp enchants are present
author Chris Mellon <arkanes@gmai.com>
date Sat, 16 Oct 2010 00:56:40 -0500
parents 27aa0d9ffe43
children 54e30adde56b
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:RegisterEvent("UNIT_AURA")
arkanes@4 12 -- set up the countdown timer
arkanes@4 13 -- TODO: Fancy enable/disable based on whether you have any timed buffs.
arkanes@4 14 -- Not a big deal, how often do you care about that
arkanes@8 15 -- also TODO: Maybe should bucket OnUpdates somehow
arkanes@18 16 -- AceTimer repeating events can only happen at 0.1 seconds, which is probably
arkanes@18 17 -- fast enough for updating, but makes the animation look jerky
arkanes@18 18 self.update = CreateFrame("FRAME")
arkanes@18 19 self.update:SetScript("OnUpdate", function() self:OnUpdate() end)
arkanes@7 20 self.dirty = true -- force an immediate scan on login
arkanes@14 21 self:HideBlizzardBuffFrames()
arkanes@18 22 self:RegisterChatCommand("kbf", "ToggleAnchor")
arkanes@0 23 end
arkanes@1 24 -- naming convention
arkanes@17 25 -- a "frame" is the top-level button (secure button from the header, or one I make myself)
arkanes@17 26 -- that will contain the UI information about the buff
arkanes@17 27
arkanes@17 28 -- a "bar" is a frame that has the icon, status bar, ect associated with it
arkanes@0 29
arkanes@8 30 function kbf:HideBlizzardBuffFrames()
arkanes@8 31 local function HideBlizFrame(frame)
arkanes@8 32 if not frame then return end
arkanes@8 33 frame:UnregisterAllEvents()
arkanes@8 34 frame:SetScript("OnUpdate", nil)
arkanes@8 35 frame:Hide()
arkanes@8 36 frame.Show = function() end
arkanes@8 37 end
arkanes@8 38 HideBlizFrame(BuffFrame)
arkanes@8 39 HideBlizFrame(ConsolidatedBuffs)
arkanes@18 40 HideBlizFrame(TemporaryEnchantFrame)
arkanes@8 41
arkanes@8 42 end
arkanes@8 43
arkanes@4 44 function kbf:OnUpdate()
arkanes@10 45 local unit = self.secureFrame:GetAttribute("unit")
arkanes@10 46 local buffCount = 0
arkanes@4 47 for idx=1,99 do
arkanes@4 48 local frame = self.secureFrame:GetAttribute("child"..idx)
arkanes@10 49 if not (frame and frame:IsShown()) then break end
arkanes@10 50 buffCount = buffCount + 1
arkanes@6 51 if self.dirty then
arkanes@10 52 if self:BindBarToBuff(frame, unit) then break end
arkanes@9 53 end
arkanes@10 54 self:UpdateBarExpirationTime(frame)
arkanes@12 55 -- Don't forget to refresh shown tooltips
arkanes@12 56 if ( GameTooltip:IsOwned(frame) ) then
arkanes@15 57 self:OnEnter(frame)
arkanes@12 58 end
arkanes@10 59 end
arkanes@10 60 -- temporary enchants
arkanes@10 61 -- TODO: The blizz secure aura header binds both temp enchants
arkanes@10 62 -- to the main hand. No support for cancelling weapon enchants
arkanes@10 63 -- until this gets fixed up
arkanes@17 64 local tempEnchant = self.secureFrame:GetAttribute("tempEnchant1")
arkanes@17 65 if tempEnchant and tempEnchant:IsShown() then
arkanes@17 66 if self.dirty or true then
arkanes@17 67 self:BindBarToWeaponEnchant(tempEnchant, 16)
arkanes@17 68 end
arkanes@17 69 self:UpdateBarExpirationTime(tempEnchant)
arkanes@19 70 buffCount = buffCount + 1
arkanes@17 71 end
arkanes@17 72 tempEnchant = self.secureFrame:GetAttribute("tempEnchant2")
arkanes@17 73 if tempEnchant and tempEnchant:IsShown() then
arkanes@17 74 if self.dirty or true then
arkanes@17 75 self:BindBarToWeaponEnchant(tempEnchant, 17)
arkanes@17 76 end
arkanes@17 77 self:UpdateBarExpirationTime(tempEnchant)
arkanes@19 78 buffCount = buffCount + 1
arkanes@17 79 end
arkanes@10 80
arkanes@10 81 -- debuffs
arkanes@11 82 -- Since debuffs aren't cancellable, don't need to use the secure header
arkanes@11 83 -- for them. This could be rewritten to support useful features like
arkanes@11 84 -- sorting & scaling and stuff. Honestly, should at least be alphabetical.
arkanes@10 85 for idx=1,99 do
arkanes@10 86 local frame = self.debuffFrames[idx]
arkanes@10 87 if self.dirty then
arkanes@10 88 local name, rank, icon, stacks, debuffType, duration, expirationTime = UnitAura(unit, idx, "HARMFUL")
arkanes@10 89 if not name then
arkanes@10 90 -- out of debuffs, hide all the rest of them
arkanes@10 91 for jdx = idx, 99 do
arkanes@10 92 local bar = self.debuffFrames[jdx]
arkanes@10 93 if bar then bar:Hide() else break end
arkanes@10 94 end
arkanes@10 95 break
arkanes@10 96 end
arkanes@10 97 if not frame then
arkanes@10 98 frame = self:ConstructBar(nil, 1, 0, 0)
arkanes@10 99 self.debuffFrames[idx] = frame
arkanes@10 100 end
arkanes@10 101 self:SetBarAppearance(frame, name, icon, stacks, duration, expirationTime)
arkanes@10 102 frame:ClearAllPoints()
arkanes@10 103 -- position it under all the buffs, with a half-bar spacing
arkanes@19 104 frame:SetPoint("TOP", self.anchor, "BOTTOM", 0, (buffCount * -16))
arkanes@10 105 frame:Show()
arkanes@13 106 frame.filter = "HARMFUL"
arkanes@13 107 frame.unit = unit
arkanes@16 108 frame.index = idx
arkanes@13 109 frame:SetScript("OnEnter", function() kbf:OnEnter(frame) end)
arkanes@13 110 frame:SetScript("OnLeave", function() GameTooltip:Hide() end)
arkanes@10 111 buffCount = buffCount + 1
arkanes@10 112 else
arkanes@10 113 -- not dirty, so no frame means we're done
arkanes@10 114 if not frame then break end
arkanes@6 115 end
arkanes@10 116 self:UpdateBarExpirationTime(frame)
arkanes@13 117 if ( GameTooltip:IsOwned(frame) ) then
arkanes@15 118 self:OnEnter(frame)
arkanes@13 119 end
arkanes@4 120 end
arkanes@6 121 self.dirty = nil
arkanes@4 122 end
arkanes@4 123
arkanes@0 124 function kbf:UNIT_AURA(event, unit)
arkanes@2 125 if unit ~= self.secureFrame:GetAttribute("unit") then return end
arkanes@6 126 self.dirty = true
arkanes@0 127 end
arkanes@0 128
arkanes@10 129 function kbf:UpdateBarExpirationTime(frame)
arkanes@10 130 if frame.expirationTime then
arkanes@10 131 local remaining = frame.expirationTime - GetTime()
arkanes@10 132 remaining = math.max(0, remaining)
arkanes@10 133 local perc = remaining / frame.duration
arkanes@10 134 frame.timertext:SetText(self:FormatTimeText(remaining))
arkanes@10 135 frame.statusbar:SetValue(remaining)
arkanes@10 136 end
arkanes@10 137 end
arkanes@10 138
arkanes@17 139 function kbf:BindBarToWeaponEnchant(parentFrame, slotOverride)
arkanes@17 140 local index = parentFrame:GetAttribute("index")
arkanes@17 141 -- allow passing of explicit slot in order to work around aura header bug
arkanes@17 142 local slot = slotOverride or parentFrame:GetAttribute("target-slot")
arkanes@17 143 local itemIndex = slot - 15 -- 1MH, 2OF
arkanes@17 144 local RETURNS_PER_ITEM = 3
arkanes@17 145 local hasEnchant, remaining, enchantCharges = select(RETURNS_PER_ITEM * (itemIndex - 1) + 1, GetWeaponEnchantInfo())
arkanes@17 146 -- remaining time is in milliseconds
arkanes@17 147 if not hasEnchant then return end
arkanes@17 148 local remaining = remaining / 1000
arkanes@17 149 if not hasEnchant then return end -- this should never happen
arkanes@17 150 local icon = GetInventoryItemTexture("player", slot)
arkanes@17 151 -- this is terrible, but I hate myself and everyone else.
arkanes@17 152 -- We're going to assume that the duration of the temp enchant
arkanes@17 153 -- is either 60 minutes, or however long is left, because poisons are 1 hour
arkanes@17 154 local duration = max((60 * 60), remaining)
arkanes@17 155 local expirationTime = GetTime() + remaining
arkanes@17 156 -- TODO
arkanes@17 157 local name = GetItemInfo(GetInventoryItemID("player", slot))
arkanes@17 158 if not parentFrame.icon then
arkanes@17 159 self:ConstructBar(parentFrame, 1, 0, 1)
arkanes@17 160 end
arkanes@17 161 self:SetBarAppearance(parentFrame, name, icon, enchantCharges, duration, expirationTime)
arkanes@17 162 end
arkanes@17 163
arkanes@3 164 function kbf:BindBarToBuff(parentFrame, unit)
arkanes@3 165 local index = parentFrame:GetAttribute("index")
arkanes@3 166 local filter = parentFrame:GetAttribute("filter")
arkanes@10 167 local name, rank, icon, stacks, debuffType, duration, expirationTime,
arkanes@3 168 unitCaster, isStealable, shouldConsolidate, spellId = UnitAura(unit, index, filter)
arkanes@10 169 if not name then return end
arkanes@2 170 if not parentFrame.icon then
arkanes@2 171 self:ConstructBar(parentFrame)
arkanes@0 172 end
arkanes@10 173 self:SetBarAppearance(parentFrame, name, icon, stacks, duration, expirationTime)
arkanes@10 174 end
arkanes@10 175
arkanes@10 176 function kbf:SetBarAppearance(parentFrame, name, icon, stacks, duration, expirationTime)
arkanes@2 177 parentFrame.icon:SetNormalTexture(icon)
arkanes@0 178 if stacks and stacks > 0 then
arkanes@2 179 parentFrame.text:SetText(string.format("%s(%d)", name, stacks))
arkanes@0 180 else
arkanes@2 181 parentFrame.text:SetText(name)
arkanes@0 182 end
arkanes@4 183 parentFrame.timertext:SetText(self:FormatTimeText(duration))
arkanes@4 184 -- store duration information
arkanes@6 185 if duration and duration > 0 then
arkanes@4 186 parentFrame.expirationTime = expirationTime
arkanes@4 187 parentFrame.duration = duration
arkanes@4 188 parentFrame.statusbar:SetMinMaxValues(0, duration)
arkanes@4 189 else
arkanes@4 190 parentFrame.expirationTime = nil
arkanes@4 191 parentFrame.duration = 0
arkanes@4 192 parentFrame.statusbar:SetMinMaxValues(0,1)
arkanes@4 193 parentFrame.statusbar:SetValue(1)
arkanes@4 194 end
arkanes@4 195 end
arkanes@4 196
arkanes@17 197 -- expects time seconds
arkanes@4 198 function kbf:FormatTimeText(time)
arkanes@4 199 if not time or time == 0 then return "" end
arkanes@4 200 local timetext
arkanes@4 201 local h = floor(time/3600)
arkanes@4 202 local m = time - (h*3600)
arkanes@4 203 m = floor(m/60)
arkanes@4 204 local s = time - ((h*3600) + (m*60))
arkanes@4 205 if h > 0 then
arkanes@4 206 timetext = ("%d:%02d"):format(h, m)
arkanes@4 207 elseif m > 0 then
arkanes@4 208 timetext = string.format("%d:%02d", m, floor(s))
arkanes@4 209 elseif s < 10 then
arkanes@4 210 timetext = string.format("%1.1f", s)
arkanes@4 211 else
arkanes@4 212 timetext = string.format("%.0f", floor(s))
arkanes@4 213 end
arkanes@4 214 return timetext
arkanes@0 215 end
arkanes@0 216
arkanes@12 217 function KBF:OnEnter(button, motion)
arkanes@12 218 -- this is for the secure buttons, so use the attributes
arkanes@12 219 -- I'd like a better place to position this but it's funky for right now, handle it later
arkanes@13 220 local unit = button.unit or button:GetAttribute("unit")
arkanes@13 221 local filter = button.filter or button:GetAttribute("filter")
arkanes@15 222 local index = button:GetAttribute("index") or button.index
arkanes@17 223 if unit and filter and index then
arkanes@17 224 GameTooltip:SetOwner(button, "ANCHOR_BOTTOMLEFT");
arkanes@17 225 GameTooltip:SetFrameLevel(button:GetFrameLevel() + 2);
arkanes@17 226 GameTooltip:SetUnitAura(unit, index, filter);
arkanes@17 227 return
arkanes@17 228 end
arkanes@17 229 local slot = button:GetAttribute("target-slot") -- temp enchant
arkanes@12 230 GameTooltip:SetOwner(button, "ANCHOR_BOTTOMLEFT");
arkanes@13 231 GameTooltip:SetFrameLevel(button:GetFrameLevel() + 2);
arkanes@17 232 GameTooltip:SetInventoryItem("player", slot)
arkanes@12 233 end
arkanes@12 234
arkanes@1 235 -- creates a icon + statusbar bar
arkanes@10 236 function kbf:ConstructBar(frame, r, g, b)
arkanes@0 237 local texture = "Interface\\TargetingFrame\\UI-StatusBar"
arkanes@1 238 -- Because of secureframe suckiness, these height & width numbers
arkanes@1 239 -- have to be consistent with the stuff in KBF.xml
arkanes@0 240 local height = 16
arkanes@1 241 local width = 200 -- this is the width *without* the icon
arkanes@0 242 local font, _ style = GameFontHighlight:GetFont()
arkanes@10 243 local r = r or 0
arkanes@10 244 local g = g or 1
arkanes@10 245 local b = b or 0
arkanes@10 246 local bgcolor = {r, g, b, 0.5}
arkanes@10 247 local color = {r, g, b, 1}
arkanes@0 248 local fontsize = 11
arkanes@0 249 local timertextwidth = fontsize * 3.6
arkanes@0 250 local textcolor = {1, 1, 1, 1}
arkanes@0 251 local timertextcolor = {1, 1, 1, 1}
arkanes@10 252 if not frame then
arkanes@10 253 frame = CreateFrame("Button", nil, UIParent) -- the "top level" frame that represents the bar as a whole
arkanes@10 254 frame:SetHeight(16)
arkanes@10 255 frame:SetWidth(200 + 16)
arkanes@10 256 end
arkanes@1 257 local bar = frame
arkanes@0 258 bar.icon = CreateFrame("Button", nil, bar) -- the icon
arkanes@0 259 bar.statusbarbg = CreateFrame("StatusBar", nil, bar) -- the bars background
arkanes@0 260 bar.statusbar = CreateFrame("StatusBar", nil, bar) -- and the bars foreground
arkanes@0 261 bar.text = bar.statusbar:CreateFontString(nil, "OVERLAY") -- the label text
arkanes@0 262 bar.timertext = bar.statusbar:CreateFontString(nil, "OVERLAY") -- and the timer text
arkanes@0 263
arkanes@0 264 -- the icon
arkanes@0 265 bar.icon:ClearAllPoints()
arkanes@0 266 bar.icon:SetPoint("LEFT", bar, "LEFT", 0, 0)
arkanes@0 267 -- icons are square
arkanes@0 268 bar.icon:SetWidth(height)
arkanes@0 269 bar.icon:SetHeight(height)
arkanes@2 270 --bar.icon:EnableMouse(false)
arkanes@0 271 -- the status bar background & foreground
arkanes@0 272 local function setupStatusBar(sb, color)
arkanes@0 273 sb:ClearAllPoints()
arkanes@0 274 sb:SetHeight(height)
arkanes@0 275 sb:SetWidth(width)
arkanes@0 276 -- offset the height of the frame on the x-axis for the icon.
arkanes@0 277 sb:SetPoint("TOPLEFT", bar, "TOPLEFT", height, 0)
arkanes@0 278 sb:SetStatusBarTexture(texture)
arkanes@0 279 sb:GetStatusBarTexture():SetVertTile(false)
arkanes@0 280 sb:GetStatusBarTexture():SetHorizTile(false)
arkanes@0 281 sb:SetStatusBarColor(unpack(color))
arkanes@0 282 sb:SetMinMaxValues(0,1)
arkanes@0 283 sb:SetValue(1)
arkanes@0 284 end
arkanes@0 285 setupStatusBar(bar.statusbarbg, bgcolor)
arkanes@0 286 setupStatusBar(bar.statusbar, color)
arkanes@0 287 bar.statusbarbg:SetFrameLevel(bar.statusbarbg:GetFrameLevel()-1) -- make sure the bg frame stays in the back
arkanes@0 288 -- timer text
arkanes@0 289 bar.timertext:SetFontObject(GameFontHighlight)
arkanes@0 290 bar.timertext:SetFont(GameFontHighlight:GetFont())
arkanes@0 291 bar.timertext:SetHeight(height)
arkanes@0 292 bar.timertext:SetWidth(timertextwidth)
arkanes@0 293 bar.timertext:SetPoint("LEFT", bar.statusbar, "LEFT", 0, 0)
arkanes@0 294 bar.timertext:SetJustifyH("RIGHT")
arkanes@0 295 bar.timertext:SetText("time")
arkanes@0 296 bar.timertext:SetTextColor(timertextcolor[1], timertextcolor[2], timertextcolor[3], timertextcolor[4])
arkanes@0 297
arkanes@0 298 -- and the label text
arkanes@0 299 bar.text:SetFontObject(GameFontHighlight)
arkanes@0 300 bar.text:SetFont(GameFontHighlight:GetFont())
arkanes@0 301 bar.text:SetHeight(height)
arkanes@0 302 bar.text:SetWidth((width - timertextwidth) *.9)
arkanes@0 303 bar.text:SetPoint("RIGHT", bar.statusbar, "RIGHT", 0, 0)
arkanes@0 304 bar.text:SetJustifyH("LEFT")
arkanes@0 305 bar.text:SetText("text")
arkanes@0 306 bar.text:SetTextColor(textcolor[1], textcolor[2], textcolor[3], textcolor[4])
arkanes@0 307
arkanes@0 308 bar:EnableMouse(true)
arkanes@0 309 return bar
arkanes@0 310 end
arkanes@0 311
arkanes@0 312 function kbf:CreateAnchorFrame()
arkanes@0 313 -- give it a name so it'll remember its position
arkanes@0 314 local anchor = CreateFrame("FRAME", "KBFAnchorFrame", UIParent)
arkanes@0 315 anchor:SetClampedToScreen(true)
arkanes@0 316 anchor:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",
arkanes@0 317 edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
arkanes@18 318 tile = true, tileSize = 16, edgeSize = 12,
arkanes@0 319 insets = { left = 4, right = 4, top = 4, bottom = 4 },
arkanes@0 320 })
arkanes@0 321 local text = anchor:CreateFontString(nil, "OVERLAY") -- the label text
arkanes@0 322 text:SetFontObject(GameFontHighlight)
arkanes@0 323 text:SetFont(GameFontHighlight:GetFont())
arkanes@0 324 text:SetPoint("TOPLEFT", anchor, "TOPLEFT", 0, 0)
arkanes@0 325 text:SetPoint("BOTTOMRIGHT", anchor, "BOTTOMRIGHT", 0, 0)
arkanes@0 326 text:SetText("KBF ANCHOR")
arkanes@18 327 anchor:SetWidth(200 +16)
arkanes@18 328 anchor:SetHeight(16)
arkanes@0 329 -- movability
arkanes@0 330 anchor:EnableMouse(true)
arkanes@0 331 anchor:SetMovable(true)
arkanes@0 332 anchor:RegisterForDrag("LeftButton")
arkanes@0 333 anchor:SetScript("OnDragStart", anchor.StartMoving)
arkanes@0 334 anchor:SetScript("OnDragStop", anchor.StopMovingOrSizing)
arkanes@0 335 anchor:ClearAllPoints()
arkanes@0 336 anchor:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
arkanes@0 337 anchor:Hide()
arkanes@0 338
arkanes@18 339 local frame = CreateFrame("FRAME", "KBFBuffFrame", UIParent, "SecureAuraHeaderTemplate")
arkanes@0 340 --local frame = anchor
arkanes@0 341 frame:SetAttribute("filter", "HELPFUL")
arkanes@0 342 frame:SetAttribute("template", "KBFSecureUnitAuraTemplate")
arkanes@3 343 frame:SetAttribute("point", "TOP")
arkanes@0 344 frame:SetAttribute("wrapAfter", 100) -- required due to bugs in secure header
arkanes@0 345 frame:SetAttribute("consolidateTo", nil)
arkanes@2 346 frame:SetAttribute("xOffset", 0)
arkanes@3 347 frame:SetAttribute("yOffset", -16)
arkanes@3 348 frame:SetAttribute("minWidth", 216)
arkanes@3 349 frame:SetAttribute("minHeight", 16)
arkanes@2 350 frame:SetAttribute("unit", "player") -- TODO: figure out the vehicle swapping stuff
arkanes@3 351 frame:SetAttribute("sortMethod", "NAME")
arkanes@3 352 frame:SetAttribute("sortOrder", "-")
arkanes@10 353 -- TODO: SecureAuraHeader doesn't correcltly implement the temp enchants
arkanes@17 354 frame:SetAttribute("weaponTemplate", "KBFSecureUnitAuraTemplate")
arkanes@17 355 frame:SetAttribute("includeWeapons", 1)
arkanes@18 356 frame:SetPoint("TOP", anchor, "TOP", 0, 0)
arkanes@5 357 frame:Show() -- has to be shown, otherwise the child frames don't show
arkanes@0 358 self.secureFrame = frame
arkanes@0 359 return anchor
arkanes@0 360 end
arkanes@18 361
arkanes@18 362 function kbf:ShowAnchor()
arkanes@18 363 self.secureFrame:ClearAllPoints()
arkanes@18 364 self.secureFrame:SetPoint("TOP", self.anchor, "BOTTOM", 0, 0)
arkanes@18 365 self.anchor:Show()
arkanes@18 366 end
arkanes@18 367
arkanes@18 368 function kbf:HideAnchor()
arkanes@18 369 self.secureFrame:ClearAllPoints()
arkanes@18 370 self.secureFrame:SetPoint("TOP", self.anchor, "TOP", 0, 0)
arkanes@18 371 self.anchor:Hide()
arkanes@18 372 end
arkanes@18 373
arkanes@18 374 function kbf:ToggleAnchor()
arkanes@18 375 if self.anchor:IsShown() then
arkanes@18 376 self:HideAnchor()
arkanes@18 377 else
arkanes@18 378 self:ShowAnchor()
arkanes@18 379 end
arkanes@18 380 end