Repository navigation
Expand file tree
/
Copy pathactions.lua
More file actions
1224 lines (1205 loc) · 37.7 KB
/
Copy pathactions.lua
File metadata and controls
1224 lines (1205 loc) · 37.7 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
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local addonName, G = ...
local function formatBindingKey(k)
return k:gsub('CTRL%-', 'C'):gsub('SHIFT%-', 'S')
end
local stackables = {}
local makeAction = (function()
local function consume(mealDB, potionDB, nethergon)
local potionText = (function()
local s = ''
for _, consumable in ipairs(potionDB) do
s = s .. '/use item:' .. consumable[1] .. '\n'
end
return s
end)()
local nethergonText = '/use item:' .. nethergon .. '\n' .. potionText
local currentDB
local function inNethergonZone()
local id = select(8, GetInstanceInfo())
return id == 550 or id == 552 or id == 553 or id == 554
end
local function computeItem(levelarg)
local level = levelarg or UnitLevel('player')
if inNethergonZone() and currentDB == potionDB and level >= 55 and GetItemCount(nethergon) > 0 then
return nethergon
end
for _, consumable in ipairs(currentDB) do
local item, minlevel = unpack(consumable)
if level >= minlevel and GetItemCount(item) > 0 then
return item
end
end
end
local function computeAttr(item)
if currentDB == mealDB then
return '/use item:' .. item
elseif inNethergonZone() then
return nethergonText
else
return potionText
end
end
local function updateItem(level)
local item = computeItem(level)
return {
attr = item and ('/run FollowUnit("player")\n' .. computeAttr(item)) or nil,
ui = item and { item = item } or { hide = true },
}
end
local function updateDB(db)
currentDB = db
return updateItem()
end
return function()
return updateDB(mealDB),
{
BAG_UPDATE_DELAYED = function()
return updateItem()
end,
PLAYER_ENTERING_WORLD = function()
return updateItem()
end,
PLAYER_LEVEL_UP = function(level)
return updateItem(level)
end,
PLAYER_REGEN_DISABLED = function()
return updateDB(potionDB)
end,
PLAYER_REGEN_ENABLED = function()
return updateDB(mealDB)
end,
}
end
end
local lang = {
action = function(action)
local num = action.action
local function update()
return {
attr = HasAction(num) and ('#action:' .. num) or '',
icon = GetActionTexture(num),
name = GetActionText(num),
}
end
local init = Mixin(update(), {
cooldown = { action = num },
count = { action = num },
tooltip = { action = num },
update = { action = num },
})
return init,
{
ACTIONBAR_SLOT_CHANGED = function(slot)
return slot == num and update() or {}
end,
}
end,
aura = function(action)
local index, filter = action.aura.index, action.aura.filter
local function update(unit)
if not UnitIsUnit(unit, 'player') then
return {}
else
local _, icon = UnitAura('player', index, filter)
return {
alpha = icon and 1.0 or 0.0,
icon = icon,
}
end
end
local attr = string.format('#cancelaura:%2d:%s', index, filter)
return Mixin({ attr = attr }, update('player')), { UNIT_AURA = update }
end,
bandage = function()
local macro = ''
for _, entry in ipairs(G.BandageDB) do
macro = macro .. '/use item:' .. entry[1] .. '\n'
end
local function currentSkill()
for i = 1, GetNumSkillLines() do
local name, _, _, value = GetSkillLineInfo(i)
if name == PROFESSIONS_FIRST_AID then
return value
end
end
return 0
end
local function currentItem()
local skill = currentSkill()
for _, entry in ipairs(G.BandageDB) do
local item, minskill = unpack(entry)
if skill >= minskill and GetItemCount(item) > 0 then
return item
end
end
end
local function update()
local item = currentItem()
return { ui = item and { item = item } or { hide = true } }
end
return Mixin(update(), { attr = macro }),
{
BAG_UPDATE_DELAYED = update,
CHAT_MSG_SKILL = update,
}
end,
buff = function(action)
return {
attr = '/click PallyPowerAuto RightButton\n/click ' .. addonName .. 'BuffButton',
count = action.reagent and { item = action.reagent },
icon = 135938,
tooltip = { text = 'Buff' },
}
end,
drink = consume(G.DrinkDB, G.ManaPotionDB, 32902),
eat = consume(G.FoodDB, G.HealthPotionDB, 32905),
emote = function(action)
local emote = action.emote
local keyName = 'CLICK ' .. addonName .. 'ActionButton' .. action.index .. ':LeftButton'
local function update()
local key = GetBindingKey(keyName) or ''
return { name = formatBindingKey(key) .. ' - ' .. emote }
end
return Mixin(update(), { attr = '/' .. emote }), { UPDATE_BINDINGS = update }
end,
healset = function(action)
local healset = action.healset
local spellset = {}
for _, spell in ipairs(healset.spells) do
local ranks = G.SpellDB[spell]
for i, rank in ipairs(ranks) do
local rn = #ranks - i + 1
local fullName = ('%s(Rank %d)'):format(spell, rn)
local actionText = ''
for w in spell:gmatch('%S+') do
actionText = actionText .. w:sub(1, 1)
end
actionText = actionText .. rn
table.insert(spellset, {
action = {
attr = '/dismount [noflying]\n/cast [@mouseover,help,nodead][] ' .. fullName,
icon = GetSpellTexture(spell),
name = actionText,
ui = { spell = fullName },
},
id = rank[1],
})
end
end
local idealpcts = healset.ranks
local myrank = #idealpcts - action.rank + 1
local function makeDistance(t, k)
return function(a, b)
local aa = math.abs(t[a] - k)
local bb = math.abs(t[b] - k)
return aa < bb
end
end
local function update()
local known = {}
for i, spell in ipairs(spellset) do
if IsSpellKnown(spell.id) then
table.insert(known, i)
end
end
if #known <= #idealpcts then
local myindex = known[myrank]
return myindex and spellset[myindex].action or { attr = '' }
else
local klo, khi = known[1], known[#known]
local assignedpcts = {}
for i, j in ipairs(known) do
assignedpcts[i] = 1 - (j - klo) / (khi - klo)
end
local assigneds = {}
for i, j in ipairs(assignedpcts) do
assigneds[i] = makeDistance(idealpcts, j)
end
local ideals = {}
for i, j in ipairs(idealpcts) do
ideals[i] = makeDistance(assignedpcts, j)
end
for assigned, ideal in pairs(G.StableMarriage(ideals, assigneds)) do
if ideal == myrank then
return spellset[known[assigned]].action
end
end
-- This shouldn't happen...
return { attr = '' }
end
end
return update(), { SPELLS_CHANGED = update }
end,
invslot = function(action)
local slot = action.invslot
local function update()
local item = GetInventoryItemID('player', slot)
return not item and { attr = '' }
or {
attr = '/use ' .. slot,
ui = { item = item },
}
end
return update(), { PLAYER_EQUIPMENT_CHANGED = update }
end,
item = function(action)
return {
attr = '/use item:' .. action.item,
ui = { item = action.item },
}
end,
macro = function(action)
return {
attr = action.macro,
icon = action.texture,
name = action.actionText,
tooltip = action.tooltip and { text = action.tooltip },
ui = action.ui,
}
end,
mount = function()
local function getMount(db)
for _, entry in ipairs(db) do
local item, spell = unpack(entry)
if item and GetItemCount(item) > 0 then
return '/use', 'item:' .. item, {
ui = { item = item },
}
elseif IsSpellKnown(spell) then
return '/cast', (GetSpellInfo(spell)), {
ui = { spell = spell },
}
end
end
end
local function update()
local gcmd, garg, ground = getMount(G.MountGroundDB)
local fcmd, farg, flight = getMount(G.MountFlightDB)
if gcmd and fcmd then
local macro = (
string.format('%s [nomounted,flyable] %s\n', fcmd, farg)
.. string.format('%s [nomounted,noflyable] %s\n', gcmd, garg)
.. '/dismount [mounted]'
)
-- TODO update icon etc to use flight mount based on OnUpdate IsFlyableArea
return Mixin({ attr = macro }, ground)
elseif gcmd then
local macro = string.format('%s [nomounted] %s\n/dismount [mounted]', gcmd, garg)
return Mixin({ attr = macro }, ground)
elseif fcmd then
local macro = string.format('%s [nomounted] %s\n/dismount [mounted]', fcmd, farg)
return Mixin({ attr = macro }, flight)
else
return { attr = '' }
end
end
return update(), {
BAG_UPDATE_DELAYED = update,
SPELLS_CHANGED = update,
}
end,
noncombat = function(action)
local macro = ('/run SelectGossipOption(%d)'):format(action.noncombat)
local init = {
attr = '',
icon = 134400,
}
return init,
{
GOSSIP_SHOW = function()
return { attr = macro }
end,
GOSSIP_CLOSED = function()
return { attr = '' }
end,
}
end,
oneof = function(action)
local page, spells = action.oneofpage, action.oneof
local function update()
local known = {}
for _, name in ipairs(spells) do
local id = select(7, GetSpellInfo(name))
if id and IsSpellKnown(id) then
known[id] = true
end
end
if not next(known) then
return { attr = '' }
end
local spell = spells[1]
for i = 1, 40 do
local buff = select(10, UnitBuff('player', i))
if not buff then
break
end
if known[buff] then
spell = buff
break
end
end
return { attr = '#page:' .. page, ui = { spell = spell } }
end
return {}, {
SPELLS_CHANGED = update,
UNIT_AURA = update,
}
end,
page = function(action)
return {
attr = '#page:' .. action.page,
count = action.reagent and { item = action.reagent },
icon = action.texture,
name = action.actionText,
tooltip = action.tooltip and { text = action.tooltip },
}
end,
petaction = function(action)
local num = action.petaction
local function update()
local _, texture, isToken, isActive, autoCastAllowed, autoCastEnabled = GetPetActionInfo(num)
local icon = isToken and _G[texture] or texture
return {
alpha = icon and 1.0 or 0.0,
autocast = autoCastAllowed and (autoCastEnabled and 'enabled' or 'disabled') or 'disallowed',
checked = { value = isActive or false },
icon = icon,
}
end
local init = Mixin(update(), {
attr = '#petaction:' .. num,
cooldown = { petaction = num },
tooltip = { petaction = num },
})
return init, {
PET_BAR_UPDATE = update,
PET_UI_UPDATE = update,
UNIT_PET = update,
}
end,
shapeshift = function(action)
local shapes = action.shapeshift
local macro = '/cast'
for shape, spell in pairs(shapes) do
macro = macro .. (' [form:%d,@mouseover,help,nodead][form:%d] %s;'):format(shape, shape, spell)
end
local function update()
local shape = GetShapeshiftForm()
local spell = shape and shapes[shape] or nil
local spellid = spell and select(7, GetSpellInfo(spell)) or nil
return { ui = spellid and IsSpellKnown(spellid) and { spell = spellid } or { hide = true } }
end
return { attr = macro }, {
SPELLS_CHANGED = update,
UPDATE_SHAPESHIFT_FORM = update,
}
end,
shoot = function()
local macro = table.concat({
'/cast [equipped:bows] Shoot Bow',
'/cast [equipped:crossbows] Shoot Crossbow',
'/cast [equipped:guns] Shoot Gun',
'/cast [equipped:thrown] Throw',
'/cast [equipped:wand] !Shoot',
}, '\n')
local function update()
local ammo = GetInventoryItemID('player', 0)
local ranged = GetInventoryItemID('player', 18)
local item = ammo ~= 0 and ammo or ranged
return {
checked = { spell = 5019 },
color = 1.0,
count = item and { item = item } or { value = -1 },
ui = ranged and { item = ranged } or { hide = true },
update = { reset = true },
}
end
return Mixin(update(), { attr = macro }), { PLAYER_EQUIPMENT_CHANGED = update }
end,
spell = function(action)
local shortName = action.spell
local rankStr = action.rank and ('Rank ' .. action.rank) or nil
local fullName = shortName .. (rankStr and ('(' .. rankStr .. ')') or '')
local macrot = { '/cast' }
if action.mouseover then
table.insert(macrot, '[@mouseover,help,nodead][]')
elseif action.options then
table.insert(macrot, action.options)
end
table.insert(macrot, fullName)
local macro = (
(action.dismount ~= false and '/dismount [noflying]\n' or '')
.. (action.stopcasting and '/stopcasting\n' or '')
.. table.concat(macrot, ' ')
)
local function update()
local spellid = select(7, GetSpellInfo(shortName, rankStr))
return {
attr = spellid and IsSpellKnown(spellid) and macro or '',
-- Use the spell base name for GetSpellTexture; more likely to work on login.
icon = GetSpellTexture(shortName),
sparkle = action.sparkle and spellid and { aura = spellid },
}
end
local init = Mixin(update(), {
count = { spell = fullName },
name = action.actionText,
ui = { spell = fullName },
})
return init, { SPELLS_CHANGED = update }
end,
spells = function(action)
local spells = action.spells
local prefix = (
(action.stopcasting and '/stopcasting\n' or '')
.. '/cast'
.. (action.mouseover and ' [@mouseover,help,nodead][] ' or ' ')
)
local function update()
for _, name in ipairs(spells) do
local id = select(7, GetSpellInfo(name))
if id and IsSpellKnown(id) then
return {
attr = prefix .. name,
ui = { spell = name },
}
end
end
return {}
end
return {}, { SPELLS_CHANGED = update }
end,
}
return function(action)
for k, v in pairs(lang) do
if action[k] then
return v(action)
end
end
error('invalid action')
end
end)()
local updateButton = (function()
local lang = {
alpha = function(button, alpha)
button:SetAlpha(alpha)
end,
autocast = (function()
local autocastLang = {
disabled = function(button)
button.AutoCastable:Show()
AutoCastShine_AutoCastStop(button.AutoCastShine)
end,
disallowed = function(button)
button.AutoCastable:Hide()
AutoCastShine_AutoCastStop(button.AutoCastShine)
end,
enabled = function(button)
button.AutoCastable:Show()
AutoCastShine_AutoCastStart(button.AutoCastShine)
end,
}
return function(button, autocast)
if button.AutoCastable then
assert(autocastLang[autocast], 'invalid autocast ' .. autocast)(button)
end
end
end)(),
checked = (function()
local checkedLang = {
spell = function(spell)
return IsCurrentSpell(spell) or IsAutoRepeatSpell(spell)
end,
value = function(value)
return value
end,
}
return function(button, checked)
if button.SetChecked then
local k, v = next(checked)
local fn = assert(checkedLang[k], 'invalid checked program ' .. tostring(k))
button:SetChecked(fn(v))
end
end
end)(),
color = function(button, color)
if button.icon then
button.icon:SetVertexColor(color, color, color)
end
end,
cooldown = (function()
local cooldownLang = {
action = function(action)
return GetActionCooldown(action)
end,
item = function(item)
return C_Container.GetItemCooldown(item)
end,
petaction = function(petaction)
return GetPetActionCooldown(petaction)
end,
reset = function()
return 0, 0, 0
end,
spell = function(spell)
return GetSpellCooldown(spell)
end,
}
return function(button, prog)
if button.cooldown then
local k, v = next(prog)
local start, duration, enable, modRate = cooldownLang[k](v)
CooldownFrame_Set(button.cooldown, start, duration, enable, false, modRate)
end
end
end)(),
count = (function()
local countLang = {
action = function(action)
return IsConsumableAction(action) and GetActionCount(action) or -1
end,
item = function(item)
local stackable = stackables[item]
if stackable == nil then
C_Item.RequestLoadItemDataByID(item)
end
return (stackable or IsConsumableItem(item)) and GetItemCount(item) or -1
end,
spell = function(spell)
return IsConsumableSpell(spell) and GetSpellCount(spell) or -1
end,
value = function(value)
return value
end,
}
return function(button, prog)
if button.Count then
local k, v = next(prog)
local count = countLang[k](v)
button.Count:SetText(count < 0 and '' or count > 9999 and '*' or tostring(count))
end
end
end)(),
icon = function(button, icon)
if button.icon then
button.icon:SetTexture(icon)
end
end,
name = function(button, name)
(button.Name or button.Text):SetText(name)
end,
sparkle = (function()
local sparkleLang = {
aura = function(aura)
for i = 1, 40 do
local spellid = select(10, UnitAura('player', i))
if spellid == aura then
return false
end
end
return true
end,
}
return function(button, sparkle)
local k, v = next(sparkle)
local on = assert(sparkleLang[k])(v)
local fn = on and AutoCastShine_AutoCastStart or AutoCastShine_AutoCastStop
fn(button.AutoCastShine)
end
end)(),
tooltip = (function()
local tooltipLang = {
action = function(action)
GameTooltip:SetAction(action)
end,
item = function(item)
GameTooltip:SetHyperlink('item:' .. item)
end,
petaction = function(petaction)
GameTooltip:SetPetAction(petaction)
end,
spell = function(spell)
if type(spell) == 'string' then
spell = select(7, GetSpellInfo(spell))
end
GameTooltip:SetSpellByID(spell)
local subtext = GetSpellSubtext(spell)
if subtext then
GameTooltipTextRight1:SetText(subtext)
GameTooltipTextRight1:SetTextColor(0.5, 0.5, 0.5)
GameTooltipTextRight1:Show()
GameTooltip:Show()
end
end,
text = function(text)
GameTooltip:SetText(text)
end,
}
return function(button, tooltip)
local k, v = next(tooltip)
local fn = tooltipLang[k]
button.ttfn = fn and function()
fn(v)
end or nil
end
end)(),
update = (function()
local function updateColor(isFooInRange, isUsableFoo)
local function getColor(foo)
if not InCombatLockdown() and isFooInRange(foo, 'target') == 0 then
return 0.8, 0.1, 0.1
end
local isUsable, notEnoughMana = isUsableFoo(foo)
if isUsable then
return 1.0, 1.0, 1.0
elseif notEnoughMana then
return 0.5, 0.5, 1.0
else
return 0.4, 0.4, 0.4
end
end
return function(foo, button)
if button.icon then
button.icon:SetVertexColor(getColor(foo))
end
end
end
local updateLang = {
action = updateColor(IsActionInRange, IsUsableAction),
item = updateColor(IsItemInRange, IsUsableItem),
reset = function() end,
spell = updateColor(IsSpellInRange, IsUsableSpell),
}
return function(button, prog)
local k, v = next(prog)
updateLang[k](v, button)
end
end)(),
}
return function(button, update)
if button then
for k, v in pairs(update) do
assert(lang[k], 'unknown function ' .. k)(button, v)
end
end
end
end)()
local function makeActionButtons()
local attachToGrid = (function()
local width, height = 36, 18
local frames = {}
for _ = 1, 96 do
local frame = CreateFrame('Frame')
frame:SetSize(width, height)
table.insert(frames, frame)
end
for i, frame in ipairs(frames) do
if i <= 84 then
frame:SetPoint('BOTTOM', frames[i + 12], 'TOP')
end
if (i - 1) % 12 < 5 then
frame:SetPoint('RIGHT', frames[i + 1], 'LEFT')
elseif (i - 1) % 12 > 6 then
frame:SetPoint('LEFT', frames[i - 1], 'RIGHT')
end
end
frames[90]:SetPoint('BOTTOMRIGHT', UIParent, 'BOTTOM')
frames[91]:SetPoint('BOTTOMLEFT', UIParent, 'BOTTOM')
return function(frame, row, col)
local index = (row - 1) * 24 + col
frame:ClearAllPoints()
frame:SetPoint('TOPLEFT', frames[index], 'TOPLEFT')
frame:SetPoint('BOTTOMRIGHT', frames[index + 12], 'BOTTOMRIGHT')
end
end)()
local scripts = {
OnEnter = function(self)
if self.ttfn then
GameTooltip_SetDefaultAnchor(GameTooltip, self)
self.ttfn()
end
end,
OnEvent = function(self)
local binder = addonName .. 'ActionButton' .. self:GetID()
local key = GetBindingKey('CLICK ' .. binder .. ':LeftButton')
if key then
self.HotKey:SetText(formatBindingKey(key))
self.HotKey:Show()
else
self.HotKey:Hide()
end
end,
OnLeave = function()
GameTooltip:Hide()
end,
}
local buttons = {}
local parent = CreateFrame('Frame', addonName .. 'ActionRoot', _G[addonName .. 'Root'])
for row = 1, 4 do
for col = 1, 12 do
local button = CreateFrame(
'CheckButton',
addonName .. 'ActionButton' .. (#buttons + 1),
parent,
'ActionButtonTemplate, SecureActionButtonTemplate'
)
attachToGrid(button, row, col)
button:SetMotionScriptsWhileDisabled(true)
button.icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
button.HotKey:SetFont(button.HotKey:GetFont(), 13, 'OUTLINE')
button.HotKey:SetVertexColor(0.75, 0.75, 0.75)
button.HotKey:SetPoint('TOPLEFT', button, 'TOPLEFT', -2, -4)
button.Count:SetFont(button.Count:GetFont(), 16, 'OUTLINE')
button:SetNormalTexture('Interface\\Buttons\\UI-Quickslot2')
button.NormalTexture:SetTexCoord(0, 0, 0, 0)
button.cooldown:SetSwipeColor(0, 0, 0)
if button.AutoCastOverlay then -- addonmaker is evil
button.AutoCastOverlay:SetScale(1.2)
end
button:RegisterEvent('UPDATE_BINDINGS')
for k, v in pairs(scripts) do
button:SetScript(k, v)
end
table.insert(buttons, button)
end
end
return buttons
end
local actionPage = 'invalid'
local actionButtonState = {}
local function setupHeader(actions, defaultPage, actionButtons)
local prefix = addonName .. 'ActionButton'
local header = CreateFrame('Frame', prefix .. 'Header', UIParent, 'SecureHandlerStateTemplate')
header:Execute(([[defaultPage = %q]]):format(defaultPage))
header:Execute([[
actionPages = newtable()
buttons = newtable()
updateActionButton = [=[
local idx = ...
local currentPage = owner:GetAttribute('fractionpage')
local actionPage = actionPages[currentPage]
local attr = actionPage.attrs[idx] or ''
local type_, action, macrotext, index, filter
if attr:sub(1, 8) == '#action:' then
type_, action = 'action', tonumber(attr:sub(9))
elseif attr:sub(1, 11) == '#petaction:' then
type_, action = 'pet', tonumber(attr:sub(12))
elseif attr:sub(1, 12) == '#cancelaura:' then
type_, index, filter = 'cancelaura', tonumber(attr:sub(13, 14)), attr:sub(16)
else
type_, macrotext = 'macro', attr
end
local button = buttons[idx]
button:SetAttribute('type', type_)
button:SetAttribute('action', action)
button:SetAttribute('macrotext', macrotext)
button:SetAttribute('index', index)
button:SetAttribute('filter', filter)
if attr ~= '' then
owner:CallMethod('InsecureActionButtonRefresh', idx)
button:Show()
else
button:Hide()
end
]=]
updateActionAttr = [=[
local pageName, idx, attr = ...
actionPages[pageName].attrs[idx] = attr
if pageName == owner:GetAttribute('fractionpage') then
owner:Run(updateActionButton, idx)
end
]=]
updateActionPage = [=[
local page = ...
local currentPage = owner:GetAttribute('fractionpage')
if page ~= currentPage then
owner:CallMethod('InsecureUpdateActionPage', page)
owner:SetAttribute('fractionpage', page)
for buttonid, button in ipairs(buttons) do
owner:Run(updateActionButton, buttonid)
end
end
]=]
updatePageOnClick = [=[
local buttonid = ...
local currentPage = actionPages[owner:GetAttribute('fractionpage')]
local attr = currentPage.attrs[buttonid] or ''
local page = attr:sub(1, 6) == '#page:' and attr:sub(7) or currentPage.nextActionPage
owner:Run(updateActionPage, page)
]=]
]])
for name, page in pairs(actions) do
header:Execute(([[
local name, nextActionPage = %q, %q
local t = newtable()
t.attrs = newtable()
t.nextActionPage = nextActionPage
actionPages[name] = t
]]):format(name, page.nextActionPage))
end
for idx, button in ipairs(actionButtons) do
button:Hide()
button:SetID(idx)
header:WrapScript(
button,
'OnClick',
'return nil, true',
[[
owner:Run(updatePageOnClick, self:GetID())
]]
)
header:SetFrameRef('tmp', button)
header:Execute([[tinsert(buttons, self:GetFrameRef('tmp'))]])
end
RegisterAttributeDriver(header, 'state-petexists', '[@pet,exists] true; false')
header:SetAttribute(
'_onstate-petexists',
[=[
-- If we just got a pet and it's not a hunter/warlock pet, switch to the pet page.
-- If we just lost a pet and we're on the pet page, go back to the default page.
local petExists = newstate == 'true'
local creatureFamily, petName = PlayerPetSummary()
if petExists and not creatureFamily and petName ~= 'Shadowfiend' then
owner:Run(updateActionPage, 'pet')
elseif not petExists and owner:GetAttribute('fractionpage') == 'pet' then
owner:Run(updateActionPage, defaultPage)
end
]=]
)
header.InsecureActionButtonRefresh = function(_, idx)
local reset = {
alpha = 1.0,
autocast = 'disallowed',
checked = { value = false },
color = 1.0,
cooldown = { reset = true },
count = { value = -1 },
icon = 136235, -- samwise
name = '',
tooltip = { reset = true },
}
updateButton(actionButtons[idx], Mixin(reset, actionButtonState[actionPage][idx]))
end
header.InsecureUpdateActionPage = function(_, newPage)
actionPage = newPage
end
header:RegisterEvent('PLAYER_ENTERING_WORLD')
header:SetScript('OnEvent', function(self)
self:Execute([[self:Run(updateActionPage, defaultPage)]])
end)
for page in pairs(actions) do
local name = page:gsub('^%l', string.upper)
local switch = CreateFrame('Button', prefix .. name .. 'Switcher', header, 'SecureActionButtonTemplate')
header:WrapScript(
switch,
'OnClick',
'return nil, true',
([=[
local page = %q
owner:Run(updateActionPage, owner:GetAttribute('fractionpage') == page and defaultPage or page)
]=]):format(page)
)
end
local defaultSwitch = CreateFrame('Button', prefix .. 'DefaultSwitcher', header, 'SecureActionButtonTemplate')
header:WrapScript(defaultSwitch, 'OnClick', 'return nil, true', 'owner:Run(updateActionPage, defaultPage)')
local function updateAttr(pageName, idx, attr)
header:Execute(([[self:Run(updateActionAttr, %q, %d, %q)]]):format(pageName, idx, attr))
end
return updateAttr
end
local function setupActions(actions, defaultPage, actionButtons)
local updateAttr = setupHeader(actions, defaultPage, actionButtons)
local maybeSetAttr, drainPendingAttrs = (function()
local pendingAttrs = {}
local function maybeSetAttr(pageName, idx, attr)
if InCombatLockdown() then
pendingAttrs[pageName] = pendingAttrs[pageName] or {}
pendingAttrs[pageName][idx] = attr
else
updateAttr(pageName, idx, attr)
end
end
local function drainPendingAttrs()
for pageName, pageAttrs in pairs(pendingAttrs) do
for idx, attr in pairs(pageAttrs) do
updateAttr(pageName, idx, attr)
end
wipe(pageAttrs)
end
end
return maybeSetAttr, drainPendingAttrs
end)()
local updateAction = (function()
local uiLang = {
hide = function()
return {
alpha = 0.0,
tooltip = { reset = true },
}
end,
item = function(item)
return {
alpha = 1.0,
color = IsUsableItem(item) and 1.0 or 0.4,
cooldown = { item = item },
count = { item = item },
icon = GetItemIcon(item),
tooltip = { item = item },
update = { item = item },
}
end,
spell = function(spell)
return {
alpha = 1.0,
checked = { spell = spell },
cooldown = { spell = spell },
count = { spell = spell },
icon = GetSpellTexture(spell),
tooltip = { spell = spell },
update = { spell = spell },
}
end,
}
return function(pageName, idx, update)
if update.attr then
maybeSetAttr(pageName, idx, update.attr)
update.attr = nil
end
if update.ui then
local k, v = next(update.ui)
update = Mixin({}, uiLang[k](v), update)
update.ui = nil
end
Mixin(actionButtonState[pageName][idx], update)
if pageName == actionPage then
updateButton(actionButtons[idx], update)
end
end
end)()
local handlers = {}
local function addHandler(ev, handler)
handlers[ev] = handlers[ev] or {}
table.insert(handlers[ev], handler)
end