-
Notifications
You must be signed in to change notification settings - Fork 58
/
ouf.lua
909 lines (748 loc) · 26.6 KB
/
ouf.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
local parent, ns = ...
local global = C_AddOns.GetAddOnMetadata(parent, 'X-oUF')
local _VERSION = '@project-version@'
if(_VERSION:find('project%-version')) then
_VERSION = 'devel'
end
local oUF = ns.oUF
local Private = oUF.Private
local argcheck = Private.argcheck
local error = Private.error
local print = Private.print --luacheck: no unused
local unitExists = Private.unitExists
local styles, style = {}
local callback, objects, headers = {}, {}, {}
local elements = {}
local activeElements = {}
local PetBattleFrameHider = CreateFrame('Frame', (global or parent) .. '_PetBattleFrameHider', UIParent, 'SecureHandlerStateTemplate')
PetBattleFrameHider:SetAllPoints()
PetBattleFrameHider:SetFrameStrata('LOW')
RegisterStateDriver(PetBattleFrameHider, 'visibility', '[petbattle] hide; show')
local function updateActiveUnit(self, event)
-- Calculate units to work with
local realUnit, modUnit = SecureButton_GetUnit(self), SecureButton_GetModifiedUnit(self)
-- _GetUnit() doesn't rewrite playerpet -> pet like _GetModifiedUnit does.
if(realUnit == 'playerpet') then
realUnit = 'pet'
elseif(realUnit == 'playertarget') then
realUnit = 'target'
end
if(modUnit == 'pet' and realUnit ~= 'pet') then
modUnit = 'vehicle'
end
if(not unitExists(modUnit)) then return end
-- Change the active unit and run a full update.
if(Private.UpdateUnits(self, modUnit, realUnit)) then
self:UpdateAllElements(event or 'RefreshUnit')
return true
end
end
local function evalUnitAndUpdate(self, event)
if(not updateActiveUnit(self, event)) then
return self:UpdateAllElements(event)
end
end
local function iterateChildren(...)
for i = 1, select('#', ...) do
local obj = select(i, ...)
if(type(obj) == 'table' and obj.isChild) then
updateActiveUnit(obj, 'iterateChildren')
end
end
end
local function onAttributeChanged(self, name, value)
if(name == 'unit' and value) then
if(self.hasChildren) then
iterateChildren(self:GetChildren())
end
if(not self:GetAttribute('oUF-onlyProcessChildren')) then
updateActiveUnit(self, 'OnAttributeChanged')
end
end
end
local frame_metatable = {
__index = CreateFrame('Button')
}
Private.frame_metatable = frame_metatable
for k, v in next, {
--[[ frame:EnableElement(name, unit)
Used to activate an element for the given unit frame.
* self - unit frame for which the element should be enabled
* name - name of the element to be enabled (string)
* unit - unit to be passed to the element's Enable function. Defaults to the frame's unit (string?)
--]]
EnableElement = function(self, name, unit)
argcheck(name, 2, 'string')
argcheck(unit, 3, 'string', 'nil')
local element = elements[name]
if(not element or self:IsElementEnabled(name)) then return end
if(element.enable(self, unit or self.unit)) then
activeElements[self][name] = true
if(element.update) then
table.insert(self.__elements, element.update)
end
end
end,
--[[ frame:DisableElement(name)
Used to deactivate an element for the given unit frame.
* self - unit frame for which the element should be disabled
* name - name of the element to be disabled (string)
--]]
DisableElement = function(self, name)
argcheck(name, 2, 'string')
local enabled = self:IsElementEnabled(name)
if(not enabled) then return end
local update = elements[name].update
if(update) then
for k, func in next, self.__elements do
if(func == update) then
table.remove(self.__elements, k)
break
end
end
end
activeElements[self][name] = nil
return elements[name].disable(self)
end,
--[[ frame:IsElementEnabled(name)
Used to check if an element is enabled on the given frame.
* self - unit frame
* name - name of the element (string)
--]]
IsElementEnabled = function(self, name)
argcheck(name, 2, 'string')
local element = elements[name]
if(not element) then return end
local active = activeElements[self]
return active and active[name]
end,
--[[ frame:Enable(asState)
Used to toggle the visibility of a unit frame based on the existence of its unit. This is a reference to
`RegisterUnitWatch`.
* self - unit frame
* asState - if true, the frame's "state-unitexists" attribute will be set to a boolean value denoting whether the
unit exists; if false, the frame will be shown if its unit exists, and hidden if it does not (boolean)
--]]
Enable = RegisterUnitWatch,
--[[ frame:Disable()
Used to UnregisterUnitWatch for the given frame and hide it.
* self - unit frame
--]]
Disable = function(self)
UnregisterUnitWatch(self)
self:Hide()
end,
--[[ frame:IsEnabled()
Used to check if a unit frame is registered with the unit existence monitor. This is a reference to
`UnitWatchRegistered`.
* self - unit frame
--]]
IsEnabled = UnitWatchRegistered,
--[[ frame:UpdateAllElements(event)
Used to update all enabled elements on the given frame.
* self - unit frame
* event - event name to pass to the elements' update functions (string)
--]]
UpdateAllElements = function(self, event)
local unit = self.unit
if(not unitExists(unit)) then return end
assert(type(event) == 'string', "Invalid argument 'event' in UpdateAllElements.")
if(self.PreUpdate) then
--[[ Callback: frame:PreUpdate(event)
Fired before the frame is updated.
* self - the unit frame
* event - the event triggering the update (string)
--]]
self:PreUpdate(event)
end
for _, func in next, self.__elements do
func(self, event, unit)
end
if(self.PostUpdate) then
--[[ Callback: frame:PostUpdate(event)
Fired after the frame is updated.
* self - the unit frame
* event - the event triggering the update (string)
--]]
self:PostUpdate(event)
end
end,
} do
frame_metatable.__index[k] = v
end
local function onShow(self)
evalUnitAndUpdate(self, 'OnShow')
end
local function updatePet(self, event, unit)
local petUnit
if(unit == 'target') then
return
elseif(unit == 'player') then
petUnit = 'pet'
else
-- Convert raid26 -> raidpet26
petUnit = unit:gsub('^(%a+)(%d+)', '%1pet%2')
end
if(self.unit ~= petUnit) then return end
evalUnitAndUpdate(self, event)
end
local function updateRaid(self, event)
local unitGUID = UnitGUID(self.unit)
if(unitGUID and unitGUID ~= self.unitGUID) then
self.unitGUID = unitGUID
self:UpdateAllElements(event)
end
end
-- boss6-8 exsist in some encounters, but unit event registration seems to be
-- completely broken for them, so instead we use OnUpdate to update them.
local eventlessUnits = {
boss6 = true,
boss7 = true,
boss8 = true,
}
local function isEventlessUnit(unit)
return unit:match('%w+target') or eventlessUnits[unit]
end
local function initObject(unit, style, styleFunc, header, ...)
local num = select('#', ...)
for i = 1, num do
local object = select(i, ...)
local objectUnit = object:GetAttribute('oUF-guessUnit') or unit
local suffix = object:GetAttribute('unitsuffix')
-- Handle the case where someone has modified the unitsuffix attribute in
-- oUF-initialConfigFunction.
if(suffix and not objectUnit:match(suffix)) then
objectUnit = objectUnit .. suffix
end
object.__elements = {}
object.style = style
object = setmetatable(object, frame_metatable)
-- Expose the frame through oUF.objects.
table.insert(objects, object)
-- We have to force update the frames when PEW fires.
-- It's also important to evaluate units before running an update
-- because sometimes events that are required for unit updates end up
-- not firing because of loading screens. For instance, there's a slight
-- delay between UNIT_EXITING_VEHICLE and UNIT_EXITED_VEHICLE during
-- which a user can go through a loading screen after which the player
-- frame will be stuck with the 'vehicle' unit.
object:RegisterEvent('PLAYER_ENTERING_WORLD', evalUnitAndUpdate, true)
if(not isEventlessUnit(objectUnit)) then
object:RegisterEvent('UNIT_ENTERED_VEHICLE', evalUnitAndUpdate)
object:RegisterEvent('UNIT_EXITED_VEHICLE', evalUnitAndUpdate)
-- We don't need to register UNIT_PET for the player unit. We register it
-- mainly because UNIT_EXITED_VEHICLE and UNIT_ENTERED_VEHICLE don't always
-- have pet information when they fire for party and raid units.
if(objectUnit ~= 'player') then
object:RegisterEvent('UNIT_PET', updatePet)
end
end
if(not header) then
-- No header means it's a frame created through :Spawn().
object:SetAttribute('*type1', 'target')
object:SetAttribute('*type2', 'togglemenu')
object:SetAttribute('toggleForVehicle', true)
if(isEventlessUnit(objectUnit)) then
oUF:HandleEventlessUnit(object)
else
oUF:HandleUnit(object)
end
else
-- update the frame when its prev unit is replaced with a new one
-- updateRaid relies on UnitGUID to detect the unit change
object:RegisterEvent('GROUP_ROSTER_UPDATE', updateRaid, true)
if(num > 1) then
if(object:GetParent() == header) then
object.hasChildren = true
else
object.isChild = true
end
end
if(suffix == 'target') then
oUF:HandleEventlessUnit(object)
end
end
Private.UpdateUnits(object, objectUnit)
styleFunc(object, objectUnit, not header)
object:HookScript('OnAttributeChanged', onAttributeChanged)
-- NAME_PLATE_UNIT_ADDED fires after the frame is shown, so there's no
-- need to call UAE multiple times
if(not object.isNamePlate) then
object:SetScript('OnShow', onShow)
end
activeElements[object] = {}
for element in next, elements do
object:EnableElement(element, objectUnit)
end
for _, func in next, callback do
func(object)
end
-- Make Clique kinda happy
if(not object.isNamePlate) then
_G.ClickCastFrames = _G.ClickCastFrames or {}
_G.ClickCastFrames[object] = true
end
end
end
local function walkObject(object, unit)
local parent = object:GetParent()
local style = parent.style or style
local styleFunc = styles[style]
local header = parent:GetAttribute('oUF-headerType') and parent
-- Check if we should leave the main frame blank.
if(object:GetAttribute('oUF-onlyProcessChildren')) then
object.hasChildren = true
object:HookScript('OnAttributeChanged', onAttributeChanged)
return initObject(unit, style, styleFunc, header, object:GetChildren())
end
return initObject(unit, style, styleFunc, header, object, object:GetChildren())
end
--[[ oUF:RegisterInitCallback(func)
Used to add a function to a table to be executed upon unit frame/header initialization.
* self - the global oUF object
* func - function to be added
--]]
function oUF:RegisterInitCallback(func)
table.insert(callback, func)
end
--[[ oUF:RegisterMetaFunction(name, func)
Used to make a (table of) function(s) available to all unit frames.
* self - the global oUF object
* name - unique name of the function (string)
* func - function or a table of functions (function or table)
--]]
function oUF:RegisterMetaFunction(name, func)
argcheck(name, 2, 'string')
argcheck(func, 3, 'function', 'table')
if(frame_metatable.__index[name]) then
return
end
frame_metatable.__index[name] = func
end
--[[ oUF:RegisterStyle(name, func)
Used to register a style with oUF. This will also set the active style if it hasn't been set yet.
* self - the global oUF object
* name - name of the style
* func - function(s) defining the style (function or table)
--]]
function oUF:RegisterStyle(name, func)
argcheck(name, 2, 'string')
argcheck(func, 3, 'function', 'table')
if(styles[name]) then return error('Style [%s] already registered.', name) end
if(not style) then style = name end
styles[name] = func
end
--[[ oUF:SetActiveStyle(name)
Used to set the active style.
* self - the global oUF object
* name - name of the style (string)
--]]
function oUF:SetActiveStyle(name)
argcheck(name, 2, 'string')
if(not styles[name]) then return error('Style [%s] does not exist.', name) end
style = name
end
--[[ oUF:GetActiveStyle()
Used to get the active style.
* self - the global oUF object
--]]
function oUF:GetActiveStyle()
return style
end
do
local function iter(_, n)
-- don't expose the style functions.
return (next(styles, n))
end
--[[ oUF:IterateStyles()
Returns an iterator over all registered styles.
* self - the global oUF object
--]]
function oUF.IterateStyles()
return iter, nil, nil
end
end
local getCondition
do
local conditions = {
raid40 = '[@raid26,exists] show;',
raid25 = '[@raid11,exists] show;',
raid10 = '[@raid6,exists] show;',
raid = '[group:raid] show;',
party = '[group:party,nogroup:raid] show;',
solo = '[@player,exists,nogroup:party] show;',
}
function getCondition(...)
local cond = ''
for i = 1, select('#', ...) do
local short = select(i, ...)
local condition = conditions[short]
if(condition) then
cond = cond .. condition
end
end
return cond .. 'hide'
end
end
local function generateName(unit, ...)
local name = 'oUF_' .. style:gsub('^oUF_?', ''):gsub('[^%a%d_]+', '')
local raid, party, groupFilter, unitsuffix
for i = 1, select('#', ...), 2 do
local att, val = select(i, ...)
if(att == 'oUF-initialConfigFunction') then
unitsuffix = val:match('unitsuffix[%p%s]+(%a+)')
elseif(att == 'showRaid') then
raid = val ~= false and val ~= nil
elseif(att == 'showParty') then
party = val ~= false and val ~= nil
elseif(att == 'groupFilter') then
groupFilter = val
end
end
local append
if(raid) then
if(groupFilter) then
if(type(groupFilter) == 'number' and groupFilter > 0) then
append = 'Raid' .. groupFilter
elseif(groupFilter:match('MAINTANK')) then
append = 'MainTank'
elseif(groupFilter:match('MAINASSIST')) then
append = 'MainAssist'
else
local _, count = groupFilter:gsub(',', '')
if(count == 0) then
append = 'Raid' .. groupFilter
else
append = 'Raid'
end
end
else
append = 'Raid'
end
elseif(party) then
append = 'Party'
elseif(unit) then
append = unit:gsub('^%l', string.upper)
end
if(append) then
name = name .. append .. (unitsuffix or '')
end
-- Change oUF_LilyRaidRaid into oUF_LilyRaid
name = name:gsub('(%u%l+)([%u%l]*)%1', '%1')
-- Change oUF_LilyTargettarget into oUF_LilyTargetTarget
name = name:gsub('t(arget)', 'T%1')
name = name:gsub('p(et)', 'P%1')
name = name:gsub('f(ocus)', 'F%1')
local base = name
local i = 2
while(_G[name]) do
name = base .. i
i = i + 1
end
return name
end
do
local function styleProxy(self, frame)
return walkObject(_G[frame])
end
-- There has to be an easier way to do this.
local initialConfigFunction = [[
local header = self:GetParent()
local frames = table.new()
table.insert(frames, self)
self:GetChildList(frames)
for i = 1, #frames do
local frame = frames[i]
local unit
-- There's no need to do anything on frames with onlyProcessChildren
if(not frame:GetAttribute('oUF-onlyProcessChildren')) then
RegisterUnitWatch(frame)
-- Attempt to guess what the header is set to spawn.
local groupFilter = header:GetAttribute('groupFilter')
if(type(groupFilter) == 'string' and groupFilter:match('MAIN[AT]')) then
local role = groupFilter:match('MAIN([AT])')
if(role == 'T') then
unit = 'maintank'
else
unit = 'mainassist'
end
elseif(header:GetAttribute('showRaid')) then
unit = 'raid'
elseif(header:GetAttribute('showParty')) then
unit = 'party'
end
local headerType = header:GetAttribute('oUF-headerType')
local suffix = frame:GetAttribute('unitsuffix')
if(unit and suffix) then
if(headerType == 'pet' and suffix == 'target') then
unit = unit .. headerType .. suffix
else
unit = unit .. suffix
end
elseif(unit and headerType == 'pet') then
unit = unit .. headerType
end
frame:SetAttribute('*type1', 'target')
frame:SetAttribute('*type2', 'togglemenu')
frame:SetAttribute('oUF-guessUnit', unit)
end
local body = header:GetAttribute('oUF-initialConfigFunction')
if(body) then
frame:Run(body, unit)
end
end
header:CallMethod('styleFunction', self:GetName())
local clique = header:GetFrameRef('clickcast_header')
if(clique) then
clique:SetAttribute('clickcast_button', self)
clique:RunAttribute('clickcast_register')
end
]]
--[[ oUF:SpawnHeader(overrideName, template, visibility, ...)
Used to create a group header and apply the currently active style to it.
* self - the global oUF object
* overrideName - unique global name to be used for the header. Defaults to an auto-generated name based on the name
of the active style and other arguments passed to `:SpawnHeader` (string?)
* template - name of a template to be used for creating the header. Defaults to `'SecureGroupHeaderTemplate'`
(string?)
* visibility - macro conditional(s) which define when to display the header (string).
* ... - further argument pairs. Consult [Group Headers](https://warcraft.wiki.gg/wiki/SecureGroupHeaderTemplate)
for possible values. If preferred, the attributes can be an associative table.
In addition to the standard group headers, oUF implements some of its own attributes. These can be supplied by the
layout, but are optional. PingableUnitFrameTemplate is inherited for Ping support.
* oUF-initialConfigFunction - can contain code that will be securely run at the end of the initial secure
configuration (string?)
* oUF-onlyProcessChildren - can be used to force headers to only process children (boolean?)
--]]
function oUF:SpawnHeader(overrideName, template, visibility, ...)
if(not style) then return error('Unable to create frame. No styles have been registered.') end
template = (template or 'SecureGroupHeaderTemplate')
local isPetHeader = template:match('PetHeader')
local name = overrideName or generateName(nil, ...)
local header = CreateFrame('Frame', name, PetBattleFrameHider, template)
header:SetAttribute('template', 'SecureUnitButtonTemplate, SecureHandlerStateTemplate, SecureHandlerEnterLeaveTemplate, PingableUnitFrameTemplate')
if(...) then
if(type(...) == 'table') then
for att, val in next, (...) do
header:SetAttribute(att, val)
end
else
for i = 1, select('#', ...), 2 do
local att, val = select(i, ...)
if(not att) then break end
header:SetAttribute(att, val)
end
end
end
header.style = style
header.styleFunction = styleProxy
header.visibility = visibility
-- Expose the header through oUF.headers.
table.insert(headers, header)
-- We set it here so layouts can't directly override it.
header:SetAttribute('initialConfigFunction', initialConfigFunction)
header:SetAttribute('_initialAttributeNames', '_onenter,_onleave,refreshUnitChange,_onstate-vehicleui')
header:SetAttribute('_initialAttribute-_onenter', [[
local snippet = self:GetAttribute('clickcast_onenter')
if(snippet) then
self:Run(snippet)
end
]])
header:SetAttribute('_initialAttribute-_onleave', [[
local snippet = self:GetAttribute('clickcast_onleave')
if(snippet) then
self:Run(snippet)
end
]])
header:SetAttribute('_initialAttribute-refreshUnitChange', [[
local unit = self:GetAttribute('unit')
if(unit) then
RegisterStateDriver(self, 'vehicleui', '[@' .. unit .. ',unithasvehicleui]vehicle; novehicle')
else
UnregisterStateDriver(self, 'vehicleui')
end
]])
header:SetAttribute('_initialAttribute-_onstate-vehicleui', [[
local unit = self:GetAttribute('unit')
if(newstate == 'vehicle' and unit and UnitPlayerOrPetInRaid(unit) and not UnitTargetsVehicleInRaidUI(unit)) then
self:SetAttribute('toggleForVehicle', false)
else
self:SetAttribute('toggleForVehicle', true)
end
]])
header:SetAttribute('oUF-headerType', isPetHeader and 'pet' or 'group')
if(_G.Clique) then
SecureHandlerSetFrameRef(header, 'clickcast_header', _G.Clique.header)
end
if(header:GetAttribute('showParty')) then
self:DisableBlizzard('party')
end
if(visibility) then
local type, list = string.split(' ', visibility, 2)
if(list and type == 'custom') then
RegisterAttributeDriver(header, 'state-visibility', list)
header.visibility = list
else
local condition = getCondition(string.split(',', visibility))
RegisterAttributeDriver(header, 'state-visibility', condition)
header.visibility = condition
end
end
return header
end
end
--[[ oUF:Spawn(unit, overrideName)
Used to create a single unit frame and apply the currently active style to it.
* self - the global oUF object
* unit - the frame's unit (string)
* overrideName - unique global name to use for the unit frame. Defaults to an auto-generated name based on the unit
(string?)
oUF implements some of its own attributes. These can be supplied by the layout, but are optional.
PingableUnitFrameTemplate is inherited for Ping support.
* oUF-enableArenaPrep - can be used to toggle arena prep support. Defaults to true (boolean)
--]]
function oUF:Spawn(unit, overrideName)
argcheck(unit, 2, 'string')
if(not style) then return error('Unable to create frame. No styles have been registered.') end
unit = unit:lower()
local name = overrideName or generateName(unit)
local object = CreateFrame('Button', name, PetBattleFrameHider, 'SecureUnitButtonTemplate, PingableUnitFrameTemplate')
Private.UpdateUnits(object, unit)
self:DisableBlizzard(unit)
walkObject(object, unit)
object:SetAttribute('unit', unit)
RegisterUnitWatch(object)
return object
end
--[[ oUF:SpawnNamePlates(prefix, callback, variables)
Used to create nameplates and apply the currently active style to them.
* self - the global oUF object
* prefix - prefix for the global name of the nameplate. Defaults to an auto-generated prefix (string?)
* callback - function to be called after a nameplate unit or the player's target has changed. The arguments passed to
the callback are the updated nameplate, if any, the event that triggered the update, and the new unit
(function?)
* variables - list of console variable-value pairs to be set when the player logs in (table?)
PingableUnitFrameTemplate is inherited for Ping support.
--]]
function oUF:SpawnNamePlates(namePrefix, nameplateCallback, nameplateCVars)
argcheck(nameplateCallback, 3, 'function', 'nil')
argcheck(nameplateCVars, 4, 'table', 'nil')
if(not style) then return error('Unable to create frame. No styles have been registered.') end
if(_G.oUF_NamePlateDriver) then return error('oUF nameplate driver has already been initialized.') end
local style = style
local prefix = namePrefix or generateName()
-- Because there's no way to prevent nameplate settings updates without tainting UI,
-- and because forbidden nameplates exist, we have to allow default nameplate
-- driver to create, update, and remove Blizz nameplates.
-- Disable only not forbidden nameplates.
hooksecurefunc(NamePlateDriverFrame, 'AcquireUnitFrame', self.DisableNamePlate)
local eventHandler = CreateFrame('Frame', 'oUF_NamePlateDriver')
eventHandler:RegisterEvent('NAME_PLATE_UNIT_ADDED')
eventHandler:RegisterEvent('NAME_PLATE_UNIT_REMOVED')
eventHandler:RegisterEvent('PLAYER_TARGET_CHANGED')
if(IsLoggedIn()) then
if(nameplateCVars) then
for cvar, value in next, nameplateCVars do
SetCVar(cvar, value)
end
end
else
eventHandler:RegisterEvent('PLAYER_LOGIN')
end
eventHandler:SetScript('OnEvent', function(_, event, unit)
if(event == 'PLAYER_LOGIN') then
if(nameplateCVars) then
for cvar, value in next, nameplateCVars do
SetCVar(cvar, value)
end
end
elseif(event == 'PLAYER_TARGET_CHANGED') then
local nameplate = C_NamePlate.GetNamePlateForUnit('target')
if(nameplateCallback) then
nameplateCallback(nameplate and nameplate.unitFrame, event, 'target')
end
-- UAE is called after the callback to reduce the number of
-- ForceUpdate calls layout devs have to do themselves
if(nameplate) then
nameplate.unitFrame:UpdateAllElements(event)
end
elseif(event == 'NAME_PLATE_UNIT_ADDED' and unit) then
local nameplate = C_NamePlate.GetNamePlateForUnit(unit)
if(not nameplate) then return end
if(not nameplate.unitFrame) then
nameplate.style = style
nameplate.unitFrame = CreateFrame('Button', prefix..nameplate:GetName(), nameplate, 'PingableUnitFrameTemplate')
nameplate.unitFrame:EnableMouse(false)
nameplate.unitFrame.isNamePlate = true
Private.UpdateUnits(nameplate.unitFrame, unit)
walkObject(nameplate.unitFrame, unit)
else
Private.UpdateUnits(nameplate.unitFrame, unit)
end
nameplate.unitFrame:SetAttribute('unit', unit)
if(nameplate.UnitFrame) then
if(nameplate.UnitFrame.WidgetContainer) then
nameplate.UnitFrame.WidgetContainer:SetParent(nameplate.unitFrame)
nameplate.UnitFrame.WidgetContainer:SetIgnoreParentAlpha(true)
nameplate.unitFrame.WidgetContainer = nameplate.UnitFrame.WidgetContainer
end
if(nameplate.UnitFrame.SoftTargetFrame) then
nameplate.UnitFrame.SoftTargetFrame:SetParent(nameplate.unitFrame)
nameplate.UnitFrame.SoftTargetFrame:SetIgnoreParentAlpha(true)
nameplate.unitFrame.SoftTargetFrame = nameplate.UnitFrame.SoftTargetFrame
end
end
if(nameplateCallback) then
nameplateCallback(nameplate.unitFrame, event, unit)
end
-- UAE is called after the callback to reduce the number of
-- ForceUpdate calls layout devs have to do themselves
nameplate.unitFrame:UpdateAllElements(event)
elseif(event == 'NAME_PLATE_UNIT_REMOVED' and unit) then
local nameplate = C_NamePlate.GetNamePlateForUnit(unit)
if(not nameplate) then return end
nameplate.unitFrame:SetAttribute('unit', nil)
if(nameplateCallback) then
nameplateCallback(nameplate.unitFrame, event, unit)
end
end
end)
end
--[[ oUF:AddElement(name, update, enable, disable)
Used to register an element with oUF.
* self - the global oUF object
* name - unique name of the element (string)
* update - used to update the element (function)
* enable - used to enable the element for a given unit frame and unit (function)
* disable - used to disable the element for a given unit frame (function)
--]]
function oUF:AddElement(name, update, enable, disable)
argcheck(name, 2, 'string')
argcheck(update, 3, 'function', 'nil')
argcheck(enable, 4, 'function')
argcheck(disable, 5, 'function')
if(elements[name]) then return error('Element [%s] is already registered.', name) end
elements[name] = {
update = update;
enable = enable;
disable = disable;
}
end
oUF.version = _VERSION
--[[ oUF.objects
Array containing all unit frames created by `oUF:Spawn`.
--]]
oUF.objects = objects
--[[ oUF.headers
Array containing all group headers created by `oUF:SpawnHeader`.
--]]
oUF.headers = headers
if(global) then
if(parent ~= 'oUF' and global == 'oUF') then
error('%s is doing it wrong and setting its global to "oUF".', parent)
elseif(_G[global]) then
error('%s is setting its global to an existing name "%s".', parent, global)
else
_G[global] = oUF
end
end