-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAddon.lua
More file actions
389 lines (336 loc) · 11.2 KB
/
Addon.lua
File metadata and controls
389 lines (336 loc) · 11.2 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
-- Addon.lua
-- @Author : Dencer (tdaddon@163.com)
-- @Link : https://dengsir.github.io
-- @Date : 5/18/2020, 11:25:23 AM
--
---@type string, ns
local ADDON, NS = ...
---@class ns
---@field Inspect Inspect
---@field Talent Talent
---@field Glyph Glyph
---@field ItemLevelCalculator ItemLevelCalculator
---@field Events Events
---@field SpecGear SpecGear
local ns = NS
local ShowUIPanel = LibStub('LibShowUIPanel-1.0').ShowUIPanel
local HideUIPanel = LibStub('LibShowUIPanel-1.0').HideUIPanel
---@class UI
---@field BaseItem UI.BaseItem
---@field GearItem UI.GearItem
---@field GearFrame UI.GearFrame
---@field GemItem UI.GemItem
---@field EnchantItem UI.EnchantItem
---@field InspectFrame UI.InspectFrame
---@field InspectGearFrame UI.InspectGearFrame
---@field GlyphItem UI.GlyphItem
---@field TalentFrame UI.TalentFrame
---@field CharacterGearFrame UI.CharacterGearFrame
---@field GlyphFrame UI.GlyphFrame
---@field PaperDoll UI.PaperDoll
---@field InspectTalent UI.InspectTalent
---@field ModelFrame UI.ModelFrame
---@field SlotItem UI.SlotItem
---@field MenuButton UI.MenuButton
ns.UI = {}
ns.L = LibStub('AceLocale-3.0'):GetLocale(ADDON)
ns.VERSION = tonumber((C_AddOns.GetAddOnMetadata(ADDON, 'Version'):gsub('(%d+)%.?', function(x)
return format('%02d', tonumber(x))
end))) or 0
_G.BINDING_NAME_TDINSPECT_VIEW_TARGET = ns.L['Inspect target']
_G.BINDING_NAME_TDINSPECT_VIEW_MOUSEOVER = ns.L['Inspect mouseover']
_G.BINDING_CATEGORY_TDINSPECT = C_AddOns.GetAddOnMetadata(ADDON, 'Title')
---@class Addon: AceAddon, LibClass-2.0, EventHandler
local Addon = LibStub('AceAddon-3.0'):NewAddon(ADDON, 'LibClass-2.0')
ns.Addon = Addon
function Addon:OnInitialize()
ns.Events:Embed(Addon)
self:SetupDatabase()
self:SetupCharacterProfile()
self:SetupAnyAccount()
self:SetupGearParent()
self:SetupOptionFrame()
end
function Addon:OnEnable()
self:Event('ADDON_LOADED')
self:Event('TDINSPECT_READY')
self:Event('TDINSPECT_TALENT_READY', 'TDINSPECT_READY')
self:Event('TDINSPECT_OPTION_CHANGED')
end
function Addon:OnModuleCreated(module)
ns[module:GetName()] = module
end
function Addon:OnClassCreated(class, name)
local uiName = name:match('^UI%.(.+)$')
if uiName then
ns.UI[uiName] = class
ns.Events:Embed(class)
else
ns[name] = class
end
end
function Addon:SetupDatabase()
---@class tdInspectProfile: table
local profile = { --
global = { --
characters = {},
userCache = {},
},
profile = { --
characterGear = true,
inspectGear = true,
inspectCompare = true,
showTalentBackground = true,
closeCharacterFrameWhenInspect = true,
showOptionButtonInCharacter = true,
showOptionButtonInInspect = true,
showGem = true,
showEnchant = true,
showLost = true,
showGemsFront = false,
showRangedEnchantOnlyHunter = true,
showLowLevelCharacters = false,
itemLevelColor = 'Light',
},
}
---@type tdInspectProfile | AceDB.Schema
ns.db = LibStub('AceDB-3.0'):New('TDDB_INSPECT2', profile, true)
if not ns.db.global.version or ns.db.global.version < 20000 then
wipe(ns.db.global.userCache)
end
for k, v in pairs(ns.db.global.userCache) do
if not v.class then
ns.db.global.userCache[k] = nil
end
end
ns.db.global.version = ns.VERSION
end
function Addon:SetupCharacterProfile()
---@class CharacterProfile: table
local characterProfile = {gears = {}, equipmentSets = {}, aliases = {}}
local name = ns.UnitName('player')
local char = ns.db.global.characters[name]
if type(char) ~= 'table' then
char = nil
end
ns.db.global.characters[name] = ns.CopyDefaults(char, characterProfile)
---@type CharacterProfile
ns.char = ns.db.global.characters[name]
end
function Addon:DeleteCharacter(name)
ns.db.global.characters[name] = nil
self.characters = nil
end
function Addon:SetupAnyAccount()
if not _G.TDDB_INSPECT_ANYACCOUNT then
return
end
ns.hasAnyAccount = true
ns.otherCharacters = {}
for _, v in pairs(_G.TDDB_INSPECT_ANYACCOUNT) do
if v.global then
if v.global.userCache then
for name, p in pairs(v.global.userCache) do
if not ns.db.global.userCache[name] or p.timestamp > ns.db.global.userCache[name].timestamp then
ns.db.global.userCache[name] = p
end
end
end
if v.global.characters then
for name, p in pairs(v.global.characters) do
if not ns.db.global.characters[name] then
ns.otherCharacters[name] = p
end
end
end
end
end
_G.TDDB_INSPECT_ANYACCOUNT = nil
end
function Addon:SetupGearParent()
self.CharacterGearParent = CreateFrame('Frame', nil, PaperDollFrame)
if PaperDollFrame.Inset then
self.CharacterGearParent:SetPoint('TOPLEFT', PaperDollFrame, 'TOPRIGHT', 0, 0)
else
self.CharacterGearParent:SetPoint('TOPLEFT', PaperDollFrame, 'TOPRIGHT', -33, -12)
end
self.CharacterGearParent:SetSize(1, 1)
self.CharacterGearParent:SetScript('OnShow', function()
self:OpenCharacterGearFrame()
end)
end
function Addon:SetupUI()
-- blizzard ui bug
if InspectPVPFrame then
InspectPVPFrame:SetPoint('TOPLEFT', -16, 12)
for _, region in ipairs({InspectPVPFrame:GetRegions()}) do
if region:GetObjectType() == 'Texture' and region:GetDrawLayer() == 'BACKGROUND' then
region:Hide()
end
end
end
self.InspectFrame = ns.UI.InspectFrame:Bind(InspectFrame)
end
function Addon:ADDON_LOADED(_, addon)
if addon ~= 'Blizzard_InspectUI' then
return
end
self:SetupUI()
self:UnEvent('ADDON_LOADED')
end
function Addon:TDINSPECT_READY(_, unit, name)
if not InspectFrame then
return
end
if unit == ns.Inspect.unit or name == ns.Inspect.unitName then
ShowUIPanel(self.InspectFrame)
if ns.db.profile.closeCharacterFrameWhenInspect and not InCombatLockdown() then
HideUIPanel(CharacterFrame)
end
end
end
function Addon:TDINSPECT_OPTION_CHANGED(_, key, value)
if key == 'characterGear' then
if value then
if self.CharacterGearParent:IsShown() then
self:OpenCharacterGearFrame()
end
elseif self.CharacterGearFrame then
if not ns.db.profile.inspectCompare or not self.InspectGearFrame or not self.InspectGearFrame:IsShown() then
self.CharacterGearFrame:Hide()
end
end
elseif key == 'inspectGear' and InspectPaperDollFrame then
if value then
if InspectPaperDollFrame:IsShown() then
self:OpenInspectGearFrame()
elseif self.InspectGearFrame then
self.InspectGearFrame:Hide()
end
elseif self.InspectGearFrame then
self.InspectGearFrame:Hide()
end
elseif key == 'inspectCompare' and InspectPaperDollFrame then
if value then
if InspectPaperDollFrame:IsVisible() then
self:OpenInspectGearFrame()
end
elseif self.CharacterGearFrame then
self.CharacterGearFrame:Hide()
if self.CharacterGearParent:IsShown() then
self:OpenCharacterGearFrame()
end
end
end
end
---@return UI.CharacterGearFrame
function Addon:GetCharacterGearFrame()
if not self.CharacterGearFrame then
self.CharacterGearFrame = ns.UI.CharacterGearFrame:Create(self.CharacterGearParent)
end
return self.CharacterGearFrame
end
function Addon:GetInspectGearFrame()
if not self.InspectGearFrame then
self.InspectGearFrame = ns.UI.InspectGearFrame:Create(InspectPaperDollFrame, true)
self.InspectGearFrame:SetPoint('TOPLEFT', InspectPaperDollFrame, 'TOPRIGHT', 0, 0)
end
return self.InspectGearFrame
end
function Addon:OpenCharacterGearFrame()
if ns.db.profile.characterGear then
local characterGearFrame = self:GetCharacterGearFrame()
if characterGearFrame:IsShown() then
return
end
characterGearFrame:TapTo(self.CharacterGearParent, 'TOPLEFT')
characterGearFrame:Show()
end
end
function Addon:OpenInspectGearFrame()
if ns.db.profile.inspectGear then
local inspectGearFrame = self:GetInspectGearFrame()
inspectGearFrame:Show()
if ns.db.profile.inspectCompare then
local characterGearFrame = self:GetCharacterGearFrame()
characterGearFrame:TapTo(inspectGearFrame, 'TOPRIGHT')
characterGearFrame:Show()
end
end
end
function Addon:GetCharacters()
if not self.characters then
self.characters = {}
local function TouchCharacter(name, otherAccount)
local db = ns.db.global.userCache[name]
if not db or not db.class then
return
end
local class = select(2, GetClassInfo(db.class))
local color = select(4, GetClassColor(class))
local coloredName = format('|c%s%s%s|r', color, Ambiguate(name, 'none'), otherAccount and '*' or '')
local sameRealm = ns.IsPlayerInOurRealm(name)
local low = db.level < ns.MAX_LEVEL
tinsert(self.characters, {
name = name,
coloredName = coloredName,
class = db.class,
level = db.level,
low = low,
sameRealm = sameRealm,
otherAccount = otherAccount,
})
end
local function TouchCharacters(characters, otherAccount)
if not characters then
return
end
for k in pairs(characters) do
TouchCharacter(k, otherAccount)
end
end
TouchCharacters(ns.db.global.characters)
TouchCharacters(ns.otherCharacters, true)
sort(self.characters, function(a, b)
if a.level ~= b.level then
return a.level > b.level
end
if a.sameRealm ~= b.sameRealm then
return a.sameRealm
end
if a.otherAccount ~= b.otherAccount then
return not a.otherAccount
end
if a.class ~= b.class then
return a.class < b.class
end
return a.level > b.level
end)
end
return self.characters
end
function Addon:InspectTarget()
InspectUnit('target')
end
function Addon:InspectMouseover()
local unit = select(2, GameTooltip:GetUnit())
if unit and not UnitExists(unit) then
unit = 'mouseover'
end
if not UnitExists(unit) then
unit = 'target'
end
if unit == 'mouseover' then
local guid = UnitGUID(unit)
if guid then
unit = UnitTokenFromGUID(guid) or unit
end
end
if not unit then
return
end
InspectUnit(unit)
end
if not InspectTalentFrameSpentPoints then
InspectTalentFrameSpentPoints = {SetPoint = nop}
end