annotate classes/PetActionButton.lua @ 130:6e4a11b9d290

Converted PetAction to new class layout
author Flick <flickerstreak@gmail.com>
date Fri, 06 Mar 2009 23:44:55 +0000
parents
children e39d80bb0b7a
rev   line source
flickerstreak@130 1 local ReAction = ReAction
flickerstreak@130 2 local L = ReAction.L
flickerstreak@130 3 local _G = _G
flickerstreak@130 4 local CreateFrame = CreateFrame
flickerstreak@130 5 local format = string.format
flickerstreak@130 6 local GetCVar = GetCVar
flickerstreak@130 7 local InCombatLockdown = InCombatLockdown
flickerstreak@130 8 local GetPetActionInfo = GetPetActionInfo
flickerstreak@130 9 local GetPetActionSlotUsable = GetPetActionSlotUsable
flickerstreak@130 10 local GetPetActionCooldown = GetPetActionCooldown
flickerstreak@130 11 local AutoCastShine_AutoCastStart = AutoCastShine_AutoCastStart
flickerstreak@130 12 local AutoCastShine_AutoCastStop = AutoCastShine_AutoCastStop
flickerstreak@130 13 local SetDesaturation = SetDesaturation
flickerstreak@130 14 local CooldownFrame_SetTimer = CooldownFrame_SetTimer
flickerstreak@130 15 local GameTooltip_SetDefaultAnchor = GameTooltip_SetDefaultAnchor
flickerstreak@130 16
flickerstreak@130 17 ReAction:UpdateRevision("$Revision: 154 $")
flickerstreak@130 18
flickerstreak@130 19 --
flickerstreak@130 20 -- Secure snippets
flickerstreak@130 21 -- These are run within the context of the bar's sandbox, as the
flickerstreak@130 22 -- buttons themselves do not have their own sandbox.
flickerstreak@130 23 --
flickerstreak@130 24 local _onDragStart = -- function(self, button, kind, value, ...)
flickerstreak@130 25 [[
flickerstreak@130 26 if lockButtons and (PlayerInCombat() or not lockButtonsCombat) and not IsModifiedClick("PICKUPACTION") then
flickerstreak@130 27 return kind, value, ...
flickerstreak@130 28 else
flickerstreak@130 29 return "petaction", self:GetAttribute("action")
flickerstreak@130 30 end
flickerstreak@130 31 ]]
flickerstreak@130 32
flickerstreak@130 33 local _onReceiveDrag = -- function(self, button, kind, value, ...)
flickerstreak@130 34 [[
flickerstreak@130 35 if kind then -- pet spells on the cursor return nil from GetCursorInfo(), which is very strange
flickerstreak@130 36 return kind, value, ...
flickerstreak@130 37 end
flickerstreak@130 38 return "petaction", self:GetAttribute("action")
flickerstreak@130 39 ]]
flickerstreak@130 40
flickerstreak@130 41 --
flickerstreak@130 42 -- private
flickerstreak@130 43 --
flickerstreak@130 44 local eventList = {
flickerstreak@130 45 "PLAYER_CONTROL_LOST",
flickerstreak@130 46 "PLAYER_CONTROL_GAINED",
flickerstreak@130 47 "PLAYER_FARSIGHT_FOCUS_CHANGED",
flickerstreak@130 48 "UNIT_PET",
flickerstreak@130 49 "UNIT_FLAGS",
flickerstreak@130 50 "UNIT_AURA",
flickerstreak@130 51 "PET_BAR_UPDATE",
flickerstreak@130 52 "PET_BAR_UPDATE_COOLDOWN",
flickerstreak@130 53 "UPDATE_BINDINGS",
flickerstreak@130 54 }
flickerstreak@130 55
flickerstreak@130 56 --
flickerstreak@130 57 -- Pet Action Button class
flickerstreak@130 58 --
flickerstreak@130 59 local Super = ReAction.Button
flickerstreak@130 60 local Pet = setmetatable( { }, { __index = Super } )
flickerstreak@130 61 ReAction.Button.PetAction = Pet
flickerstreak@130 62
flickerstreak@130 63 function Pet:New( idx, config, bar, idHint )
flickerstreak@130 64 local name = format("ReAction_%s_PetAction_%d",bar:GetName(),idx)
flickerstreak@130 65
flickerstreak@130 66 self = Super.New(self, name, config, bar, idx, "SecureActionButtonTemplate, ActionButtonTemplate" )
flickerstreak@130 67
flickerstreak@130 68 local f = self:GetFrame()
flickerstreak@130 69 if not f.autoCastTexture then
flickerstreak@130 70 -- store with the frame for recycling
flickerstreak@130 71 f.autoCastShine = CreateFrame("Frame",name.."Shine",f,"AutoCastShineTemplate")
flickerstreak@130 72 local tex = f:CreateTexture(nil,"OVERLAY")
flickerstreak@130 73 tex:SetTexture([[Interface\Buttons\UI-AutoCastableOverlay]])
flickerstreak@130 74 tex:SetHeight(58)
flickerstreak@130 75 tex:SetWidth(58)
flickerstreak@130 76 tex:SetPoint("CENTER")
flickerstreak@130 77 f.autoCastTexture = tex
flickerstreak@130 78 end
flickerstreak@130 79 local barFrame = bar:GetFrame()
flickerstreak@130 80
flickerstreak@130 81 local frames = { }
flickerstreak@130 82 self.frames = frames
flickerstreak@130 83 frames.icon = _G[name.."Icon"]
flickerstreak@130 84 frames.flash = _G[name.."Flash"]
flickerstreak@130 85 frames.hotkey = _G[name.."HotKey"]
flickerstreak@130 86 frames.count = _G[name.."Count"]
flickerstreak@130 87 frames.name = _G[name.."Name"]
flickerstreak@130 88 frames.border = _G[name.."Border"]
flickerstreak@130 89 frames.cooldown = _G[name.."Cooldown"]
flickerstreak@130 90 frames.normalTexture = _G[name.."NormalTexture"]
flickerstreak@130 91
flickerstreak@130 92 -- resize to 30x30
flickerstreak@130 93 f:SetHeight(30)
flickerstreak@130 94 f:SetWidth(30)
flickerstreak@130 95
flickerstreak@130 96 -- move the cooldown around
flickerstreak@130 97 local cd = self.frames.cooldown
flickerstreak@130 98 cd:ClearAllPoints()
flickerstreak@130 99 cd:SetWidth(33)
flickerstreak@130 100 cd:SetHeight(33)
flickerstreak@130 101 cd:SetPoint("CENTER", f, "CENTER", -2, -1)
flickerstreak@130 102
flickerstreak@130 103 self.hotkey = frames.hotkey -- alias for Button methods
flickerstreak@130 104 self.border = frames.border -- alias for Button methods
flickerstreak@130 105
flickerstreak@130 106 -- set up the base action ID
flickerstreak@130 107 self:SetActionIDPool("pet",10)
flickerstreak@130 108 config.actionID = self:AcquireActionID(config.actionID, idHint, true)
flickerstreak@130 109
flickerstreak@130 110 -- attribute setup
flickerstreak@130 111 -- In order to get the full behavior of the pet buttons
flickerstreak@130 112 -- (petattack, toggle autocast, start/stop attack) we need
flickerstreak@130 113 -- to use a secure click proxy type instead of a "pet" type.
flickerstreak@130 114 f:SetAttribute("type","pet")
flickerstreak@130 115 f:SetAttribute("type2","click")
flickerstreak@130 116 f:SetAttribute("clickbutton2",_G["PetActionButton"..config.actionID])
flickerstreak@130 117 f:SetAttribute("action",config.actionID)
flickerstreak@130 118 f:SetAttribute("checkselfcast", true)
flickerstreak@130 119 f:SetAttribute("checkfocuscast", true)
flickerstreak@130 120
flickerstreak@130 121 -- non secure scripts
flickerstreak@130 122 f:SetScript("OnEvent", function(frame, ...) self:OnEvent(...) end)
flickerstreak@130 123 f:SetScript("OnEnter", function(frame) self:OnEnter() end)
flickerstreak@130 124 f:SetScript("OnLeave", function(frame) self:OnLeave() end)
flickerstreak@130 125 f:SetScript("OnAttributeChanged", function(frame, attr, value) self:OnAttributeChanged(attr, value) end)
flickerstreak@130 126 f:SetScript("PreClick", function(frame) self:PreClick() end)
flickerstreak@130 127 f:SetScript("OnDragStart", function(frame) self:OnDragStart() end)
flickerstreak@130 128 f:SetScript("OnReceiveDrag", function(frame) self:OnReceiveDrag() end)
flickerstreak@130 129
flickerstreak@130 130 -- secure handlers
flickerstreak@130 131 barFrame:WrapScript(f, "OnDragStart", _onDragStart)
flickerstreak@130 132 barFrame:WrapScript(f, "OnReceiveDrag", _onReceiveDrag)
flickerstreak@130 133
flickerstreak@130 134 -- event registration
flickerstreak@130 135 f:EnableMouse(true)
flickerstreak@130 136 f:RegisterForDrag("LeftButton", "RightButton")
flickerstreak@130 137 f:RegisterForClicks("AnyUp")
flickerstreak@130 138 for _, evt in pairs(eventList) do
flickerstreak@130 139 f:RegisterEvent(evt)
flickerstreak@130 140 end
flickerstreak@130 141
flickerstreak@130 142 -- attach to skinner
flickerstreak@130 143 bar:SkinButton(self,
flickerstreak@130 144 {
flickerstreak@130 145 AutoCast = f.autoCastShine,
flickerstreak@130 146 AutoCastable = f.autoCastTexture
flickerstreak@130 147 })
flickerstreak@130 148
flickerstreak@130 149 self:Refresh()
flickerstreak@130 150 f:Show()
flickerstreak@130 151
flickerstreak@130 152 return self
flickerstreak@130 153 end
flickerstreak@130 154
flickerstreak@130 155 function Pet:Destroy()
flickerstreak@130 156 self:GetFrame():UnregisterAllEvents()
flickerstreak@130 157 self:ReleaseActionID(self:GetConfig().actionID)
flickerstreak@130 158 Super.Destroy(self)
flickerstreak@130 159 end
flickerstreak@130 160
flickerstreak@130 161 function Pet:Refresh()
flickerstreak@130 162 Super.Refresh(self)
flickerstreak@130 163 self:Update()
flickerstreak@130 164 self:UpdateHotkey()
flickerstreak@130 165 end
flickerstreak@130 166
flickerstreak@130 167 function Pet:GetActionID()
flickerstreak@130 168 return self.config.actionID
flickerstreak@130 169 end
flickerstreak@130 170
flickerstreak@130 171 function Pet:SetActionID(id)
flickerstreak@130 172 if not InCombatLockdown() then
flickerstreak@130 173 if id < 0 or id > 10 then
flickerstreak@130 174 ReAction:UserError(L["Pet action ID range is 1-10"])
flickerstreak@130 175 return
flickerstreak@130 176 end
flickerstreak@130 177 self.config.actionID = id
flickerstreak@130 178 f:SetAttribute("clickbutton2",_G["PetActionButton"..id])
flickerstreak@130 179 f:SetAttribute("action",id)
flickerstreak@130 180 self:Update()
flickerstreak@130 181 self:UpdateHotkey()
flickerstreak@130 182 end
flickerstreak@130 183 end
flickerstreak@130 184
flickerstreak@130 185 function Pet:Update()
flickerstreak@130 186 local action = self.config.actionID
flickerstreak@130 187 local name, subtext, texture, isToken, isActive, autoCastAllowed, autoCastEnabled = GetPetActionInfo(action)
flickerstreak@130 188 local f = self:GetFrame()
flickerstreak@130 189 local icon = self.frames.icon
flickerstreak@130 190
flickerstreak@130 191 if isToken then
flickerstreak@130 192 icon:SetTexture(_G[texture])
flickerstreak@130 193 self.tooltipName = _G[name]
flickerstreak@130 194 else
flickerstreak@130 195 icon:SetTexture(texture)
flickerstreak@130 196 self.tooltipName = name
flickerstreak@130 197 end
flickerstreak@130 198
flickerstreak@130 199 self.isToken = isToken
flickerstreak@130 200 self.tooltipSubtext = subtext
flickerstreak@130 201 f:SetChecked( isActive and 1 or 0 )
flickerstreak@130 202
flickerstreak@130 203 if autoCastAllowed then
flickerstreak@130 204 f.autoCastTexture:Show()
flickerstreak@130 205 else
flickerstreak@130 206 f.autoCastTexture:Hide()
flickerstreak@130 207 end
flickerstreak@130 208
flickerstreak@130 209 if autoCastEnabled then
flickerstreak@130 210 AutoCastShine_AutoCastStart(f.autoCastShine)
flickerstreak@130 211 else
flickerstreak@130 212 AutoCastShine_AutoCastStop(f.autoCastShine)
flickerstreak@130 213 end
flickerstreak@130 214
flickerstreak@130 215 if texture then
flickerstreak@130 216 if GetPetActionSlotUsable(action) then
flickerstreak@130 217 SetDesaturation(icon,nil)
flickerstreak@130 218 else
flickerstreak@130 219 SetDesaturation(icon,1)
flickerstreak@130 220 end
flickerstreak@130 221 icon:Show()
flickerstreak@130 222 f:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2")
flickerstreak@130 223 else
flickerstreak@130 224 icon:Hide()
flickerstreak@130 225 f:SetNormalTexture("Interface\\Buttons\\UI-Quickslot")
flickerstreak@130 226 end
flickerstreak@130 227
flickerstreak@130 228 self:UpdateCooldown()
flickerstreak@130 229 end
flickerstreak@130 230
flickerstreak@130 231 function Pet:UpdateCooldown()
flickerstreak@130 232 CooldownFrame_SetTimer(self.frames.cooldown, GetPetActionCooldown(self.config.actionID))
flickerstreak@130 233 end
flickerstreak@130 234
flickerstreak@130 235 function Pet:SetTooltip()
flickerstreak@130 236 if self.tooltipName then
flickerstreak@130 237 local f = self:GetFrame()
flickerstreak@130 238 local uber = GetCVar("UberTooltips")
flickerstreak@130 239 if self.isToken or (uber == "0") then
flickerstreak@130 240 if uber == "0" then
flickerstreak@130 241 GameTooltip:SetOwner(f, "ANCHOR_RIGHT")
flickerstreak@130 242 else
flickerstreak@130 243 GameTooltip_SetDefaultAnchor(GameTooltip, f)
flickerstreak@130 244 end
flickerstreak@130 245 GameTooltip:SetText(self.tooltipName)
flickerstreak@130 246 if self.tooltipSubtext then
flickerstreak@130 247 GameTooltip:AddLine(self.tooltipSubtext, "", 0.5, 0.5, 0.5)
flickerstreak@130 248 end
flickerstreak@130 249 GameTooltip:Show()
flickerstreak@130 250 else
flickerstreak@130 251 GameTooltip_SetDefaultAnchor(GameTooltip, f)
flickerstreak@130 252 GameTooltip:SetPetAction(self.config.actionID)
flickerstreak@130 253 end
flickerstreak@130 254 else
flickerstreak@130 255 GameTooltip:Hide()
flickerstreak@130 256 end
flickerstreak@130 257 end
flickerstreak@130 258
flickerstreak@130 259 function Pet:OnEvent(event, unit)
flickerstreak@130 260 if event =="PET_BAR_UPDATE_COOLDOWN" then
flickerstreak@130 261 self:UpdateCooldown()
flickerstreak@130 262 elseif event == "UPDATE_BINDINGS" then
flickerstreak@130 263 self:UpdateHotkey()
flickerstreak@130 264 elseif event == "UNIT_PET" then
flickerstreak@130 265 if unit == "player" then
flickerstreak@130 266 self:Update()
flickerstreak@130 267 end
flickerstreak@130 268 elseif event == "UNIT_FLAGS" or event == "UNIT_AURA" then
flickerstreak@130 269 if unit == "pet" then
flickerstreak@130 270 self:Update()
flickerstreak@130 271 end
flickerstreak@130 272 else
flickerstreak@130 273 self:Update()
flickerstreak@130 274 end
flickerstreak@130 275 end
flickerstreak@130 276
flickerstreak@130 277 function Pet:OnEnter()
flickerstreak@130 278 self:SetTooltip()
flickerstreak@130 279 end
flickerstreak@130 280
flickerstreak@130 281 function Pet:OnLeave()
flickerstreak@130 282 GameTooltip:Hide()
flickerstreak@130 283 end
flickerstreak@130 284
flickerstreak@130 285 function Pet:OnAttributeChanged(attr,value)
flickerstreak@130 286 self:Update()
flickerstreak@130 287 end
flickerstreak@130 288
flickerstreak@130 289 function Pet:PreClick()
flickerstreak@130 290 self:GetFrame():SetChecked(0)
flickerstreak@130 291 end
flickerstreak@130 292
flickerstreak@130 293 function Pet:OnDragStart()
flickerstreak@130 294 self:SetChecked(0)
flickerstreak@130 295 self:Update()
flickerstreak@130 296 end
flickerstreak@130 297
flickerstreak@130 298 function Pet:OnReceiveDrag()
flickerstreak@130 299 self:SetChecked(0)
flickerstreak@130 300 self:Update()
flickerstreak@130 301 end
flickerstreak@130 302