changeset 4:75634f25a762

correctly update values when timed
author Chris Mellon <arkanes@gmai.com>
date Tue, 12 Oct 2010 17:45:59 -0500
parents bb1143608eb4
children 43340216545c
files KBF.lua
diffstat 1 files changed, 48 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/KBF.lua	Tue Oct 12 17:16:28 2010 -0500
+++ b/KBF.lua	Tue Oct 12 17:45:59 2010 -0500
@@ -14,11 +14,27 @@
     self.buffBars = {}
     self.anchor:Show()
     self:RegisterEvent("UNIT_AURA")
+    -- set up the countdown timer
+    -- TODO: Fancy enable/disable based on whether you have any timed buffs.
+    -- Not a big deal, how often do you care about that
+    self.anchor:SetScript("OnUpdate", function() self:OnUpdate() end)
 end
 -- naming convention
 -- "frame" is the secure aura button created by the group handler
 -- "bar" is the set of icon + status bars that we create to show the buff time
 
+function kbf:OnUpdate()
+    for idx=1,99 do
+        local frame = self.secureFrame:GetAttribute("child"..idx)
+        if not (frame and frame.expirationTime) then break end
+        local remaining = frame.expirationTime - GetTime()
+        remaining = math.max(0, remaining)
+        local perc = remaining / frame.duration
+        frame.timertext:SetText(self:FormatTimeText(remaining))
+        frame.statusbar:SetValue(remaining)
+    end
+end
+
 function kbf:UNIT_AURA(event, unit)
     if unit ~= self.secureFrame:GetAttribute("unit") then return end
     for idx=1,99 do
@@ -33,8 +49,6 @@
     local filter = parentFrame:GetAttribute("filter")
     local name, rank, icon, count, debuffType, duration, expirationTime, 
         unitCaster, isStealable, shouldConsolidate, spellId = UnitAura(unit, index, filter)
--- This shouldn't be neccesary as the bar is parented to the frame, which is
--- hidden by the group handler
     if not parentFrame.icon then
         self:ConstructBar(parentFrame)
     end
@@ -44,6 +58,38 @@
     else
         parentFrame.text:SetText(name)
     end
+    parentFrame.timertext:SetText(self:FormatTimeText(duration))
+    -- store duration information
+    if duration > 0 then
+        parentFrame.expirationTime = expirationTime
+        parentFrame.duration = duration
+        parentFrame.statusbar:SetMinMaxValues(0, duration)
+        parentFrame.statusbar:SetValue(duration)
+    else
+        parentFrame.expirationTime = nil
+        parentFrame.duration = 0
+        parentFrame.statusbar:SetMinMaxValues(0,1)
+        parentFrame.statusbar:SetValue(1)
+    end
+end
+
+function kbf:FormatTimeText(time)
+    if not time or time == 0 then return "" end
+    local timetext
+    local h = floor(time/3600)
+    local m = time - (h*3600)
+    m = floor(m/60)
+    local s = time - ((h*3600) + (m*60))
+    if h > 0 then
+        timetext = ("%d:%02d"):format(h, m)
+    elseif m > 0 then
+        timetext = string.format("%d:%02d", m, floor(s))
+    elseif s < 10 then
+        timetext = string.format("%1.1f", s)
+    else
+        timetext = string.format("%.0f", floor(s))
+    end
+    return timetext
 end
 
 -- creates a icon + statusbar bar