view modules/PetAction.lua @ 179:bf64e71701e2

Remove support for hunter aspects in stance bar. Remove option to disable DK/paladin auras in stance bar. (drycoded)
author Flick <flickerstreak@gmail.com>
date Wed, 20 Oct 2010 23:40:41 +0000
parents df68b5a40490
children e63aefb8a555
line wrap: on
line source
--[[
  ReAction Pet Action button module

  The button module implements standard action button functionality by wrapping Blizzard's 
  PetActionButton frame and associated functions.

--]]

-- local imports
local addonName, addonTable = ...
local ReAction = addonTable.ReAction
local L = ReAction.L
local _G = _G
local CreateFrame = CreateFrame
local format = string.format

-- module declaration
local moduleID = "PetAction"
local module = ReAction:NewModule( moduleID )

-- Button class
local Button = ReAction.Button.PetAction

-- private
local function UpdateButtonLock(bar)
  local f = bar:GetFrame()
  f:SetAttribute("lockbuttons",bar.config.lockButtons)
  f:SetAttribute("lockbuttonscombat",bar.config.lockButtonsCombat)
  f:Execute(
    [[
      lockButtons = self:GetAttribute("lockbuttons")
      lockButtonsCombat = self:GetAttribute("lockbuttonscombat")
    ]])
end

-- module methods
function module:OnInitialize()
  self.db = ReAction.db:RegisterNamespace( moduleID,
    { 
      profile = {
        buttons = { }
      }
    }
  )
  self.buttons = { }

  ReAction:RegisterBarOptionGenerator(self, "GetBarOptions")

  ReAction.RegisterCallback(self, "OnCreateBar")
  ReAction.RegisterCallback(self, "OnDestroyBar")
  ReAction.RegisterCallback(self, "OnRefreshBar")
  ReAction.RegisterCallback(self, "OnEraseBar")
  ReAction.RegisterCallback(self, "OnRenameBar")
end

function module:OnEnable()
  ReAction:RegisterBarType(L["Pet Action Bar"], 
    { 
      type = moduleID ,
      defaultButtonSize = 30,
      defaultBarRows = 1,
      defaultBarCols = 10,
      defaultBarSpacing = 8
    })
end

function module:OnDisable()
  ReAction:UnregisterBarType(L["Pet Action Bar"])
end

function module:OnCreateBar(event, bar, name)
  if bar.config.type == moduleID then
    -- auto show/hide when pet exists
    bar:RegisterUnitWatch("pet",true)
    self:OnRefreshBar(event, bar, name)
  end
end

function module:OnRefreshBar(event, bar, name)
  if bar.config.type == moduleID then
    if self.buttons[bar] == nil then
      self.buttons[bar] = { }
    end
    local btns = self.buttons[bar]
    local profile = self.db.profile
    if profile.buttons[name] == nil then
      profile.buttons[name] = {}
    end
    local btnCfg = profile.buttons[name]

    local r, c = bar:GetButtonGrid()
    local n = r*c
    for i = 1, n do
      if btnCfg[i] == nil then
        btnCfg[i] = {}
      end
      if btns[i] == nil then
        local success, r = pcall(Button.New,Button,i,btnCfg[i],bar,i>1 and btnCfg[i-1].actionID)
        if success and r then
          btns[i] = r
          bar:AddButton(i,r)
        else
          n = i - 1
          bar:ClipNButtons(n)
          break
        end
      end
      btns[i]:Refresh()
    end
    for i = n+1, #btns do
      if btns[i] then
        bar:RemoveButton(btns[i])
        btns[i] = btns[i]:Destroy()
        if btnCfg[i] then
          btnCfg[i] = nil
        end
      end
    end
    UpdateButtonLock(bar)
  end
end

function module:OnDestroyBar(event, bar, name)
  if self.buttons[bar] then
    local btns = self.buttons[bar]
    for _,b in pairs(btns) do
      if b then
        b:Destroy()
      end
    end
    self.buttons[bar] = nil
  end
end

function module:OnEraseBar(event, bar, name)
  self.db.profile.buttons[name] = nil
end

function module:OnRenameBar(event, bar, oldname, newname)
  local b = self.db.profile.buttons
  b[newname], b[oldname] = b[oldname], nil
end


---- Options ----
local Handler = { }
local meta = { __index = Handler }

function Handler:New(bar)
  return setmetatable(
    {
      bar = bar,
      config = bar.config
    }, meta)
end

function Handler:GetLockButtons()
  return self.config.lockButtons
end

function Handler:SetLockButtons(info, value)
  self.config.lockButtons = value
  UpdateButtonLock(self.bar)
end

function Handler:GetLockButtonsCombat()
  return self.config.lockButtonsCombat
end

function Handler:SetLockButtonsCombat(info, value)
  self.config.lockButtonsCombat = value
  UpdateButtonLock(self.bar)
end

function Handler:LockButtonsCombatDisabled()
  return not self.config.lockButtons
end


function module:GetBarOptions(bar)
  if bar.config.type == moduleID then
    return {
      type = "group",
      name = L["Pet Buttons"],
      handler = Handler:New(bar),
      args = {
        lockButtons = {
          name = L["Lock Buttons"],
          desc = L["Prevents picking up/dragging actions (use SHIFT to override this behavior)"],
          order = 2,
          type = "toggle",
          get = "GetLockButtons",
          set = "SetLockButtons",
        },
        lockOnlyCombat = {
          name = L["Only in Combat"],
          desc = L["Only lock the buttons when in combat"],
          order = 3,
          type = "toggle",
          disabled = "LockButtonsCombatDisabled",
          get = "GetLockButtonsCombat",
          set = "SetLockButtonsCombat",
        },
      }
    }
  end
end