View Single Post
11-28-16, 03:33 AM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Re: Grouping bars

Just handle it like any other variable-length collection of objects or values.

Creating a bar group:
Code:
local barGroup = CreateFrame("Frame", nil, UIParent)
-- add title bar, backdrop, etc. here
barGroup.bars = {}
Adding a bar to the group:
Code:
local bar = LibStub("LibCandyBar-3.0"):New(texture, 100, 16)
bar:SetDuration(90)
bar:SetLabel("Something")
bar:Start()

bar:SetParent(barGroup)
table.insert(barGroup.bars, bar)
MyAddon:ArrangeBars()
Code:
function MyAddon:ArrangeBars()
    for i = 1, #barGroup.bars do
        local bar = barGroup.bars[i]
        bar:SetPoint("LEFT", 1, 0)
        bar:SetPoint("RIGHT", -1, 0)
        if i > 1 then
            bar:SetPoint("TOP", barGroup.bars[i - 1], "BOTTOM", 0, -1)
        else
            bar:SetPoint("TOP", barGroup, 0, -1)
        end
    end
end
Removing references to the bar when it ends:
Code:
LibStub("LibCandyBar-3.0"):RegisterCallback(MyAddon, "LibCandyBar_Stop")

function MyAddon:LibCandyBar_Stop(_, bar)
    for i = 1, #barGroup.bars do
        if barGroup.bars[i] == bar then
            table.remove(barGroup.bars, i)
            break
        end
    end
    self:ArrangeBars()
end
Please note that I have never used LibCandyBar-3.0, so all of the above is based on a quick reading of the API documentation you linked, and a few assumptions since that documentation is somewhat lacking.

Re: Animation

What are you trying to animate? Any half-decent timer bar library should already handle animating the bar to show time elapsed/remaining. The animations you see on Blizzard bars like your health bar would not make sense on a timer bar, since the value of a timer bar changes very frequently in very small increments -- there are no sudden large changes you want to smooth out or call attention to (eg. by flashing).

Re: Sorting

Just sort the table that contains a list of active bars before you position them:

Code:
local function sortBarsByRemainingTime(a, b)
    return a.remaining < b.remaining
end

function MyAddon:ArrangeBars()
    table.sort(barGroup.bars, sortBarsByRemainingTime)
    for i = 1, #barGroup.bars do
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote