view modules/Totem.lua @ 228:ad40cd3fc7e9

pull states config out of namespace
author Flick
date Mon, 21 Mar 2011 11:33:15 -0700
parents c4b134512c50
children
line wrap: on
line source
--[[
  ReAction Totem button module

--]]

-- local imports
local addonName, addonTable = ...
local ReAction = addonTable.ReAction
local L = ReAction.L
local _G = _G

-- button 
local Button = ReAction.Button.MultiCast

-- module declaration
local moduleID = "Totem"
local module = ReAction:NewModule( moduleID,
  "AceEvent-3.0"
  -- mixins go here
)

-- handlers
function module:OnInitialize()
  self.buttons = { }

  ReAction.RegisterCallback(self, "OnCreateBar", "OnRefreshBar")
  ReAction.RegisterCallback(self, "OnDestroyBar")
  ReAction.RegisterCallback(self, "OnRefreshBar")

  self:RegisterEvent("UPDATE_MULTI_CAST_ACTIONBAR","PLAYER_ENTERING_WORLD")
end

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

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

    local r, c = bar:GetButtonGrid()
    local n = min(r*c,6)
    Button.SetupBarHeader(bar)
    for i = 1, n do
      if btnCfg[i] == nil then
        btnCfg[i] = {}
      end
      if not btns[i] then
        local success, r = pcall(Button.New,Button,i,btnCfg,bar)
        if success then
          btns[i] = r
          if r then
            bar:AddButton(i,r)
          end
        else
          geterrorhandler()(r)
          n = i - 1
          bar:ClipNButtons(n)
          break
        end
      end
      if btns[i] then
        btns[i]:Refresh()
      end
    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
  end

end

function module:UPDATE_MULTI_CAST_ACTIONBAR()
  if not InCombatLockdown() then
    for bar in pairs(self.buttons) do
      self:OnRefreshBar("OnRefreshBar", bar, bar:GetName())
    end
  end
end

function module:PLAYER_ENTERING_WORLD()
  for bar in pairs(self.buttons) do
    self:OnRefreshBar("OnRefreshBar", bar, bar:GetName())
  end
end