view modules/Bag.lua @ 223:c4b134512c50

Move RegisterBarType from modules to button classes
author Flick <flickerstreak@gmail.com>
date Mon, 22 Nov 2010 10:25:18 -0800
parents bb13624de7e1
children
line wrap: on
line source
--[[
  ReAction Bag button module

--]]

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

-- Bag button 
local Button = ReAction.Button.Bag

-- module declaration
local moduleID = "Bag"
local module = ReAction:NewModule( moduleID
  -- mixins go here
)

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

  ReAction.RegisterCallback(self, "OnCreateBar", "OnRefreshBar")
  ReAction.RegisterCallback(self, "OnDestroyBar")
  ReAction.RegisterCallback(self, "OnRefreshBar")
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 = 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].bagID)
        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
  end

end



-- hook some functions to propagate to our bag buttons
hooksecurefunc("Disable_BagButtons", 
  function()
    for _, buttons in pairs(module.buttons) do
      for _, b in pairs(buttons) do
        local f = b:GetFrame()
        f:Disable()
        SetDesaturation(b.frames.icon,1)
      end
    end
  end)

hooksecurefunc("Enable_BagButtons",
  function()
    for _, buttons in pairs(module.buttons) do
      for _, b in pairs(buttons) do
        local f = b:GetFrame()
        f:Enable()
        SetDesaturation(b.frames.icon,nil)
      end
    end
  end)

hooksecurefunc("ContainerFrame_OnHide",
  function()
    for _, buttons in pairs(module.buttons) do
      for _, b in pairs(buttons) do
        b:Update()
      end
    end
  end)

hooksecurefunc("ContainerFrame_OnShow",
  function()
    for _, buttons in pairs(module.buttons) do
      for _, b in pairs(buttons) do
        b:Update()
      end
    end
  end)