flickerstreak@28: --[[ flickerstreak@53: ReAction Pet Action button module flickerstreak@53: flickerstreak@53: The button module implements standard action button functionality by wrapping Blizzard's flickerstreak@77: PetActionButton frame and associated functions. flickerstreak@28: flickerstreak@28: --]] flickerstreak@28: flickerstreak@28: -- local imports flickerstreak@175: local addonName, addonTable = ... flickerstreak@175: local ReAction = addonTable.ReAction flickerstreak@28: local L = ReAction.L flickerstreak@28: local _G = _G flickerstreak@53: local CreateFrame = CreateFrame flickerstreak@95: local format = string.format flickerstreak@28: flickerstreak@28: -- module declaration flickerstreak@28: local moduleID = "PetAction" flickerstreak@28: local module = ReAction:NewModule( moduleID ) flickerstreak@28: flickerstreak@130: -- Button class flickerstreak@130: local Button = ReAction.Button.PetAction flickerstreak@77: flickerstreak@102: -- private flickerstreak@102: local function UpdateButtonLock(bar) flickerstreak@102: local f = bar:GetFrame() flickerstreak@102: f:SetAttribute("lockbuttons",bar.config.lockButtons) flickerstreak@102: f:SetAttribute("lockbuttonscombat",bar.config.lockButtonsCombat) flickerstreak@102: f:Execute( flickerstreak@102: [[ flickerstreak@102: lockButtons = self:GetAttribute("lockbuttons") flickerstreak@102: lockButtonsCombat = self:GetAttribute("lockbuttonscombat") flickerstreak@102: ]]) flickerstreak@102: end flickerstreak@102: flickerstreak@28: -- module methods flickerstreak@28: function module:OnInitialize() flickerstreak@53: self.db = ReAction.db:RegisterNamespace( moduleID, flickerstreak@28: { flickerstreak@28: profile = { flickerstreak@28: buttons = { } flickerstreak@28: } flickerstreak@28: } flickerstreak@28: ) flickerstreak@53: self.buttons = { } flickerstreak@63: flickerstreak@63: ReAction:RegisterBarOptionGenerator(self, "GetBarOptions") flickerstreak@63: flickerstreak@63: ReAction.RegisterCallback(self, "OnCreateBar") flickerstreak@63: ReAction.RegisterCallback(self, "OnDestroyBar") flickerstreak@63: ReAction.RegisterCallback(self, "OnRefreshBar") flickerstreak@63: ReAction.RegisterCallback(self, "OnEraseBar") flickerstreak@63: ReAction.RegisterCallback(self, "OnRenameBar") flickerstreak@28: end flickerstreak@28: flickerstreak@28: function module:OnEnable() flickerstreak@53: ReAction:RegisterBarType(L["Pet Action Bar"], flickerstreak@53: { flickerstreak@53: type = moduleID , flickerstreak@53: defaultButtonSize = 30, flickerstreak@53: defaultBarRows = 1, flickerstreak@53: defaultBarCols = 10, flickerstreak@53: defaultBarSpacing = 8 flickerstreak@53: }) flickerstreak@28: end flickerstreak@28: flickerstreak@28: function module:OnDisable() flickerstreak@53: ReAction:UnregisterBarType(L["Pet Action Bar"]) flickerstreak@28: end flickerstreak@28: flickerstreak@63: function module:OnCreateBar(event, bar, name) flickerstreak@53: if bar.config.type == moduleID then flickerstreak@53: -- auto show/hide when pet exists flickerstreak@148: bar:RegisterUnitWatch("pet",true) flickerstreak@63: self:OnRefreshBar(event, bar, name) flickerstreak@28: end flickerstreak@28: end flickerstreak@28: flickerstreak@63: function module:OnRefreshBar(event, bar, name) flickerstreak@53: if bar.config.type == moduleID then flickerstreak@53: if self.buttons[bar] == nil then flickerstreak@53: self.buttons[bar] = { } flickerstreak@53: end flickerstreak@53: local btns = self.buttons[bar] flickerstreak@53: local profile = self.db.profile flickerstreak@63: if profile.buttons[name] == nil then flickerstreak@63: profile.buttons[name] = {} flickerstreak@53: end flickerstreak@63: local btnCfg = profile.buttons[name] flickerstreak@53: flickerstreak@53: local r, c = bar:GetButtonGrid() flickerstreak@53: local n = r*c flickerstreak@53: for i = 1, n do flickerstreak@53: if btnCfg[i] == nil then flickerstreak@53: btnCfg[i] = {} flickerstreak@53: end flickerstreak@53: if btns[i] == nil then flickerstreak@130: local success, r = pcall(Button.New,Button,i,btnCfg[i],bar,i>1 and btnCfg[i-1].actionID) flickerstreak@94: if success and r then flickerstreak@94: btns[i] = r flickerstreak@94: bar:AddButton(i,r) flickerstreak@94: else flickerstreak@94: n = i - 1 flickerstreak@94: bar:ClipNButtons(n) flickerstreak@94: break flickerstreak@94: end flickerstreak@53: end flickerstreak@77: btns[i]:Refresh() flickerstreak@53: end flickerstreak@53: for i = n+1, #btns do flickerstreak@53: if btns[i] then flickerstreak@77: bar:RemoveButton(btns[i]) flickerstreak@53: btns[i] = btns[i]:Destroy() flickerstreak@53: if btnCfg[i] then flickerstreak@53: btnCfg[i] = nil flickerstreak@53: end flickerstreak@53: end flickerstreak@53: end flickerstreak@102: UpdateButtonLock(bar) flickerstreak@53: end flickerstreak@53: end flickerstreak@53: flickerstreak@63: function module:OnDestroyBar(event, bar, name) flickerstreak@53: if self.buttons[bar] then flickerstreak@53: local btns = self.buttons[bar] flickerstreak@53: for _,b in pairs(btns) do flickerstreak@53: if b then flickerstreak@53: b:Destroy() flickerstreak@53: end flickerstreak@53: end flickerstreak@53: self.buttons[bar] = nil flickerstreak@53: end flickerstreak@53: end flickerstreak@53: flickerstreak@63: function module:OnEraseBar(event, bar, name) flickerstreak@63: self.db.profile.buttons[name] = nil flickerstreak@53: end flickerstreak@53: flickerstreak@63: function module:OnRenameBar(event, bar, oldname, newname) flickerstreak@53: local b = self.db.profile.buttons flickerstreak@53: b[newname], b[oldname] = b[oldname], nil flickerstreak@53: end flickerstreak@53: flickerstreak@53: flickerstreak@60: ---- Options ---- flickerstreak@102: local Handler = { } flickerstreak@102: local meta = { __index = Handler } flickerstreak@102: flickerstreak@102: function Handler:New(bar) flickerstreak@102: return setmetatable( flickerstreak@102: { flickerstreak@102: bar = bar, flickerstreak@102: config = bar.config flickerstreak@102: }, meta) flickerstreak@102: end flickerstreak@102: flickerstreak@102: function Handler:GetLockButtons() flickerstreak@130: return self.config.lockButtons flickerstreak@102: end flickerstreak@102: flickerstreak@102: function Handler:SetLockButtons(info, value) flickerstreak@102: self.config.lockButtons = value flickerstreak@102: UpdateButtonLock(self.bar) flickerstreak@102: end flickerstreak@102: flickerstreak@102: function Handler:GetLockButtonsCombat() flickerstreak@102: return self.config.lockButtonsCombat flickerstreak@102: end flickerstreak@102: flickerstreak@102: function Handler:SetLockButtonsCombat(info, value) flickerstreak@102: self.config.lockButtonsCombat = value flickerstreak@102: UpdateButtonLock(self.bar) flickerstreak@102: end flickerstreak@102: flickerstreak@102: function Handler:LockButtonsCombatDisabled() flickerstreak@130: return not self.config.lockButtons flickerstreak@102: end flickerstreak@102: flickerstreak@102: flickerstreak@60: function module:GetBarOptions(bar) flickerstreak@91: if bar.config.type == moduleID then flickerstreak@91: return { flickerstreak@91: type = "group", flickerstreak@91: name = L["Pet Buttons"], flickerstreak@102: handler = Handler:New(bar), flickerstreak@91: args = { flickerstreak@102: lockButtons = { flickerstreak@102: name = L["Lock Buttons"], flickerstreak@147: desc = L["Prevents picking up/dragging actions (use SHIFT to override this behavior)"], flickerstreak@102: order = 2, flickerstreak@102: type = "toggle", flickerstreak@102: get = "GetLockButtons", flickerstreak@102: set = "SetLockButtons", flickerstreak@102: }, flickerstreak@102: lockOnlyCombat = { flickerstreak@102: name = L["Only in Combat"], flickerstreak@102: desc = L["Only lock the buttons when in combat"], flickerstreak@102: order = 3, flickerstreak@102: type = "toggle", flickerstreak@102: disabled = "LockButtonsCombatDisabled", flickerstreak@102: get = "GetLockButtonsCombat", flickerstreak@102: set = "SetLockButtonsCombat", flickerstreak@102: }, flickerstreak@91: } flickerstreak@60: } flickerstreak@91: end flickerstreak@59: end flickerstreak@59: flickerstreak@53: