-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMobList.lua
476 lines (421 loc) · 16.6 KB
/
MobList.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
--[[
* Copyright (c) 2011-2020 by Adam Hellberg.
*
* This file is part of KillTrack.
*
* KillTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KillTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KillTrack. If not, see <http://www.gnu.org/licenses/>.
--]]
-- Beware of some possibly messy code in this file
---@class KillTrack
local KT = select(2, ...)
---@class KillTrackMobList
local ML = {}
KT.MobList = ML
local KTT = KT.Tools
local Sort = KT.Sort.Desc
---@type { Id: integer, Name: string, gKills: integer, cKills: integer }[]
local Mobs = nil
local LastFilter = nil
local LastOffset = 0
-- Frame Constants
local FRAME_WIDTH = 600
local FRAME_HEIGHT = 534
local HEADER_HEIGHT = 24
local HEADER_LEFT = 3
local HEADER_TOP = -80
local ROW_HEIGHT = 15
local ROW_COUNT = 27
local ROW_TEXT_PADDING = 5
local ID_WIDTH = 100
local NAME_WIDTH = 300
local CHAR_WIDTH = 100
local GLOBAL_WIDTH = 100
local SCROLL_WIDTH = 27 -- Scrollbar width
local STATUS_TEXT = "Showing entries %d through %d out of %d total (%d hidden)"
---@type table|BackdropTemplate|Frame
local frame = nil
local created = false
-- Frame helper functions
---@param parent table|Frame
---@return Button
local function CreateHeader(parent)
local h = CreateFrame("Button", nil, parent)
h:SetHeight(HEADER_HEIGHT)
h:SetNormalFontObject("GameFontHighlightSmall")
local bgl = h:CreateTexture(nil, "BACKGROUND")
bgl:SetTexture("Interface\\FriendsFrame\\WhoFrame-ColumnTabs")
bgl:SetWidth(5)
bgl:SetHeight(HEADER_HEIGHT)
bgl:SetPoint("TOPLEFT")
bgl:SetTexCoord(0, 0.07815, 0, 0.75)
local bgr = h:CreateTexture(nil, "BACKGROUND")
bgr:SetTexture("Interface\\FriendsFrame\\WhoFrame-ColumnTabs")
bgr:SetWidth(5)
bgr:SetHeight(HEADER_HEIGHT)
bgr:SetPoint("TOPRIGHT")
bgr:SetTexCoord(0.90625, 0.96875, 0, 0.75)
local bgm = h:CreateTexture(nil, "BACKGROUND")
bgm:SetTexture("Interface\\FriendsFrame\\WhoFrame-ColumnTabs")
bgm:SetHeight(HEADER_HEIGHT)
bgm:SetPoint("LEFT", bgl, "RIGHT")
bgm:SetPoint("RIGHT", bgr, "LEFT")
bgm:SetTexCoord(0.07815, 0.90625, 0, 0.75)
local hl = h:CreateTexture()
h:SetHighlightTexture("Interface\\PaperDollInfoFrame\\UI-Character-Tab-Highlight", "ADD")
hl:SetPoint("TOPLEFT", bgl, "TOPLEFT", -2, 5)
hl:SetPoint("BOTTOMRIGHT", bgr, "BOTTOMRIGHT", 2, -7)
return h
end
---@param container table|Frame
---@param previous table|Frame
---@return Button
local function CreateRow(container, previous)
local row = CreateFrame("Button", nil, container)
row:SetHighlightTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight", "ADD")
row:SetHeight(ROW_HEIGHT)
row:SetPoint("LEFT")
row:SetPoint("RIGHT")
row:SetPoint("TOPLEFT", previous, "BOTTOMLEFT", 0, 0)
---@diagnostic disable-next-line: inject-field
row.idField = row:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
row.idField:SetHeight(ROW_HEIGHT)
row.idField:SetWidth(ID_WIDTH - ROW_TEXT_PADDING * 2)
row.idField:SetPoint("LEFT", row, "LEFT", ROW_TEXT_PADDING, 0)
row.idField:SetJustifyH("RIGHT")
---@diagnostic disable-next-line: inject-field
row.nameField = row:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
row.nameField:SetHeight(ROW_HEIGHT)
row.nameField:SetWidth(NAME_WIDTH - SCROLL_WIDTH - ROW_TEXT_PADDING * 3)
row.nameField:SetPoint("LEFT", row.idField, "RIGHT", 2 * ROW_TEXT_PADDING, 0)
row.nameField:SetJustifyH("LEFT")
---@diagnostic disable-next-line: inject-field
row.charKillField = row:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
row.charKillField:SetHeight(ROW_HEIGHT)
row.charKillField:SetWidth(CHAR_WIDTH - ROW_TEXT_PADDING * 2)
row.charKillField:SetPoint("LEFT", row.nameField, "RIGHT", 2 * ROW_TEXT_PADDING, 0)
row.charKillField:SetJustifyH("RIGHT")
---@diagnostic disable-next-line: inject-field
row.globalKillField = row:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
row.globalKillField:SetHeight(ROW_HEIGHT)
row.globalKillField:SetWidth(GLOBAL_WIDTH - ROW_TEXT_PADDING * 2)
row.globalKillField:SetPoint("LEFT", row.charKillField, "RIGHT", 2 * ROW_TEXT_PADDING, 0)
row.globalKillField:SetJustifyH("RIGHT")
row:SetScript("OnClick", function(s)
local id = tonumber(s.idField:GetText())
if not id then return end
local name = s.nameField:GetText()
KT:ShowDelete(id, name)
end)
row:SetScript("OnEnter", function(s)
local id = tonumber(s.idField:GetText())
if not id then return end
local globalData = KT.Global.MOBS[id]
if not globalData then return end
local killTimestamp = globalData.LastKillAt
if not killTimestamp then return end
local lastKillAt = KTT:FormatDateTime(killTimestamp)
local tpString = ("Last killed at %s"):format(lastKillAt)
GameTooltip:SetOwner(s, "ANCHOR_NONE")
GameTooltip:SetPoint("TOPLEFT", s, "BOTTOMLEFT")
GameTooltip:ClearLines()
GameTooltip:AddLine(tpString)
GameTooltip:Show()
end)
row:SetScript("OnLeave", function()
GameTooltip:Hide()
end)
return row
end
function ML:Show()
if not created then
ML:Create()
end
if frame:IsShown() then return end
frame:Show()
end
function ML:Hide()
if not frame or not frame:IsShown() then return end
frame:Hide()
end
function ML:Toggle()
if frame and frame:IsShown() then
ML:Hide()
else
ML:Show()
end
end
function ML:Create()
if frame then return end
frame = CreateFrame("Frame", nil, UIParent, BackdropTemplateMixin and "BackdropTemplate")
frame:Hide()
frame:SetToplevel(true)
frame:EnableMouse(true)
frame:SetMovable(true)
frame:SetPoint("CENTER")
frame:SetWidth(FRAME_WIDTH)
frame:SetHeight(FRAME_HEIGHT)
local bd = {
bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true,
edgeSize = 16,
tileSize = 32,
insets = {
left = 2.5,
right = 2.5,
top = 2.5,
bottom = 2.5
}
}
frame:SetBackdrop(bd)
---@diagnostic disable-next-line: inject-field
frame.titleLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
frame.titleLabel:SetWidth(250)
frame.titleLabel:SetHeight(16)
frame.titleLabel:SetPoint("TOP", frame, "TOP", 0, -5)
frame.titleLabel:SetText("KillTrack Mob Database (" .. KT.Version .. ")")
frame:SetScript("OnMouseDown", function(s) s:StartMoving() end)
frame:SetScript("OnMouseUp", function(s) s:StopMovingOrSizing() end)
frame:SetScript("OnShow", function() ML:UpdateMobs(Sort, LastFilter) ML:UpdateEntries(LastOffset) end)
---@diagnostic disable-next-line: inject-field
frame.closeButton = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
frame.closeButton:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -1, -1)
frame.closeButton:SetScript("OnClick", function() ML:Hide() end)
---@diagnostic disable-next-line: inject-field
frame.purgeButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
frame.purgeButton:SetHeight(24)
frame.purgeButton:SetWidth(100)
frame.purgeButton:SetPoint("TOPLEFT", frame, "TOPLEFT", 10, -5)
frame.purgeButton:SetText("Purge Data")
frame.purgeButton:SetScript("OnClick", function()
KT:ShowPurge()
end)
---@diagnostic disable-next-line: inject-field
frame.resetButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
frame.resetButton:SetHeight(24)
frame.resetButton:SetWidth(100)
frame.resetButton:SetPoint("TOPLEFT", frame.purgeButton, "BOTTOMLEFT", 0, -3)
frame.resetButton:SetText("Reset All")
frame.resetButton:SetScript("OnClick", function()
KT:ShowReset()
end)
---@diagnostic disable-next-line: inject-field
frame.helpLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
frame.helpLabel:SetWidth(380)
frame.helpLabel:SetHeight(16)
frame.helpLabel:SetPoint("TOP", frame, "TOP", 0, -28)
frame.helpLabel:SetWordWrap(true)
frame.helpLabel:SetMaxLines(2)
frame.helpLabel:SetText(
"Click on an individual entry to delete it from the database. Use the search to filter database by name.")
---@diagnostic disable-next-line: inject-field
frame.searchBox = CreateFrame("EditBox", "KillTrackMobListSearchBox", frame, "SearchBoxTemplate")
frame.searchBox:SetWidth(200)
frame.searchBox:SetHeight(16)
frame.searchBox:SetPoint("TOPLEFT", frame.resetButton, "BOTTOMLEFT", 8, -3)
frame.searchBox:HookScript("OnTextChanged", function(s)
local text = s:GetText()
if (not _G[s:GetName() .. "ClearButton"]:IsShown()) then
text = nil
LastFilter = nil
end
if not text or text == "" then
ML:UpdateMobs(Sort)
else
ML:UpdateMobs(Sort, text)
end
ML:UpdateEntries(LastOffset)
end)
frame.searchBox:SetScript("OnEnterPressed", function(s) s:ClearFocus() end)
---@diagnostic disable-next-line: inject-field
frame.searchBox.clearButton = _G[frame.searchBox:GetName() .. "ClearButton"]
local sBoxOldFunc = frame.searchBox.clearButton:GetScript("OnHide")
frame.searchBox.clearButton:SetScript("OnHide", function(s)
if sBoxOldFunc then sBoxOldFunc(s) end
if not frame:IsShown() then return end
ML:UpdateMobs(Sort, nil)
ML:UpdateEntries(LastOffset)
end)
---@diagnostic disable-next-line: inject-field
frame.searchTipLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
frame.searchTipLabel:SetWidth(200)
frame.searchTipLabel:SetHeight(16)
frame.searchTipLabel:SetPoint("LEFT", frame.searchBox, "RIGHT", -22, 0)
frame.searchTipLabel:SetText("(Supports Lua patterns)")
---@diagnostic disable-next-line: inject-field
frame.idHeader = CreateHeader(frame)
frame.idHeader:SetPoint("TOPLEFT", frame, "TOPLEFT", HEADER_LEFT, HEADER_TOP)
frame.idHeader:SetWidth(ID_WIDTH)
frame.idHeader:SetText("NPC ID")
frame.idHeader:SetScript("OnClick", function()
local sort = KT.Sort.IdAsc
if Sort == sort then
sort = KT.Sort.IdDesc
end
ML:UpdateMobs(sort, LastFilter)
ML:UpdateEntries(LastOffset)
end)
---@diagnostic disable-next-line: inject-field
frame.nameHeader = CreateHeader(frame)
frame.nameHeader:SetPoint("TOPLEFT", frame.idHeader, "TOPRIGHT", -2, 0)
frame.nameHeader:SetWidth(NAME_WIDTH - SCROLL_WIDTH)
frame.nameHeader:SetText("Name")
frame.nameHeader:SetScript("OnClick", function()
local sort = KT.Sort.AlphaA
if Sort == sort then
sort = KT.Sort.AlphaD
end
ML:UpdateMobs(sort, LastFilter)
ML:UpdateEntries(LastOffset)
end)
---@diagnostic disable-next-line: inject-field
frame.charKillHeader = CreateHeader(frame)
frame.charKillHeader:SetPoint("TOPLEFT", frame.nameHeader, "TOPRIGHT", -2, 0)
frame.charKillHeader:SetWidth(CHAR_WIDTH)
frame.charKillHeader:SetText("Character Kills")
frame.charKillHeader:SetScript("OnClick", function()
local sort = KT.Sort.CharDesc
if Sort == sort then
sort = KT.Sort.CharAsc
end
ML:UpdateMobs(sort, LastFilter)
ML:UpdateEntries(LastOffset)
end)
---@diagnostic disable-next-line: inject-field
frame.globalKillHeader = CreateHeader(frame)
frame.globalKillHeader:SetPoint("TOPLEFT", frame.charKillHeader, "TOPRIGHT", -2, 0)
frame.globalKillHeader:SetWidth(GLOBAL_WIDTH + HEADER_LEFT)
frame.globalKillHeader:SetText("Global Kills")
frame.globalKillHeader:SetScript("OnClick", function()
local sort = KT.Sort.Desc
if Sort == sort then
sort = KT.Sort.Asc
end
ML:UpdateMobs(sort, LastFilter)
ML:UpdateEntries(LastOffset)
end)
---@diagnostic disable-next-line: inject-field
frame.rows = CreateFrame("Frame", nil, frame)
frame.rows:SetPoint("LEFT")
frame.rows:SetPoint("RIGHT", frame, "RIGHT", -SCROLL_WIDTH, 0)
frame.rows:SetPoint("TOP", frame.idHeader, "BOTTOM", 0, 0)
frame.rows:SetPoint("BOTTOM", frame, "BOTTOM", 0, 0)
frame.rows:SetPoint("TOPLEFT", frame.idHeader, "BOTTOMLEFT", 0, 30)
local previous = frame.idHeader
for i = 1, ROW_COUNT do
local key = "row" .. i
frame.rows[key] = CreateRow(frame.rows, previous)
frame.rows[key].idField:SetText("")
frame.rows[key].nameField:SetText("")
frame.rows[key].charKillField:SetText("")
frame.rows[key].globalKillField:SetText("")
previous = frame.rows[key]
end
---@diagnostic disable-next-line: inject-field
frame.rows.scroller = CreateFrame(
"ScrollFrame",
"KillTrackMobListScrollFrame",
frame.rows,
"FauxScrollFrameTemplateLight")
frame.rows.scroller.name = frame.rows.scroller:GetName()
frame.rows.scroller:SetWidth(frame.rows:GetWidth())
frame.rows.scroller:SetPoint("TOPRIGHT", frame.rows, "TOPRIGHT", -1, -2)
frame.rows.scroller:SetPoint("BOTTOMRIGHT", 0, 4)
frame.rows.scroller:SetScript(
"OnVerticalScroll",
function(s, val)
FauxScrollFrame_OnVerticalScroll(
s, val, ROW_HEIGHT,
function()
local offset = FauxScrollFrame_GetOffset(frame.rows.scroller)
ML:UpdateEntries(offset)
end
)
end
)
self:UpdateMobs(Sort)
---@diagnostic disable-next-line: inject-field
frame.statusLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
frame.statusLabel:SetWidth(420)
frame.statusLabel:SetHeight(16)
frame.statusLabel:SetPoint("BOTTOM", frame, "BOTTOM", 0, 8)
frame.statusLabel:SetText(STATUS_TEXT:format(1, ROW_COUNT, #Mobs))
self:UpdateEntries(LastOffset)
created = true
end
---@param sort KillTrackMobSortMode?
---@param filter string?
function ML:UpdateMobs(sort, filter)
sort = (sort or Sort) or KT.Sort.Desc
Sort = sort
LastFilter = filter
Mobs = KT:GetSortedMobTable(Sort, filter and filter:lower() or nil)
FauxScrollFrame_Update(frame.rows.scroller, #Mobs, ROW_COUNT, ROW_HEIGHT)
end
---@param offset integer?
function ML:UpdateEntries(offset)
if (#Mobs <= 0) then
for i = 1, ROW_COUNT do
local row = frame.rows["row" .. i]
row.idField:SetText("")
if i == 1 then
row.nameField:SetText("No entries in database or none matched search!")
else
row.nameField:SetText("")
end
row.charKillField:SetText("")
row.globalKillField:SetText("")
row:Disable()
end
frame.statusLabel:SetText(STATUS_TEXT:format(0, 0, 0))
return
elseif #Mobs < ROW_COUNT then
for i = 1, ROW_COUNT do
local row = frame.rows["row" .. i]
row.idField:SetText("")
row.nameField:SetText("")
row.charKillField:SetText("")
row.globalKillField:SetText("")
row:Disable()
end
end
offset = (tonumber(offset) or LastOffset) or 0
LastOffset = offset
local limit = ROW_COUNT
if limit > #Mobs then
limit = #Mobs
end
for i = 1, limit do
local row = frame.rows["row" .. i]
local mob = Mobs[i + offset]
row.idField:SetText(mob.Id)
row.nameField:SetText(mob.Name)
row.charKillField:SetText(mob.cKills)
row.globalKillField:SetText(mob.gKills)
row:Enable()
end
local mobCount = KTT:TableLength(KT.Global.MOBS)
local hidden = mobCount - #Mobs
frame.statusLabel:SetText(STATUS_TEXT:format(1 + offset, math.min(#Mobs, offset + ROW_COUNT), #Mobs, hidden))
if offset == 0 then
_G[frame.rows.scroller.name .. "ScrollBarScrollUpButton"]:Disable()
else
_G[frame.rows.scroller.name .. "ScrollBarScrollUpButton"]:Enable()
end
if offset + ROW_COUNT == #Mobs then
_G[frame.rows.scroller.name .. "ScrollBarScrollDownButton"]:Disable()
else
_G[frame.rows.scroller.name .. "ScrollBarScrollDownButton"]:Enable()
end
end