view StanceButton.lua @ 274:2897480842dd

Reuse Overlay metatable
author Flick
date Wed, 11 May 2011 10:45:03 -0700
parents c918ff9ac787
children 36a29870bf34
line wrap: on
line source
local addonName, addonTable = ...
local ReAction = addonTable.ReAction
local L = ReAction.L
local _G = _G
local CreateFrame = CreateFrame
local format = string.format
local GetCVar = GetCVar
local GameTooltip_SetDefaultAnchor = GameTooltip_SetDefaultAnchor
local CooldownFrame_SetTimer = CooldownFrame_SetTimer
local InCombatLockdown = InCombatLockdown
local GetNumShapeshiftForms = GetNumShapeshiftForms
local GetShapeshiftFormInfo = GetShapeshiftFormInfo
local IsUsableSpell = IsUsableSpell
local GetSpellInfo = GetSpellInfo

--
-- private
--
local playerClass = select(2,UnitClass("player"))

local eventList = {
  "PLAYER_REGEN_ENABLED",
  "PLAYER_ENTERING_WORLD",
  "UPDATE_SHAPESHIFT_FORM",
  "UPDATE_SHAPESHIFT_FORMS",
  "UPDATE_SHAPESHIFT_USABLE",
  "UPDATE_SHAPESHIFT_COOLDOWN",
  "UPDATE_BINDINGS",
}

--
-- Stance Button class
--
local buttonTypeID = "Stance"
local Super = ReAction.Button
local Stance = setmetatable(
  { 
    defaultBarConfig = { 
      type = buttonTypeID,
      btnHeight = 36,
      btnWidth = 36,
      btnRows = 1,
      btnColumns = 6,
      spacing = 3
    }, 

    barType = L["Stance Bar"], 
    buttonTypeID = buttonTypeID
  },
  { __index = Super } )

ReAction.Button.Stance = Stance
ReAction:RegisterBarType(Stance)

function Stance:New( config, bar, idx, idHint )
  local name = format("ReAction_%s_Stance_%d",bar:GetName(),idx)
 
  self = Super.New(self, name, config, bar, idx, "SecureActionButtonTemplate, ActionButtonTemplate" )

  local f = self:GetFrame()
  local barFrame = bar:GetFrame()
  local config = self:GetConfig()

  -- set up the stance ID
  config.stanceID = config.stanceID or idx

  -- attribute setup
  f:SetAttribute("type","spell")

  -- non secure scripts
  f:SetScript("OnEvent", function(frame, ...) self:OnEvent(...) end)
  f:SetScript("OnEnter", function(frame) self:OnEnter() end)
  f:SetScript("OnLeave", function(frame) self:OnLeave() end)
  f:SetScript("PreClick", function(frame, ...) self:PreClick(...) end)

  -- secure handlers
  -- (none)

  -- event registration
  for _, evt in pairs(eventList) do
    f:RegisterEvent(evt)
  end

  -- attach to skinner
  bar:SkinButton(self)

  -- initial display
  if ReAction:GetConfigMode() then
    self:GetFrame():Show()
  end

  self:Refresh()

  return self
end

function Stance:GetActionID()
  return self.config.stanceID
end

function Stance:AcquireActionID()
  -- don't use pool
end

function Stance:ReleaseActionID()
  -- don't use pool
end

function Stance:UpdateAction()
  if InCombatLockdown() then
    self.updatePending = true
  else
    self.updatePending = false
    local idx = self:GetActionID()
    local f = self:GetFrame()
    if idx > GetNumShapeshiftForms() then
      f:Hide()
    else
      f:SetAttribute("spell", select(2,GetShapeshiftFormInfo(idx)))
      f:Show()
      self:Update()
    end
  end
end

function Stance:Refresh()
  Super.Refresh(self)
  self:UpdateHotkey()
  self:UpdateAction()
end

function Stance:Update()
  local texture, _, isActive, isCastable = GetShapeshiftFormInfo(self:GetActionID())
  
  local icon = self.frames.icon
  icon:SetTexture(texture)
  self:GetFrame():SetChecked( isActive and 1 or 0 )
  if isCastable then
    self.frames.hotkey:Show()
    icon:SetVertexColor(1.0, 1.0, 1.0)
  else
    icon:SetVertexColor(0.4, 0.4, 0.4)
  end

  self:UpdateCooldown()
end

function Stance:UpdateCooldown()
  local start, duration, enabled = GetShapeshiftFormCooldown(self:GetActionID())
  if start then
    CooldownFrame_SetTimer(self.frames.cooldown, start, duration, enabled)
  end
end

function Stance:SetTooltip()
  if GetCVar("UberTooltips") == "1" then
    GameTooltip_SetDefaultAnchor(GameTooltip, self:GetFrame())
  else
    GameTooltip:SetOwner(self:GetFrame(), "ANCHOR_RIGHT")
  end
  GameTooltip:SetShapeshift(self:GetActionID())
end

function Stance:OnEnter()
  self:SetTooltip()
end

function Stance:OnLeave()
  GameTooltip:Hide()
end

function Stance:PreClick()
  local f = self:GetFrame()
  f:SetChecked( not f:GetChecked() )
end

function Stance:OnEvent(event, arg)
  if event == "PLAYER_REGEN_ENABLED" then
    if self.updatePending then
      self:UpdateAction()
    end
  elseif event == "UPDATE_SHAPESHIFT_COOLDOWN" then
    self:UpdateCooldown()
  elseif event == "UPDATE_SHAPESHIFT_FORMS" then
    self:UpdateAction()
  elseif event == "UNIT_AURA" then
    if arg == "player" then
      self:Update()
    end
  elseif event == "UPDATE_BINDINGS" then
    self:UpdateHotkey()
  else
    self:Update()
  end
end

function Stance:ShowGridTemp(show)
  if show then
    self:GetFrame():Show()
  else
    self:UpdateAction()
  end
end