annotate modules/PetAction.lua @ 147:901c91dc1bf2

Some refactoring of configmode/keybind handlers
author Flick <flickerstreak@gmail.com>
date Thu, 07 May 2009 23:55:00 +0000
parents 6e4a11b9d290
children de1da46dadb3
rev   line source
flickerstreak@28 1 --[[
flickerstreak@53 2 ReAction Pet Action button module
flickerstreak@53 3
flickerstreak@53 4 The button module implements standard action button functionality by wrapping Blizzard's
flickerstreak@77 5 PetActionButton frame and associated functions.
flickerstreak@28 6
flickerstreak@28 7 --]]
flickerstreak@28 8
flickerstreak@28 9 -- local imports
flickerstreak@28 10 local ReAction = ReAction
flickerstreak@28 11 local L = ReAction.L
flickerstreak@28 12 local _G = _G
flickerstreak@53 13 local CreateFrame = CreateFrame
flickerstreak@95 14 local format = string.format
flickerstreak@28 15
flickerstreak@80 16 ReAction:UpdateRevision("$Revision$")
flickerstreak@77 17
flickerstreak@28 18 -- module declaration
flickerstreak@28 19 local moduleID = "PetAction"
flickerstreak@28 20 local module = ReAction:NewModule( moduleID )
flickerstreak@28 21
flickerstreak@130 22 -- Button class
flickerstreak@130 23 local Button = ReAction.Button.PetAction
flickerstreak@77 24
flickerstreak@102 25 -- private
flickerstreak@102 26 local function UpdateButtonLock(bar)
flickerstreak@102 27 local f = bar:GetFrame()
flickerstreak@102 28 f:SetAttribute("lockbuttons",bar.config.lockButtons)
flickerstreak@102 29 f:SetAttribute("lockbuttonscombat",bar.config.lockButtonsCombat)
flickerstreak@102 30 f:Execute(
flickerstreak@102 31 [[
flickerstreak@102 32 lockButtons = self:GetAttribute("lockbuttons")
flickerstreak@102 33 lockButtonsCombat = self:GetAttribute("lockbuttonscombat")
flickerstreak@102 34 ]])
flickerstreak@102 35 end
flickerstreak@102 36
flickerstreak@28 37 -- module methods
flickerstreak@28 38 function module:OnInitialize()
flickerstreak@53 39 self.db = ReAction.db:RegisterNamespace( moduleID,
flickerstreak@28 40 {
flickerstreak@28 41 profile = {
flickerstreak@28 42 buttons = { }
flickerstreak@28 43 }
flickerstreak@28 44 }
flickerstreak@28 45 )
flickerstreak@53 46 self.buttons = { }
flickerstreak@63 47
flickerstreak@63 48 ReAction:RegisterBarOptionGenerator(self, "GetBarOptions")
flickerstreak@63 49
flickerstreak@63 50 ReAction.RegisterCallback(self, "OnCreateBar")
flickerstreak@63 51 ReAction.RegisterCallback(self, "OnDestroyBar")
flickerstreak@63 52 ReAction.RegisterCallback(self, "OnRefreshBar")
flickerstreak@63 53 ReAction.RegisterCallback(self, "OnEraseBar")
flickerstreak@63 54 ReAction.RegisterCallback(self, "OnRenameBar")
flickerstreak@63 55 ReAction.RegisterCallback(self, "OnConfigModeChanged")
flickerstreak@28 56 end
flickerstreak@28 57
flickerstreak@28 58 function module:OnEnable()
flickerstreak@53 59 ReAction:RegisterBarType(L["Pet Action Bar"],
flickerstreak@53 60 {
flickerstreak@53 61 type = moduleID ,
flickerstreak@53 62 defaultButtonSize = 30,
flickerstreak@53 63 defaultBarRows = 1,
flickerstreak@53 64 defaultBarCols = 10,
flickerstreak@53 65 defaultBarSpacing = 8
flickerstreak@53 66 })
flickerstreak@28 67 end
flickerstreak@28 68
flickerstreak@28 69 function module:OnDisable()
flickerstreak@53 70 ReAction:UnregisterBarType(L["Pet Action Bar"])
flickerstreak@28 71 end
flickerstreak@28 72
flickerstreak@63 73 function module:OnCreateBar(event, bar, name)
flickerstreak@53 74 if bar.config.type == moduleID then
flickerstreak@53 75 -- auto show/hide when pet exists
flickerstreak@53 76 bar:GetFrame():SetAttribute("unit","pet")
flickerstreak@90 77 if not ReAction:GetConfigMode() then
flickerstreak@90 78 RegisterUnitWatch(bar:GetFrame())
flickerstreak@90 79 end
flickerstreak@63 80 self:OnRefreshBar(event, bar, name)
flickerstreak@28 81 end
flickerstreak@28 82 end
flickerstreak@28 83
flickerstreak@63 84 function module:OnRefreshBar(event, bar, name)
flickerstreak@53 85 if bar.config.type == moduleID then
flickerstreak@53 86 if self.buttons[bar] == nil then
flickerstreak@53 87 self.buttons[bar] = { }
flickerstreak@53 88 end
flickerstreak@53 89 local btns = self.buttons[bar]
flickerstreak@53 90 local profile = self.db.profile
flickerstreak@63 91 if profile.buttons[name] == nil then
flickerstreak@63 92 profile.buttons[name] = {}
flickerstreak@53 93 end
flickerstreak@63 94 local btnCfg = profile.buttons[name]
flickerstreak@53 95
flickerstreak@53 96 local r, c = bar:GetButtonGrid()
flickerstreak@53 97 local n = r*c
flickerstreak@53 98 for i = 1, n do
flickerstreak@53 99 if btnCfg[i] == nil then
flickerstreak@53 100 btnCfg[i] = {}
flickerstreak@53 101 end
flickerstreak@53 102 if btns[i] == nil then
flickerstreak@130 103 local success, r = pcall(Button.New,Button,i,btnCfg[i],bar,i>1 and btnCfg[i-1].actionID)
flickerstreak@94 104 if success and r then
flickerstreak@94 105 btns[i] = r
flickerstreak@94 106 bar:AddButton(i,r)
flickerstreak@94 107 else
flickerstreak@94 108 n = i - 1
flickerstreak@94 109 bar:ClipNButtons(n)
flickerstreak@94 110 break
flickerstreak@94 111 end
flickerstreak@53 112 end
flickerstreak@77 113 btns[i]:Refresh()
flickerstreak@53 114 end
flickerstreak@53 115 for i = n+1, #btns do
flickerstreak@53 116 if btns[i] then
flickerstreak@77 117 bar:RemoveButton(btns[i])
flickerstreak@53 118 btns[i] = btns[i]:Destroy()
flickerstreak@53 119 if btnCfg[i] then
flickerstreak@53 120 btnCfg[i] = nil
flickerstreak@53 121 end
flickerstreak@53 122 end
flickerstreak@53 123 end
flickerstreak@102 124 UpdateButtonLock(bar)
flickerstreak@53 125 end
flickerstreak@53 126 end
flickerstreak@53 127
flickerstreak@63 128 function module:OnDestroyBar(event, bar, name)
flickerstreak@53 129 if self.buttons[bar] then
flickerstreak@53 130 local btns = self.buttons[bar]
flickerstreak@53 131 for _,b in pairs(btns) do
flickerstreak@53 132 if b then
flickerstreak@53 133 b:Destroy()
flickerstreak@53 134 end
flickerstreak@53 135 end
flickerstreak@53 136 self.buttons[bar] = nil
flickerstreak@53 137 end
flickerstreak@53 138 end
flickerstreak@53 139
flickerstreak@63 140 function module:OnEraseBar(event, bar, name)
flickerstreak@63 141 self.db.profile.buttons[name] = nil
flickerstreak@53 142 end
flickerstreak@53 143
flickerstreak@63 144 function module:OnRenameBar(event, bar, oldname, newname)
flickerstreak@53 145 local b = self.db.profile.buttons
flickerstreak@53 146 b[newname], b[oldname] = b[oldname], nil
flickerstreak@53 147 end
flickerstreak@53 148
flickerstreak@53 149
flickerstreak@63 150 function module:OnConfigModeChanged(event, mode)
flickerstreak@63 151 for _, bar in ReAction:IterateBars() do
flickerstreak@53 152 if bar and self.buttons[bar] then
flickerstreak@130 153 for b in bar:IterateButtons() do
flickerstreak@130 154 b:UpdateActionIDLabel(mode)
flickerstreak@130 155 end
flickerstreak@53 156 local f = bar:GetFrame()
flickerstreak@53 157 if mode then
flickerstreak@53 158 UnregisterUnitWatch(f)
flickerstreak@53 159 f:Show()
flickerstreak@53 160 else
flickerstreak@53 161 RegisterUnitWatch(f)
flickerstreak@53 162 end
flickerstreak@53 163 end
flickerstreak@53 164 end
flickerstreak@53 165 end
flickerstreak@53 166
flickerstreak@60 167 ---- Options ----
flickerstreak@102 168 local Handler = { }
flickerstreak@102 169 local meta = { __index = Handler }
flickerstreak@102 170
flickerstreak@102 171 function Handler:New(bar)
flickerstreak@102 172 return setmetatable(
flickerstreak@102 173 {
flickerstreak@102 174 bar = bar,
flickerstreak@102 175 config = bar.config
flickerstreak@102 176 }, meta)
flickerstreak@102 177 end
flickerstreak@102 178
flickerstreak@102 179 function Handler:GetLockButtons()
flickerstreak@130 180 return self.config.lockButtons
flickerstreak@102 181 end
flickerstreak@102 182
flickerstreak@102 183 function Handler:SetLockButtons(info, value)
flickerstreak@102 184 self.config.lockButtons = value
flickerstreak@102 185 UpdateButtonLock(self.bar)
flickerstreak@102 186 end
flickerstreak@102 187
flickerstreak@102 188 function Handler:GetLockButtonsCombat()
flickerstreak@102 189 return self.config.lockButtonsCombat
flickerstreak@102 190 end
flickerstreak@102 191
flickerstreak@102 192 function Handler:SetLockButtonsCombat(info, value)
flickerstreak@102 193 self.config.lockButtonsCombat = value
flickerstreak@102 194 UpdateButtonLock(self.bar)
flickerstreak@102 195 end
flickerstreak@102 196
flickerstreak@102 197 function Handler:LockButtonsCombatDisabled()
flickerstreak@130 198 return not self.config.lockButtons
flickerstreak@102 199 end
flickerstreak@102 200
flickerstreak@102 201
flickerstreak@60 202 function module:GetBarOptions(bar)
flickerstreak@91 203 if bar.config.type == moduleID then
flickerstreak@91 204 return {
flickerstreak@91 205 type = "group",
flickerstreak@91 206 name = L["Pet Buttons"],
flickerstreak@102 207 handler = Handler:New(bar),
flickerstreak@91 208 args = {
flickerstreak@102 209 lockButtons = {
flickerstreak@102 210 name = L["Lock Buttons"],
flickerstreak@147 211 desc = L["Prevents picking up/dragging actions (use SHIFT to override this behavior)"],
flickerstreak@102 212 order = 2,
flickerstreak@102 213 type = "toggle",
flickerstreak@102 214 get = "GetLockButtons",
flickerstreak@102 215 set = "SetLockButtons",
flickerstreak@102 216 },
flickerstreak@102 217 lockOnlyCombat = {
flickerstreak@102 218 name = L["Only in Combat"],
flickerstreak@102 219 desc = L["Only lock the buttons when in combat"],
flickerstreak@102 220 order = 3,
flickerstreak@102 221 type = "toggle",
flickerstreak@102 222 disabled = "LockButtonsCombatDisabled",
flickerstreak@102 223 get = "GetLockButtonsCombat",
flickerstreak@102 224 set = "SetLockButtonsCombat",
flickerstreak@102 225 },
flickerstreak@91 226 }
flickerstreak@60 227 }
flickerstreak@91 228 end
flickerstreak@59 229 end
flickerstreak@59 230
flickerstreak@53 231