Mercurial > wow > reaction
comparison PetActionButton.lua @ 257:920d17851a93 stable
Merge 1.1 beta 4 to stable
author | Flick |
---|---|
date | Tue, 12 Apr 2011 16:06:31 -0700 |
parents | 65f2805957a0 |
children | c918ff9ac787 |
comparison
equal
deleted
inserted
replaced
210:b2b105747466 | 257:920d17851a93 |
---|---|
1 local addonName, addonTable = ... | |
2 local ReAction = addonTable.ReAction | |
3 local L = ReAction.L | |
4 local _G = _G | |
5 local CreateFrame = CreateFrame | |
6 local format = string.format | |
7 local GetCVar = GetCVar | |
8 local InCombatLockdown = InCombatLockdown | |
9 local GetPetActionInfo = GetPetActionInfo | |
10 local GetPetActionSlotUsable = GetPetActionSlotUsable | |
11 local GetPetActionCooldown = GetPetActionCooldown | |
12 local AutoCastShine_AutoCastStart = AutoCastShine_AutoCastStart | |
13 local AutoCastShine_AutoCastStop = AutoCastShine_AutoCastStop | |
14 local SetDesaturation = SetDesaturation | |
15 local CooldownFrame_SetTimer = CooldownFrame_SetTimer | |
16 local GameTooltip_SetDefaultAnchor = GameTooltip_SetDefaultAnchor | |
17 | |
18 -- | |
19 -- Secure snippets | |
20 -- These are run within the context of the bar's sandbox, as the | |
21 -- buttons themselves do not have their own sandbox. | |
22 -- | |
23 local _onDragStart = -- function(self, button, kind, value, ...) | |
24 [[ | |
25 if lockButtons and (PlayerInCombat() or not lockButtonsCombat) and not IsModifiedClick("PICKUPACTION") then | |
26 return kind, value, ... | |
27 else | |
28 return "petaction", self:GetAttribute("action") | |
29 end | |
30 ]] | |
31 | |
32 local _onReceiveDrag = -- function(self, button, kind, value, ...) | |
33 [[ | |
34 if kind then -- pet spells on the cursor return nil from GetCursorInfo(), which is very strange | |
35 return kind, value, ... | |
36 end | |
37 return "petaction", self:GetAttribute("action") | |
38 ]] | |
39 | |
40 -- | |
41 -- private | |
42 -- | |
43 local eventList = { | |
44 "PLAYER_CONTROL_LOST", | |
45 "PLAYER_CONTROL_GAINED", | |
46 "PLAYER_FARSIGHT_FOCUS_CHANGED", | |
47 "UNIT_PET", | |
48 "UNIT_FLAGS", | |
49 "UNIT_AURA", | |
50 "PET_BAR_UPDATE", | |
51 "PET_BAR_UPDATE_COOLDOWN", | |
52 "PET_BAR_UPDATE_USABLE", | |
53 "UPDATE_BINDINGS", | |
54 } | |
55 | |
56 -- | |
57 -- Pet Action Button class | |
58 -- | |
59 local buttonTypeID = "PetAction" | |
60 local Super = ReAction.Button | |
61 local Pet = setmetatable( | |
62 { | |
63 defaultBarConfig = { | |
64 type = buttonTypeID, | |
65 btnWidth = 30, | |
66 btnHeight = 30, | |
67 btnRows = 1, | |
68 btnColumns = 10, | |
69 spacing = 8, | |
70 buttons = { } | |
71 }, | |
72 | |
73 barType = L["Pet Action Bar"], | |
74 buttonTypeID = buttonTypeID | |
75 }, | |
76 { __index = Super } ) | |
77 | |
78 ReAction.Button.PetAction = Pet | |
79 ReAction:RegisterBarType(Pet) | |
80 | |
81 function Pet:New( config, bar, idx, idHint ) | |
82 local name = format("ReAction_%s_PetAction_%d",bar:GetName(),idx) | |
83 | |
84 self = Super.New(self, name, config, bar, idx, "SecureActionButtonTemplate, ActionButtonTemplate" ) | |
85 | |
86 local f = self:GetFrame() | |
87 if not f.autoCastTexture then | |
88 -- store autocast stuff with the frame for recycling | |
89 local tex = f:CreateTexture(nil,"OVERLAY") | |
90 tex:SetTexture([[Interface\Buttons\UI-AutoCastableOverlay]]) | |
91 tex:SetHeight(58) | |
92 tex:SetWidth(58) | |
93 tex:SetPoint("CENTER") | |
94 f.autoCastTexture = tex | |
95 f.autoCastShine = CreateFrame("Frame",name.."Shine",f,"AutoCastShineTemplate") -- create after autocast texture so it's on top | |
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 -- resize to 30x30 | |
103 f:SetHeight(30) | |
104 f:SetWidth(30) | |
105 local nt = _G[name.."NormalTexture"] | |
106 nt:SetHeight(54) | |
107 nt:SetWidth(54) | |
108 end | |
109 local barFrame = bar:GetFrame() | |
110 | |
111 -- set up the base action ID | |
112 self:SetActionIDPool("pet",10) | |
113 config.actionID = self:AcquireActionID(config.actionID, idHint, true) | |
114 | |
115 -- attribute setup | |
116 -- There's no secure way to do PetAutoCastToggle by actionID, so use | |
117 -- a click-through proxy to the Blizzard pet buttons for right-click | |
118 -- Note that technically this doesn't do PetStopAttack() when | |
119 -- IsPetAttackActive() is true: however that's only true when using | |
120 -- Eyes of the Beast and appears not to really do anything (at least | |
121 -- I can't find any difference) | |
122 f:SetAttribute("type","pet") | |
123 f:SetAttribute("type2","click") | |
124 f:SetAttribute("clickbutton2",_G["PetActionButton"..config.actionID]) | |
125 f:SetAttribute("action",config.actionID) | |
126 f:SetAttribute("checkselfcast", true) | |
127 f:SetAttribute("checkfocuscast", true) | |
128 | |
129 -- non secure scripts | |
130 f:SetScript("OnEvent", function(frame, ...) self:OnEvent(...) end) | |
131 f:SetScript("OnEnter", function(frame) self:OnEnter() end) | |
132 f:SetScript("OnLeave", function(frame) self:OnLeave() end) | |
133 f:SetScript("OnAttributeChanged", function(frame, attr, value) self:OnAttributeChanged(attr, value) end) | |
134 f:SetScript("PreClick", function(frame) self:PreClick() end) | |
135 f:SetScript("OnDragStart", function(frame) self:OnDragStart() end) | |
136 f:SetScript("OnReceiveDrag", function(frame) self:OnReceiveDrag() end) | |
137 | |
138 -- secure handlers | |
139 barFrame:WrapScript(f, "OnDragStart", _onDragStart) | |
140 barFrame:WrapScript(f, "OnReceiveDrag", _onReceiveDrag) | |
141 | |
142 -- event registration | |
143 f:EnableMouse(true) | |
144 f:RegisterForDrag("LeftButton", "RightButton") | |
145 f:RegisterForClicks("AnyUp") | |
146 for _, evt in pairs(eventList) do | |
147 f:RegisterEvent(evt) | |
148 end | |
149 | |
150 -- attach to skinner | |
151 bar:SkinButton(self, | |
152 { | |
153 AutoCast = f.autoCastShine, | |
154 AutoCastable = f.autoCastTexture | |
155 }) | |
156 | |
157 self:Refresh() | |
158 f:Show() | |
159 | |
160 return self | |
161 end | |
162 | |
163 function Pet:SetupBar(bar) | |
164 Super.SetupBar(self,bar) | |
165 | |
166 -- auto show/hide when pet exists | |
167 bar:RegisterUnitWatch("pet",true) | |
168 | |
169 self:UpdateButtonLock(bar) | |
170 end | |
171 | |
172 function Pet:UpdateButtonLock(bar) | |
173 local f = bar:GetFrame() | |
174 f:SetAttribute("lockbuttons",bar.config.lockButtons) | |
175 f:SetAttribute("lockbuttonscombat",bar.config.lockButtonsCombat) | |
176 f:Execute( | |
177 [[ | |
178 lockButtons = self:GetAttribute("lockbuttons") | |
179 lockButtonsCombat = self:GetAttribute("lockbuttonscombat") | |
180 ]]) | |
181 end | |
182 | |
183 function Pet:Refresh() | |
184 Super.Refresh(self) | |
185 self:Update() | |
186 self:UpdateHotkey() | |
187 end | |
188 | |
189 function Pet:GetActionID() | |
190 return self.config.actionID | |
191 end | |
192 | |
193 function Pet:SetActionID(id) | |
194 if not InCombatLockdown() then | |
195 if id < 0 or id > 10 then | |
196 ReAction:UserError(L["Pet action ID range is 1-10"]) | |
197 return | |
198 end | |
199 self.config.actionID = id | |
200 f:SetAttribute("clickbutton2",_G["PetActionButton"..id]) | |
201 f:SetAttribute("action",id) | |
202 self:Update() | |
203 self:UpdateHotkey() | |
204 end | |
205 end | |
206 | |
207 function Pet:Update() | |
208 local action = self.config.actionID | |
209 local name, subtext, texture, isToken, isActive, autoCastAllowed, autoCastEnabled = GetPetActionInfo(action) | |
210 local f = self:GetFrame() | |
211 local icon = self.frames.icon | |
212 | |
213 if isToken then | |
214 icon:SetTexture(_G[texture]) | |
215 self.tooltipName = _G[name] | |
216 else | |
217 icon:SetTexture(texture) | |
218 self.tooltipName = name | |
219 end | |
220 | |
221 self.isToken = isToken | |
222 self.tooltipSubtext = subtext | |
223 f:SetChecked( isActive and 1 or 0 ) | |
224 | |
225 if autoCastAllowed then | |
226 f.autoCastTexture:Show() | |
227 else | |
228 f.autoCastTexture:Hide() | |
229 end | |
230 | |
231 if autoCastEnabled then | |
232 AutoCastShine_AutoCastStart(f.autoCastShine) | |
233 else | |
234 AutoCastShine_AutoCastStop(f.autoCastShine) | |
235 end | |
236 | |
237 if texture then | |
238 if GetPetActionSlotUsable(action) then | |
239 SetDesaturation(icon,nil) | |
240 else | |
241 SetDesaturation(icon,1) | |
242 end | |
243 icon:Show() | |
244 f:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2") | |
245 else | |
246 icon:Hide() | |
247 f:SetNormalTexture("Interface\\Buttons\\UI-Quickslot") | |
248 end | |
249 | |
250 self:UpdateCooldown() | |
251 end | |
252 | |
253 function Pet:UpdateCooldown() | |
254 CooldownFrame_SetTimer(self.frames.cooldown, GetPetActionCooldown(self.config.actionID)) | |
255 end | |
256 | |
257 function Pet:SetTooltip() | |
258 if self.tooltipName then | |
259 local f = self:GetFrame() | |
260 local uber = GetCVar("UberTooltips") | |
261 if self.isToken or (uber == "0") then | |
262 if uber == "0" then | |
263 GameTooltip:SetOwner(f, "ANCHOR_RIGHT") | |
264 else | |
265 GameTooltip_SetDefaultAnchor(GameTooltip, f) | |
266 end | |
267 GameTooltip:SetText(self.tooltipName) | |
268 if self.tooltipSubtext then | |
269 GameTooltip:AddLine(self.tooltipSubtext, "", 0.5, 0.5, 0.5) | |
270 end | |
271 GameTooltip:Show() | |
272 else | |
273 GameTooltip_SetDefaultAnchor(GameTooltip, f) | |
274 GameTooltip:SetPetAction(self.config.actionID) | |
275 end | |
276 else | |
277 GameTooltip:Hide() | |
278 end | |
279 end | |
280 | |
281 function Pet:OnEvent(event, unit) | |
282 if event =="PET_BAR_UPDATE_COOLDOWN" then | |
283 self:UpdateCooldown() | |
284 elseif event == "UPDATE_BINDINGS" then | |
285 self:UpdateHotkey() | |
286 elseif event == "UNIT_PET" then | |
287 if unit == "player" then | |
288 self:Update() | |
289 end | |
290 elseif event == "UNIT_FLAGS" or event == "UNIT_AURA" then | |
291 if unit == "pet" then | |
292 self:Update() | |
293 end | |
294 else | |
295 self:Update() | |
296 end | |
297 end | |
298 | |
299 function Pet:OnEnter() | |
300 self:SetTooltip() | |
301 end | |
302 | |
303 function Pet:OnLeave() | |
304 GameTooltip:Hide() | |
305 end | |
306 | |
307 function Pet:OnAttributeChanged(attr,value) | |
308 self:Update() | |
309 end | |
310 | |
311 function Pet:PreClick() | |
312 self:GetFrame():SetChecked(0) | |
313 end | |
314 | |
315 function Pet:OnDragStart() | |
316 self:SetChecked(0) | |
317 self:Update() | |
318 end | |
319 | |
320 function Pet:OnReceiveDrag() | |
321 self:SetChecked(0) | |
322 self:Update() | |
323 end | |
324 |