View Single Post
11-30-16, 05:44 AM   #9
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Okay, I have turned Phanx's code into a library that is a wrapper (zing! ) for LibCandyBar-3.0. I will test it after some sleep.

LibCandyStore-1.0 adds three functions to LibCandyBar:
  • CreateBarGroup
  • AddBarToGroup
  • RemoveBarFromGroup
I think I can safely remove lines 36, 37, 85 and 86 but first testing. The code is fully documented, but if there are issues (probably) or questions, please let me know.
Lua Code:
  1. --[[
  2.     Whom is doing what with this library
  3.     $Date: $
  4.     $Author: $
  5.     $URL: $
  6.     $Id: $
  7.     @class file
  8.     @name LibCandyStore-1.0.lua
  9. ]]--
  10.  
  11. -- register with LibStub ------------------------------------------------------
  12. local lib = LibStub:NewLibrary("LibCandyStore-1.0", "$Revision: 1 $")
  13. if not lib then
  14.     return -- no upgrade necessary
  15. end
  16.  
  17. -- embed library into addons --------------------------------------------------
  18. lib.embeds = lib.embeds or {}
  19. local mixins = {
  20.     "CreateBarGroup",
  21.     "AddBarToGroup",
  22.     "RemoveBarFromGroup"
  23. }
  24. function lib:Embed(target)
  25.     for k, v in pairs(mixins) do
  26.         target[v] = self[v]
  27.     end
  28.     self.embeds[target] = true
  29.     return target
  30. end
  31.  
  32. -- register with LibCandyBar-3.0 ----------------------------------------------
  33. LibStub("LibCandyBar-3.0"):RegisterCallback(lib, "LibCandyBar_Stop", lib.BarStopped)
  34.  
  35. -- local variables ------------------------------------------------------------
  36. lib.barGroups = {}
  37. local barGroups = lib.barGroups -- less typing
  38.  
  39. local default_backdrop = {
  40.     bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
  41.     edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
  42.     inset = 4,
  43.     edgeSize = 8,
  44.     tile = true,
  45.     insets = {left = 2, right = 2, top = 2, bottom = 2}
  46. }
  47.  
  48. -- APIs -----------------------------------------------------------------------
  49.  
  50. --- Creates a bar group with its parameters
  51. -- @name //MyAddOn//:CreateBarGroup
  52. -- @paramsig backdrop, width, height, [name, point, x, y]
  53. -- @param name The global string name of the group frame
  54. -- Like CreateFrame, name can be nil, and unique if it is not
  55. -- @param backdrop Table used in SetBackdrop
  56. -- Default table uses UI-Tooltip data
  57. -- @param width Number of pixels wide for the bar group
  58. -- Defaults to 100
  59. -- @param height Number of pixels high for the bar group
  60. -- Defaults to 160
  61. -- @param point Where in UIParent do you want the frame
  62. -- ("CENTER", "TOPLEFT", "BOTTOM", etc). Optional, defaults to "CENTER"
  63. -- @param x The number of pixels for horizontal offset from point
  64. -- Optional, defaults to 0
  65. -- @param y The number of pixels for vertical offset from point
  66. -- Optional, defaults to 0
  67. -- @return barGroup Your new bar group frame
  68. function lib:CreateBarGroup(backdrop, width, height, name, point, x, y)
  69.     name = name and string.trim(name) or name
  70.     backdrop = backdrop or default_backdrop
  71.     width = width or 100
  72.     height = height or 160
  73.     point = point or "CENTER"
  74.     x = x or 0
  75.     y = y or 0
  76.  
  77.     local barGroup = CreateFrame("Frame", name, UIParent)
  78.     barGroup:SetClampedToScreen(true)
  79.     barGroup:SetSize(width, height)
  80.     barGroup:SetPoint(point)
  81.  
  82.     barGroup:SetBackdrop(backdrop)
  83.     barGroup.name = name
  84.     barGroup.bars = {}
  85.     barGroup:Show()
  86.     table.insert(barGroups, barGroup)
  87.     return barGroup
  88. end
  89.  
  90. --- Moves a LibCandyBar-3.0 bar to a bar group
  91. -- @name //MyAddOn//:AddBarToGroup
  92. -- @param bar The bar created by LCB-3
  93. -- @param barGroup The return value from MyAddOn:CreateBarGroup()
  94. -- @usage local bar = LibStub("LibCandyBar-3.0"):New(texture, 100, 16)
  95. -- bar:SetDuration(90)
  96. -- bar:SetLabel("Something")
  97. -- bar:Start()
  98. -- MyAddOn:AddBarToGroup(bar, someBarGroup)
  99. function lib:AddBarToGroup(bar, barGroup)
  100.     -- might need to remove bar
  101.     -- get its data first
  102.     bar.oldParent = bar:GetParent()
  103.     bar.oldPoint, bar.oldRelTo, bar.oldRelPoint, bar.oldxOffset, bar.oldyOffset = bar:GetPoint()
  104.  
  105.     bar:SetParent(barGroup)
  106.     table.insert(barGroup.bars, bar)
  107.     self:ArrangeBars(barGroup.bars)
  108. end
  109.  
  110. --- Removes a bar from a group
  111. -- Then restores its parent and SetPoint
  112. -- @name //MyAddOn//:RemoveBarFromGroup
  113. -- @param bar The bar to be removed
  114. -- @param barGroup From which to remove the bar
  115. function lib:RemoveBarFromGroup(bar, barGroup)
  116.     bar:SetParent(bar.oldParent)
  117.     bar:SetPoint(bar.oldPoint, bar.oldRelTo, bar.oldRelPoint, bar.oldxOffset, bar.oldyOffset)
  118.     table.remove(barGroup.bars, bar)
  119.     self:ArrangeBars(barGroup.bars)
  120. end
  121.  
  122. -- internal use bar sorting ---------------------------------------------------
  123. local function SortBarsByRemainingTime(a, b)
  124.     return a.remaining < b.remaining
  125. end
  126.  
  127. function lib:ArrangeBars(barGroup)
  128.     table.sort(barGroup.bars, SortBarsByRemainingTime)
  129.     for i = 1, #barGroup.bars do
  130.         local bar = barGroup.bars[i]
  131.         bar:SetPoint("LEFT", 1, 0)
  132.         bar:SetPoint("RIGHT", -1, 0)
  133.         if i > 1 then
  134.             bar:SetPoint("TOP", barGroup.bars[i - 1], "BOTTOM", 0, -1)
  135.         else
  136.             bar:SetPoint("TOP", barGroup, 0, -1)
  137.         end
  138.     end
  139. end
  140.  
  141. -- handle LibCandyBar callback ------------------------------------------------
  142. function lib:BarStopped(_, bar, barGroup)
  143.     for i = 1, #barGroup.bars do
  144.         if barGroup.bars[i] == bar then
  145.             table.remove(barGroup.bars, i)
  146.             break
  147.         end
  148.     end
  149.     self:ArrangeBars(barGroup.bars)
  150. end
  151.  
  152. -- EoF embed cleanup ----------------------------------------------------------
  153. for AddOn in pairs(lib.embeds) do
  154.     lib:Embed(AddOn)
  155. end
  Reply With Quote