flickerstreak@28: --[[ flickerstreak@28: ReAction Pet Action Bar module flickerstreak@28: flickerstreak@28: --]] flickerstreak@28: flickerstreak@28: -- local imports flickerstreak@28: local ReAction = ReAction flickerstreak@28: local L = ReAction.L flickerstreak@28: local _G = _G flickerstreak@28: flickerstreak@28: -- module declaration flickerstreak@28: local moduleID = "PetAction" flickerstreak@28: local module = ReAction:NewModule( moduleID ) flickerstreak@28: flickerstreak@28: flickerstreak@28: -- module methods flickerstreak@28: function module:OnInitialize() flickerstreak@28: self.db = ReAction.db:RegisterNamespace( moduleID, flickerstreak@28: { flickerstreak@28: profile = { flickerstreak@28: buttons = { } flickerstreak@28: } flickerstreak@28: } flickerstreak@28: ) flickerstreak@28: flickerstreak@28: self.buttons = { } flickerstreak@28: end flickerstreak@28: flickerstreak@28: function module:OnEnable() flickerstreak@28: end flickerstreak@28: flickerstreak@28: function module:OnDisable() flickerstreak@28: end flickerstreak@28: flickerstreak@28: function module:GetGlobalBarOptions(opts) flickerstreak@28: if self.globalBarOpts == nil then flickerstreak@28: self.globalBarOpts = { flickerstreak@28: newActionBar = { flickerstreak@28: type = "execute", flickerstreak@28: name = L["New Pet Action Bar"], flickerstreak@28: desc = L["Create a new bar of pet action buttons"], flickerstreak@28: func = function() flickerstreak@28: ReAction:CreateBar() flickerstreak@28: end, flickerstreak@28: disabled = InCombatLockdown, flickerstreak@28: } flickerstreak@28: } flickerstreak@28: end flickerstreak@28: return self.globalBarOpts flickerstreak@28: end flickerstreak@28: flickerstreak@28: flickerstreak@28: -- use-count of action IDs flickerstreak@28: local ActionIDList = setmetatable( {}, { flickerstreak@28: __index = function(self, idx) flickerstreak@28: if idx == nil then flickerstreak@28: for i = 1, 10 do flickerstreak@28: if rawget(self,i) == nil then flickerstreak@28: rawset(self,i,1) flickerstreak@28: return i flickerstreak@28: end flickerstreak@28: end flickerstreak@28: else flickerstreak@28: local c = rawget(self,idx) or 0 flickerstreak@28: rawset(self,idx,c+1) flickerstreak@28: return idx flickerstreak@28: end flickerstreak@28: end, flickerstreak@28: __newindex = function(self,idx,value) flickerstreak@28: if value == nil then flickerstreak@28: value = rawget(self,idx) flickerstreak@28: if value == 1 then flickerstreak@28: value = nil flickerstreak@28: elseif value then flickerstreak@28: value = value - 1 flickerstreak@28: end flickerstreak@28: end flickerstreak@28: rawset(self,idx,value) flickerstreak@28: end flickerstreak@28: }) flickerstreak@28: flickerstreak@28: flickerstreak@28: -- button class methods flickerstreak@28: local Button = { } flickerstreak@28: flickerstreak@28: local function Constructor( self, bar, idx, config ) flickerstreak@28: self.bar, self.idx, self.config = bar, idx, config flickerstreak@28: flickerstreak@28: local barFrame = bar:GetFrame() flickerstreak@28: flickerstreak@28: self.name = config.name or "ReAction_"..bar:GetName().."_Pet_"..idx flickerstreak@28: config.actionID = ActionIDList[config.actionID] -- gets a free one if none configured flickerstreak@28: flickerstreak@28: local f = CreateFrame("CheckButton", self.name, barFrame, "PetActionButtonTemplate") flickerstreak@28: f:SetID(config.actionID) -- PetActionButtonTemplate isn't a proper SecureActionButton (question: is this protected? does it cause taint?) flickerstreak@28: self.frame = f flickerstreak@28: barFrame:SetAttribute("addchild",f) flickerstreak@28: flickerstreak@28: -- auto show/hide when pet exists flickerstreak@28: -- this gets called once per button, which is inefficient but otherwise harmless flickerstreak@28: barFrame:SetAttribute("unit","pet") flickerstreak@28: RegisterUnitWatch(barFrame) flickerstreak@28: flickerstreak@28: self:Refresh(bar,idx) flickerstreak@28: end flickerstreak@28: flickerstreak@28: function Button:Destroy() flickerstreak@28: local f = self.frame flickerstreak@28: f:UnregisterAllEvents() flickerstreak@28: f:Hide() flickerstreak@28: f:SetParent(UIParent) flickerstreak@28: f:ClearAllPoints() flickerstreak@28: if self.name then flickerstreak@28: _G[self.name] = nil flickerstreak@28: end flickerstreak@28: ActionIDList[self.config.actionID] = nil flickerstreak@28: self.frame = nil flickerstreak@28: self.config = nil flickerstreak@28: self.bar = nil flickerstreak@28: end flickerstreak@28: flickerstreak@28: flickerstreak@28: -- export as a class-factory to module flickerstreak@28: module.BtnClass = { flickerstreak@28: new = function(self, ...) flickerstreak@28: local x = { } flickerstreak@28: for k,v in pairs(Button) do flickerstreak@28: x[k] = v flickerstreak@28: end flickerstreak@28: Constructor(x, ...) flickerstreak@28: return x flickerstreak@28: end flickerstreak@28: }