diff modules/Bag.lua @ 146:86564b5cbbff

Added bag button support
author Flick <flickerstreak@gmail.com>
date Thu, 30 Apr 2009 16:51:43 +0000
parents
children 901c91dc1bf2
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/Bag.lua	Thu Apr 30 16:51:43 2009 +0000
@@ -0,0 +1,198 @@
+--[[
+  ReAction Bag button module
+
+--]]
+
+-- local imports
+local ReAction = 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
+)
+
+-- libraries
+local KB = LibStub("LibKeyBound-1.0")
+
+-- handlers
+function module:OnInitialize()
+  self.db = ReAction.db:RegisterNamespace( moduleID,
+    {
+      profile = { 
+        buttons = { }
+      }
+    }
+  )
+
+  self.buttons = { }
+
+  ReAction.RegisterCallback(self, "OnCreateBar", "OnRefreshBar")
+  ReAction.RegisterCallback(self, "OnDestroyBar")
+  ReAction.RegisterCallback(self, "OnRefreshBar")
+  ReAction.RegisterCallback(self, "OnEraseBar")
+  ReAction.RegisterCallback(self, "OnRenameBar")
+  ReAction.RegisterCallback(self, "OnConfigModeChanged")
+
+  KB.RegisterCallback(self, "LIBKEYBOUND_ENABLED")
+  KB.RegisterCallback(self, "LIBKEYBOUND_DISABLED")
+  KB.RegisterCallback(self, "LIBKEYBOUND_MODE_COLOR_CHANGED","LIBKEYBOUND_ENABLED")
+end
+
+function module:OnEnable()
+  ReAction:RegisterBarType(L["Bag Bar"], 
+    { 
+      type = moduleID ,
+      defaultButtonSize = 30,
+      defaultBarRows = 1,
+      defaultBarCols = 6,
+      defaultBarSpacing = 4
+    })
+end
+
+function module:OnDisable()
+  ReAction:UnregisterBarType(L["Bag 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 = 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,profile,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
+
+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:OnConfigModeChanged(event, mode)
+  for bar in pairs(self.buttons) do
+    for b in bar:IterateButtons() do
+      b:UpdateActionIDLabel(mode)
+    end
+  end
+end
+
+function module:LIBKEYBOUND_ENABLED(evt)
+  for _, buttons in pairs(self.buttons) do
+    for _, b in pairs(buttons) do
+      b:SetKeybindMode(true)
+    end
+  end
+end
+
+function module:LIBKEYBOUND_DISABLED(evt)
+  for _, buttons in pairs(self.buttons) do
+    for _, b in pairs(buttons) do
+      b:SetKeybindMode(false)
+    end
+  end
+end
+
+function module:RefreshAll()
+  for bar in pairs(self.buttons) do
+    self:OnRefreshBar(nil,bar,bar:GetName())
+  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)