view classes/ReAction_PetActionDisplay.lua @ 10:f3a7bfebc283

Version 0.33
author Flick <flickerstreak@gmail.com>
date Tue, 20 Mar 2007 21:37:38 +0000
parents c05fd3e18b4f
children
line wrap: on
line source
-- The ReAction.PetActionDisplay mixin defines 'regular' action button display functionality
-- and is an implementation of the ReAction.IDisplay and ReAction.ActionType.IDisplay interfaces.
--
-- This Mixin assumes that it has been mixed in with a ReAction-derived class which implements
-- the ReAction.IActionType and ReAction.IColorScheme interfaces.
--
-- This mixin uses properties of self.config to define display elements:
--
-- self.config = {
--   keyBindLoc = "POSITION",       -- keybind anchor location
--   showKeyBind = true/false,      -- show keybind labels
--   showGrid = true/false,         -- always show empty buttons
-- }
--

local AceOO = AceLibrary("AceOO-2.0")

ReAction.PetActionDisplay = AceOO.Mixin {
  -- ReAction.IDisplay interface
  "SetupDisplay",
  "UpdateDisplay",
  "TempShow",
  "GetActionFrame",
  "GetBaseButtonSize",
  "DisplayID",
  "DisplayHotkey",

  -- ReAction.PetActionType.IDisplay interface
  "DisplayAutoCast",
  "DisplayAutoCastable",
  "DisplayInUse",
  "DisplayUsable",
  "DisplayIcon",
  "DisplayCooldown",

  -- Event handlers
  "PreClick",
  "PostClick",
  "OnDragStart",
  "OnReceiveDrag",
  "OnEnter",
  "OnLeave",

  -- internal functions
  "ApplyLayout",
  "ApplyStyle",
  "DisplayVisibility",
}

local RAPAD = ReAction.PetActionDisplay


-- private constants
local _G = getfenv(0)

local actionIDColor = { r=1.00,  g=0.82,  b=0.00,  a=1.00 } -- gold


-- private functions
-- extract and return color fields from a table, to be fed into SetVertexColor()/SetTextColor()
local function tcolor(c)
  return c.r, c.g, c.b, c.a
end


-----------------------------------
-- Interface Implementation Methods
-----------------------------------
function RAPAD:SetupDisplay( name )
  -- create the button widget
  local b = CreateFrame("CheckButton", name, nil, "ActionButtonTemplate, SecureActionButtonTemplate")
  b:EnableMouse()
  b:RegisterForDrag("LeftButton", "RightButton")
  b:RegisterForClicks("AnyUp")
  b:SetScript("PreClick",      function(arg1) self:PreClick(arg1) end)
  b:SetScript("PostClick",     function(arg1) self:PostClick(arg1) end)
  b:SetScript("OnDragStart",   function(arg1) self:OnDragStart(arg1) end)
  b:SetScript("OnReceiveDrag", function() self:OnReceiveDrag() end)
  b:SetScript("OnEnter",       function() self:OnEnter() end)
  b:SetScript("OnLeave",       function() self:OnLeave() end)


  b:SetWidth(30)
  b:SetHeight(30)
  
  local tx = b:GetNormalTexture()
  tx:ClearAllPoints()
  tx:SetWidth(54)
  tx:SetHeight(54)
  tx:SetPoint("CENTER",0,-1)

  local autoCastable = b:CreateTexture(nil, "OVERLAY")
  autoCastable:SetTexture("Interface\\Buttons\\UI-AutoCastableOverlay")
  autoCastable:SetWidth(58)
  autoCastable:SetHeight(58)
  autoCastable:SetPoint("CENTER")
  autoCastable:Hide()

  local autoCast = CreateFrame("Model",nil,b)
  autoCast:SetModel("Interface\\Buttons\\UI-AutoCastButton.mdx")
  autoCast:SetScale(1.2)
  autoCast:SetAllPoints()
  autoCast:SetSequence(0)
  autoCast:SetSequenceTime(0,0)
  autoCast:SetFrameStrata("HIGH") -- Otherwise it won't appear on top of the icon for some reason
  autoCast:Hide()


  local cd = _G[name.."Cooldown"]
	cd:SetScale(1);
	cd:ClearAllPoints();
	cd:SetWidth(33);
	cd:SetHeight(33);
	cd:SetPoint("CENTER", -2, -1);

  -- store references to the various sub-frames of ActionButtonTemplate so we don't have to look it up all the time
  self.frames      = {
    button         = b,
    autoCastable   = autoCastable, --_G[name.."AutoCastable"],
    autoCast       = autoCast, --_G[name.."AutoCast"],
    hotkey         = _G[name.."HotKey"],
    cooldown       = cd,
    icon           = _G[name.."Icon"],
    normalTexture  = tx, --_G[name.."NormalTexture2"],
    actionID       = nil,    -- defer creating actionID font string until it's actually requested
  }

  self.tmpShow_ = 0
