annotate modules/Bag.lua @ 177:5c01c0d3bf79

updated LibShowGrid reference
author Flick <flickerstreak@gmail.com>
date Wed, 20 Oct 2010 18:30:24 +0000
parents df68b5a40490
children e63aefb8a555
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.db = ReAction.db:RegisterNamespace( moduleID,
flickerstreak@146 24 {
flickerstreak@146 25 profile = {
flickerstreak@146 26 buttons = { }
flickerstreak@146 27 }
flickerstreak@146 28 }
flickerstreak@146 29 )
flickerstreak@146 30
flickerstreak@146 31 self.buttons = { }
flickerstreak@146 32
flickerstreak@146 33 ReAction.RegisterCallback(self, "OnCreateBar", "OnRefreshBar")
flickerstreak@146 34 ReAction.RegisterCallback(self, "OnDestroyBar")
flickerstreak@146 35 ReAction.RegisterCallback(self, "OnRefreshBar")
flickerstreak@146 36 ReAction.RegisterCallback(self, "OnEraseBar")
flickerstreak@146 37 ReAction.RegisterCallback(self, "OnRenameBar")
flickerstreak@146 38 end
flickerstreak@146 39
flickerstreak@146 40 function module:OnEnable()
flickerstreak@146 41 ReAction:RegisterBarType(L["Bag Bar"],
flickerstreak@146 42 {
flickerstreak@146 43 type = moduleID ,
flickerstreak@146 44 defaultButtonSize = 30,
flickerstreak@146 45 defaultBarRows = 1,
flickerstreak@146 46 defaultBarCols = 6,
flickerstreak@146 47 defaultBarSpacing = 4
flickerstreak@146 48 })
flickerstreak@146 49 end
flickerstreak@146 50
flickerstreak@146 51 function module:OnDisable()
flickerstreak@146 52 ReAction:UnregisterBarType(L["Bag Bar"])
flickerstreak@146 53 end
flickerstreak@146 54
flickerstreak@146 55 function module:OnDestroyBar(event, bar, name)
flickerstreak@146 56 local btns = self.buttons[bar]
flickerstreak@146 57 if btns then
flickerstreak@146 58 for _,b in pairs(btns) do
flickerstreak@146 59 if b then
flickerstreak@146 60 b:Destroy()
flickerstreak@146 61 end
flickerstreak@146 62 end
flickerstreak@146 63 self.buttons[bar] = nil
flickerstreak@146 64 end
flickerstreak@146 65 end
flickerstreak@146 66
flickerstreak@146 67 function module:OnRefreshBar(event, bar, name)
flickerstreak@146 68 if bar.config.type == moduleID then
flickerstreak@146 69 local btns = self.buttons[bar]
flickerstreak@146 70 if btns == nil then
flickerstreak@146 71 btns = { }
flickerstreak@146 72 self.buttons[bar] = btns
flickerstreak@146 73 end
flickerstreak@146 74 local profile = self.db.profile
flickerstreak@146 75 if profile.buttons[name] == nil then
flickerstreak@146 76 profile.buttons[name] = {}
flickerstreak@146 77 end
flickerstreak@146 78 local btnCfg = profile.buttons[name]
flickerstreak@146 79
flickerstreak@146 80 local r, c = bar:GetButtonGrid()
flickerstreak@146 81 local n = r*c
flickerstreak@146 82 for i = 1, n do
flickerstreak@146 83 if btnCfg[i] == nil then
flickerstreak@146 84 btnCfg[i] = {}
flickerstreak@146 85 end
flickerstreak@146 86 if btns[i] == nil then
flickerstreak@146 87 local success, r = pcall(Button.New,Button,i,profile,bar,i>1 and btnCfg[i-1].bagID)
flickerstreak@146 88 if success and r then
flickerstreak@146 89 btns[i] = r
flickerstreak@146 90 bar:AddButton(i,r)
flickerstreak@146 91 else
flickerstreak@146 92 n = i - 1
flickerstreak@146 93 bar:ClipNButtons(n)
flickerstreak@146 94 break
flickerstreak@146 95 end
flickerstreak@146 96 end
flickerstreak@146 97 btns[i]:Refresh()
flickerstreak@146 98 end
flickerstreak@146 99 for i = n+1, #btns do
flickerstreak@146 100 if btns[i] then
flickerstreak@146 101 bar:RemoveButton(btns[i])
flickerstreak@146 102 btns[i] = btns[i]:Destroy()
flickerstreak@146 103 if btnCfg[i] then
flickerstreak@146 104 btnCfg[i] = nil
flickerstreak@146 105 end
flickerstreak@146 106 end
flickerstreak@146 107 end
flickerstreak@146 108 end
flickerstreak@146 109
flickerstreak@146 110 end
flickerstreak@146 111
flickerstreak@146 112 function module:OnEraseBar(event, bar, name)
flickerstreak@146 113 self.db.profile.buttons[name] = nil
flickerstreak@146 114 end
flickerstreak@146 115
flickerstreak@146 116 function module:OnRenameBar(event, bar, oldName, newName)
flickerstreak@146 117 local b = self.db.profile.buttons
flickerstreak@146 118 b[newname], b[oldname] = b[oldname], nil
flickerstreak@146 119 end
flickerstreak@146 120
flickerstreak@146 121
flickerstreak@146 122 -- hook some functions to propagate to our bag buttons
flickerstreak@146 123 hooksecurefunc("Disable_BagButtons",
flickerstreak@146 124 function()
flickerstreak@146 125 for _, buttons in pairs(module.buttons) do
flickerstreak@146 126 for _, b in pairs(buttons) do
flickerstreak@146 127 local f = b:GetFrame()
flickerstreak@146 128 f:Disable()
flickerstreak@146 129 SetDesaturation(b.frames.icon,1)
flickerstreak@146 130 end
flickerstreak@146 131 end
flickerstreak@146 132 end)
flickerstreak@146 133
flickerstreak@146 134 hooksecurefunc("Enable_BagButtons",
flickerstreak@146 135 function()
flickerstreak@146 136 for _, buttons in pairs(module.buttons) do
flickerstreak@146 137 for _, b in pairs(buttons) do
flickerstreak@146 138 local f = b:GetFrame()
flickerstreak@146 139 f:Enable()
flickerstreak@146 140 SetDesaturation(b.frames.icon,nil)
flickerstreak@146 141 end
flickerstreak@146 142 end
flickerstreak@146 143 end)
flickerstreak@146 144
flickerstreak@146 145 hooksecurefunc("ContainerFrame_OnHide",
flickerstreak@146 146 function()
flickerstreak@146 147 for _, buttons in pairs(module.buttons) do
flickerstreak@146 148 for _, b in pairs(buttons) do
flickerstreak@146 149 b:Update()
flickerstreak@146 150 end
flickerstreak@146 151 end
flickerstreak@146 152 end)
flickerstreak@146 153
flickerstreak@146 154 hooksecurefunc("ContainerFrame_OnShow",
flickerstreak@146 155 function()
flickerstreak@146 156 for _, buttons in pairs(module.buttons) do
flickerstreak@146 157 for _, b in pairs(buttons) do
flickerstreak@146 158 b:Update()
flickerstreak@146 159 end
flickerstreak@146 160 end
flickerstreak@146 161 end)