annotate modules/Bag.lua @ 146:86564b5cbbff

Added bag button support
author Flick <flickerstreak@gmail.com>
date Thu, 30 Apr 2009 16:51:43 +0000
parents
children 901c91dc1bf2
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@146 7 local ReAction = ReAction
flickerstreak@146 8 local L = ReAction.L
flickerstreak@146 9 local _G = _G
flickerstreak@146 10
flickerstreak@146 11 -- Bag button
flickerstreak@146 12 local Button = ReAction.Button.Bag
flickerstreak@146 13
flickerstreak@146 14 -- module declaration
flickerstreak@146 15 local moduleID = "Bag"
flickerstreak@146 16 local module = ReAction:NewModule( moduleID
flickerstreak@146 17 -- mixins go here
flickerstreak@146 18 )
flickerstreak@146 19
flickerstreak@146 20 -- libraries
flickerstreak@146 21 local KB = LibStub("LibKeyBound-1.0")
flickerstreak@146 22
flickerstreak@146 23 -- handlers
flickerstreak@146 24 function module:OnInitialize()
flickerstreak@146 25 self.db = ReAction.db:RegisterNamespace( moduleID,
flickerstreak@146 26 {
flickerstreak@146 27 profile = {
flickerstreak@146 28 buttons = { }
flickerstreak@146 29 }
flickerstreak@146 30 }
flickerstreak@146 31 )
flickerstreak@146 32
flickerstreak@146 33 self.buttons = { }
flickerstreak@146 34
flickerstreak@146 35 ReAction.RegisterCallback(self, "OnCreateBar", "OnRefreshBar")
flickerstreak@146 36 ReAction.RegisterCallback(self, "OnDestroyBar")
flickerstreak@146 37 ReAction.RegisterCallback(self, "OnRefreshBar")
flickerstreak@146 38 ReAction.RegisterCallback(self, "OnEraseBar")
flickerstreak@146 39 ReAction.RegisterCallback(self, "OnRenameBar")
flickerstreak@146 40 ReAction.RegisterCallback(self, "OnConfigModeChanged")
flickerstreak@146 41
flickerstreak@146 42 KB.RegisterCallback(self, "LIBKEYBOUND_ENABLED")
flickerstreak@146 43 KB.RegisterCallback(self, "LIBKEYBOUND_DISABLED")
flickerstreak@146 44 KB.RegisterCallback(self, "LIBKEYBOUND_MODE_COLOR_CHANGED","LIBKEYBOUND_ENABLED")
flickerstreak@146 45 end
flickerstreak@146 46
flickerstreak@146 47 function module:OnEnable()
flickerstreak@146 48 ReAction:RegisterBarType(L["Bag Bar"],
flickerstreak@146 49 {
flickerstreak@146 50 type = moduleID ,
flickerstreak@146 51 defaultButtonSize = 30,
flickerstreak@146 52 defaultBarRows = 1,
flickerstreak@146 53 defaultBarCols = 6,
flickerstreak@146 54 defaultBarSpacing = 4
flickerstreak@146 55 })
flickerstreak@146 56 end
flickerstreak@146 57
flickerstreak@146 58 function module:OnDisable()
flickerstreak@146 59 ReAction:UnregisterBarType(L["Bag Bar"])
flickerstreak@146 60 end
flickerstreak@146 61
flickerstreak@146 62 function module:OnDestroyBar(event, bar, name)
flickerstreak@146 63 local btns = self.buttons[bar]
flickerstreak@146 64 if btns then
flickerstreak@146 65 for _,b in pairs(btns) do
flickerstreak@146 66 if b then
flickerstreak@146 67 b:Destroy()
flickerstreak@146 68 end
flickerstreak@146 69 end
flickerstreak@146 70 self.buttons[bar] = nil
flickerstreak@146 71 end
flickerstreak@146 72 end
flickerstreak@146 73
flickerstreak@146 74 function module:OnRefreshBar(event, bar, name)
flickerstreak@146 75 if bar.config.type == moduleID then
flickerstreak@146 76 local btns = self.buttons[bar]
flickerstreak@146 77 if btns == nil then
flickerstreak@146 78 btns = { }
flickerstreak@146 79 self.buttons[bar] = btns
flickerstreak@146 80 end
flickerstreak@146 81 local profile = self.db.profile
flickerstreak@146 82 if profile.buttons[name] == nil then
flickerstreak@146 83 profile.buttons[name] = {}
flickerstreak@146 84 end
flickerstreak@146 85 local btnCfg = profile.buttons[name]
flickerstreak@146 86
flickerstreak@146 87 local r, c = bar:GetButtonGrid()
flickerstreak@146 88 local n = r*c
flickerstreak@146 89 for i = 1, n do
flickerstreak@146 90 if btnCfg[i] == nil then
flickerstreak@146 91 btnCfg[i] = {}
flickerstreak@146 92 end
flickerstreak@146 93 if btns[i] == nil then
flickerstreak@146 94 local success, r = pcall(Button.New,Button,i,profile,bar,i>1 and btnCfg[i-1].bagID)
flickerstreak@146 95 if success and r then
flickerstreak@146 96 btns[i] = r
flickerstreak@146 97 bar:AddButton(i,r)
flickerstreak@146 98 else
flickerstreak@146 99 n = i - 1
flickerstreak@146 100 bar:ClipNButtons(n)
flickerstreak@146 101 break
flickerstreak@146 102 end
flickerstreak@146 103 end
flickerstreak@146 104 btns[i]:Refresh()
flickerstreak@146 105 end
flickerstreak@146 106 for i = n+1, #btns do
flickerstreak@146 107 if btns[i] then
flickerstreak@146 108 bar:RemoveButton(btns[i])
flickerstreak@146 109 btns[i] = btns[i]:Destroy()
flickerstreak@146 110 if btnCfg[i] then
flickerstreak@146 111 btnCfg[i] = nil
flickerstreak@146 112 end
flickerstreak@146 113 end
flickerstreak@146 114 end
flickerstreak@146 115 end
flickerstreak@146 116
flickerstreak@146 117 end
flickerstreak@146 118
flickerstreak@146 119 function module:OnEraseBar(event, bar, name)
flickerstreak@146 120 self.db.profile.buttons[name] = nil
flickerstreak@146 121 end
flickerstreak@146 122
flickerstreak@146 123 function module:OnRenameBar(event, bar, oldName, newName)
flickerstreak@146 124 local b = self.db.profile.buttons
flickerstreak@146 125 b[newname], b[oldname] = b[oldname], nil
flickerstreak@146 126 end
flickerstreak@146 127
flickerstreak@146 128 function module:OnConfigModeChanged(event, mode)
flickerstreak@146 129 for bar in pairs(self.buttons) do
flickerstreak@146 130 for b in bar:IterateButtons() do
flickerstreak@146 131 b:UpdateActionIDLabel(mode)
flickerstreak@146 132 end
flickerstreak@146 133 end
flickerstreak@146 134 end
flickerstreak@146 135
flickerstreak@146 136 function module:LIBKEYBOUND_ENABLED(evt)
flickerstreak@146 137 for _, buttons in pairs(self.buttons) do
flickerstreak@146 138 for _, b in pairs(buttons) do
flickerstreak@146 139 b:SetKeybindMode(true)
flickerstreak@146 140 end
flickerstreak@146 141 end
flickerstreak@146 142 end
flickerstreak@146 143
flickerstreak@146 144 function module:LIBKEYBOUND_DISABLED(evt)
flickerstreak@146 145 for _, buttons in pairs(self.buttons) do
flickerstreak@146 146 for _, b in pairs(buttons) do
flickerstreak@146 147 b:SetKeybindMode(false)
flickerstreak@146 148 end
flickerstreak@146 149 end
flickerstreak@146 150 end
flickerstreak@146 151
flickerstreak@146 152 function module:RefreshAll()
flickerstreak@146 153 for bar in pairs(self.buttons) do
flickerstreak@146 154 self:OnRefreshBar(nil,bar,bar:GetName())
flickerstreak@146 155 end
flickerstreak@146 156 end
flickerstreak@146 157
flickerstreak@146 158
flickerstreak@146 159 -- hook some functions to propagate to our bag buttons
flickerstreak@146 160 hooksecurefunc("Disable_BagButtons",
flickerstreak@146 161 function()
flickerstreak@146 162 for _, buttons in pairs(module.buttons) do
flickerstreak@146 163 for _, b in pairs(buttons) do
flickerstreak@146 164 local f = b:GetFrame()
flickerstreak@146 165 f:Disable()
flickerstreak@146 166 SetDesaturation(b.frames.icon,1)
flickerstreak@146 167 end
flickerstreak@146 168 end
flickerstreak@146 169 end)
flickerstreak@146 170
flickerstreak@146 171 hooksecurefunc("Enable_BagButtons",
flickerstreak@146 172 function()
flickerstreak@146 173 for _, buttons in pairs(module.buttons) do
flickerstreak@146 174 for _, b in pairs(buttons) do
flickerstreak@146 175 local f = b:GetFrame()
flickerstreak@146 176 f:Enable()
flickerstreak@146 177 SetDesaturation(b.frames.icon,nil)
flickerstreak@146 178 end
flickerstreak@146 179 end
flickerstreak@146 180 end)
flickerstreak@146 181
flickerstreak@146 182 hooksecurefunc("ContainerFrame_OnHide",
flickerstreak@146 183 function()
flickerstreak@146 184 for _, buttons in pairs(module.buttons) do
flickerstreak@146 185 for _, b in pairs(buttons) do
flickerstreak@146 186 b:Update()
flickerstreak@146 187 end
flickerstreak@146 188 end
flickerstreak@146 189 end)
flickerstreak@146 190
flickerstreak@146 191 hooksecurefunc("ContainerFrame_OnShow",
flickerstreak@146 192 function()
flickerstreak@146 193 for _, buttons in pairs(module.buttons) do
flickerstreak@146 194 for _, b in pairs(buttons) do
flickerstreak@146 195 b:Update()
flickerstreak@146 196 end
flickerstreak@146 197 end
flickerstreak@146 198 end)