--[[
Whom is doing what with this library
$Date: $
$Author: $
$URL: $
$Id: $
@class file
@name LibCandyStore-1.0.lua
]]--
-- register with LibStub ------------------------------------------------------
local lib = LibStub:NewLibrary("LibCandyStore-1.0", "$Revision: 1 $")
if not lib then
return -- no upgrade necessary
end
-- embed library into addons --------------------------------------------------
lib.embeds = lib.embeds or {}
local mixins = {
"CreateBarGroup",
"AddBarToGroup",
"RemoveBarFromGroup"
}
function lib:Embed(target)
for k, v in pairs(mixins) do
target[v] = self[v]
end
self.embeds[target] = true
return target
end
-- register with LibCandyBar-3.0 ----------------------------------------------
LibStub("LibCandyBar-3.0"):RegisterCallback(lib, "LibCandyBar_Stop", lib.BarStopped)
-- local variables ------------------------------------------------------------
lib.barGroups = {}
local barGroups = lib.barGroups -- less typing
local default_backdrop = {
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
inset = 4,
edgeSize = 8,
tile = true,
insets = {left = 2, right = 2, top = 2, bottom = 2}
}
-- APIs -----------------------------------------------------------------------
--- Creates a bar group with its parameters
-- @name //MyAddOn//:CreateBarGroup
-- @paramsig backdrop, width, height, [name, point, x, y]
-- @param name The global string name of the group frame
-- Like CreateFrame, name can be nil, and unique if it is not
-- @param backdrop Table used in SetBackdrop
-- Default table uses UI-Tooltip data
-- @param width Number of pixels wide for the bar group
-- Defaults to 100
-- @param height Number of pixels high for the bar group
-- Defaults to 160
-- @param point Where in UIParent do you want the frame
-- ("CENTER", "TOPLEFT", "BOTTOM", etc). Optional, defaults to "CENTER"
-- @param x The number of pixels for horizontal offset from point
-- Optional, defaults to 0
-- @param y The number of pixels for vertical offset from point
-- Optional, defaults to 0
-- @return barGroup Your new bar group frame
function lib:CreateBarGroup(backdrop, width, height, name, point, x, y)
name = name and string.trim(name) or name
backdrop = backdrop or default_backdrop
width = width or 100
height = height or 160
point = point or "CENTER"
x = x or 0
y = y or 0
local barGroup = CreateFrame("Frame", name, UIParent)
barGroup:SetClampedToScreen(true)
barGroup:SetSize(width, height)
barGroup:SetPoint(point)
barGroup:SetBackdrop(backdrop)
barGroup.name = name
barGroup.bars = {}
barGroup:Show()
table.insert(barGroups, barGroup)
return barGroup
end
--- Moves a LibCandyBar-3.0 bar to a bar group
-- @name //MyAddOn//:AddBarToGroup
-- @param bar The bar created by LCB-3
-- @param barGroup The return value from MyAddOn:CreateBarGroup()
-- @usage local bar = LibStub("LibCandyBar-3.0"):New(texture, 100, 16)
-- bar:SetDuration(90)
-- bar:SetLabel("Something")
-- bar:Start()
-- MyAddOn:AddBarToGroup(bar, someBarGroup)
function lib:AddBarToGroup(bar, barGroup)
-- might need to remove bar
-- get its data first
bar.oldParent = bar:GetParent()
bar.oldPoint, bar.oldRelTo, bar.oldRelPoint, bar.oldxOffset, bar.oldyOffset = bar:GetPoint()
bar:SetParent(barGroup)
table.insert(barGroup.bars, bar)
self:ArrangeBars(barGroup.bars)
end
--- Removes a bar from a group
-- Then restores its parent and SetPoint
-- @name //MyAddOn//:RemoveBarFromGroup
-- @param bar The bar to be removed
-- @param barGroup From which to remove the bar
function lib:RemoveBarFromGroup(bar, barGroup)
bar:SetParent(bar.oldParent)
bar:SetPoint(bar.oldPoint, bar.oldRelTo, bar.oldRelPoint, bar.oldxOffset, bar.oldyOffset)
table.remove(barGroup.bars, bar)
self:ArrangeBars(barGroup.bars)
end
-- internal use bar sorting ---------------------------------------------------
local function SortBarsByRemainingTime(a, b)
return a.remaining < b.remaining
end
function lib:ArrangeBars(barGroup)
table.sort(barGroup.bars, SortBarsByRemainingTime)
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
-- handle LibCandyBar callback ------------------------------------------------
function lib:BarStopped(_, bar, barGroup)
for i = 1, #barGroup.bars do
if barGroup.bars[i] == bar then
table.remove(barGroup.bars, i)
break
end
end
self:ArrangeBars(barGroup.bars)
end
-- EoF embed cleanup ----------------------------------------------------------
for AddOn in pairs(lib.embeds) do
lib:Embed(AddOn)
end