-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcore.lua
350 lines (270 loc) · 9.26 KB
/
core.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
------------------------------------------------------------
-- Configuration variables have moved to the in-game menu --
-- Do not change anything in this file --
------------------------------------------------------------
local addon, TNI = ...
local LNR = LibStub("LibNameplateRegistry-1.0")
LibStub("AceAddon-3.0"):NewAddon(TNI, addon, "AceConsole-3.0")
--@alpha@
local DEBUG = false
local function debugprint(...)
if DEBUG then
print("TNI DEBUG:", ...)
end
end
_G.TNI = TNI
--@end-alpha@
-----
-- Error callbacks
-----
local print, format = print, string.format
local function errorPrint(fatal, formatString, ...)
local message = "|cffFF0000LibNameplateRegistry has encountered a" .. (fatal and " fatal" or "n") .. " error:|r"
print("TargetNameplateIndicator:", message, format(formatString, ...))
end
function TNI:OnError_FatalIncompatibility(callback, incompatibilityType)
local detailedMessage
if incompatibilityType == "TRACKING: OnHide" or incompatibilityType == "TRACKING: OnShow" then
detailedMessage = "LibNameplateRegistry missed several nameplate show and hide events."
elseif incompatibilityType == "TRACKING: OnShow missed" then
detailedMessage = "A nameplate was hidden but never shown."
else
detailedMessage = "Something has gone terribly wrong!"
end
errorPrint(true, "(Error Code: %s) %s", incompatibilityType, detailedMessage)
end
------
-- Initialisation
------
local defaults
do
local function CreateUnitReactionTypeDefaults()
return {
enable = true,
texture = "Interface\\AddOns\\TargetNameplateIndicator\\Textures\\Reticule",
height = 50,
width = 50,
frameStrata = "BACKGROUND",
opacity = 1,
texturePoint = "BOTTOM",
anchorPoint = "TOP",
xOffset = 0,
yOffset = 5,
}
end
local function CreateUnitDefaults()
return {
enable = true,
self = CreateUnitReactionTypeDefaults(),
friendly = CreateUnitReactionTypeDefaults(),
hostile = CreateUnitReactionTypeDefaults(),
}
end
defaults = {
profile = {
target = CreateUnitDefaults(),
mouseover = CreateUnitDefaults(),
focus = CreateUnitDefaults(),
targettarget = CreateUnitDefaults(),
}
}
end
function TNI:OnInitialize()
LNR:Embed(self)
self.db = LibStub("AceDB-3.0"):New("TargetNameplateIndicatorDB", defaults, true)
self:RegisterOptions() -- Defined in options.lua
self:LNR_RegisterCallback("LNR_ERROR_FATAL_INCOMPATIBILITY", "OnError_FatalIncompatibility")
--@alpha@
if DEBUG then
TNI:LNR_RegisterCallback("LNR_DEBUG", debugprint)
end
--@end-alpha@
end
function TNI:OnEnable()
for unit, indicator in pairs(self.Indicators) do
indicator:Show()
end
end
function TNI:OnDisable()
for unit, indicator in pairs(self.Indicators) do
indicator:Hide()
end
end
function TNI:RefreshIndicator(unit)
local indicator = self.Indicators[unit]
if not indicator then
error("Invalid unit \"" + unit + "\"")
end
indicator:Refresh()
end
------
-- Indicator functions
------
--- @type table<string, Indicator>
TNI.Indicators = {}
--- @class Indicator : Frame
--- @field Texture Texture
--- @field enabled boolean
--- @field unit string
--- @field priority number
--- @field LNR_RegisterCallback fun(self: Indicator, eventName: string, callbackName: string)
--- @field GetPlateByGUID fun(string) : Frame, table
local Indicator = {}
function Indicator:Update(nameplate)
self.currentNameplate = nameplate
self.Texture:ClearAllPoints()
local unitConfig = TNI.db.profile[self.unit]
local config = UnitIsUnit("player", self.unit) and unitConfig.self or
UnitIsFriend("player", self.unit) and unitConfig.friendly or unitConfig.hostile
self:SetShown(unitConfig.enable)
self.enabled = unitConfig.enable;
if nameplate and config.enable then
local texture = config.texture
if texture == "custom" then
texture = config.textureCustom
end
self:SetFrameStrata(config.frameStrata)
self.Texture:Show()
self.Texture:SetTexture(texture)
self.Texture:SetSize(config.width, config.height)
self.Texture:SetAlpha(config.opacity)
self.Texture:SetPoint(config.texturePoint, nameplate, config.anchorPoint, config.xOffset, config.yOffset)
else
self.Texture:Hide()
end
end
function Indicator:Refresh()
self:Update(self.currentNameplate)
end
function Indicator:OnRecyclePlate(callback, nameplate, plateData)
--@alpha@
debugprint("Callback fired (recycle)", self.unit, nameplate == self.currentNameplate)
--@end-alpha@
if nameplate == self.currentNameplate then
self:Update()
end
end
-- Checks if other indicators are already displaying on this indicator's unit, hides lower priority indicators and returns true when this indicator should be shown.
--
-- - If no other indicator is displaying, this returns true.
-- - If a lower priority indicator is displaying, it is hidden and this returns true.
-- - If an equal or higher priority indicator is displaying, this returns false.
function Indicator:CheckAndHideLowerPriorityIndicators()
for unit, indicator in pairs(TNI.Indicators) do
if indicator.enabled and self.unit ~= indicator.unit and UnitIsUnit(self.unit, unit) then -- If the indicator is for a different unit token but it's the same unit,
if self.priority > indicator.priority then -- If this indicator is a higher priority, hide the other indicator and return true
indicator:Update()
return true
else -- If this indicator is a lower or equal priority, return false
return false
end
end
end
-- No other indicator is displaying, return true
return true
end
-- Verfies that the current nameplate (if there is one) has a unit token and disables the indicator and throws an error if it doesn't.
-- Returns true if there's no issue.
function Indicator:VerifyNameplateUnitToken()
if self.currentNameplate and not self.currentNameplate.namePlateUnitToken then
TNI.db.profile[self.unit].enable = false
self:Hide()
error((
"TargetNameplateIndicator: %s indicator found a nameplate without a unit token and as such is unable to function." ..
" This is usually caused by AddOns that replace the default nameplates (e.g. EKPlates)." ..
" This indicator will now be disabled until it's re-enabled in the options menu."
):format(self.unit))
end
return true
end
local function CreateIndicator(unit, priority)
local indicator = CreateFrame("Frame", "TargetNameplateIndicator_" .. unit)
--- @cast indicator Indicator
indicator:SetFrameStrata("BACKGROUND")
indicator.Texture = indicator:CreateTexture("$parentTexture", "OVERLAY")
indicator.unit = unit
indicator.priority = priority
LNR:Embed(indicator)
Mixin(indicator, Indicator)
indicator:LNR_RegisterCallback("LNR_ON_RECYCLE_PLATE", "OnRecyclePlate")
indicator:SetScript("OnEvent", function(self, event, ...)
self[event](self, ...)
end)
TNI.Indicators[unit] = indicator
return indicator
end
------
-- Non-target Indicator functions
------
--- @class NonTargetIndicator : Indicator
local NonTargetIndicator = {}
function NonTargetIndicator:OnUpdate()
-- If there's a current nameplate and it's still this indicator's unit, do nothing
if self.currentNameplate and self:VerifyNameplateUnitToken() and UnitIsUnit(self.unit, self.currentNameplate.namePlateUnitToken) then
return
end
-- If there isn't a current nameplate and this indicator's unit doesn't exist, do nothing
if not self.currentNameplate and not UnitExists(self.unit) then
return
end
local nameplate, plateData = self:GetPlateByGUID(UnitGUID(self.unit))
local shouldDisplay = self:CheckAndHideLowerPriorityIndicators()
--@alpha@
debugprint(self.unit, "changed", nameplate, "shouldDisplay?", shouldDisplay)
--@end-alpha@
-- If the nameplate for this indicator's unit doesn't already have a higher priority indicator displaying on it, update the indicator; otherwise hide it.
if shouldDisplay then
self:Update(nameplate)
else
self:Update(nil)
end
end
local function CreateNonTargetIndicator(unit, priority)
local indicator = CreateIndicator(unit, priority)
--- @cast indicator NonTargetIndicator
Mixin(indicator, NonTargetIndicator)
indicator:SetScript("OnUpdate", indicator.OnUpdate)
return indicator
end
------
-- Target Indicator
------
--- @class TargetIndicator : Indicator
local TargetIndicator = CreateIndicator("target", 100)
function TargetIndicator:PLAYER_TARGET_CHANGED()
local nameplate, plateData = self:GetPlateByGUID(UnitGUID("target"))
--@alpha@
debugprint("Player target changed", nameplate)
--@end-alpha@
if not nameplate then
self:Update()
end
end
function TargetIndicator:OnTargetPlateOnScreen(callback, nameplate, plateData)
--@alpha@
debugprint("Callback fired (target found)")
--@end-alpha@
local shouldDisplay = self:CheckAndHideLowerPriorityIndicators()
if shouldDisplay then
self:Update(nameplate)
else
self:Update()
end
end
TargetIndicator:RegisterEvent("PLAYER_TARGET_CHANGED")
TargetIndicator:LNR_RegisterCallback("LNR_ON_TARGET_PLATE_ON_SCREEN", "OnTargetPlateOnScreen")
------
-- Mouseover Indicator
------
---@diagnostic disable-next-line: unused-local
local MouseoverIndicator = CreateNonTargetIndicator("mouseover", 10)
------
-- Focus Indicator
------
---@diagnostic disable-next-line: unused-local
local FocusIndicator = CreateNonTargetIndicator("focus", 90)
------
-- Target of Target Indicator
------
---@diagnostic disable-next-line: unused-local
local TargetOfTargetIndicator = CreateNonTargetIndicator("targettarget", 50)