end

function RAPAD:UpdateDisplay()
  self:ApplyLayout()
  self:ApplyStyle()
  self:DisplayVisibility()
  -- refresh the action ID display
  if ReAction.showIDs_ then
    self:DisplayID(true)
  end
end

function RAPAD:TempShow( visible )
  visible = visible and true or false -- force data integrity
  self.showTmp_ = max(0, (self.showTmp_ or 0) + (visible and 1 or -1))
  self:DisplayVisibility()
end

function RAPAD:GetActionFrame()
  return self.frames.button
end

function RAPAD:GetBaseButtonSize()
  return 30
end

function RAPAD:DisplayID( show )
  local f = self.frames.actionID
  if show then
    if not f then
      -- create the actionID label
      f = self.frames.button:CreateFontString(nil,"ARTWORK","NumberFontNormalSmall")
      f:SetPoint("BOTTOMLEFT")
      f:SetTextColor( tcolor(actionIDColor) )
      self.frames.actionID = f
    end
    f:SetText(tostring(self:GetID()))
    f:Show()
  elseif f then
    f:Hide()
  end
end

function RAPAD:DisplayHotkey(txt,button)
  if button == nil or button == "LeftButton" then
    -- only left-button hotkeys supported
    self.frames.hotkey:SetText(string.upper(txt or ""))
  end
end

function RAPAD:DisplayInUse( inUse )
  self.frames.button:SetChecked( inUse and 1 or 0 )
end

function RAPAD:DisplayUsable( usable )
  SetDesaturation(self.frames.icon, (not usable) and 1 or nil) -- argument is backward
  self.frames.hotkey:SetTextColor(self:GetHotkeyColor(usable, false, false, self.frames.hotkey:GetText()) )
end

function RAPAD:DisplayAutoCast( show )
  if show then 
    self.frames.autoCast:Show()
  else
    self.frames.autoCast:Hide()
  end
end

function RAPAD:DisplayAutoCastable( show )
  if show then 
    self.frames.autoCastable:Show()
  else
    self.frames.autoCastable:Hide()
  end
end

function RAPAD:DisplayIcon( texture )
  local f = self.frames.button
  local icon = self.frames.icon
  if texture then
    icon:SetTexture(texture)
    icon:Show()
    f:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2")
  else
    f:SetScript("OnUpdate",nil)
    icon:Hide()
    f:SetNormalTexture("Interface\\Buttons\\UI-Quickslot")
  end
  self:DisplayVisibility()
end

function RAPAD:DisplayCooldown( start, duration, enable )
  CooldownFrame_SetTimer(self.frames.cooldown, start, duration, enable)
end






----------------------
-- Event Handlers
----------------------
function RAPAD:PreClick()
  -- not sure why we have to do this
  self.frames.button:SetChecked(0)
end

function RAPAD:PostClick()
  self:UpdateAction()
end

function RAPAD:OnDragStart()
  if LOCK_ACTIONBAR ~= "1" or IsShiftKeyDown() then
    self:PickupAction()
  end
end

function RAPAD:OnReceiveDrag()
  self:PlaceAction()
end

function RAPAD:OnEnter()
  self:SetTooltip() -- from ReAction base class
end

function RAPAD:OnLeave()
  self:ClearTooltip() -- from ReAction base class
end



----------------------
-- Internal methods
----------------------

local function placeLabel( label, anchor )
  local top = string.match(anchor,"TOP")
  local bottom = string.match(anchor, "BOTTOM")
  label:ClearAllPoints()
  label:SetWidth(40)
  label:SetPoint(top or bottom,0,top and 2 or -2)
  local j
  if string.match(anchor,"LEFT") then
    j = "LEFT"
  elseif string.match(anchor,"RIGHT") then
    j = "RIGHT"
  else
    j = "CENTER"
  end
  label:SetJustifyH(j)
end


function RAPAD:ApplyLayout()
  local f = self.frames

  if self.config.keyBindLoc then
    placeLabel(f.hotkey, self.config.keyBindLoc)
  end

  if self.config.showKeyBind then
    f.hotkey:Show()
  else
    f.hotkey:Hide()
  end

  if self.config.showBorder then
    f.normalTexture:SetAlpha(1)
  else
    f.normalTexture:SetAlpha(0)
  end
end

function RAPAD:ApplyStyle()

end

function RAPAD:DisplayVisibility()
  local b = self.frames.button

  -- can't hide/show in combat
  if not InCombatLockdown() then
    if b:GetAttribute("statehidden") then
      b:Hide()
    elseif not self:IsActionEmpty() then
      b:GetNormalTexture():SetAlpha(1.0)
      b:Show()
    elseif self.showTmp_ and self.showTmp_ > 0 or self.config.showGrid then
      b:GetNormalTexture():SetAlpha(0.5)
      self.frames.cooldown:Hide()
      b:Show()
    else
      b:Hide()
    end
  end
end