-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathLibGetFrame-1.0.lua
291 lines (270 loc) · 8.57 KB
/
LibGetFrame-1.0.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
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
local MAJOR_VERSION = "LibGetFrame-1.0"
local MINOR_VERSION = 24
if not LibStub then error(MAJOR_VERSION .. " requires LibStub.") end
local lib = LibStub:NewLibrary(MAJOR_VERSION, MINOR_VERSION)
if not lib then return end
lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib)
local callbacks = lib.callbacks
local GetPlayerInfoByGUID, UnitExists, IsAddOnLoaded, C_Timer, UnitIsUnit, SecureButton_GetUnit = GetPlayerInfoByGUID, UnitExists, IsAddOnLoaded, C_Timer, UnitIsUnit, SecureButton_GetUnit
local tinsert, CopyTable, wipe = tinsert, CopyTable, wipe
local maxDepth = 50
local defaultFramePriorities = {
-- raid frames
[1] = "^Vd1", -- vuhdo
[2] = "^Vd2", -- vuhdo
[3] = "^Vd3", -- vuhdo
[4] = "^Vd4", -- vuhdo
[5] = "^Vd5", -- vuhdo
[6] = "^Vd", -- vuhdo
[7] = "^HealBot", -- healbot
[8] = "^GridLayout", -- grid
[9] = "^Grid2Layout", -- grid2
[10] = "^PlexusLayout", -- plexus
[11] = "^ElvUF_RaidGroup", -- elv
[12] = "^oUF_bdGrid", -- bdgrid
[13] = "^oUF_.-Raid", -- generic oUF
[14] = "^LimeGroup", -- lime
[15] = "^SUFHeaderraid", -- suf
-- party frames
[16] = "^AleaUI_GroupHeader", -- Alea
[17] = "^SUFHeaderparty", --suf
[18] = "^ElvUF_PartyGroup", -- elv
[19] = "^oUF_.-Party", -- generic oUF
[20] = "^PitBull4_Groups_Party", -- pitbull4
[21] = "^CompactRaid", -- blizz
[22] = "^CompactParty", -- blizz
-- player frame
[23] = "^SUFUnitplayer",
[24] = "^PitBull4_Frames_Player",
[25] = "^ElvUF_Player",
[26] = "^oUF_.-Player",
[27] = "^PlayerFrame",
}
local defaultPlayerFrames = {
"SUFUnitplayer",
"PitBull4_Frames_Player",
"ElvUF_Player",
"oUF_.-Player",
"oUF_PlayerPlate",
"PlayerFrame",
}
local defaultTargetFrames = {
"SUFUnittarget",
"PitBull4_Frames_Target",
"ElvUF_Target",
"oUF_.-Target",
"TargetFrame",
}
local defaultTargettargetFrames = {
"SUFUnittargetarget",
"PitBull4_Frames_Target's target",
"ElvUF_TargetTarget",
"oUF_.-TargetTarget",
"oUF_ToT",
"TargetTargetFrame",
}
local defaultPartyTargetFrames = {
"SUFChildpartytarget%d",
}
local GetFramesCache = {}
local FrameToUnitFresh = {}
local FrameToUnit = {}
local UpdatedFrames = {}
local function ScanFrames(depth, frame, ...)
if not frame then return end
if depth < maxDepth
and frame.IsForbidden
and not frame:IsForbidden()
then
local frameType = frame:GetObjectType()
if frameType == "Frame" or frameType == "Button" then
ScanFrames(depth + 1, frame:GetChildren())
end
if frameType == "Button" then
local unit = SecureButton_GetUnit(frame)
local name = frame:GetName()
if unit and frame:IsVisible() and name then
GetFramesCache[frame] = name
if unit ~= FrameToUnit[frame] then
FrameToUnit[frame] = unit
UpdatedFrames[frame] = unit
end
FrameToUnitFresh[frame] = unit
end
end
end
ScanFrames(depth, ...)
end
local wait = false
local function doScanForUnitFrames()
wait = false
wipe(UpdatedFrames)
wipe(GetFramesCache)
wipe(FrameToUnitFresh)
ScanFrames(0, UIParent)
callbacks:Fire("GETFRAME_REFRESH")
for frame, unit in pairs(UpdatedFrames) do
callbacks:Fire("FRAME_UNIT_UPDATE", frame, unit)
end
for frame, unit in pairs(FrameToUnit) do
if FrameToUnitFresh[frame] ~= unit then
callbacks:Fire("FRAME_UNIT_REMOVED", frame, unit)
FrameToUnit[frame] = nil
end
end
end
local function ScanForUnitFrames(noDelay)
if noDelay then
doScanForUnitFrames()
elseif not wait then
wait = true
C_Timer.After(1, function()
doScanForUnitFrames()
end)
end
end
local function isFrameFiltered(name, ignoredFrames)
for _, filter in pairs(ignoredFrames) do
if name:find(filter) then
return true
end
end
return false
end
local function GetUnitFrames(target, ignoredFrames)
if not UnitExists(target) then
if type(target) == "string" and target:find("Player") then
target = select(6, GetPlayerInfoByGUID(target))
else
target = target:gsub(" .*", "")
if not UnitExists(target) then
return
end
end
end
local frames
for frame, frameName in pairs(GetFramesCache) do
local unit = SecureButton_GetUnit(frame)
if unit and UnitIsUnit(unit, target)
and not isFrameFiltered(frameName, ignoredFrames)
then
frames = frames or {}
frames[frame] = frameName
end
end
return frames
end
local function ElvuiWorkaround(frame)
if IsAddOnLoaded("ElvUI") and frame and frame:GetName():find("^ElvUF_") and frame.Health then
return frame.Health
else
return frame
end
end
local defaultOptions = {
framePriorities = defaultFramePriorities,
ignorePlayerFrame = true,
ignoreTargetFrame = true,
ignoreTargettargetFrame = true,
ignorePartyTargetFrame = true,
playerFrames = defaultPlayerFrames,
targetFrames = defaultTargetFrames,
targettargetFrames = defaultTargettargetFrames,
partyTargetFrames = defaultPartyTargetFrames,
ignoreFrames = {
"PitBull4_Frames_Target's target's target",
"ElvUF_PartyGroup%dUnitButton%dTarget",
"ElvUF_FocusTarget",
"RavenButton"
},
returnAll = false,
}
local GetFramesCacheListener
local function Init(noDelay)
GetFramesCacheListener = CreateFrame("Frame")
GetFramesCacheListener:RegisterEvent("PLAYER_REGEN_DISABLED")
GetFramesCacheListener:RegisterEvent("PLAYER_REGEN_ENABLED")
GetFramesCacheListener:RegisterEvent("PLAYER_ENTERING_WORLD")
GetFramesCacheListener:RegisterEvent("GROUP_ROSTER_UPDATE")
GetFramesCacheListener:SetScript("OnEvent", function() ScanForUnitFrames(false) end)
ScanForUnitFrames(noDelay)
end
function lib.GetUnitFrame(target, opt)
if type(GetFramesCacheListener) ~= "table" then Init(true) end
opt = opt or {}
setmetatable(opt, { __index = defaultOptions })
if not target then return end
local ignoredFrames = CopyTable(opt.ignoreFrames)
if opt.ignorePlayerFrame then
for _,v in pairs(opt.playerFrames) do
tinsert(ignoredFrames, v)
end
end
if opt.ignoreTargetFrame then
for _,v in pairs(opt.targetFrames) do
tinsert(ignoredFrames, v)
end
end
if opt.ignoreTargettargetFrame then
for _,v in pairs(opt.targettargetFrames) do
tinsert(ignoredFrames, v)
end
end
if opt.ignorePartyTargetFrame then
for _,v in pairs(opt.partyTargetFrames) do
tinsert(ignoredFrames, v)
end
end
local frames = GetUnitFrames(target, ignoredFrames)
if not frames then return end
if not opt.returnAll then
for i = 1, #opt.framePriorities do
for frame, frameName in pairs(frames) do
if frameName:find(opt.framePriorities[i]) then
return ElvuiWorkaround(frame)
end
end
end
local next = next
return ElvuiWorkaround(next(frames))
else
for frame in pairs(frames) do
frames[frame] = ElvuiWorkaround(frame)
end
return frames
end
end
lib.GetFrame = lib.GetUnitFrame -- compatibility
-- nameplates
function lib.GetUnitNameplate(unit)
if not unit then return end
local nameplate = C_NamePlate.GetNamePlateForUnit(unit)
if nameplate then
-- credit to Exality for https://wago.io/explosiveorbs
if nameplate.unitFrame and nameplate.unitFrame.Health then
-- elvui
return nameplate.unitFrame.Health
elseif nameplate.unitFramePlater then
-- plater
return nameplate.unitFramePlater.healthBar
elseif nameplate.kui then
-- kui
return nameplate.kui.HealthBar
elseif nameplate.extended then
-- tidyplates
--nameplate.extended.visual.healthbar:SetHeight(tidyplatesHeight)
return nameplate.extended.visual.healthbar
elseif nameplate.TPFrame then
-- tidyplates: threat plates
return nameplate.TPFrame.visual.healthbar
elseif nameplate.ouf then
-- bdNameplates
return nameplate.ouf.Health
elseif nameplate.UnitFrame then
-- default
return nameplate.UnitFrame.healthBar
else
return nameplate
end
end
end