annotate modules/Bag.lua @ 221:bb13624de7e1

un-namespace Bag bar config
author Flick <flickerstreak@gmail.com>
date Sun, 21 Nov 2010 12:51:24 -0800
parents e63aefb8a555
children c4b134512c50
rev   line source
flickerstreak@146 1 --[[
flickerstreak@146 2 ReAction Bag button module
flickerstreak@146 3
flickerstreak@146 4 --]]
flickerstreak@146 5
flickerstreak@146 6 -- local imports
flickerstreak@175 7 local addonName, addonTable = ...
flickerstreak@175 8 local ReAction = addonTable.ReAction
flickerstreak@146 9 local L = ReAction.L
flickerstreak@146 10 local _G = _G
flickerstreak@146 11
flickerstreak@146 12 -- Bag button
flickerstreak@146 13 local Button = ReAction.Button.Bag
flickerstreak@146 14
flickerstreak@146 15 -- module declaration
flickerstreak@146 16 local moduleID = "Bag"
flickerstreak@146 17 local module = ReAction:NewModule( moduleID
flickerstreak@146 18 -- mixins go here
flickerstreak@146 19 )
flickerstreak@146 20
flickerstreak@146 21 -- handlers
flickerstreak@146 22 function module:OnInitialize()
flickerstreak@146 23 self.buttons = { }
flickerstreak@146 24
flickerstreak@146 25 ReAction.RegisterCallback(self, "OnCreateBar", "OnRefreshBar")
flickerstreak@146 26 ReAction.RegisterCallback(self, "OnDestroyBar")
flickerstreak@146 27 ReAction.RegisterCallback(self, "OnRefreshBar")
flickerstreak@146 28 end
flickerstreak@146 29
flickerstreak@146 30 function module:OnEnable()
flickerstreak@218 31 ReAction:RegisterBarType(Button)
flickerstreak@146 32 end
flickerstreak@146 33
flickerstreak@146 34 function module:OnDisable()
flickerstreak@218 35 ReAction:UnregisterBarType(Button)
flickerstreak@146 36 end
flickerstreak@146 37
flickerstreak@146 38 function module:OnDestroyBar(event, bar, name)
flickerstreak@146 39 local btns = self.buttons[bar]
flickerstreak@146 40 if btns then
flickerstreak@146 41 for _,b in pairs(btns) do
flickerstreak@146 42 if b then
flickerstreak@146 43 b:Destroy()
flickerstreak@146 44 end
flickerstreak@146 45 end
flickerstreak@146 46 self.buttons[bar] = nil
flickerstreak@146 47 end
flickerstreak@146 48 end
flickerstreak@146 49
flickerstreak@146 50 function module:OnRefreshBar(event, bar, name)
flickerstreak@221 51 local config = bar:GetConfig()
flickerstreak@221 52 if config.type == moduleID then
flickerstreak@146 53 local btns = self.buttons[bar]
flickerstreak@146 54 if btns == nil then
flickerstreak@146 55 btns = { }
flickerstreak@146 56 self.buttons[bar] = btns
flickerstreak@146 57 end
flickerstreak@221 58 if not config.buttons then
flickerstreak@221 59 config.buttons = { }
flickerstreak@146 60 end
flickerstreak@221 61 local btnCfg = config.buttons
flickerstreak@146 62
flickerstreak@146 63 local r, c = bar:GetButtonGrid()
flickerstreak@146 64 local n = r*c
flickerstreak@146 65 for i = 1, n do
flickerstreak@146 66 if btnCfg[i] == nil then
flickerstreak@146 67 btnCfg[i] = {}
flickerstreak@146 68 end
flickerstreak@146 69 if btns[i] == nil then
flickerstreak@221 70 local success, r = pcall(Button.New,Button,i,btnCfg[i],bar,i>1 and btnCfg[i-1].bagID)
flickerstreak@146 71 if success and r then
flickerstreak@146 72 btns[i] = r
flickerstreak@146 73 bar:AddButton(i,r)
flickerstreak@146 74 else
flickerstreak@146 75 n = i - 1
flickerstreak@146 76 bar:ClipNButtons(n)
flickerstreak@146 77 break
flickerstreak@146 78 end
flickerstreak@146 79 end
flickerstreak@146 80 btns[i]:Refresh()
flickerstreak@146 81 end
flickerstreak@146 82 for i = n+1, #btns do
flickerstreak@146 83 if btns[i] then
flickerstreak@146 84 bar:RemoveButton(btns[i])
flickerstreak@146 85 btns[i] = btns[i]:Destroy()
flickerstreak@146 86 if btnCfg[i] then
flickerstreak@146 87 btnCfg[i] = nil
flickerstreak@146 88 end
flickerstreak@146 89 end
flickerstreak@146 90 end
flickerstreak@146 91 end
flickerstreak@146 92
flickerstreak@146 93 end
flickerstreak@146 94
flickerstreak@146 95
flickerstreak@146 96
flickerstreak@146 97 -- hook some functions to propagate to our bag buttons
flickerstreak@146 98 hooksecurefunc("Disable_BagButtons",
flickerstreak@146 99 function()
flickerstreak@146 100 for _, buttons in pairs(module.buttons) do
flickerstreak@146 101 for _, b in pairs(buttons) do
flickerstreak@146 102 local f = b:GetFrame()
flickerstreak@146 103 f:Disable()
flickerstreak@146 104 SetDesaturation(b.frames.icon,1)
flickerstreak@146 105 end
flickerstreak@146 106 end
flickerstreak@146 107 end)
flickerstreak@146 108
flickerstreak@146 109 hooksecurefunc("Enable_BagButtons",
flickerstreak@146 110 function()
flickerstreak@146 111 for _, buttons in pairs(module.buttons) do
flickerstreak@146 112 for _, b in pairs(buttons) do
flickerstreak@146 113 local f = b:GetFrame()
flickerstreak@146 114 f:Enable()
flickerstreak@146 115 SetDesaturation(b.frames.icon,nil)
flickerstreak@146 116 end
flickerstreak@146 117 end
flickerstreak@146 118 end)
flickerstreak@146 119
flickerstreak@146 120 hooksecurefunc("ContainerFrame_OnHide",
flickerstreak@146 121 function()
flickerstreak@146 122 for _, buttons in pairs(module.buttons) do
flickerstreak@146 123 for _, b in pairs(buttons) do
flickerstreak@146 124 b:Update()
flickerstreak@146 125 end
flickerstreak@146 126 end
flickerstreak@146 127 end)
flickerstreak@146 128
flickerstreak@146 129 hooksecurefunc("ContainerFrame_OnShow",
flickerstreak@146 130 function()
flickerstreak@146 131 for _, buttons in pairs(module.buttons) do
flickerstreak@146 132 for _, b in pairs(buttons) do
flickerstreak@146 133 b:Update()
flickerstreak@146 134 end
flickerstreak@146 135 end
flickerstreak@146 136 end)