flickerstreak@146: --[[ flickerstreak@146: ReAction Bag button module flickerstreak@146: flickerstreak@146: --]] flickerstreak@146: flickerstreak@146: -- local imports flickerstreak@175: local addonName, addonTable = ... flickerstreak@175: local ReAction = addonTable.ReAction flickerstreak@146: local L = ReAction.L flickerstreak@146: local _G = _G flickerstreak@146: flickerstreak@146: -- Bag button flickerstreak@146: local Button = ReAction.Button.Bag flickerstreak@146: flickerstreak@146: -- module declaration flickerstreak@146: local moduleID = "Bag" flickerstreak@146: local module = ReAction:NewModule( moduleID flickerstreak@146: -- mixins go here flickerstreak@146: ) flickerstreak@146: flickerstreak@146: -- handlers flickerstreak@146: function module:OnInitialize() flickerstreak@146: self.db = ReAction.db:RegisterNamespace( moduleID, flickerstreak@146: { flickerstreak@146: profile = { flickerstreak@146: buttons = { } flickerstreak@146: } flickerstreak@146: } flickerstreak@146: ) flickerstreak@146: flickerstreak@146: self.buttons = { } flickerstreak@146: flickerstreak@146: ReAction.RegisterCallback(self, "OnCreateBar", "OnRefreshBar") flickerstreak@146: ReAction.RegisterCallback(self, "OnDestroyBar") flickerstreak@146: ReAction.RegisterCallback(self, "OnRefreshBar") flickerstreak@146: ReAction.RegisterCallback(self, "OnEraseBar") flickerstreak@146: ReAction.RegisterCallback(self, "OnRenameBar") flickerstreak@146: end flickerstreak@146: flickerstreak@146: function module:OnEnable() flickerstreak@146: ReAction:RegisterBarType(L["Bag Bar"], flickerstreak@146: { flickerstreak@146: type = moduleID , flickerstreak@146: defaultButtonSize = 30, flickerstreak@146: defaultBarRows = 1, flickerstreak@146: defaultBarCols = 6, flickerstreak@146: defaultBarSpacing = 4 flickerstreak@146: }) flickerstreak@146: end flickerstreak@146: flickerstreak@146: function module:OnDisable() flickerstreak@146: ReAction:UnregisterBarType(L["Bag Bar"]) flickerstreak@146: end flickerstreak@146: flickerstreak@146: function module:OnDestroyBar(event, bar, name) flickerstreak@146: local btns = self.buttons[bar] flickerstreak@146: if btns then flickerstreak@146: for _,b in pairs(btns) do flickerstreak@146: if b then flickerstreak@146: b:Destroy() flickerstreak@146: end flickerstreak@146: end flickerstreak@146: self.buttons[bar] = nil flickerstreak@146: end flickerstreak@146: end flickerstreak@146: flickerstreak@146: function module:OnRefreshBar(event, bar, name) flickerstreak@146: if bar.config.type == moduleID then flickerstreak@146: local btns = self.buttons[bar] flickerstreak@146: if btns == nil then flickerstreak@146: btns = { } flickerstreak@146: self.buttons[bar] = btns flickerstreak@146: end flickerstreak@146: local profile = self.db.profile flickerstreak@146: if profile.buttons[name] == nil then flickerstreak@146: profile.buttons[name] = {} flickerstreak@146: end flickerstreak@146: local btnCfg = profile.buttons[name] flickerstreak@146: flickerstreak@146: local r, c = bar:GetButtonGrid() flickerstreak@146: local n = r*c flickerstreak@146: for i = 1, n do flickerstreak@146: if btnCfg[i] == nil then flickerstreak@146: btnCfg[i] = {} flickerstreak@146: end flickerstreak@146: if btns[i] == nil then flickerstreak@146: local success, r = pcall(Button.New,Button,i,profile,bar,i>1 and btnCfg[i-1].bagID) flickerstreak@146: if success and r then flickerstreak@146: btns[i] = r flickerstreak@146: bar:AddButton(i,r) flickerstreak@146: else flickerstreak@146: n = i - 1 flickerstreak@146: bar:ClipNButtons(n) flickerstreak@146: break flickerstreak@146: end flickerstreak@146: end flickerstreak@146: btns[i]:Refresh() flickerstreak@146: end flickerstreak@146: for i = n+1, #btns do flickerstreak@146: if btns[i] then flickerstreak@146: bar:RemoveButton(btns[i]) flickerstreak@146: btns[i] = btns[i]:Destroy() flickerstreak@146: if btnCfg[i] then flickerstreak@146: btnCfg[i] = nil flickerstreak@146: end flickerstreak@146: end flickerstreak@146: end flickerstreak@146: end flickerstreak@146: flickerstreak@146: end flickerstreak@146: flickerstreak@146: function module:OnEraseBar(event, bar, name) flickerstreak@146: self.db.profile.buttons[name] = nil flickerstreak@146: end flickerstreak@146: flickerstreak@146: function module:OnRenameBar(event, bar, oldName, newName) flickerstreak@146: local b = self.db.profile.buttons flickerstreak@146: b[newname], b[oldname] = b[oldname], nil flickerstreak@146: end flickerstreak@146: flickerstreak@146: flickerstreak@146: -- hook some functions to propagate to our bag buttons flickerstreak@146: hooksecurefunc("Disable_BagButtons", flickerstreak@146: function() flickerstreak@146: for _, buttons in pairs(module.buttons) do flickerstreak@146: for _, b in pairs(buttons) do flickerstreak@146: local f = b:GetFrame() flickerstreak@146: f:Disable() flickerstreak@146: SetDesaturation(b.frames.icon,1) flickerstreak@146: end flickerstreak@146: end flickerstreak@146: end) flickerstreak@146: flickerstreak@146: hooksecurefunc("Enable_BagButtons", flickerstreak@146: function() flickerstreak@146: for _, buttons in pairs(module.buttons) do flickerstreak@146: for _, b in pairs(buttons) do flickerstreak@146: local f = b:GetFrame() flickerstreak@146: f:Enable() flickerstreak@146: SetDesaturation(b.frames.icon,nil) flickerstreak@146: end flickerstreak@146: end flickerstreak@146: end) flickerstreak@146: flickerstreak@146: hooksecurefunc("ContainerFrame_OnHide", flickerstreak@146: function() flickerstreak@146: for _, buttons in pairs(module.buttons) do flickerstreak@146: for _, b in pairs(buttons) do flickerstreak@146: b:Update() flickerstreak@146: end flickerstreak@146: end flickerstreak@146: end) flickerstreak@146: flickerstreak@146: hooksecurefunc("ContainerFrame_OnShow", flickerstreak@146: function() flickerstreak@146: for _, buttons in pairs(module.buttons) do flickerstreak@146: for _, b in pairs(buttons) do flickerstreak@146: b:Update() flickerstreak@146: end flickerstreak@146: end flickerstreak@146: end)