-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_frame.lua
410 lines (364 loc) · 13.8 KB
/
unit_frame.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
local addonName, addon = ...
setfenv(1, addon)
local function updateBackdrop(unitFrame)
if _G.UnitExists(unitFrame.unit) and _G.UnitIsUnit(unitFrame.unit .. "target", "player") and
not _G.UnitIsUnit(unitFrame.unit, "player")
then
unitFrame:SetBackdropBorderColor(1.0, 1.0, 1.0, .6)
--unitFrame:SetBackdropBorderColor(.5, .5, .5, 1.0)
--unitFrame:SetBackdropBorderColor(0.0, 0.0, 0.0, .75)
else
unitFrame:SetBackdropBorderColor(0.0, 0.0, 0.0, 1.0)
end
end
-- TODO: do something more clever, like unregistering all events.
function enableUnitFrame(unitFrame)
if unitFrame.enabled then return end
unitFrame:SetScript("OnEvent", function(self, event, ...)
return self[event](self, ...)
end)
if not _G.string.match(unitFrame.unit, "arena") then
_G.RegisterUnitWatch(unitFrame)
end
unitFrame:update()
unitFrame.enabled = true
end
function disableUnitFrame(unitFrame)
if not unitFrame.enabled then return end
unitFrame:SetScript("OnEvent", function() end)
if _G.UnitWatchRegistered(unitFrame) then
_G.UnregisterUnitWatch(unitFrame)
end
unitFrame:Hide()
unitFrame.enabled = false
end
function createUnitFrame(attributes)
local unitFrame, unitButton
if _G.string.match(attributes.unit, "arena") then
-- Are SecureHandlerAttributeTemplate and SecureHandlerStateTemplate mutually exclusive? I can't get the
-- "_onstate-unitexists" snippet to execute when using both. Is it okay to parent a secure frame to an insecure one?
--unitFrame = _G.CreateFrame("Frame", attributes.name, _G.UIParent)
unitButton = _G.CreateFrame("Button", attributes.name, _G.UIParent,
"SecureHandlerStateTemplate,SecureHandlerShowHideTemplate")
-- This is true: unitButton:IsProtected().
--unitButton:SetAllPoints()
unitFrame = unitButton -- TODO: remove.
else
unitButton = _G.CreateFrame("Button", attributes.name, _G.UIParent, "SecureUnitButtonTemplate")
unitButton:SetAttribute("*type1", "target")
unitButton:SetAttribute("*type2", "togglemenu")
unitButton:SetAttribute("*type3", "focus")
unitFrame = unitButton
end
unitFrame:SetAttribute("unit", attributes.unit)
unitFrame:SetFrameLevel(10)
unitFrame:SetPoint(attributes.point, _G[attributes.relativeTo], attributes.relativePoint,
attributes.xOffset, attributes.yOffset)
unitFrame:SetWidth(attributes.width)
--unitFrame:SetHitRectInsets(2, 2, 2, 2)
unitFrame.unit = attributes.unit
unitFrame:SetBackdrop(settings.unitFrameBackdrop)
unitFrame:SetBackdropBorderColor(0.0, 0.0, 0.0, 1.0)
unitFrame:RegisterForClicks("AnyDown")
--------------------------------------------------------------------------------------------------
unitFrame.bars = {}
local yOffset = -settings.insets.top
local function createBar(i)
local mirror = attributes.bars[i].mirror
local bar = attributes.bars[i].create(attributes.unit, mirror, unitFrame)
local height = attributes.bars[i].height
bar:SetParent(unitFrame)
bar:SetPoint("TOPLEFT", settings.spacing, yOffset)
bar:SetPoint("TOPRIGHT", -settings.spacing, yOffset)
bar:SetHeight(height)
yOffset = yOffset - height
return bar
end
unitFrame.bars[1] = createBar(1)
for i = 2, #attributes.bars do
local spacer = unitFrame:CreateTexture()
spacer:SetColorTexture(0.0, 0.0, 0.0)
spacer:SetPoint("TOPLEFT", settings.spacing, yOffset)
spacer:SetPoint("TOPRIGHT", -settings.spacing, yOffset)
spacer:SetHeight(1)
yOffset = yOffset - settings.spacing
unitFrame.bars[i] = createBar(i)
end
unitFrame:SetHeight(_G.math.abs(yOffset - settings.insets.bottom))
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
unitFrame:SetScript("OnEvent", function(self, event, ...)
return self[event](self, ...)
end)
-- Stuff we need to do when PLAYER_LOGIN fires.
function unitFrame:initialize()
for k, bar in _G.ipairs(self.bars) do
if bar.initialize then bar:initialize(self.unit) end
end
end
-- The UI is not locked down yet when PLAYER_LOGIN fires, _G.InCombatLockdown() is false. PLAYER_REGEN_DISABLED can
-- be used to detect when the player is entering combat. IsInInstance() already returns useful information.
if _G.string.match(attributes.unit, "arena") then
function unitFrame:PLAYER_LOGIN()
self:initialize()
if not unitFrame:HasScript("OnShow") then
unitFrame:SetScript("OnShow", function(self) end)
end
unitFrame:HookScript("OnShow", function(self)
self:update()
end)
end
else
function unitFrame:PLAYER_LOGIN()
self:initialize()
if not unitFrame:HasScript("OnShow") then
unitFrame:SetScript("OnShow", function(self) end)
end
unitFrame:HookScript("OnShow", function(self)
self:update()
end)
end
end
unitFrame:RegisterEvent("PLAYER_LOGIN")
-- Stuff we need to do when PLAYER_ENTERING_WORLD fires or when the unit changes.
if _G.string.match(unitFrame.unit, "arena") then
function unitFrame:update()
updateBackdrop(self)
for _, bar in _G.ipairs(self.bars) do
if bar.update then bar:update(self.unit) end
end
end
--[[
elseif unitFrame.unit == "target" or unitFrame.unit == "focus" then
function unitFrame:update()
if _G.UnitExists(self.unit) then
updateBackdrop(self)
for _, bar in _G.ipairs(self.bars) do
if bar.update then bar:update(self.unit) end
end
end
end
]]
else
function unitFrame:update()
if _G.UnitExists(self.unit) then
updateBackdrop(self)
for _, bar in _G.ipairs(self.bars) do
if bar.update then bar:update(self.unit) end
end
end
end
end
-- TODO: required?
function unitFrame:UNIT_LEVEL(unit)
_G.assert(_G.UnitIsUnit(unit, self.unit))
self:update()
end
unitFrame:RegisterUnitEvent("UNIT_LEVEL", unitFrame.unit)
function unitFrame:UNIT_NAME_UPDATE(unit)
_G.assert(_G.UnitIsUnit(unit, self.unit))
self:update()
end
unitFrame:RegisterUnitEvent("UNIT_NAME_UPDATE", unitFrame.unit)
function unitFrame:UNIT_PHASE(unit)
self:update()
end
unitFrame:RegisterUnitEvent("UNIT_PHASE", unitFrame.unit)
function unitFrame:UNIT_CONNECTION(unit, hasConnected)
self:update()
end
unitFrame:RegisterUnitEvent("UNIT_CONNECTION", unitFrame.unit)
function unitFrame:COMBAT_LOG_EVENT_UNFILTERED(_, event, _, _, _, _, _, destGUID, _, _, _)
if event == "UNIT_DIED" and _G.UnitGUID(self.unit) == destGUID then
self:update()
end
end
unitFrame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
if _G.string.match(unitFrame.unit, "arena") then
function unitFrame:PLAYER_ENTERING_WORLD()
_G.assert(not _G.InCombatLockdown())
local _, instanceType = _G.IsInInstance()
-- http://wowprogramming.com/utils/xmlbrowser/test/FrameXML/SecureStateDriver.lua
if _G.UnitWatchRegistered(unitFrame) then
_G.UnregisterUnitWatch(unitFrame)
end
if instanceType == "arena" then
-- The "state-unitexists" attribute will be set to a boolean value denoting whether the unit exists.
_G.RegisterUnitWatch(unitFrame, true)
elseif instanceType == "pvp" then
_G.RegisterUnitWatch(unitFrame)
end
if instanceType == "arena" then
self:ARENA_PREP_OPPONENT_SPECIALIZATIONS()
else
self:Hide()
end
end
else
function unitFrame:PLAYER_ENTERING_WORLD()
if _G.UnitExists(self.unit) then
self:update()
end
end
end
unitFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
--if unitFrame.unit == "target" or unitFrame.unit == "focus" or _G.string.match(unitFrame.unit, "arena") then
function unitFrame:PLAYER_TARGET_CHANGED(cause)
if self.unit == "target" then
self:update()
elseif _G.UnitExists(self.unit) and _G.UnitIsUnit(self.unit, "player") then
updateBackdrop(self)
end
end
unitFrame:RegisterEvent("PLAYER_TARGET_CHANGED") -- This is faster than UNIT_TARGET.
unitFrame:RegisterUnitEvent("UNIT_TARGET", unitFrame.unit)
function unitFrame:UNIT_TARGET(unit)
updateBackdrop(self)
end
unitFrame:RegisterUnitEvent("UNIT_TARGET", unitFrame.unit)
--end
if not _G.string.match(unitFrame.unit, "arena") and unitFrame.unit ~= "player" then
_G.RegisterUnitWatch(unitFrame)
end
----------------------------------------------------------------------------------------------------------------------
if unitFrame.unit == "player" then
-- ...
elseif unitFrame.unit == "target" then
--[[
function unitFrame:PLAYER_TARGET_CHANGED(cause)
self:update()
end
unitFrame:RegisterEvent("PLAYER_TARGET_CHANGED") -- This is faster than UNIT_TARGET.
]]
elseif unitFrame.unit == "focus" then
function unitFrame:PLAYER_FOCUS_CHANGED(cause)
self:update()
end
unitFrame:RegisterEvent("PLAYER_FOCUS_CHANGED")
elseif unitFrame.unit == "vehicle" then
function unitFrame:UNIT_ENTERED_VEHICLE(unit)
if unit == "player" then -- I don't know why we need to check this, but apparently we do.
self:update()
end
end
unitFrame:RegisterUnitEvent("UNIT_ENTERED_VEHICLE", "player")
elseif _G.string.match(unitFrame.unit, "arena") then
unitFrame:SetAttribute("_onstate-unitexists", [[ -- arguments: self, stateid, newstate
if newstate then
self:CallMethod("update")
if not self:IsShown() then self:Show() end
end
]])
--[=[
unitFrame:SetAttribute("_onattributechanged", [[ -- arguments: self, name, value
--print("_onattributechanged", self, name, value)
if name == "state-unitexists" then
-- ...
end
]])
--]=]
function unitFrame:ARENA_PREP_OPPONENT_SPECIALIZATIONS()
--if _G.InCombatLockdown() then return end
local specId
for i = 1, _G.MAX_ARENA_ENEMIES do
if unitFrame.unit == "arena" .. i then
specId = _G.GetArenaOpponentSpec(i)
break
end
end
if specId and specId > 0 or _G.UnitExists(unitFrame.unit) then
if not self:IsShown() then
self:Show()
else
self:update()
end
else
if self:IsShown() then
self:Hide()
end
end
end
unitFrame:RegisterEvent("ARENA_PREP_OPPONENT_SPECIALIZATIONS")
function unitFrame:ARENA_OPPONENT_UPDATE(unit, eventType)
if unit ~= self.unit then return end
--_G.print("ARENA_OPPONENT_UPDATE", unit, eventType)
-- Calling UnitIsUnit() probably doesn't always make a whole lot of sense as UnitExists(unit) might be false.
if eventType == "cleared" then
-- When exactly does this happen? Seems to be at the start of an arena match for arena1 to arena5.
local oppNumber = _G.tonumber(_G.string.sub(unit, 6)) -- Example value for unit: "arena1".
if not _G.UnitExists(unit) and not _G.GetArenaOpponentSpec(oppNumber) then
self:Hide()
end
elseif eventType == "destroyed" then
-- Typically, _G.UnitExists(unit) seems to be true here.
-- TODO: do something to indicate the unit is gone, but don't hide the frame.
--[[
if not _G.InCombatLockdown() then
self:Hide() -- This will be trobule if InCombatLockdown(). TODO: use a secure button frame for clicks and an
-- insecure frame for everything else.
end
]]
elseif _G.UnitExists(unit) then
self:update()
elseif eventType == "unseen" then
_G.assert(not _G.UnitExists(unit))
end
end
-- I think RegisterUnitEvent() does not work for ARENA_OPPONENT_UPDATE.
unitFrame:RegisterEvent("ARENA_OPPONENT_UPDATE")
--unitFrame:Hide() -- Frames are implicitly shown upon creation.
elseif _G.string.match(unitFrame.unit, "party") then
function unitFrame:GROUP_ROSTER_UPDATE()
if _G.UnitExists(self.unit) then
self:update()
end
end
unitFrame:RegisterEvent("GROUP_ROSTER_UPDATE")
function unitFrame:PARTY_MEMBERS_CHANGED()
self:update()
end
unitFrame:RegisterEvent("PARTY_MEMBER_ENABLE")
function unitFrame:PARTY_MEMBER_ENABLE(id)
if _G.string.match(self.unit, id) then
self:update()
end
end
unitFrame:RegisterEvent("PARTY_MEMBER_ENABLE")
function unitFrame:PARTY_MEMBER_DISABLE(id)
if _G.string.match(self.unit, id) then
self:update()
end
end
unitFrame:RegisterEvent("PARTY_MEMBER_DISABLE")
-- http://wowprogramming.com/utils/xmlbrowser/test/FrameXML/PartyMemberFrame.lua
function unitFrame:UNIT_OTHER_PARTY_CHANGED(unit)
if unit == self.unit then
-- ...
end
end
unitFrame:RegisterUnitEvent("PARTY_MEMBER_DISABLE", unitFrame.unit)
elseif _G.string.match(unitFrame.unit, "raid") then
_G.error() -- Not implemented.
end
----------------------------------------------------------------------------------------------------------------------
unitFrame:SetScript("OnEnter", function(self, motion)
--self:SetBackdropBorderColor(1.0, 1.0, 1.0, 1.0)
-- See "http://www.wowwiki.com/Talk:UIOBJECT_GameTooltip".
_G.GameTooltip_SetDefaultAnchor(_G.GameTooltip, _G.WorldFrame)
_G.GameTooltip:SetUnit(attributes.unit)
-- Took these lines (more or less) from blizzard's "UnitFrame_UpdateTooltip". See
-- "http://wowprogramming.com/utils/xmlbrowser/test/FrameXML/UnitFrame.lua".
local r, g, b = _G.GameTooltip_UnitColor(attributes.unit)
_G.GameTooltipTextLeft1:SetTextColor(r, g, b)
end)
unitFrame:SetScript("OnLeave", function(self, motion)
--self:SetBackdropBorderColor(0.0, 0.0, 0.0, 1.0)
_G.GameTooltip:FadeOut()
end)
unitFrame.enabled = true
if attributes.disabled then
disableUnitFrame(unitFrame)
end
return unitFrame
end
-- vim: tw=120 sts=2 sw=2 et