This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectiveTracker.lua
665 lines (527 loc) · 18 KB
/
ObjectiveTracker.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
local _, addon = ...
local module = addon:RegisterModule("ObjectiveTracker")
local LINE_TYPE_ANIM = { template = "QuestObjectiveAnimLineTemplate", freeLines = { } };
local function blockFits(block, frame, offsetY)
return frame.contentsHeight + block.height - offsetY < frame.maxHeight
end
local function scheduleFunction(func, ...)
local args = ...
C_Timer.After(0.5, function()
func(args)
end)
end
local function isComplete(entry)
return entry.itemCount >= entry.goal
end
local function isGroupComplete(group)
for _, entry in ipairs(group.entries) do
if not isComplete(entry) then
return false
end
end
return true
end
local function getObjectiveStyles(entry)
local metQuantity = isComplete(entry)
local dashStyle = metQuantity and OBJECTIVE_DASH_STYLE_HIDE or OBJECTIVE_DASH_STYLE_SHOW
if metQuantity then
return dashStyle, OBJECTIVE_TRACKER_COLOR["Complete"]
end
if entry.max == entry.min or entry.max == entry.goal then
return dashStyle, OBJECTIVE_TRACKER_COLOR["Normal"]
end
if entry.goal == entry.min then
return dashStyle, OBJECTIVE_TRACKER_COLOR["Failed"]
end
return dashStyle, OBJECTIVE_TRACKER_COLOR["Normal"]
end
local function getObjectiveText(entry)
if entry.goal == entry.max then
return string.format(
"%d/%d %s",
min(entry.goal, entry.itemCount),
entry.goal,
entry.displayName)
end
return string.format(
"%d/%d (max. %d) %s",
min(entry.goal, entry.itemCount),
entry.goal,
entry.max,
entry.displayName)
end
local objectiveTrackerFrame = CreateFrame("Frame", nil, ObjectiveTrackerBlocksFrame, "ObjectiveTrackerHeaderTemplate")
objectiveTrackerFrame.MinimizeButton:SetScript("OnClick", function(button)
PlaySound(SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON)
local trackerModule = button:GetParent().module
trackerModule:SetCollapsed(not trackerModule:IsCollapsed())
module:ObjectiveTracker_Update()
end)
local objectiveTrackerModule = ObjectiveTracker_GetModuleInfoTable("GATHERPANEL_TRACKER_MODULE")
objectiveTrackerModule:SetHeader(objectiveTrackerFrame, addon.T["GATHERING"])
function objectiveTrackerModule:GetRelatedModules()
local modules = {}
local trackerModules = Shallowcopy(ObjectiveTrackerFrame.MODULES)
table.insert(trackerModules, objectiveTrackerModule)
for _, trackerModule in ipairs(trackerModules) do
if trackerModule.Header == self.Header then
table.insert(modules, trackerModule)
end
end
return modules
end
function objectiveTrackerModule:Update()
if self.continuableContainer then
self.continuableContainer:Cancel()
end
self.continuableContainer = ContinuableContainer:Create()
local entries = GatherPanel_GetItemList()
for _, entry in pairs(entries) do
if entry.type == "ITEM" then
local item = Item:CreateFromItemID(entry.id)
self.continuableContainer:AddContinuable(item)
end
end
self.allItemsLoaded = true
self.allItemsLoaded = self.continuableContainer:ContinueOnLoad(function()
if self.allItemsLoaded then
self:BeginLayout()
self:Layout()
self:EndLayout()
else
module:InitObjectiveTracker(nil, true)
end
end)
end
function objectiveTrackerModule:Layout()
if not addon.Variables.user.showObjectiveTracker then
return
end
local groups = addon.getGroups()
for _, group in ipairs(groups) do
self:AddGroup(group)
end
end
function objectiveTrackerModule:AddGroup(group)
local block = self:GetBlock(group.id)
block.group = group
self:SetBlockHeader(block, group.groupData.name)
local allObjectivesComplete = true
local hasObjectives = false
for _, entry in ipairs(group.entries) do
if entry.tracked and entry.goal and entry.goal > 0
and (addon.Variables.user.showCompleted or entry.goal > entry.itemCount) then
hasObjectives = true
if entry.itemCount < entry.goal then
allObjectivesComplete = false
break
end
end
end
if not hasObjectives then
block.used = false
self:FreeUnusedLines(block)
return
end
if allObjectivesComplete and addon.Variables.user.showCompleted then
self:AddCompleteGroup(block)
else
self:AddObjectives(block, group.entries)
end
block:SetHeight(block.height)
if self:AddBlock(block) then
self:FreeUnusedLines(block)
block:Show()
else
block.used = false
self:FreeUnusedLines(block)
end
end
function objectiveTrackerModule:AddCompleteGroup(block)
local text = addon.T["ALL_TRACKED_IN_GROUP_COMPLETE"]
local dashStyle = OBJECTIVE_DASH_STYLE_HIDE
local colorStyle = OBJECTIVE_TRACKER_COLOR["Complete"]
local line = self:AddObjective(block, 0, text, LINE_TYPE_ANIM, nil, dashStyle, colorStyle)
line:Show()
line.Check:SetShown(false)
end
function objectiveTrackerModule:AddObjectives(block, entries)
for _, entry in ipairs(entries) do
self:AddObjectiveLine(block, entry)
end
end
function objectiveTrackerModule:AddObjectiveLine(block, entry)
if not entry.goal or entry.goal == 0 then
return
end
local completed = isComplete(entry)
if completed and not addon.Variables.user.showCompleted then
return
end
return self:AddOrUpdateObjectiveLine(block, entry)
end
function objectiveTrackerModule:AddOrUpdateObjectiveLine(block, entry)
local completed = isComplete(entry)
local dashStyle, colorStyle = getObjectiveStyles(entry)
local text = getObjectiveText(entry)
local line = self:AddObjective(block, entry.id, text, LINE_TYPE_ANIM, nil, dashStyle, colorStyle)
line.Check:SetShown(completed)
line.block = block
line.FadeOutAnim:SetScript("OnFinished", function()
module:ObjectiveTracker_Update()
end)
return line
end
function objectiveTrackerModule:AddBlock(block)
local header = block.module.Header
local blockAdded = false
if not header or header.added then
blockAdded = self:InternalAddBlock(block)
elseif ObjectiveTracker_CanFitBlock(block, header) then
if ObjectiveTracker_AddHeader(header) then
blockAdded = self:InternalAddBlock(block)
end
end
if not blockAdded then
block.module.hasSkippedBlocks = true
end
return blockAdded
end
function objectiveTrackerModule:InternalAddBlock(block)
local trackerModule = block.module or DEFAULT_OBJECTIVE_TRACKER_MODULE
local blocksFrame = trackerModule.BlocksFrame
block.nextBlock = nil
if not block.isHeader then
trackerModule.potentialBlocksAddedThisLayout = (trackerModule.potentialBlocksAddedThisLayout or 0) + 1
end
if not block.isHeader and trackerModule:IsCollapsed() then
return false
end
block.offsetY = nil
module:AnchorBlock(block, blocksFrame.currentBlock, not trackerModule.ignoreFit)
if not block.offsetY then
return false
end
if not trackerModule.topBlock then
trackerModule.topBlock = block
end
if not trackerModule.firstBlock and not block.isHeader then
trackerModule.firstBlock = block
end
if blocksFrame.currentBlock then
blocksFrame.currentBlock.nextBlock = block
end
blocksFrame.currentBlock = block
blocksFrame.contentsHeight = blocksFrame.contentsHeight + block.height - block.offsetY
trackerModule.contentsAnimHeight = trackerModule.contentsAnimHeight + block.height
trackerModule.contentsHeight = trackerModule.contentsHeight + block.height - block.offsetY
return true
end
function objectiveTrackerModule:OnBlockHeaderClick(block, mouseButton)
if mouseButton == "LeftButton" then
if IsModifiedClick("QUESTWATCHTOGGLE") then
local shouldTrack = false
local groupId = block.id
addon.trackGroup(nil, groupId, shouldTrack)
else
addon:ShowGroupInFrame(block.id)
end
end
end
function objectiveTrackerModule:FadeOutAllLines(block)
for _, line in pairs(block.lines) do
line.state = "FADING"
line.FadeOutAnim:Play()
end
end
function objectiveTrackerModule:GlowLine(line, onFinishedFunc)
if onFinishedFunc then
line.Glow.Anim:SetScript("OnFinished", onFinishedFunc)
end
line.Glow.Anim:Play()
line.Sheen.Anim:Play()
line.Check:Show()
line.CheckFlash.Anim:Play()
end
function module:AnchorBlock(block, anchorBlock, checkFit)
block:ClearAllPoints()
if anchorBlock then
self:AnchorBlockToReferenceBlock(block, anchorBlock, checkFit)
else
self:AnchorBlockToBlocksFrame(block, checkFit)
end
end
function module:AnchorBlockToReferenceBlock(block, anchorBlock, checkFit)
local trackerModule = block.module
local blocksFrame = trackerModule.BlocksFrame
block.offsetX, block.offsetY = ObjectiveTracker_GetBlockOffset(block)
if anchorBlock.isHeader then
block.offsetY = trackerModule.fromHeaderOffsetY
end
if checkFit and not blockFits(block, blocksFrame, block.offsetY) then
block.offsetY = nil
return
end
if block.isHeader then
block.offsetY = block.offsetY + anchorBlock.module.fromModuleOffsetY
block:SetPoint("LEFT", OBJECTIVE_TRACKER_HEADER_OFFSET_X, 0)
else
block:SetPoint("LEFT", block.offsetX, 0)
end
block:SetPoint("TOP", anchorBlock, "BOTTOM", 0, block.offsetY)
end
function module:AnchorBlockToBlocksFrame(block, checkFit)
local trackerModule = block.module
local blocksFrame = trackerModule.BlocksFrame
local offsetX = ObjectiveTracker_GetBlockOffset(block)
block.offsetY = 0
if checkFit and not blockFits(block, blocksFrame, block.offsetY) then
block.offsetY = nil
return
end
local anchorFrame = blocksFrame.ScrollContents or blocksFrame
if block.isHeader then
block:SetPoint("TOPLEFT", anchorFrame, "TOPLEFT", OBJECTIVE_TRACKER_HEADER_OFFSET_X,block.offsetY)
else
block:SetPoint("TOPLEFT", anchorFrame, "TOPLEFT", offsetX, block.offsetY)
end
end
function module:Init()
module:InitObjectiveTracker(nil, true)
end
function module:FullUpdate()
module:UpdateObjectiveTracker(nil, true)
end
function module:UpdateItem(entry, oldCount)
if oldCount == entry.itemCount then
return
end
if oldCount >= entry.goal and oldCount > entry.itemCount then
return
end
for _, group in ipairs(addon.getGroups()) do
self:UpdateEntryInGroup(entry, group)
end
end
function module:UpdateEntryInGroup(entry, group)
if #group.entries == 0 then
return
end
if group.id ~= entry.parent then
return
end
local block = objectiveTrackerModule:GetExistingBlock(group.id)
if not block then
return
end
local line = objectiveTrackerModule:GetLine(block, entry.id, LINE_TYPE_ANIM)
if line then
local _, previousLine = line:GetPointByName("TOPLEFT")
local originalLastLine = block.currentLine
block.currentLine = previousLine or block.HeaderText
objectiveTrackerModule:AddOrUpdateObjectiveLine(block, entry)
block.currentLine = originalLastLine
if isGroupComplete(group) then
return objectiveTrackerModule:GlowLine(line, function()
objectiveTrackerModule:FadeOutAllLines(block)
end)
end
if isComplete(entry) then
if addon.Variables.user.showCompleted then
return objectiveTrackerModule:GlowLine(line)
else
return objectiveTrackerModule:GlowLine(line, function()
line.FadeOutAnim:Play()
end)
end
end
end
end
function module:InitObjectiveTracker(reason, internalReason)
local tracker = ObjectiveTrackerFrame
if not tracker.initialized then
scheduleFunction(self.UpdateObjectiveTracker, self)
return
end
if module.isUpdating or tracker.isUpdating then
scheduleFunction(self.UpdateObjectiveTracker, self)
return
end
module.isUpdating = true
self:UpdateObjectiveTracker(reason, internalReason)
end
function module:UpdateObjectiveTracker(reason, internalReason)
local tracker = ObjectiveTrackerFrame
local trackerModules, trackerModulesInOrder
if tracker.MODULES then
trackerModules = Shallowcopy(tracker.MODULES)
trackerModulesInOrder = Shallowcopy(tracker.MODULES_UI_ORDER)
else
trackerModules= {}
trackerModulesInOrder = {}
end
table.insert(trackerModules, objectiveTrackerModule)
table.insert(trackerModulesInOrder, objectiveTrackerModule)
tracker.BlocksFrame.maxHeight = tracker.BlocksFrame:GetHeight()
if tracker.BlocksFrame.maxHeight == 0 then
self.isUpdating = false
return
end
tracker.BlocksFrame.currentBlock = nil
tracker.BlocksFrame.contentsHeight = 0
local currentHeaders = module:ObjectiveTracker_GetVisibleHeaders();
for _, trackerModule in ipairs(trackerModules) do
if trackerModule.Header then
trackerModule.Header.added = nil
end
end
local gotMoreRoomThisPass = false
local updateReason = reason or OBJECTIVE_TRACKER_UPDATE_ALL
for _, trackerModule in ipairs(trackerModules) do
local internalOverride = trackerModule == objectiveTrackerModule and (
internalReason or updateReason == OBJECTIVE_TRACKER_UPDATE_ALL)
if internalOverride then
trackerModule:Update()
elseif trackerModule == objectiveTrackerModule then
local wantsMoreRoom = trackerModule.hasSkippedBlocks and gotMoreRoomThisPass
local justShowsUp = trackerModule.Header and trackerModule.Header.animating
if wantsMoreRoom or justShowsUp then
trackerModule:Update()
else
trackerModule:StaticReanchor()
end
else
if trackerModule.oldContentsHeight - trackerModule.contentsHeight >= 1 then
gotMoreRoomThisPass = true
end
trackerModule:StaticReanchor()
end
end
self:ObjectiveTracker_ReorderModules()
self:ObjectiveTracker_AnimateHeaders(currentHeaders)
for _, trackerModule in ipairs(trackerModules) do
ObjectiveTracker_CheckAndHideHeader(trackerModule.Header)
end
if tracker.BlocksFrame.currentBlock then
tracker.HeaderMenu:Show()
else
tracker.HeaderMenu:Hide()
end
tracker.BlocksFrame.currentBlock = nil
self.isUpdating = false
if tracker:IsInDefaultPosition() then
UIParent_ManageFramePositions()
end
end
function module:GetRelatedModulesForUpdate(trackerModule)
if trackerModule then
return tInvert(trackerModule:GetRelatedModules())
end
return nil
end
function module:IsRelatedModuleForUpdate(trackerModule, moduleLookup)
if moduleLookup then
return moduleLookup[trackerModule] ~= nil
end
return false
end
function module:ObjectiveTracker_Initialize()
self:InitObjectiveTracker()
end
function module:ObjectiveTracker_Update(reason)
self:InitObjectiveTracker(reason)
end
function module:ObjectiveTracker_ReorderModules()
local trackerModulesInOrder = Shallowcopy(ObjectiveTrackerFrame.MODULES_UI_ORDER)
table.insert(trackerModulesInOrder, objectiveTrackerModule)
local visibleCount = self:ObjectiveTracker_CountVisibleModules()
local showMinimizeButtons = visibleCount > 1
local headerMenu = ObjectiveTrackerFrame.HeaderMenu
headerMenu:ClearAllPoints()
self.lastAnchorBlock = nil
self.headerMenuPositioned = false
for _, trackerModule in ipairs(trackerModulesInOrder) do
local block = trackerModule.topBlock
if block then
self:PositionBlock(trackerModule, block, showMinimizeButtons)
end
end
end
function module:ObjectiveTracker_CountVisibleModules()
local trackerModules = Shallowcopy(ObjectiveTrackerFrame.MODULES)
table.insert(trackerModules, objectiveTrackerModule)
local count = 0
local seen = {}
for _, trackerModule in ipairs(trackerModules) do
local header = trackerModule.Header
if header and not seen[header] then
seen[header] = true
if header:IsVisible() and trackerModule:GetBlockCount() > 0 then
count = count + 1
end
end
end
return count
end
function module:PositionBlock(trackerModule, block, showMinimizeButtons)
if trackerModule:UsesSharedHeader() then
self:AnchorBlock(block, trackerModule.Header)
local containingModule = trackerModule.Header.module
if containingModule and containingModule.firstBlock then
containingModule.firstBlock:ClearAllPoints()
self:AnchorBlock(containingModule.firstBlock, trackerModule.lastBlock)
end
else
self:AnchorBlock(block, self.lastAnchorBlock)
self.lastAnchorBlock = trackerModule.lastBlock
end
local headerPoint = ObjectiveTrackerFrame.isOnLeftSideOfScreen and "LEFT" or "RIGHT"
local offsetXHeader = ObjectiveTrackerFrame.isOnLeftSideOfScreen and -10 or 0
local offsetXHeaderText = ObjectiveTrackerFrame.isOnLeftSideOfScreen and 30 or 4
local offsetXButton = ObjectiveTrackerFrame.isOnLeftSideOfScreen and 9 or -20
local headerMenu = ObjectiveTrackerFrame.HeaderMenu
if not self.headerMenuPositioned and headerMenu then
headerMenu:ClearAllPoints()
headerMenu:SetPoint(headerPoint, trackerModule.Header, headerPoint, offsetXHeader, 0)
self.headerMenuPositioned = true
end
trackerModule.Header.Text:ClearAllPoints()
trackerModule.Header.Text:SetPoint("LEFT", trackerModule.Header, "LEFT", offsetXHeaderText, -1)
local shouldShowMinimizeButton = showMinimizeButtons or trackerModule:IsCollapsed()
trackerModule.Header.MinimizeButton:SetShown(shouldShowMinimizeButton)
if shouldShowMinimizeButton then
trackerModule.Header.MinimizeButton:ClearAllPoints()
trackerModule.Header.MinimizeButton:SetPoint(headerPoint, trackerModule.Header, headerPoint, offsetXButton, 0)
end
end
function module:ObjectiveTracker_GetVisibleHeaders()
local headers = {}
local trackerModules = Shallowcopy(ObjectiveTrackerFrame.MODULES)
table.insert(trackerModules, objectiveTrackerModule)
for _, trackerModule in ipairs(trackerModules) do
local header = trackerModule.Header
if header.added and header:IsVisible() then
headers[header] = true
end
end
return headers
end
function module:ObjectiveTracker_AnimateHeaders(previouslyVisibleHeaders)
local currentHeaders = module:ObjectiveTracker_GetVisibleHeaders()
for header, isVisible in pairs(currentHeaders) do
if isVisible and not previouslyVisibleHeaders[header] then
header:PlayAddAnimation()
end
end
end
function module:ObjectiveTracker_SetModulesCollapsed(collapsed, modules)
for _, trackerModule in ipairs(modules) do
trackerModule.collapsed = collapsed
end
end
hooksecurefunc(_G, "ObjectiveTracker_Initialize", function()
module:ObjectiveTracker_Initialize()
end)
hooksecurefunc(_G, "ObjectiveTracker_Update", function(reason)
module:ObjectiveTracker_Update(reason)
end)