| 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@28 | 16 -- module declaration | 
| flickerstreak@28 | 17 local moduleID = "PetAction" | 
| flickerstreak@28 | 18 local module = ReAction:NewModule( moduleID ) | 
| flickerstreak@28 | 19 | 
| flickerstreak@130 | 20 -- Button class | 
| flickerstreak@130 | 21 local Button = ReAction.Button.PetAction | 
| flickerstreak@77 | 22 | 
| flickerstreak@102 | 23 -- private | 
| flickerstreak@102 | 24 local function UpdateButtonLock(bar) | 
| flickerstreak@102 | 25   local f = bar:GetFrame() | 
| flickerstreak@102 | 26   f:SetAttribute("lockbuttons",bar.config.lockButtons) | 
| flickerstreak@102 | 27   f:SetAttribute("lockbuttonscombat",bar.config.lockButtonsCombat) | 
| flickerstreak@102 | 28   f:Execute( | 
| flickerstreak@102 | 29     [[ | 
| flickerstreak@102 | 30       lockButtons = self:GetAttribute("lockbuttons") | 
| flickerstreak@102 | 31       lockButtonsCombat = self:GetAttribute("lockbuttonscombat") | 
| flickerstreak@102 | 32     ]]) | 
| flickerstreak@102 | 33 end | 
| flickerstreak@102 | 34 | 
| flickerstreak@28 | 35 -- module methods | 
| flickerstreak@28 | 36 function module:OnInitialize() | 
| flickerstreak@53 | 37   self.db = ReAction.db:RegisterNamespace( moduleID, | 
| flickerstreak@28 | 38     { | 
| flickerstreak@28 | 39       profile = { | 
| flickerstreak@28 | 40         buttons = { } | 
| flickerstreak@28 | 41       } | 
| flickerstreak@28 | 42     } | 
| flickerstreak@28 | 43   ) | 
| flickerstreak@53 | 44   self.buttons = { } | 
| flickerstreak@63 | 45 | 
| flickerstreak@63 | 46   ReAction:RegisterBarOptionGenerator(self, "GetBarOptions") | 
| flickerstreak@63 | 47 | 
| flickerstreak@63 | 48   ReAction.RegisterCallback(self, "OnCreateBar") | 
| flickerstreak@63 | 49   ReAction.RegisterCallback(self, "OnDestroyBar") | 
| flickerstreak@63 | 50   ReAction.RegisterCallback(self, "OnRefreshBar") | 
| flickerstreak@63 | 51   ReAction.RegisterCallback(self, "OnEraseBar") | 
| flickerstreak@63 | 52   ReAction.RegisterCallback(self, "OnRenameBar") | 
| flickerstreak@28 | 53 end | 
| flickerstreak@28 | 54 | 
| flickerstreak@28 | 55 function module:OnEnable() | 
| flickerstreak@53 | 56   ReAction:RegisterBarType(L["Pet Action Bar"], | 
| flickerstreak@53 | 57     { | 
| flickerstreak@53 | 58       type = moduleID , | 
| flickerstreak@53 | 59       defaultButtonSize = 30, | 
| flickerstreak@53 | 60       defaultBarRows = 1, | 
| flickerstreak@53 | 61       defaultBarCols = 10, | 
| flickerstreak@53 | 62       defaultBarSpacing = 8 | 
| flickerstreak@53 | 63     }) | 
| flickerstreak@28 | 64 end | 
| flickerstreak@28 | 65 | 
| flickerstreak@28 | 66 function module:OnDisable() | 
| flickerstreak@53 | 67   ReAction:UnregisterBarType(L["Pet Action Bar"]) | 
| flickerstreak@28 | 68 end | 
| flickerstreak@28 | 69 | 
| flickerstreak@63 | 70 function module:OnCreateBar(event, bar, name) | 
| flickerstreak@53 | 71   if bar.config.type == moduleID then | 
| flickerstreak@53 | 72     -- auto show/hide when pet exists | 
| flickerstreak@148 | 73     bar:RegisterUnitWatch("pet",true) | 
| flickerstreak@63 | 74     self:OnRefreshBar(event, bar, name) | 
| flickerstreak@28 | 75   end | 
| flickerstreak@28 | 76 end | 
| flickerstreak@28 | 77 | 
| flickerstreak@63 | 78 function module:OnRefreshBar(event, bar, name) | 
| flickerstreak@53 | 79   if bar.config.type == moduleID then | 
| flickerstreak@53 | 80     if self.buttons[bar] == nil then | 
| flickerstreak@53 | 81       self.buttons[bar] = { } | 
| flickerstreak@53 | 82     end | 
| flickerstreak@53 | 83     local btns = self.buttons[bar] | 
| flickerstreak@53 | 84     local profile = self.db.profile | 
| flickerstreak@63 | 85     if profile.buttons[name] == nil then | 
| flickerstreak@63 | 86       profile.buttons[name] = {} | 
| flickerstreak@53 | 87     end | 
| flickerstreak@63 | 88     local btnCfg = profile.buttons[name] | 
| flickerstreak@53 | 89 | 
| flickerstreak@53 | 90     local r, c = bar:GetButtonGrid() | 
| flickerstreak@53 | 91     local n = r*c | 
| flickerstreak@53 | 92     for i = 1, n do | 
| flickerstreak@53 | 93       if btnCfg[i] == nil then | 
| flickerstreak@53 | 94         btnCfg[i] = {} | 
| flickerstreak@53 | 95       end | 
| flickerstreak@53 | 96       if btns[i] == nil then | 
| flickerstreak@130 | 97         local success, r = pcall(Button.New,Button,i,btnCfg[i],bar,i>1 and btnCfg[i-1].actionID) | 
| flickerstreak@94 | 98         if success and r then | 
| flickerstreak@94 | 99           btns[i] = r | 
| flickerstreak@94 | 100           bar:AddButton(i,r) | 
| flickerstreak@94 | 101         else | 
| flickerstreak@94 | 102           n = i - 1 | 
| flickerstreak@94 | 103           bar:ClipNButtons(n) | 
| flickerstreak@94 | 104           break | 
| flickerstreak@94 | 105         end | 
| flickerstreak@53 | 106       end | 
| flickerstreak@77 | 107       btns[i]:Refresh() | 
| flickerstreak@53 | 108     end | 
| flickerstreak@53 | 109     for i = n+1, #btns do | 
| flickerstreak@53 | 110       if btns[i] then | 
| flickerstreak@77 | 111         bar:RemoveButton(btns[i]) | 
| flickerstreak@53 | 112         btns[i] = btns[i]:Destroy() | 
| flickerstreak@53 | 113         if btnCfg[i] then | 
| flickerstreak@53 | 114           btnCfg[i] = nil | 
| flickerstreak@53 | 115         end | 
| flickerstreak@53 | 116       end | 
| flickerstreak@53 | 117     end | 
| flickerstreak@102 | 118     UpdateButtonLock(bar) | 
| flickerstreak@53 | 119   end | 
| flickerstreak@53 | 120 end | 
| flickerstreak@53 | 121 | 
| flickerstreak@63 | 122 function module:OnDestroyBar(event, bar, name) | 
| flickerstreak@53 | 123   if self.buttons[bar] then | 
| flickerstreak@53 | 124     local btns = self.buttons[bar] | 
| flickerstreak@53 | 125     for _,b in pairs(btns) do | 
| flickerstreak@53 | 126       if b then | 
| flickerstreak@53 | 127         b:Destroy() | 
| flickerstreak@53 | 128       end | 
| flickerstreak@53 | 129     end | 
| flickerstreak@53 | 130     self.buttons[bar] = nil | 
| flickerstreak@53 | 131   end | 
| flickerstreak@53 | 132 end | 
| flickerstreak@53 | 133 | 
| flickerstreak@63 | 134 function module:OnEraseBar(event, bar, name) | 
| flickerstreak@63 | 135   self.db.profile.buttons[name] = nil | 
| flickerstreak@53 | 136 end | 
| flickerstreak@53 | 137 | 
| flickerstreak@63 | 138 function module:OnRenameBar(event, bar, oldname, newname) | 
| flickerstreak@53 | 139   local b = self.db.profile.buttons | 
| flickerstreak@53 | 140   b[newname], b[oldname] = b[oldname], nil | 
| flickerstreak@53 | 141 end | 
| flickerstreak@53 | 142 | 
| flickerstreak@53 | 143 | 
| flickerstreak@60 | 144 ---- Options ---- | 
| flickerstreak@102 | 145 local Handler = { } | 
| flickerstreak@102 | 146 local meta = { __index = Handler } | 
| flickerstreak@102 | 147 | 
| flickerstreak@102 | 148 function Handler:New(bar) | 
| flickerstreak@102 | 149   return setmetatable( | 
| flickerstreak@102 | 150     { | 
| flickerstreak@102 | 151       bar = bar, | 
| flickerstreak@102 | 152       config = bar.config | 
| flickerstreak@102 | 153     }, meta) | 
| flickerstreak@102 | 154 end | 
| flickerstreak@102 | 155 | 
| flickerstreak@102 | 156 function Handler:GetLockButtons() | 
| flickerstreak@130 | 157   return self.config.lockButtons | 
| flickerstreak@102 | 158 end | 
| flickerstreak@102 | 159 | 
| flickerstreak@102 | 160 function Handler:SetLockButtons(info, value) | 
| flickerstreak@102 | 161   self.config.lockButtons = value | 
| flickerstreak@102 | 162   UpdateButtonLock(self.bar) | 
| flickerstreak@102 | 163 end | 
| flickerstreak@102 | 164 | 
| flickerstreak@102 | 165 function Handler:GetLockButtonsCombat() | 
| flickerstreak@102 | 166   return self.config.lockButtonsCombat | 
| flickerstreak@102 | 167 end | 
| flickerstreak@102 | 168 | 
| flickerstreak@102 | 169 function Handler:SetLockButtonsCombat(info, value) | 
| flickerstreak@102 | 170   self.config.lockButtonsCombat = value | 
| flickerstreak@102 | 171   UpdateButtonLock(self.bar) | 
| flickerstreak@102 | 172 end | 
| flickerstreak@102 | 173 | 
| flickerstreak@102 | 174 function Handler:LockButtonsCombatDisabled() | 
| flickerstreak@130 | 175   return not self.config.lockButtons | 
| flickerstreak@102 | 176 end | 
| flickerstreak@102 | 177 | 
| flickerstreak@102 | 178 | 
| flickerstreak@60 | 179 function module:GetBarOptions(bar) | 
| flickerstreak@91 | 180   if bar.config.type == moduleID then | 
| flickerstreak@91 | 181     return { | 
| flickerstreak@91 | 182       type = "group", | 
| flickerstreak@91 | 183       name = L["Pet Buttons"], | 
| flickerstreak@102 | 184       handler = Handler:New(bar), | 
| flickerstreak@91 | 185       args = { | 
| flickerstreak@102 | 186         lockButtons = { | 
| flickerstreak@102 | 187           name = L["Lock Buttons"], | 
| flickerstreak@147 | 188           desc = L["Prevents picking up/dragging actions (use SHIFT to override this behavior)"], | 
| flickerstreak@102 | 189           order = 2, | 
| flickerstreak@102 | 190           type = "toggle", | 
| flickerstreak@102 | 191           get = "GetLockButtons", | 
| flickerstreak@102 | 192           set = "SetLockButtons", | 
| flickerstreak@102 | 193         }, | 
| flickerstreak@102 | 194         lockOnlyCombat = { | 
| flickerstreak@102 | 195           name = L["Only in Combat"], | 
| flickerstreak@102 | 196           desc = L["Only lock the buttons when in combat"], | 
| flickerstreak@102 | 197           order = 3, | 
| flickerstreak@102 | 198           type = "toggle", | 
| flickerstreak@102 | 199           disabled = "LockButtonsCombatDisabled", | 
| flickerstreak@102 | 200           get = "GetLockButtonsCombat", | 
| flickerstreak@102 | 201           set = "SetLockButtonsCombat", | 
| flickerstreak@102 | 202         }, | 
| flickerstreak@91 | 203       } | 
| flickerstreak@60 | 204     } | 
| flickerstreak@91 | 205   end | 
| flickerstreak@59 | 206 end | 
| flickerstreak@59 | 207 | 
| flickerstreak@53 | 208 |