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