-
Notifications
You must be signed in to change notification settings - Fork 3
/
MisdirectButton.lua
81 lines (72 loc) · 2.19 KB
/
MisdirectButton.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
---@class AddonNamespace
local addon = select(2, ...)
---@class MisdirectButton
---@field index integer
---@field targetSelector TargetSelector
local MisdirectButton = {}
local metatable = {
__index = MisdirectButton,
}
---@param name string
---@param spell integer
local function createButton(name, spell)
local button = CreateFrame("Button", name, UIParent, "SecureActionButtonTemplate")
button:Hide()
button:SetAttribute("type", "spell")
button:SetAttribute("spell", spell)
button:SetAttribute("checkselfcast", false)
button:SetAttribute("checkfocuscast", false)
button:SetAttribute("allowVehicleTarget", false)
if WOW_PROJECT_ID == WOW_PROJECT_MAINLINE then
-- Ensures the action always fires on Down, regardless of the ActionButtonUseKeyDown cvar
button:SetAttribute("pressAndHoldAction", "1")
button:SetAttribute("typerelease", "spell")
end
button:RegisterForClicks("LeftButtonDown", "LeftButtonUp")
return button
end
---@param spell integer Spell id
---@param index integer Match index to target
---@return table
function addon:CreateMisdirectButton(spell, index)
local buttonName = string.format("TankMDButton%d", index)
local legacyButtonName = "MisdirectTankButton"
if index > 1 then
legacyButtonName = string.format("MisdirectTank%dButton", index)
end
---@class MisdirectButton
local misdirectButton = setmetatable({
index = index,
button = createButton(buttonName, spell),
legacyButton = createButton(legacyButtonName, spell),
}, metatable)
return misdirectButton
end
---@param target string|nil
function MisdirectButton:SetTarget(target)
if target then
self:SetEnabled(true)
self.button:SetAttribute("unit", target)
self.legacyButton:SetAttribute("unit", target)
else
self:SetEnabled(false)
end
end
---@return string
function MisdirectButton:GetTarget()
return self.button:GetAttribute("unit")
end
---@private
function MisdirectButton:SetEnabled(enabled)
if enabled then
self.button:SetAttribute("type", "spell")
self.legacyButton:SetAttribute("type", "spell")
else
self.button:SetAttribute("type", nil)
self.legacyButton:SetAttribute("type", nil)
end
end
---@return boolean
function MisdirectButton:IsEnabled()
return self.button:GetAttribute("type") ~= nil
end