view modules/ReAction_Action/ReAction_Action.lua @ 28:21bcaf8215ff

- converted to Ace3 - rearranged file layout - configGUI menus not working right now
author Flick <flickerstreak@gmail.com>
date Mon, 17 Mar 2008 18:24:53 +0000
parents 9e1984088124
children 0d95ce7a9ec2
line wrap: on
line source
--[[
  ReAction Action-button module.

  The button module implements standard action button functionality by wrapping Blizzard's 
  ActionButton frame and associated functions. It also provides some button layout
  modification tools.

--]]

-- local imports
local ReAction = ReAction
local L = ReAction.L
local _G = _G
local CreateFrame = CreateFrame
local print = ReAction.print

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

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

function module:OnEnable()
end

function module:OnDisable()
end

function module:ApplyToBar(bar)
  self:RefreshBar(bar)
end

function module:RefreshBar(bar)
  if self.buttons[bar] == nil then
    self.buttons[bar] = { }
  end
  local btns = self.buttons[bar]
  local profile = self.db.profile
  local barName = bar:GetName()
  if profile.buttons[barName] == nil then
    profile.buttons[barName] = {}
  end
  local btnCfg = profile.buttons[barName]

  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
      btns[i] = self.BtnClass:new(bar,i,btnCfg[i])
    else
      btns[i]:Refresh(bar,i)
    end
  end
  for i = n+1, #btns do
    btns[i] = btns[i]:Destroy()
    if btnCfg[i] then
      btnCfg[i] = nil
    end
  end
end

function module:RemoveFromBar(bar)
  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:EraseBarConfig(barName)
  self.db.profile.buttons[barName] = nil
end

function module:ApplyConfigMode(mode,bars)
  for _, btns in pairs(self.buttons) do
    if btn then
      for _, b in pairs(btns) do
        if b then
          if mode then
            if not b.actionIDLabel then
              local label = b:GetFrame():CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
              label:SetAllPoints()
              label:SetJustifyH("CENTER")
              label:SetShadowColor(0,0,0,1)
              label:SetShadowOffset(2,-2)
              label:SetText(tostring(b:GetActionID()))
              b.actionIDLabel = label
            end
            b.actionIDLabel:Show()
          elseif b.actionIDLabel then
            b.actionIDLabel:Hide()
          end
        end
      end
    end
  end
end

function module:GetGlobalBarOptions(opts)  
  if self.globalBarOpts == nil then
    self.globalBarOpts = {
      newActionBar = {
        type = "execute",
        name = L["New Action Bar"],
        desc = L["Create a new bar of standard action buttons"],
        func = function()
                 ReAction:CreateBar()
               end,
        disabled = InCombatLockdown,
      }
    }
  end
  return self.globalBarOpts
end

function module:GetBarMenuOptions(bar)
  if not bar.modMenuOpts[moduleID] then
    bar.modMenuOpts[moduleID] = {
    }
  end
  return bar.modMenuOpts[moduleID]
end

function module:GetBarConfigOptions(bar)
  if not bar.modConfigOpts[moduleID] then
    bar.modConfigOpts[moduleID] = {
    }
  end
  return bar.modConfigOpts[moduleID]
end



-- use-count of action IDs
local ActionIDList = setmetatable( {}, {
  __index = function(self, idx)
    if idx == nil then
      for i = 1, 120 do
        if rawget(self,i) == nil then
          rawset(self,i,1)
          return i
        end
      end
    else
      local c = rawget(self,idx) or 0
      rawset(self,idx,c+1)
      return idx
    end
  end,
  __newindex = function(self,idx,value)
    if value == nil then
      value = rawget(self,idx)
      if value == 1 then
        value = nil
      elseif value then
        value = value - 1
      end
    end
    rawset(self,idx,value)
  end
})




------ Button class ------
local Button = { }

local function Constructor( self, bar, idx, config )
  self.bar, self.idx, self.config = bar, idx, config

  local barFrame = bar:GetFrame()

  self.name = config.name or "ReAction_"..bar:GetName().."_"..idx
  config.actionID = ActionIDList[config.actionID] -- gets a free one if none configured
  
  local f = CreateFrame("CheckButton", self.name, barFrame, "ActionBarButtonTemplate")
  -- TODO: re-implement ActionButton event handlers that don't do secure stuff

  -- this will probably cause taint, using right now for display/debugging purposes
  f:SetScript("OnAttributeChanged",
    function()
      ActionButton_UpdateAction()
    end
  )
  f:SetAttribute("action", config.actionID)
  barFrame:SetAttribute("addchild",f)
  self.frame = f
  self:Refresh(bar,idx)
end

function Button:Destroy()
  local f = self.frame
  f:UnregisterAllEvents()
  f:Hide()
  f:SetParent(UIParent)
  f:ClearAllPoints()
  if self.name then
    _G[self.name] = nil
  end
  ActionIDList[self.config.actionID] = nil
  self.frame = nil
  self.config = nil
  self.bar = nil
end

function Button:Refresh(bar,idx)
  bar:PlaceButton(self.frame, idx, 36, 36)
end

function Button:GetFrame()
  return self.frame
end

function Button:GetName()
  return self.name
end

function Button:GetActionID()
  return self.config.actionID
end


-- export as a class-factory to module
module.BtnClass = {
  new = function(self, ...)
    local x = { }
    for k,v in pairs(Button) do
      x[k] = v
    end
    Constructor(x, ...)
    return x
  end
}