comparison modules/PetAction.lua @ 130:6e4a11b9d290

Converted PetAction to new class layout
author Flick <flickerstreak@gmail.com>
date Fri, 06 Mar 2009 23:44:55 +0000
parents fb48811a8736
children 901c91dc1bf2
comparison
equal deleted inserted replaced
129:28b430de5875 130:6e4a11b9d290
20 20
21 -- module declaration 21 -- module declaration
22 local moduleID = "PetAction" 22 local moduleID = "PetAction"
23 local module = ReAction:NewModule( moduleID ) 23 local module = ReAction:NewModule( moduleID )
24 24
25 -- Button class declaration 25 -- Button class
26 local Button = { } 26 local Button = ReAction.Button.PetAction
27 27
28 -- private 28 -- private
29 local function UpdateButtonLock(bar) 29 local function UpdateButtonLock(bar)
30 local f = bar:GetFrame() 30 local f = bar:GetFrame()
31 f:SetAttribute("lockbuttons",bar.config.lockButtons) 31 f:SetAttribute("lockbuttons",bar.config.lockButtons)
105 for i = 1, n do 105 for i = 1, n do
106 if btnCfg[i] == nil then 106 if btnCfg[i] == nil then
107 btnCfg[i] = {} 107 btnCfg[i] = {}
108 end 108 end
109 if btns[i] == nil then 109 if btns[i] == nil then
110 local success, r = pcall(Button.New,Button,bar,i,btnCfg[i]) 110 local success, r = pcall(Button.New,Button,i,btnCfg[i],bar,i>1 and btnCfg[i-1].actionID)
111 if success and r then 111 if success and r then
112 btns[i] = r 112 btns[i] = r
113 bar:AddButton(i,r) 113 bar:AddButton(i,r)
114 else 114 else
115 n = i - 1 115 n = i - 1
153 b[newname], b[oldname] = b[oldname], nil 153 b[newname], b[oldname] = b[oldname], nil
154 end 154 end
155 155
156 156
157 function module:OnConfigModeChanged(event, mode) 157 function module:OnConfigModeChanged(event, mode)
158 for _, buttons in pairs(self.buttons) do
159 for _, b in pairs(buttons) do
160 b:ShowActionIDLabel(mode)
161 end
162 end
163 for _, bar in ReAction:IterateBars() do 158 for _, bar in ReAction:IterateBars() do
164 if bar and self.buttons[bar] then 159 if bar and self.buttons[bar] then
160 for b in bar:IterateButtons() do
161 b:UpdateActionIDLabel(mode)
162 end
165 local f = bar:GetFrame() 163 local f = bar:GetFrame()
166 if mode then 164 if mode then
167 UnregisterUnitWatch(f) 165 UnregisterUnitWatch(f)
168 f:Show() 166 f:Show()
169 else 167 else
201 config = bar.config 199 config = bar.config
202 }, meta) 200 }, meta)
203 end 201 end
204 202
205 function Handler:GetLockButtons() 203 function Handler:GetLockButtons()
206 return LOCK_ACTIONBAR == "1" or self.config.lockButtons 204 return self.config.lockButtons
207 end 205 end
208 206
209 function Handler:SetLockButtons(info, value) 207 function Handler:SetLockButtons(info, value)
210 self.config.lockButtons = value 208 self.config.lockButtons = value
211 UpdateButtonLock(self.bar) 209 UpdateButtonLock(self.bar)
212 end 210 end
213 211
214 function Handler:LockButtonsDisabled()
215 return LOCK_ACTIONBAR == "1"
216 end
217
218 function Handler:GetLockButtonsCombat() 212 function Handler:GetLockButtonsCombat()
219 return self.config.lockButtonsCombat 213 return self.config.lockButtonsCombat
220 end 214 end
221 215
222 function Handler:SetLockButtonsCombat(info, value) 216 function Handler:SetLockButtonsCombat(info, value)
223 self.config.lockButtonsCombat = value 217 self.config.lockButtonsCombat = value
224 UpdateButtonLock(self.bar) 218 UpdateButtonLock(self.bar)
225 end 219 end
226 220
227 function Handler:LockButtonsCombatDisabled() 221 function Handler:LockButtonsCombatDisabled()
228 return LOCK_ACTIONBAR == "1" or not self.config.lockButtons 222 return not self.config.lockButtons
229 end 223 end
230 224
231 225
232 function module:GetBarOptions(bar) 226 function module:GetBarOptions(bar)
233 if bar.config.type == moduleID then 227 if bar.config.type == moduleID then
239 lockButtons = { 233 lockButtons = {
240 name = L["Lock Buttons"], 234 name = L["Lock Buttons"],
241 desc = L["Prevents picking up/dragging actions.|nNOTE: This setting is overridden by the global setting in Blizzard's Action Buttons tab"], 235 desc = L["Prevents picking up/dragging actions.|nNOTE: This setting is overridden by the global setting in Blizzard's Action Buttons tab"],
242 order = 2, 236 order = 2,
243 type = "toggle", 237 type = "toggle",
244 disabled = "LockButtonsDisabled",
245 get = "GetLockButtons", 238 get = "GetLockButtons",
246 set = "SetLockButtons", 239 set = "SetLockButtons",
247 }, 240 },
248 lockOnlyCombat = { 241 lockOnlyCombat = {
249 name = L["Only in Combat"], 242 name = L["Only in Combat"],
258 } 251 }
259 end 252 end
260 end 253 end
261 254
262 255
263
264 ------ Button class ------
265
266 -- use-count of action IDs
267 local nActionIDs = NUM_PET_ACTION_SLOTS
268 local ActionIDList = setmetatable( {}, {
269 __index = function(self, idx)
270 if idx == nil then
271 for i = 1, nActionIDs do
272 if rawget(self,i) == nil then
273 rawset(self,i,1)
274 return i
275 end
276 end
277 error("ran out of pet action IDs")
278 else
279 local c = rawget(self,idx) or 0
280 rawset(self,idx,c+1)
281 return idx
282 end
283 end,
284 __newindex = function(self,idx,value)
285 if value == nil then
286 value = rawget(self,idx)
287 if value == 1 then
288 value = nil
289 elseif value then
290 value = value - 1
291 end
292 end
293 rawset(self,idx,value)
294 end
295 })
296
297 local frameRecycler = {}
298 local trash = CreateFrame("Frame")
299
300 local function GetActionName(f)
301 local b = f and f._reactionButton
302 if b then
303 return format("%s:%s", b.bar:GetName(), b.idx)
304 end
305 end
306
307 local function GetHotkey(f)
308 return KB:ToShortKey(GetBindingKey(format("CLICK %s:LeftButton",f:GetName())))
309 end
310
311 local function OnEnter( self )
312 if ReAction:GetKeybindMode() then
313 KB:Set(self)
314 elseif self.tooltipName then
315 local uber = GetCVar("UberTooltips")
316 if self.isToken or (uber == "0") then
317 if uber == "0" then
318 GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
319 else
320 GameTooltip_SetDefaultAnchor(GameTooltip, self)
321 end
322 GameTooltip:SetText(self.tooltipName)
323 if self.tooltipSubtext then
324 GameTooltip:AddLine(self.tooltipSubtext, "", 0.5, 0.5, 0.5)
325 end
326 GameTooltip:Show()
327 else
328 GameTooltip_SetDefaultAnchor(GameTooltip, self)
329 GameTooltip:SetPetAction(self:GetID())
330 end
331 end
332 end
333
334 local function OnLeave()
335 GameTooltip:Hide()
336 end
337
338 local meta = { __index = Button }
339
340 function Button:New( bar, idx, config )
341 -- create new self
342 self = setmetatable(
343 {
344 bar = bar,
345 idx = idx,
346 config = config,
347 }, meta )
348
349 local name = config.name or ("ReAction_%s_%s_%d"):format(bar:GetName(),moduleID,idx)
350 config.name = name
351 self.name = name
352 config.actionID = ActionIDList[config.actionID] -- gets a free one if none configured
353
354 -- have to recycle frames with the same name:
355 -- otherwise you either get references to old textures because named CreateFrame()
356 -- doesn't overwrite existing globals. Can't set them to nil in the global table,
357 -- as it causes taint.
358 local parent = bar:GetFrame()
359 local f = frameRecycler[name]
360 if f then
361 f:SetParent(parent)
362 else
363 f = CreateFrame("CheckButton", name, parent, "PetActionButtonTemplate")
364 -- ditch the old hotkey text because it's tied in ActionButton_Update() to the
365 -- standard binding. We use override bindings.
366 local hotkey = _G[name.."HotKey"]
367 hotkey:SetParent(trash)
368 hotkey = f:CreateFontString(nil, "ARTWORK", "NumberFontNormalSmallGray")
369 hotkey:SetWidth(36)
370 hotkey:SetHeight(18)
371 hotkey:SetJustifyH("RIGHT")
372 hotkey:SetJustifyV("TOP")
373 hotkey:SetPoint("TOPLEFT",f,"TOPLEFT",-2,-2)
374 f.hotkey = hotkey
375 f:HookScript("OnDragStart", function() self:Update() end)
376 f:HookScript("OnReceiveDrag", function() self:Update() end)
377 f:SetScript("OnEnter", OnEnter)
378 f:SetScript("OnLeave", OnLeave)
379 end
380 if config.actionID then
381 f:SetID(config.actionID) -- PetActionButtonTemplate isn't a proper SecureActionButton
382 end
383 f:SetFrameStrata("MEDIUM")
384 self.frame = f
385 self.icon = _G[("%sIcon"):format(name)]
386 self.acTex = _G[("%sAutoCastable"):format(name)]
387 self.acModel = _G[("%sShine"):format(name)]
388 self.cooldown = _G[("%sCooldown"):format(name)]
389 self.hotkey = f.hotkey
390 self.border = _G[("%sBorder"):format(name)]
391
392 f._reactionButton = self
393
394 f:RegisterEvent("PLAYER_CONTROL_LOST")
395 f:RegisterEvent("PLAYER_CONTROL_GAINED")
396 f:RegisterEvent("PLAYER_FARSIGHT_FOCUS_CHANGED")
397 f:RegisterEvent("UNIT_PET")
398 f:RegisterEvent("UNIT_FLAGS")
399 f:RegisterEvent("UNIT_AURA")
400 f:RegisterEvent("PET_BAR_UPDATE")
401 f:RegisterEvent("PET_BAR_UPDATE_COOLDOWN")
402
403 f:SetScript("OnEvent",
404 function(event,arg1)
405 if event =="PET_BAR_UPDATE_COOLDOWN" then
406 self:UpdateCooldown()
407 elseif event == "UPDATE_BINDINGS" then
408 self:UpdateHotkey()
409 else
410 self:Update()
411 end
412 end)
413
414 -- install drag wrappers to lock buttons
415 bar:GetFrame():WrapScript(f, "OnDragStart",
416 -- OnDragStart(self, button, kind, value, ...)
417 [[
418 if lockButtons and (PlayerInCombat() or not lockButtonsCombat) and not IsModifiedClick("PICKUPACTION") then
419 return "clear"
420 end
421 ]])
422
423 -- attach to skinner
424 bar:SkinButton(self,
425 {
426 HotKey = self.hotkey,
427 }
428 )
429
430 self:Refresh()
431 self:UpdateHotkey()
432 self:SetKeybindMode(ReAction:GetKeybindMode())
433
434 return self
435 end
436
437 function Button:Destroy()
438 local f = self.frame
439 f:UnregisterAllEvents()
440 f:Hide()
441 f:SetParent(UIParent)
442 f:ClearAllPoints()
443 if self.name then
444 frameRecycler[self.name] = f
445 _G[self.name] = nil
446 end
447 if self.config.actionID then
448 ActionIDList[self.config.actionID] = nil
449 end
450 f._reactionButton = nil
451 self.frame = nil
452 self.config = nil
453 self.bar = nil
454 end
455
456 function Button:Refresh()
457 self.bar:PlaceButton(self, 30, 30)
458 self:Update()
459 self:UpdateHotkey()
460 self.frame:Show()
461 end
462
463 function Button:GetFrame()
464 return self.frame
465 end
466
467 function Button:GetName()
468 return self.name
469 end
470
471 function Button:GetConfig()
472 return self.config
473 end
474
475 function Button:GetActionID()
476 return self.config.actionID
477 end
478
479 function Button:Update()
480 local id = self.frame:GetID()
481 local name, subtext, texture, isToken, isActive, autoCastAllowed, autoCastEnabled = GetPetActionInfo(id)
482 local f = self.frame
483
484 if isToken then
485 self.icon:SetTexture(_G[texture])
486 f.tooltipName = _G[name]
487 else
488 self.icon:SetTexture(texture)
489 f.tooltipName = name
490 end
491
492 f.isToken = isToken
493 f.tooltipSubtext = subtext
494 f:SetChecked( isActive and 1 or 0)
495
496 if autoCastAllowed then
497 self.acTex:Show()
498 else
499 self.acTex:Hide()
500 end
501
502 if autoCastEnabled then
503 AutoCastShine_AutoCastStart(self.acModel)
504 else
505 AutoCastShine_AutoCastStop(self.acModel)
506 end
507
508 if texture then
509 if GetPetActionSlotUsable(id) then
510 SetDesaturation(self.icon,nil)
511 else
512 SetDesaturation(self.icon,1)
513 end
514 self.icon:Show()
515 f:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2")
516 else
517 self.icon:Hide()
518 f:SetNormalTexture("Interface\\Buttons\\UI-Quickslot")
519 end
520
521 self:UpdateCooldown()
522 end
523
524 function Button:UpdateCooldown()
525 local start, duration, enable = GetPetActionCooldown(self.frame:GetID())
526 CooldownFrame_SetTimer(self.cooldown, start, duration, enable)
527 end
528
529 function Button:UpdateHotkey()
530 self.hotkey:SetText(GetHotkey(self.frame) or "")
531 end
532
533 function Button:ShowActionIDLabel(show)
534 if show then
535 -- store the action ID label in the frame due to frame recycling
536 if not self.actionIDLabel and self:GetActionID() then
537 local label = self.frame:CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
538 label:SetAllPoints()
539 label:SetJustifyH("CENTER")
540 label:SetShadowColor(0,0,0,1)
541 label:SetShadowOffset(2,-2)
542 label:SetText(tostring(self:GetActionID()))
543 self.actionIDLabel = label
544 end
545 self.actionIDLabel:Show()
546 elseif self.actionIDLabel then
547 self.actionIDLabel:Hide()
548 end
549 end
550
551
552 function Button:SetKeybindMode(mode)
553 if mode then
554 local f = self.frame
555 f.GetActionName = GetActionName
556 f.GetHotkey = GetHotkey
557 self.border:SetVertexColor(KB:GetColorKeyBoundMode())
558 self.border:Show()
559 else
560 self.border:Hide()
561 end
562 end
563