Repository navigation
Expand file tree
/
Copy pathengine.lua
More file actions
337 lines (278 loc) · 11.3 KB
/
Copy pathengine.lua
File metadata and controls
337 lines (278 loc) · 11.3 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
--[[--------------------------------------------------------------------------
Clickcast - engine.lua
Unit resolution, frame hooking and the actual click handler.
The click handler mirrors pfUI's pfUI.uf:ClickAction() with one deliberate
difference: when no click action is configured for the pressed
button+modifier combination, the frame's *original* OnClick script is run
instead. That keeps every existing unit frame behaving exactly as it did
before Clickcast was installed, and only overrides the clicks you bind.
----------------------------------------------------------------------------]]
local CC = Clickcast
CC.hooked = {} -- [frame] = true
CC.oldclick = {} -- [frame] = original OnClick script (may be nil)
CC.hasmouse = {} -- [frame] = true if the frame handles its own mouse events
-- buttons the original OnClick script is allowed to receive. Clickcast widens
-- every hooked frame to five buttons; frames were usually only registered for
-- left/right, so unbound extra buttons must stay silent instead of leaking
-- into a handler that never expected them.
local passthrough_buttons = {
["LeftButton"] = true,
["RightButton"] = true,
}
-- ---------------------------------------------------------------------------
-- unit strings
-- ---------------------------------------------------------------------------
local staticunits = {
["player"] = "player",
["pet"] = "pet",
["pettarget"] = "pettarget",
["target"] = "target",
["targettarget"] = "targettarget",
["targettargettarget"] = "tttarget",
["focus"] = "focus",
["focustarget"] = "focustarget",
["mouseover"] = "other",
}
function CC:IsValidUnit(unit)
if not unit or unit == "" or type(unit) ~= "string" then return nil end
if staticunits[unit] then return true end
if string.find(unit, "^party[1-4]$") then return true end
if string.find(unit, "^partypet[1-4]$") then return true end
if string.find(unit, "^party[1-4]target$") then return true end
if string.find(unit, "^raid%d+$") then return true end
if string.find(unit, "^raidpet%d+$") then return true end
if string.find(unit, "^raid%d+target$") then return true end
return nil
end
function CC:GetCategory(unit)
if not unit then return "other" end
if staticunits[unit] then return staticunits[unit] end
if string.find(unit, "^partypet%d") then return "partypet" end
if string.find(unit, "^party%d+target$") then return "partytarget" end
if string.find(unit, "^party%d") then return "party" end
if string.find(unit, "^raidpet%d") then return "raidpet" end
if string.find(unit, "^raid%d+target$") then return "raidtarget" end
if string.find(unit, "^raid%d") then return "raid" end
return "other"
end
-- Blizzard's raid frames don't expose a unit id we can rely on, so the player
-- name shown on the button is matched against the raid roster instead.
local function ResolveRaidButton(frame)
if not frame.GetName then return nil end
local name = frame:GetName()
if not name then return nil end
if not string.find(name, "^RaidGroupButton%d+$")
and not string.find(name, "^RaidPullout%d+Button%d+$") then return nil end
local label = getglobal(name .. "Name")
local player = label and label.GetText and label:GetText()
if not player or player == "" then return nil end
for i = 1, GetNumRaidMembers() do
if UnitName("raid" .. i) == player then return "raid" .. i end
end
return nil
end
-- Works out which unit a frame represents. Supports the conventions used by
-- pfUI (label/id), Blizzard and most third party frames (unit / unitid), and
-- explicit registrations through Clickcast:Register(frame, unit).
function CC:GetUnit(frame)
if not frame then return nil end
local unit
if type(frame.ccUnit) == "string" then
unit = frame.ccUnit
end
-- pfUI style: frame.label .. frame.id
if not unit and type(frame.label) == "string" and frame.label ~= "" then
unit = frame.label .. (type(frame.id) == "string" and frame.id or "")
end
if not unit and type(frame.unit) == "string" then unit = frame.unit end
if not unit and type(frame.unitid) == "string" then unit = frame.unitid end
if not unit and type(frame.unitId) == "string" then unit = frame.unitId end
if not unit and type(frame.unitstr) == "string" then unit = frame.unitstr end
if not unit and frame.GetAttribute then
local ok, value = pcall(frame.GetAttribute, frame, "unit")
if ok and type(value) == "string" then unit = value end
end
if not unit then unit = ResolveRaidButton(frame) end
if unit then unit = string.lower(unit) end
if CC:IsValidUnit(unit) then return unit end
return nil
end
-- ---------------------------------------------------------------------------
-- action lookup
-- ---------------------------------------------------------------------------
-- pfUI builds its modifier string as alt -> ctrl -> shift and only ever stores
-- single-modifier entries, so holding two modifiers resolves to nothing and
-- falls through. That behaviour is reproduced here.
function CC:GetModifier()
local modstring = ""
if IsAltKeyDown() then modstring = modstring .. "alt" end
if IsControlKeyDown() then modstring = modstring .. "ctrl" end
if IsShiftKeyDown() then modstring = modstring .. "shift" end
return modstring
end
function CC:GetAction(button)
local bid = CC.buttonid[button or ""]
if not bid then return nil end
local action = CC.db.actions[CC:ActionKey(bid, CC:GetModifier())]
if not action or action == "" then return nil end
return action
end
-- ---------------------------------------------------------------------------
-- dropdown menus (pfUI.uf:RightClickAction, generalised to any unit string)
-- ---------------------------------------------------------------------------
function CC:ShowMenu(unit)
if unit == "player" then
ToggleDropDownMenu(1, nil, PlayerFrameDropDown, "cursor")
return
elseif unit == "target" then
ToggleDropDownMenu(1, nil, TargetFrameDropDown, "cursor")
return
elseif unit == "pet" then
ToggleDropDownMenu(1, nil, PetFrameDropDown, "cursor")
return
end
local _, _, partyid = string.find(unit, "^party([1-4])$")
if partyid then
local dropdown = getglobal("PartyMemberFrame" .. partyid .. "DropDown")
if dropdown then
ToggleDropDownMenu(1, nil, dropdown, "cursor")
return
end
end
-- everything else goes through the generic unit popup
if not UnitExists(unit) then return end
local name = UnitName(unit)
FriendsDropDown.displayMode = "MENU"
FriendsDropDown.initialize = function()
UnitPopup_ShowMenu(getglobal(UIDROPDOWNMENU_OPEN_MENU), "PARTY", unit, name)
end
ToggleDropDownMenu(1, nil, FriendsDropDown, "cursor")
end
-- ---------------------------------------------------------------------------
-- casting (pfUI.uf:ClickAction, cast branch)
-- ---------------------------------------------------------------------------
function CC:Cast(action, unit)
local is_macro = string.find(action, "^%/(.+)")
if CC:UseSuperWoW() and not is_macro then
-- SuperWoW can cast directly at a unit, no target juggling required
CastSpellByName(action, unit)
return
end
local tswitch = UnitIsUnit(unit, "target")
TargetUnit(unit)
if is_macro then
RunMacroText(action)
else
CastSpellByName(action)
end
if not tswitch then TargetLastTarget() end
end
-- ---------------------------------------------------------------------------
-- click handling
-- ---------------------------------------------------------------------------
-- what a frame did before Clickcast hooked it. Used for unbound clicks, and as
-- a stand-in default for frames that never had an OnClick script at all.
function CC:Passthrough(frame, button, unit)
local original = CC.oldclick[frame]
if original then
if passthrough_buttons[button or ""] then original() end
return
end
-- the frame drives itself through OnMouseDown/OnMouseUp, which still fire on
-- their own - stepping in here would double up on whatever it already does
if CC.hasmouse[frame] then return end
if not unit then return end
if SpellIsTargeting() then
if button == "RightButton" then SpellStopTargeting() return end
if button == "LeftButton" then SpellTargetUnit(unit) return end
end
if CursorHasItem() then
DropItemOnUnit(unit)
return
end
if button == "RightButton" then
CC:ShowMenu(unit)
return
end
TargetUnit(unit)
end
function CC:OnClick(frame, button)
button = button or "LeftButton"
local unit = CC:GetUnit(frame)
if CC.db.enabled ~= "1" or not unit then
return CC:Passthrough(frame, button, unit)
end
if CC.db.units[CC:GetCategory(unit)] ~= "1" then
return CC:Passthrough(frame, button, unit)
end
local action = CC:GetAction(button)
if not action then
return CC:Passthrough(frame, button, unit)
end
-- a bound click still has to respect a pending spell cursor or a held item,
-- exactly like pfUI does before it looks at the click action
if SpellIsTargeting() then
if button == "RightButton" then SpellStopTargeting() return end
if button == "LeftButton" then SpellTargetUnit(unit) return end
end
if CursorHasItem() and button == "LeftButton" then
DropItemOnUnit(unit)
return
end
if string.find(action, "^menu") then
CC:ShowMenu(unit)
return
end
if string.find(action, "^target") then
if UnitExists(unit) then TargetUnit(unit) end
return
end
if not UnitExists(unit) then return end
CC:Cast(action, unit)
end
local function ClickHandler()
CC:OnClick(this, arg1)
end
-- ---------------------------------------------------------------------------
-- registration
-- ---------------------------------------------------------------------------
function CC:Register(frame, unit)
if not CC.db then return nil end
if type(frame) == "string" then frame = getglobal(frame) end
if not frame or type(frame) ~= "table" then return nil end
if unit then frame.ccUnit = unit end
-- pfUI ships its own click casting; running both would double every cast
if CC.db.takeover_pfui == "1" and frame.EnableClickCast then
frame.clickactions = nil
frame.EnableClickCast = function() return end
end
if CC.hooked[frame] then return true end
-- RegisterForClicks only exists on Buttons, which is what we need
if not frame.RegisterForClicks or not frame.SetScript or not frame.GetScript then
return nil
end
CC.hooked[frame] = true
CC.oldclick[frame] = frame:GetScript("OnClick")
CC.hasmouse[frame] = (frame:GetScript("OnMouseDown") or frame:GetScript("OnMouseUp")) and true or nil
frame:RegisterForClicks("LeftButtonUp", "RightButtonUp",
"MiddleButtonUp", "Button4Up", "Button5Up")
frame:SetScript("OnClick", ClickHandler)
CC:Debug("hooked " .. (frame.GetName and frame:GetName() or "<unnamed>")
.. " -> " .. (CC:GetUnit(frame) or "?"))
return true
end
function CC:CountFrames()
local count = 0
for _ in pairs(CC.hooked) do count = count + 1 end
return count
end
-- Clique compatible registration table: other addons can simply do
-- ClickCastFrames[myframe] = true
if type(ClickCastFrames) ~= "table" then ClickCastFrames = {} end
do
local existing = ClickCastFrames
setmetatable(existing, { __newindex = function(tbl, frame, value)
rawset(tbl, frame, value)
if value and CC.db then CC:Register(frame) end
end })
end