forked from kemayo/wow-silverdragon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloot.lua
930 lines (885 loc) · 26.7 KB
/
loot.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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
local myname, ns = ...
local core = LibStub("AceAddon-3.0"):GetAddon("SilverDragon")
local Debug = core.Debug
local DebugF = core.DebugF
local function safeunpack(table_or_value)
if type(table_or_value) == "table" then
return unpack(table_or_value)
end
return table_or_value
end
local function all(test, ...)
for i=1,select("#", ...) do
if not test((select(i, ...))) then
return false
end
end
return true
end
local function any(test, ...)
for i=1,select("#", ...) do
if test((select(i, ...))) then
return true
end
end
return false
end
local brokenItems = {
-- itemid : {appearanceid, sourceid}
[153268] = {25124, 90807}, -- Enclave Aspirant's Axe
}
local function GetAppearanceAndSource(itemLinkOrID)
local itemID = GetItemInfoInstant(itemLinkOrID)
if not itemID then return end
local appearanceID, sourceID = C_TransmogCollection.GetItemInfo(itemLinkOrID)
if not appearanceID then
-- sometimes the link won't actually give us an appearance, but itemID will
-- e.g. mythic Drape of Iron Sutures from Shadowmoon Burial Grounds
appearanceID, sourceID = C_TransmogCollection.GetItemInfo(itemID)
end
if not appearanceID and brokenItems[itemID] then
-- ...and there's a few that just need to be hardcoded
appearanceID, sourceID = unpack(brokenItems[itemID])
end
return appearanceID, sourceID
end
local canLearnCache = {}
local function CanLearnAppearance(itemLinkOrID)
local itemID = GetItemInfoInstant(itemLinkOrID)
if not itemID then return end
if canLearnCache[itemID] ~= nil then
return canLearnCache[itemID]
end
-- First, is this a valid source at all?
local canBeChanged, noChangeReason, canBeSource, noSourceReason = C_Transmog.GetItemInfo(itemID)
if canBeSource == nil then
-- data loading, don't cache this
return
end
if not canBeSource then
canLearnCache[itemID] = false
return false
end
local appearanceID = GetAppearanceAndSource(itemLinkOrID)
if not appearanceID then
canLearnCache[itemID] = false
return false
end
if not C_TransmogCollection.GetAppearanceSources(appearanceID) then
-- This returns nil for inappropriate appearances
canLearnCache[itemID] = false
return false
end
canLearnCache[itemID] = true
return true
end
local hasAppearanceCache = {}
local function HasAppearance(itemLinkOrID)
local itemID = GetItemInfoInstant(itemLinkOrID)
if not itemID then return end
if hasAppearanceCache[itemID] ~= nil then
return hasAppearanceCache[itemID]
end
local appearanceID, sourceID = GetAppearanceAndSource(itemLinkOrID)
if not appearanceID then
hasAppearanceCache[itemID] = false
return false
end
local _, _, _, _, sourceKnown = C_TransmogCollection.GetAppearanceSourceInfo(sourceID)
if sourceKnown then
hasAppearanceCache[itemID] = true
return true
end
local sources = C_TransmogCollection.GetAppearanceSources(appearanceID)
if not sources then
hasAppearanceCache[itemID] = false
return false
end
for _, source in pairs(sources) do
if source.isCollected == true then
hasAppearanceCache[itemID] = true
return true
end
end
return false
end
local function PlayerHasMount(mountid)
return (select(11, C_MountJournal.GetMountInfoByID(mountid)))
end
local function PlayerHasPet(petid)
return (C_PetJournal.GetNumCollectedInfo(petid) > 0)
end
local itemRestricted = function(item)
if type(item) ~= "table" then return false end
if item.covenant and item.covenant ~= C_Covenants.GetActiveCovenantID() then
return true
end
if item.class and select(2, UnitClass("player")) ~= item.class then
return true
end
return false
end
local itemIsKnowable = function(item)
if type(item) == "table" then
return (item.toy or item.mount or item.pet or item.quest or CanLearnAppearance(item[1])) -- and not itemRestricted(item)
end
return CanLearnAppearance(item)
end
local itemIsKnown = function(item)
-- returns true/false/nil for yes/no/not-knowable
if type(item) == "table" then
if item.toy then return PlayerHasToy(item[1]) end
if item.mount then return PlayerHasMount(item.mount) end
if item.pet then return PlayerHasPet(item.pet) end
if item.quest then return C_QuestLog.IsQuestFlaggedCompleted(item.quest) or C_QuestLog.IsOnQuest(item.quest) end
if CanLearnAppearance(item[1]) then return HasAppearance(item[1]) end
elseif CanLearnAppearance(item) then
return HasAppearance(item)
end
end
ns.Loot = {}
-- _G.SDLoot = ns.Loot
function ns.Loot.HasLoot(id)
if not (id and ns.mobdb[id]) then
return false
end
return ns.mobdb[id].loot
end
do
local function make_iter(test)
return function(t, prestate)
local state, item = next(t, prestate)
while state do
local ret = test(item)
if ret then
return state, ret, item
end
state, item = next(t, state)
end
end
end
local mount_iter = make_iter(function(item) return type(item) == "table" and item.mount, item end)
local pet_iter = make_iter(function(item) return type(item) == "table" and item.pet, item end)
local toy_iter = make_iter(function(item) return type(item) == "table" and item.toy and item[1], item end)
local quest_iter = make_iter(function(item) return type(item) == "table" and item.quest, item end)
local regular_iter = make_iter(function(item)
if type(item) == "number" then
return item
end
if not (item.mount or item.pet or item.toy) then
return item[1], item
end
end)
local noloot = {}
function ns.Loot.IterMounts(id)
return mount_iter, ns.mobdb[id].loot or noloot, nil
end
function ns.Loot.IterPets(id)
return pet_iter, ns.mobdb[id].loot or noloot, nil
end
function ns.Loot.IterToys(id)
return toy_iter, ns.mobdb[id].loot or noloot, nil
end
function ns.Loot.IterQuests(id)
return quest_iter, ns.mobdb[id].loot or noloot, nil
end
function ns.Loot.IterRegularLoot(id)
return regular_iter, ns.mobdb[id].loot or noloot, nil
end
end
function ns.Loot.HasToys(id)
if not (id and ns.mobdb[id] and ns.mobdb[id].loot) then return false end
for _ in ns.Loot.IterToys(id) do
return true
end
return false
end
function ns.Loot.HasMounts(id)
if not (id and ns.mobdb[id] and ns.mobdb[id].loot) then return false end
for _ in ns.Loot.IterMounts(id) do
return true
end
return false
end
function ns.Loot.HasPets(id)
if not (id and ns.mobdb[id] and ns.mobdb[id].loot) then return false end
for _ in ns.Loot.IterPets(id) do
return true
end
return false
end
function ns.Loot.HasKnowableLoot(id)
if not (id and ns.mobdb[id] and ns.mobdb[id].loot) then return false end
return any(itemIsKnowable, unpack(ns.mobdb[id].loot))
end
function ns.Loot.HasRegularLoot(id)
if not (id and ns.mobdb[id] and ns.mobdb[id].loot) then return false end
for _ in ns.Loot.IterRegularLoot(id) do
return true
end
return false
end
function ns.Loot.Cache(id)
if not (id and ns.mobdb[id] and ns.mobdb[id].loot) then return false end
for _, item in ipairs(ns.mobdb[id].loot) do
C_Item.RequestLoadItemDataByID(type(item) == "table" and item[1] or item)
end
end
ns.Loot.Status = setmetatable({}, {__call = function(_, id, include_transmog)
-- returns nil if there's no knowable loot
-- returns true if all knowable loot is collected
-- returns false if not all knowable loot is collected
-- if knowable loot, also returns the status for mount,toy,pet after the first return
if not id or not ns.mobdb[id] then
return
end
local mount = ns.Loot.Status.Mount(id)
local toy = ns.Loot.Status.Toy(id)
local pet = ns.Loot.Status.Pet(id)
local quest = ns.Loot.Status.Quest(id)
local transmog
if include_transmog then transmog = ns.Loot.Status.Transmog(id) end
if (mount == nil and toy == nil and pet == nil and quest == nil and transmog == nil) then
return nil
end
return (mount ~= false and toy ~= false and pet ~= false and quest ~= false and transmog ~= false), mount, toy, pet, quest, transmog
end})
function ns.Loot.Status.Toy(id)
if not id or not ns.mobdb[id] then return end
local ret = nil
for _, toyid, item in ns.Loot.IterToys(id) do
if not itemRestricted(item) then
if not PlayerHasToy(toyid) then
return false
end
ret = true
end
end
return ret
end
function ns.Loot.Status.Mount(id)
if not id or not ns.mobdb[id] then return end
local ret = nil
for _, mountid, item in ns.Loot.IterMounts(id) do
if not itemRestricted(item) then
if not PlayerHasMount(mountid) then
return false
end
ret = true
end
end
return ret
end
function ns.Loot.Status.Pet(id)
if not id or not ns.mobdb[id] then return end
local ret = nil
for _, petid, item in ns.Loot.IterPets(id) do
if not itemRestricted(item) then
if not PlayerHasPet(petid) then
return false
end
ret = true
end
end
return ret
end
function ns.Loot.Status.Quest(id)
if not id or not ns.mobdb[id] then return end
local ret = nil
for _, questid, item in ns.Loot.IterQuests(id) do
if not (C_QuestLog.IsQuestFlaggedCompleted(questid) or C_QuestLog.IsOnQuest(questid)) then
return false
end
ret = true
end
return ret
end
function ns.Loot.Status.Transmog(id)
if not id or not ns.mobdb[id] then return end
local ret = nil
for _, itemid, item in ns.Loot.IterRegularLoot(id) do
if not itemRestricted(item) and CanLearnAppearance(itemid) then
if not HasAppearance(itemid) then
return false
end
ret = true
end
end
return ret
end
local function get_tooltip(tooltip, i)
if i > 1 then
local comparison = _G['ShoppingTooltip'..(i-1)]
if not comparison then return end
comparison:SetOwner(tooltip, "ANCHOR_NONE")
comparison:ClearAllPoints()
local anchor = tooltip:GetOwner()
local side
local topPos = anchor:GetTop() or 0
local bottomPos = anchor:GetBottom() or 0
local bottomDist = GetScreenHeight() - bottomPos
if bottomDist > topPos then
side = "top"
else
side = "bottom"
end
if side == "top" then
comparison:SetPoint("BOTTOMLEFT", tooltip, "TOPLEFT", 0, 10)
else
comparison:SetPoint("TOPLEFT", tooltip, "BOTTOMLEFT", 0, -10)
end
return comparison
end
return tooltip
end
local Details = {
toy = function(tooltip, i, toyid)
tooltip:SetHyperlink(("item:%d"):format(toyid))
end,
mount = function(tooltip, i, mountid)
local name, spellid, texture, _, _, _, _, _, _, _, isCollected = C_MountJournal.GetMountInfoByID(mountid)
if not name then
tooltip:AddLine("mount:" .. mountid)
tooltip:AddLine(SEARCH_LOADING_TEXT, 0, 1, 1)
return
end
local _, description, source = C_MountJournal.GetMountInfoExtraByID(mountid)
tooltip:AddLine(name)
tooltip:AddTexture(texture)
tooltip:AddLine(description, 1, 1, 1, true)
tooltip:AddLine(source)
if isCollected then
tooltip:AddLine(USED, 1, 0, 0)
end
end,
pet = function(tooltip, i, petid)
local name, texture, _, mobid, source, description = C_PetJournal.GetPetInfoBySpeciesID(petid)
if not name then
tooltip:AddLine("pet:" .. petid)
tooltip:AddLine(SEARCH_LOADING_TEXT, 0, 1, 1)
return
end
local owned, limit = C_PetJournal.GetNumCollectedInfo(petid)
tooltip:AddLine(name)
tooltip:AddTexture(texture)
tooltip:AddLine(description, 1, 1, 1, true)
tooltip:AddLine(source)
tooltip:AddLine(ITEM_PET_KNOWN:format(owned, limit))
end,
item = function(tooltip, i, itemid)
tooltip:SetHyperlink(("item:%d"):format(itemid))
end,
restrictions = function(tooltip, itemdata)
if not (itemdata and type(itemdata) == "table") then return end
if itemdata.covenant then
local covenant = C_Covenants.GetCovenantData(itemdata.covenant)
local active = itemdata.covenant == C_Covenants.GetActiveCovenantID()
if covenant then
tooltip:AddLine(
ITEM_REQ_SKILL:format(COVENANT_COLORS[covenant.ID]:WrapTextInColorCode(covenant.name)),
(active and GREEN_FONT_COLOR or RED_FONT_COLOR):GetRGB()
)
end
end
if itemdata.class then
local active = select(2, UnitClass("player")) == itemdata.class
tooltip:AddLine(
ITEM_REQ_SKILL:format(RAID_CLASS_COLORS[itemdata.class]:WrapTextInColorCode(LOCALIZED_CLASS_NAMES_FEMALE[itemdata.class])),
(active and GREEN_FONT_COLOR or RED_FONT_COLOR):GetRGB()
)
end
tooltip:Show()
end,
}
ns.Loot.Details = Details
function ns.Loot.Details.UpdateTooltip(tooltip, id, only)
if not (id and ns.mobdb[id]) then
return
end
local toy = (not only or only == "toy") and ns.Loot.HasToys(id)
local mount = (not only or only == "mount") and ns.Loot.HasMounts(id)
local pet = (not only or only == "pet") and ns.Loot.HasPets(id)
local regular = (not only or only == "regular") and ns.Loot.HasRegularLoot(id)
if mount then
for i, mountid, itemdata in ns.Loot.IterMounts(id) do
Details.mount(tooltip, i, mountid)
Details.restrictions(tooltip, itemdata)
end
end
if pet then
if mount then
tooltip:AddLine("---")
end
for i, petid, itemdata in ns.Loot.IterPets(id) do
Details.pet(tooltip, i, petid)
Details.restrictions(tooltip, itemdata)
end
end
local n = (pet or mount) and 2 or 1
if toy then
local toytip
for i, toyid, itemdata in ns.Loot.IterToys(id) do
toytip = get_tooltip(toytip or tooltip, n)
if not toytip then return end -- out of comparisons
Details.toy(toytip, n, toyid)
Details.restrictions(toytip, itemdata)
n = n + 1
end
end
if regular then
local itemtip
for i, itemid, itemdata in ns.Loot.IterRegularLoot(id) do
itemtip = get_tooltip(itemtip or tooltip, n)
if not itemtip then return end -- out of comparisons
Details.item(itemtip, n, itemid)
Details.restrictions(itemtip, itemdata)
n = n + 1
end
end
end
local function requiresLabel(item)
local ret = " "
if type(item) == "table" then
-- todo: faction?
if item.covenant then
local data = C_Covenants.GetCovenantData(item.covenant)
-- local active = item.covenant == C_Covenants.GetActiveCovenantID()
if data then
ret = ret .. PARENS_TEMPLATE:format(COVENANT_COLORS[item.covenant]:WrapTextInColorCode(data.name))
end
end
if item.class then
ret = ret .. PARENS_TEMPLATE:format(RAID_CLASS_COLORS[item.class]:WrapTextInColorCode(LOCALIZED_CLASS_NAMES_FEMALE[item.class]))
end
end
if itemIsKnowable(item) then
local known = itemIsKnown(item)
if known or not itemRestricted(item) then
-- don't want to show the x, but might as well show the check
ret = ret .. CreateAtlasMarkup(known and "common-icon-checkmark" or "common-icon-redx")
end
end
return ret == " " and "" or ret
end
local Summary = {
toy = function(tooltip, i, toyid, itemdata)
local _, name, icon = C_ToyBox.GetToyInfo(toyid)
local owned = PlayerHasToy(toyid)
if name then
tooltip:AddDoubleLine(
i==1 and TOY or " ",
"|T" .. icon .. ":0|t " .. name .. requiresLabel(itemdata),
1, 1, 0,
owned and 0 or 1, owned and 1 or 0, 0
)
else
tooltip:AddDoubleLine(i==1 and TOY or " ", SEARCH_LOADING_TEXT, 1, 1, 0, 0, 1, 1)
end
end,
mount = function(tooltip, i, mountid, itemdata)
local name, _, icon, _, _, _, _, _, _, _, isCollected = C_MountJournal.GetMountInfoByID(mountid)
if name then
tooltip:AddDoubleLine(
i==1 and MOUNT or " ",
"|T" .. icon .. ":0|t " .. name .. requiresLabel(itemdata),
1, 1, 0,
isCollected and 0 or 1, isCollected and 1 or 0, 0
)
else
tooltip:AddDoubleLine(i==1 and MOUNT or " ", SEARCH_LOADING_TEXT, 1, 1, 0, 0, 1, 1)
end
end,
pet = function(tooltip, i, petid, itemdata)
local name, icon = C_PetJournal.GetPetInfoBySpeciesID(petid)
local owned, limit = C_PetJournal.GetNumCollectedInfo(petid)
if name then
local r, g, b = 1, 0, 0
if owned == limit then
r, g, b = 0, 1, 0
elseif owned > 0 then
r, g, b = 1, 1, 0
end
tooltip:AddDoubleLine(
i==1 and TOOLTIP_BATTLE_PET or " ",
"|T" .. icon .. ":0|t " .. (ITEM_SET_NAME):format(name, owned, limit) .. requiresLabel(itemdata),
1, 1, 0,
r, g, b
)
else
tooltip:AddDoubleLine(i==1 and TOOLTIP_BATTLE_PET or " ", SEARCH_LOADING_TEXT, 1, 1, 0, 0, 1, 1)
end
end,
item = function(tooltip, i, itemid, itemdata)
local name, link, quality, _, _, _, _, _, _, icon = GetItemInfo(itemid)
if name then
tooltip:AddDoubleLine(
i==1 and ENCOUNTER_JOURNAL_ITEM or " ",
"|T" .. icon .. ":0|t " .. name .. requiresLabel(itemdata),
1, 1, 0,
GetItemQualityColor(quality)
)
else
tooltip:AddDoubleLine(i==1 and ENCOUNTER_JOURNAL_ITEM or " ", SEARCH_LOADING_TEXT, 1, 1, 0, 0, 1, 1)
end
end,
}
ns.Loot.Summary = Summary
function ns.Loot.Summary.UpdateTooltip(tooltip, id, only_knowable)
if not (id and ns.mobdb[id]) then
return
end
local offset = 0
local n = 0
for i, mountid, itemdata in ns.Loot.IterMounts(id) do
n = n + 1
Summary.mount(tooltip, i - offset, mountid, itemdata)
end
offset = n
for i, toyid, itemdata in ns.Loot.IterToys(id) do
n = n + 1
Summary.toy(tooltip, i - offset, toyid, itemdata)
end
offset = n
for i, petid, itemdata in ns.Loot.IterPets(id) do
n = n + 1
Summary.pet(tooltip, i - offset, petid, itemdata)
end
if not only_knowable then
offset = n
for i, itemid, itemdata in ns.Loot.IterRegularLoot(id) do
n = n + 1
Summary.item(tooltip, i - offset, itemid, itemdata)
end
end
end
do
local ITEMS_PER_ROW = 6
local BORDER_WIDTH = 8
local ITEM_WIDTH = 37
local ITEM_HEIGHT = 37
local ITEM_XOFFSET = 4
local ITEM_YOFFSET = -5
local TITLE_SPACING = 16
-- we need non-localized covenant names for atlases
-- can't use the texturekit value from covenant data, since the atlas I want doesn't conform to it
local covenants = {
[Enum.CovenantType.Kyrian] = "Kyrian",
[Enum.CovenantType.Necrolord] = "Necrolords",
[Enum.CovenantType.NightFae] = "NightFae",
[Enum.CovenantType.Venthyr] = "Venthyr",
}
local function isMouseOver(...)
for i=1, select("#", ...) do
local frame = select(i, ...)
if not frame then
break
end
if frame.IsMouseOver then
if frame:IsMouseOver() and frame:IsVisible() then
return true
end
elseif isMouseOver(unpack(frame)) then
-- this was a table, not an actual frame
return true
end
end
return false
end
local function timer_onupdate(self, elapsed)
self.checkThreshold = self.checkThreshold + elapsed
if self.checkThreshold > 0.1 then
if isMouseOver(self.watch, self.additional) then
self.timeOffFrame = 0
else
self.timeOffFrame = self.timeOffFrame + self.checkThreshold
if self.timeOffFrame > self.allowedTimeOffFrame then
self.timeOffFrame = 0
if not self.callback or self.callback(self.watch) ~= false then
ns.Loot.Window.Release(self.watch)
end
end
end
self.checkThreshold = 0
end
end
local windowPool = CreateFramePool("Frame", UIParent, "BackdropTemplate", function(framePool, frame)
frame:Hide()
frame:ClearAllPoints()
frame:SetParent(UIParent)
frame:SetFrameStrata("HIGH")
frame:SetMovable(false)
frame:RegisterForDrag(false)
frame:SetScript("OnDragStart", nil)
frame:SetScript("OnDragStop", nil)
frame.independent = nil
if frame.Reset then
frame:Reset()
end
end)
local buttonPool = CreateFramePool("ItemButton", nil, nil, function(framePool, button)
if button.RestrictionIcon then
button.RestrictionIcon:Hide()
button.KnownIcon:Hide()
end
button.lootdata = nil
button:ClearAllPoints()
button:SetParent(nil)
button:Hide()
end)
local timerPool = CreateFramePool("Frame", UIParent, nil, function(framePool, frame)
frame:Hide()
frame:SetParent(nil)
frame.checkThreshold = 0
frame.timeOffFrame = 0
frame.additional = false
frame.callback = nil
frame.watch = nil
frame:SetScript("OnUpdate", timer_onupdate)
end)
ns.Loot.Window = {}
local function window_onclick(self, mousebutton)
if mousebutton == "RightButton" then
if self.independent then
ns.Loot.Window.Release(self)
else
self:Hide()
end
end
end
local function button_onenter(self)
local loot_tooltip = ns.Tooltip.Get("Loot")
loot_tooltip:SetFrameStrata(self:GetFrameStrata())
loot_tooltip:SetFrameLevel(self:GetFrameLevel() + 1)
if self:GetCenter() > UIParent:GetCenter() then
loot_tooltip:SetOwner(self, "ANCHOR_LEFT")
else
loot_tooltip:SetOwner(self, "ANCHOR_RIGHT")
end
loot_tooltip:SetHyperlink(self:GetItemLink())
ns.Loot.Details.restrictions(loot_tooltip, self.lootdata)
if core.debuggable then
loot_tooltip:AddDoubleLine(ID, self:GetItemID())
end
loot_tooltip:Show()
self:GetParent().tooltip = loot_tooltip
end
local function button_onleave(self)
ns.Tooltip.Get("Loot"):Hide()
self:GetParent().tooltip = nil
end
local function button_onclick(self, mousebutton)
if IsModifiedClick() then
if HandleModifiedItemClick(self:GetItemLink()) then
return
end
end
if mousebutton == "RightButton" then
if self:GetParent().independent then
ns.Loot.Window.Release(self:GetParent())
else
self:GetParent():Hide()
end
end
end
local function close_onclick(self)
ns.Loot.Window.Release(self:GetParent())
end
local WindowMixin = {
Init = function(self)
self.buttons = {}
self:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background",
edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 },
})
self:SetClampedToScreen(true)
self:SetSize(43, 43)
self:SetBackdropColor(0, 0, 0, .5)
self:EnableMouse(true)
self.title = self:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
self.title:SetPoint("TOPLEFT", BORDER_WIDTH, -BORDER_WIDTH)
self.title:SetPoint("TOPRIGHT", -BORDER_WIDTH, -BORDER_WIDTH)
self.title:Hide()
self.close = CreateFrame("Button", nil, self, "UIPanelCloseButtonNoScripts")
self.close:SetSize(18, 18)
self.close:SetPoint("CENTER", self, "TOPRIGHT", -4, -4)
self.close:SetScript("OnClick", close_onclick)
self.close:Hide()
end,
Reset = function(self)
self:SetAutoHideDelay(0)
self:ClearLoot()
self.title:Hide()
self.close:Hide()
if self.tooltip then
self.tooltip:Hide()
self.tooltip = nil
end
end,
AddItem = function(self, itemid, item)
local button, isNew = buttonPool:Acquire()
button:SetParent(self)
if isNew then
button:SetScript("OnClick", button_onclick)
button:SetScript("OnEnter", button_onenter)
button:SetScript("OnLeave", button_onleave)
local sublevel = 4
if button.IconOverlay then
sublevel = select(2, button.IconOverlay:GetDrawLayer()) + 1
end
button.RestrictionIcon = button:CreateTexture(nil, "OVERLAY", nil, sublevel)
button.RestrictionIcon:SetPoint("TOPRIGHT", 4, 4)
button.KnownIcon = button:CreateTexture(nil, "OVERLAY", nil, sublevel)
button.KnownIcon:SetPoint("BOTTOMRIGHT", 4, -4)
button.KnownIcon:SetSize(16, 16)
end
local numButtons = #self.buttons
local pos = numButtons / ITEMS_PER_ROW
if ( math.floor(pos) == pos ) then
-- This is the first button in a row.
-- button:SetPoint("TOPLEFT", self.title, "BOTTOMLEFT", 0, -(ITEM_HEIGHT - ITEM_YOFFSET) * pos)
button:SetPoint("TOPLEFT", self, "TOPLEFT", BORDER_WIDTH, -BORDER_WIDTH - (ITEM_HEIGHT - ITEM_YOFFSET) * pos - (self.title:IsShown() and TITLE_SPACING or 0))
else
button:SetPoint("TOPLEFT", self.buttons[numButtons], "TOPRIGHT", ITEM_XOFFSET, 0)
end
tinsert(self.buttons, button)
self:SizeForButtons()
if itemid then
button:SetItem(itemid)
if type(item) == "table" then
button.lootdata = item
if item.count then
button:SetItemButtonCount(item.count)
end
if item.covenant and covenants[item.covenant] then
button.RestrictionIcon:SetAtlas(("covenantchoice-panel-sigil-%s"):format(covenants[item.covenant]))
button.RestrictionIcon:SetSize(16, 20) -- these are 73x96 natively
button.RestrictionIcon:Show()
elseif item.class then
button.RestrictionIcon:SetAtlas(("groupfinder-icon-class-%s"):format(item.class))
button.RestrictionIcon:SetSize(20, 20)
button.RestrictionIcon:Show()
end
end
if itemIsKnowable(item) then
local known = itemIsKnown(item)
if known or not itemRestricted(item) then
-- don't show the x for restricted items
button.KnownIcon:SetAtlas(known and "common-icon-checkmark" or "common-icon-redx")
button.KnownIcon:Show()
end
end
end
button:Show()
return button
end,
AddLoot = function(self, loot)
for _, item in ipairs(loot) do
local itemid = type(item) == "table" and item[1] or item
if itemid then
self:AddItem(itemid, item)
end
end
end,
SizeForButtons = function(self)
local columns = math.min(#self.buttons, ITEMS_PER_ROW)
local rows = math.ceil(#self.buttons / ITEMS_PER_ROW)
self:SetSize(
(2 * BORDER_WIDTH) + math.max((columns * ITEM_WIDTH) + ((columns - 1) * math.abs(ITEM_XOFFSET)), self.title:IsShown() and self.title:GetStringWidth() or 0),
(self.title:IsShown() and TITLE_SPACING or 0) + (2 * BORDER_WIDTH) + (rows * ITEM_HEIGHT) + ((rows - 1) * math.abs(ITEM_YOFFSET))
)
end,
ClearLoot = function(self)
for _, button in ipairs(self.buttons) do
buttonPool:Release(button)
end
wipe(self.buttons)
end,
SetTitle = function(self, title)
if title then
self.title:Show()
self.title:SetText(title)
else
self.title:Hide()
end
end,
SetAutoHideDelay = function(self, delay, additional, callback)
-- this is *highly* based on LibQTip-1.0's function
delay = tonumber(delay) or 0
if delay > 0 then
self.timer = self.timer or timerPool:Acquire()
self.timer.allowedTimeOffFrame = delay
self.timer.additional = additional
self.timer.callback = callback
self.timer.watch = self
self.timer:Show()
elseif self.timer then
timerPool:Release(self.timer)
self.timer = nil
end
end,
MakeIndependent = function(self)
self.close:Show()
self:SetMovable(true)
self:RegisterForDrag("LeftButton")
self:SetScript("OnDragStart", self.OnDragStart)
self:SetScript("OnDragStop", self.StopMovingOrSizing)
self.independent = true
end,
OnDragStart = function(self)
self:StartMoving()
end
}
local function GetWindow()
local window, isNew = windowPool:Acquire()
if isNew then
Mixin(window, WindowMixin)
window:Init()
end
return window
end
ns.Loot.Window.Get = GetWindow
ns.Loot.Window.Release = function(window)
-- this will hide / clearallpoints / clearloot the window
windowPool:Release(window)
core.events:Fire("LootWindowReleased", window)
end
function ns.Loot.Window.ShowForMob(id, independent)
if not (id and ns.mobdb[id] and ns.mobdb[id].loot) then
-- TODO: error message
return false
end
local window
if independent then
for other in windowPool:EnumerateActive() do
if other.independent then
window = other
break
end
end
if window then
window:ClearLoot()
else
window = GetWindow()
window:MakeIndependent()
window:SetPoint("CENTER")
end
window:SetTitle(core:GetMobLabel(id))
else
window = GetWindow()
end
window:AddLoot(ns.mobdb[id].loot)
window:Show()
core.events:Fire("LootWindowOpened", window)
return window
end
-- debug:
-- window:AddLoot({
-- 173468, 173468, 173468, 173468, 152739, 152739, 152739, 152739, 152739, 152739, 152739, 152739, 152739,
-- })
-- /script SilverDragon:ShowLootWindowForMob(160821)
function core:ShowLootWindowForMob(id)
local window = ns.Loot.Window.ShowForMob(id, true)
end
end