diff classes/ReAction_PetActionType.lua @ 7:f920db5fc6b1

version 0.3
author Flick <flickerstreak@gmail.com>
date Tue, 20 Mar 2007 21:25:29 +0000
parents
children 0ea4c8ab1991
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/classes/ReAction_PetActionType.lua	Tue Mar 20 21:25:29 2007 +0000
@@ -0,0 +1,222 @@
+-- The ReAction.PetActionType mixin defines Pet action button functionality 
+-- and is an implementation of the ReAction.IActionType interface.
+--
+-- The Mixin assumes that it is being mixed in with a ReAction-derived class
+-- which implements the ReAction.IDisplay and ReAction.PetActionType.IDisplay interfaces.
+
+local AceOO = AceLibrary("AceOO-2.0")
+
+ReAction.PetActionType = AceOO.Mixin {
+  -- ReAction.IDisplay interface
+  "SetID",
+  "GetID",
+  "SetupAction",
+  "UpdateAction",
+  "PickupAction",
+  "PlaceAction",
+  "IsActionEmpty",
+  "UpdateTooltip",
+
+  -- Event handlers
+  "PLAYER_ENTERING_WORLD",
+  "PLAYER_CONTROL_LOST",
+  "PLAYER_CONTROL_GAINED",
+  "PLAYER_FARSIGHT_FOCUS_CHANGED",
+  "UNIT_PET",
+  "UNIT_FLAGS",
+  "UNIT_AURA",
+  "PET_BAR_UPDATE",
+  "PET_BAR_UPDATE_COOLDOWN",
+  "PET_BAR_SHOWGRID",
+  "PET_BAR_HIDEGRID",
+
+  -- Internal functions
+  "UpdateCooldown",
+}
+
+local RAPAT = ReAction.PetActionType
+
+-- Required display elements
+RAPAT.IDisplay = AceOO.Interface {
+  DisplayAutoCast     = "function", -- DisplayAutoCast(bool)
+  DisplayAutoCastable = "function", -- DisplayAutoCastable(bool)
+  DisplayIcon         = "function", -- DisplayIcon(texture), Display the icon texture (nil for empty slot)
+  DisplayCooldown     = "function", -- DisplayCooldown(start, duration, enable), display cooldown timer
+  DisplayInUse        = "function", -- DisplayInUse(bool), display whether the action is in use
+  DisplayUsable       = "function", -- DisplayUsable(bool), display whether the action can be used now
+}
+
+
+-- private constants
+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
+
+---------------------------------------
+-- ReAction.IActionType interface implementation
+---------------------------------------
+function RAPAT:SetID( id )  -- paging not supported
+  id = tonumber(id)  -- force data integrity
+  if id then
+    self.config.ids[self.barIdx] = { id }
+    local f = self:GetActionFrame()
+    f:SetAttribute("action",id)
+    -- the following is the goofy hack to work around Blizzard not exporting the pet functions securely
+    f:SetAttribute("clickbutton2",getglobal("PetActionButton"..id))
+  end
+end
+
+function RAPAT:GetID()
+  return SecureButton_GetModifiedAttribute(self:GetActionFrame(), "action", button)
+end
+
+function RAPAT:SetupAction()
+  local b = self:GetActionFrame()
+  -- Blizzard didn't support TogglePetAutocast functionality in
+  -- SecureButton_OnClick(), so we have to spoof it
+  -- by delegating right-clicks to the hidden default action buttons
+  b:SetAttribute("type", "pet")
+  b:SetAttribute("type2", "click") 
+
+  -- shift-clicking to drag locked buttons off
+	b:SetAttribute("checkselfcast", true)
+	b:SetAttribute("useparent-unit", true)
+	
+	self:RegisterEvent("PLAYER_ENTERING_WORLD")
+	self:RegisterEvent("PLAYER_CONTROL_LOST");
+	self:RegisterEvent("PLAYER_CONTROL_GAINED");
+	self:RegisterEvent("PLAYER_FARSIGHT_FOCUS_CHANGED");
+	self:RegisterEvent("UNIT_PET");
+	self:RegisterEvent("UNIT_FLAGS");
+	self:RegisterEvent("UNIT_AURA");
+	self:RegisterEvent("PET_BAR_UPDATE");
+	self:RegisterEvent("PET_BAR_UPDATE_COOLDOWN");
+	self:RegisterEvent("PET_BAR_SHOWGRID");
+	self:RegisterEvent("PET_BAR_HIDEGRID");
+  
+  self:UpdateAction()
+end
+
+function RAPAT:UpdateAction()
+  local id = self:GetID()
+  if id then
+    local name, subtext, texture, isToken, isActive, autoCastAllowed, autoCastEnabled = GetPetActionInfo(id);
+    self:DisplayIcon( isToken and getglobal(texture) or texture )
+    self:DisplayInUse( isActive )
+    self:DisplayAutoCastable( autoCastAllowed )
+    self:DisplayAutoCast( autoCastEnabled )
+    self:DisplayCooldown( GetPetActionCooldown(id) )
+    self:DisplayUsable(GetPetActionsUsable())
+    -- If it's a 'token' save away the tooltip name for updateTooltip
+    self.isToken = isToken
+    if isToken then
+      self.tooltipName = getglobal(name)
+      self.tooltipSubtext = subtext
+    end
+  end
+end
+
+function RAPAT:PickupAction()
+  PickupPetAction(self:GetID())
+  self:UpdateAction()
+end
+
+function RAPAT:PlaceAction()
+  if not InCombatLockdown() then
+    PlacePetAction(self:GetID())
+  end
+  self:UpdateAction()
+end
+
+function RAPAT:IsActionEmpty()
+  local id = self:GetID()
+  return not(id and GetPetActionInfo(id))
+end
+
+function RAPAT:UpdateTooltip()
+  local id = self:GetID()
+	if GameTooltip:IsOwned(self:GetActionFrame()) and id then
+    if self.isToken then
+      GameTooltip:SetText(self.tooltipName, 1.0, 1.0, 1.0)
+      if ( self.tooltipSubtext ) then
+        GameTooltip:AddLine(self.tooltipSubtext, "", 0.5, 0.5, 0.5);
+      end
+      GameTooltip:Show();
+    else
+      if GameTooltip:SetPetAction(id) then
+    		self.tooltipTime = TOOLTIP_UPDATE_TIME
+      end
+    end
+	else
+		self.tooltipTime = nil
+	end
+end
+
+
+-----------------------------
+-- Event Handling
+-----------------------------
+function RAPAT:PLAYER_ENTERING_WORLD()
+  self:UpdateAction()
+end
+
+function RAPAT:PLAYER_CONTROL_LOST()
+  self:UpdateAction()
+end
+
+function RAPAT:PLAYER_CONTROL_GAINED()
+  self:UpdateAction()
+end
+
+function RAPAT:PLAYER_FARSIGHT_FOCUS_CHANGED()
+  self:UpdateAction()
+end
+
+function RAPAT:UNIT_PET(unit)
+  if unit == "player" then
+    self:UpdateAction()
+  end
+end
+
+function RAPAT:UNIT_FLAGS(unit)
+  if unit == "pet" then
+    self:UpdateAction()
+  end
+end
+
+function RAPAT:UNIT_AURA(unit)
+  if unit == "pet" then
+    self:UpdateAction()
+  end
+end
+
+function RAPAT:PET_BAR_UPDATE()
+  self:UpdateAction()
+end
+
+function RAPAT:PET_BAR_UPDATE_COOLDOWN()
+  self:UpdateCooldown()
+end
+
+function RAPAT:PET_BAR_SHOWGRID()
+  self:TempShow(true)
+end
+
+function RAPAT:PET_BAR_HIDEGRID()
+  self:TempShow(false)
+end
+
+
+-------------------------------------
+-- Internal functions
+-------------------------------------
+function RAPAT:UpdateCooldown()
+  local id = self:GetID()
+  if id then
+    self:DisplayCooldown( GetPetActionCooldown(id) )
+  end
+end