view modules/Totem.lua @ 163:ab5c37989986

- totem bar now stores which page was selected across sessions - totem bar shouldn't be messed up if resized around on non-shaman toons now
author Flick <flickerstreak@gmail.com>
date Sat, 22 Aug 2009 00:12:44 +0000
parents fc08372f0c7a
children 363dd8130205
line wrap: on
line source
--[[
  ReAction Totem button module

--]]

-- local imports
local ReAction = 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.db = ReAction.db:RegisterNamespace( moduleID,
    {
      profile = { 
        buttons = { }
      }
    }
  )

  self.buttons = { }

  ReAction:RegisterOptions(self, self:GetOptions())

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

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

function module:OnEnable()
  ReAction:RegisterBarType(L["Totem Bar"], 
    { 
      type = moduleID ,
      defaultButtonSize = 36,
      defaultBarRows = 1,
      defaultBarCols = 6,
      defaultBarSpacing = 3
    })

end

function module:OnDisable()
  ReAction:UnregisterBarType(L["Totem Bar"])
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)
  if bar.config.type == moduleID then
    local btns = self.buttons[bar]
    if btns == nil then
      btns = { }
      self.buttons[bar] = btns
    end
    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 = min(r*c,6)
    Button.SetupBarHeader(bar)
    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,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: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

function module:RefreshAll()
  for bar in pairs(self.buttons) do
    self:OnRefreshBar(nil,bar,bar:GetName())
  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


---- options ----
function module:GetOptions()
  return {
    stance = 
    {
      name = L["Totem Buttons"],
      type = "group",
      args = {
        -- TODO
      }
    }
  }
end