-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTooltip.lua
More file actions
545 lines (455 loc) · 18.9 KB
/
Copy pathTooltip.lua
File metadata and controls
545 lines (455 loc) · 18.9 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
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
-------------------------------------------------------------------------------
-- Tooltip.lua
-- Hooks GameTooltip to display expected damage/DPS for supported spells
--
-- Supported versions: TBC Anniversary
-------------------------------------------------------------------------------
local _, ns = ...
local Tooltip = {}
ns.Tooltip = Tooltip
-- Resolved tooltip position from config (updated by ApplySettings)
local tooltipAnchorPoint = "TOPLEFT"
local tooltipRelativePoint = "BOTTOMLEFT"
local tooltipOffsetX = 0
local tooltipOffsetY = -4
-------------------------------------------------------------------------------
-- Formatting Helpers (delegate to ns.Format, kept for backward compatibility)
-------------------------------------------------------------------------------
function Tooltip.FormatNumber(n)
return ns.Format.FormatNumber(n)
end
function Tooltip.FormatDPS(n)
return ns.Format.FormatDPS(n)
end
function Tooltip.GetSchoolColor(school)
return ns.Format.GetSchoolColor(school)
end
function Tooltip.ColorValue(text, school)
return ns.Format.ColorValue(text, school)
end
-- Cache WoW globals
local GameTooltip = GameTooltip
local CreateFrame = CreateFrame
local format = string.format
local floor = math.floor
local concat = table.concat
-- Import shared formatting (populated by Format.lua, loaded before this file)
local Format -- forward-declared; resolved in init
local FN, FD -- FormatNumber / FormatDPS aliases
local COLOR_GOLD, COLOR_GREEN, COLOR_WHITE, COLOR_LABEL, COLOR_RESET
local MULTIPLY, ARROW
-- Re-entry guard: tracks the spellID last appended to avoid duplicate lines
local lastTooltipSpellID = nil
-------------------------------------------------------------------------------
-- Companion tooltip frame
-------------------------------------------------------------------------------
local PADDING = 10
local LINE_SPACING = 2
local MIN_WIDTH = 150
-- Created at init time
local companionFrame
-- FontString pool
local fontStrings = {}
local numActiveLines = 0
local function GetFontString(index)
if fontStrings[index] then return fontStrings[index] end
local fs = companionFrame:CreateFontString(nil, "ARTWORK")
fs:SetFontObject(GameTooltipText)
fs:SetJustifyH("LEFT")
fontStrings[index] = fs
return fs
end
local function AddLine(text, r, g, b)
numActiveLines = numActiveLines + 1
local fs = GetFontString(numActiveLines)
if r and g and b then
text = format("|cff%02x%02x%02x%s|r",
floor(r * 255 + 0.5), floor(g * 255 + 0.5), floor(b * 255 + 0.5), text)
end
fs:SetText(text)
fs:Show()
end
local function ResetLines()
for i = 1, numActiveLines do
fontStrings[i]:Hide()
end
numActiveLines = 0
end
local function FinalizeFrame()
if numActiveLines == 0 then
companionFrame:Hide()
return
end
-- Measure widest line
local maxWidth = MIN_WIDTH
for i = 1, numActiveLines do
local w = fontStrings[i]:GetStringWidth()
if w > maxWidth then maxWidth = w end
end
-- Layout lines vertically
local lineHeight = select(2, fontStrings[1]:GetFont())
for i = 1, numActiveLines do
fontStrings[i]:ClearAllPoints()
fontStrings[i]:SetPoint("TOPLEFT", companionFrame, "TOPLEFT",
PADDING, -PADDING - (i - 1) * (lineHeight + LINE_SPACING))
end
local totalHeight = PADDING * 2 + numActiveLines * lineHeight
+ (numActiveLines - 1) * LINE_SPACING
companionFrame:SetSize(maxWidth + PADDING * 2, totalHeight)
-- Re-anchor to GameTooltip using configured position
companionFrame:ClearAllPoints()
companionFrame:SetPoint(tooltipAnchorPoint, GameTooltip, tooltipRelativePoint, tooltipOffsetX, tooltipOffsetY)
companionFrame:Show()
end
-------------------------------------------------------------------------------
-- Identity line -- spell name (school-colored) + rank
-------------------------------------------------------------------------------
local function AddIdentityLine(r)
local schoolColor = ns.Format.GetSchoolColor(r.school)
local name = schoolColor .. (r.spellName or "Unknown") .. COLOR_RESET
local rank = ""
if r.rank then
rank = " " .. COLOR_GOLD .. "(Rank " .. r.rank .. ")" .. COLOR_RESET
end
AddLine(name .. rank)
end
-------------------------------------------------------------------------------
-- Value line -- expected damage/DPS (the "money line")
-------------------------------------------------------------------------------
local function GetValueLabel(outputType)
if outputType == "healing" then return "healing expected"
elseif outputType == "absorption" then return "absorption"
else return "expected" end
end
local function GetRateLabel(outputType)
if outputType == "healing" then return "HPS"
elseif outputType == "absorption" then return "APS"
else return "DPS" end
end
local function AddValueLine(r)
local valueLabel = GetValueLabel(r.outputType)
local rateLabel = GetRateLabel(r.outputType)
local schoolColor = ns.Format.GetSchoolColor(r.school)
local dmgStr = schoolColor .. FN(r.expectedDamageWithMiss) .. COLOR_RESET
local dpsStr = COLOR_GREEN .. FD(r.dps) .. " " .. rateLabel .. COLOR_RESET
AddLine(format("%s %s (%s)", dmgStr, valueLabel, dpsStr))
end
-------------------------------------------------------------------------------
-- Coefficient line -- labeled
-------------------------------------------------------------------------------
local function AddCoeffLine(r)
if r.spellType == "utility" then return end
local isMelee = r.dodgeChance ~= nil
if isMelee then return end -- melee has no coefficient
local label = COLOR_LABEL .. "Coeff:" .. COLOR_RESET .. " "
if r.spellType == "hybrid" then
local dc = r.directCoefficient or 0
local dotc = r.dotCoefficient or 0
AddLine(" " .. label .. format("%s%.2f + %.2f%s", COLOR_WHITE, dc, dotc, COLOR_RESET))
elseif r.coefficient then
AddLine(" " .. label .. format("%s%.3f%s", COLOR_WHITE, r.coefficient, COLOR_RESET))
end
end
-------------------------------------------------------------------------------
-- Cast time line -- labeled
-------------------------------------------------------------------------------
local function FormatCastTime(r)
if (r.baseCastTime or 0) <= 0 then
return "instant"
elseif r.spellType == "channel" then
return format("%.1fs channel", r.castTime)
else
return format("%.1fs", r.castTime)
end
end
local function AddCastLine(r)
if r.spellType == "utility" then return end
local label = COLOR_LABEL .. "Cast:" .. COLOR_RESET .. " "
AddLine(" " .. label .. COLOR_WHITE .. FormatCastTime(r) .. COLOR_RESET)
end
-------------------------------------------------------------------------------
-- Talent line -- labeled, only shown when talentDamageBonus > 0
-------------------------------------------------------------------------------
local function AddTalentLine(r)
if (r.talentDamageBonus or 0) <= 0 then return end
local label = COLOR_LABEL .. "Talents:" .. COLOR_RESET .. " "
AddLine(" " .. label .. format("%s+%.0f%%%s",
COLOR_GREEN, r.talentDamageBonus * 100, COLOR_RESET))
end
-------------------------------------------------------------------------------
-- Stats line -- labeled, SP/AP + crit (hidden when 0%) + hit
-------------------------------------------------------------------------------
local function GetPowerLabel(r)
if r.dodgeChance ~= nil then return "AP" end
return "SP"
end
local function AddStatsLine(r)
local label = COLOR_LABEL .. "Stats:" .. COLOR_RESET .. " "
local powerLabel = GetPowerLabel(r)
local parts = {}
parts[#parts + 1] = format("%s+%s%s %s",
COLOR_WHITE, FN(r.spellPowerBonus or 0), COLOR_RESET, powerLabel)
if (r.critChance or 0) > 0 then
parts[#parts + 1] = format("%s%.1f%%%s crit (%s%s%.2f%s)",
COLOR_WHITE, r.critChance * 100, COLOR_RESET,
COLOR_WHITE, MULTIPLY, r.critMultiplier or 0, COLOR_RESET)
end
if r.hitChance then
parts[#parts + 1] = format("%s%d%%%s hit",
COLOR_WHITE, floor(r.hitChance * 100 + 0.5), COLOR_RESET)
end
AddLine(" " .. label .. concat(parts, " | "))
end
-------------------------------------------------------------------------------
-- Melee stats + avoidance lines -- labeled
-------------------------------------------------------------------------------
local function AddMeleeStatsLines(r)
-- Line 1: Stats label with AP + crit
local statsLabel = COLOR_LABEL .. "Stats:" .. COLOR_RESET .. " "
local parts1 = {}
parts1[#parts1 + 1] = format("%s+%s%s AP",
COLOR_WHITE, FN(r.spellPowerBonus or 0), COLOR_RESET)
if (r.critChance or 0) > 0 then
parts1[#parts1 + 1] = format("%s%.1f%%%s crit (%s%s%.2f%s)",
COLOR_WHITE, r.critChance * 100, COLOR_RESET,
COLOR_WHITE, MULTIPLY, r.critMultiplier or 0, COLOR_RESET)
end
AddLine(" " .. statsLabel .. concat(parts1, " | "))
-- Line 2: Avoidance label with hit + dodge + parry + armor
local avoidLabel = COLOR_LABEL .. "Avoidance:" .. COLOR_RESET .. " "
local parts2 = {}
if r.hitChance then
parts2[#parts2 + 1] = format("%s%d%%%s hit",
COLOR_WHITE, floor(r.hitChance * 100 + 0.5), COLOR_RESET)
end
if r.dodgeChance and r.dodgeChance > 0 then
parts2[#parts2 + 1] = format("%s%.1f%%%s dodge",
COLOR_WHITE, r.dodgeChance * 100, COLOR_RESET)
end
if r.parryChance and r.parryChance > 0 then
parts2[#parts2 + 1] = format("%s%.1f%%%s parry",
COLOR_WHITE, r.parryChance * 100, COLOR_RESET)
end
if r.armorReduction and r.armorReduction > 0 then
parts2[#parts2 + 1] = format("%s%.0f%%%s armor",
COLOR_WHITE, r.armorReduction * 100, COLOR_RESET)
end
if #parts2 > 0 then
AddLine(" " .. avoidLabel .. concat(parts2, " | "))
end
end
-------------------------------------------------------------------------------
-- Breakdown line -- labeled, for DoT and Channel spells
-- Shows tick damage, total damage, duration, and tick count
-------------------------------------------------------------------------------
local function AddBreakdownLine(r)
local label = COLOR_LABEL .. "Breakdown:" .. COLOR_RESET .. " "
local sc = ns.Format.GetSchoolColor(r.school)
local tickStr = sc .. FN(r.tickDamage or r.tickDmg or 0) .. COLOR_RESET
local totalStr = sc .. FN(r.expectedDamageWithMiss or r.totalDmg or 0) .. COLOR_RESET
local tickCount = r.numTicks or 0
local duration = r.duration or 0
AddLine(format(" %s%s/tick | %s total (%ds, %d ticks)",
label, tickStr, totalStr, duration, tickCount))
end
-------------------------------------------------------------------------------
-- Hybrid breakdown lines -- Direct + DoT sub-lines with labels
-------------------------------------------------------------------------------
local function AddHybridBreakdownLines(r)
local sc = ns.Format.GetSchoolColor(r.school)
-- Direct line
local directLabel = COLOR_LABEL .. "Direct:" .. COLOR_RESET .. " "
local directStr = sc .. FN(r.directDamage or 0) .. COLOR_RESET
local directParts = { directStr }
if (r.critChance or 0) > 0 then
directParts[#directParts + 1] = format("%s%.1f%%%s crit (%s%s%.2f%s)",
COLOR_WHITE, r.critChance * 100, COLOR_RESET,
COLOR_WHITE, MULTIPLY, r.critMultiplier or 0, COLOR_RESET)
end
AddLine(" " .. directLabel .. concat(directParts, " | "))
-- DoT line
local dotLabel = COLOR_LABEL .. "DoT:" .. COLOR_RESET .. " "
local tickStr = sc .. FN(r.tickDamage or 0) .. COLOR_RESET
local dotTotalStr = sc .. FN(r.dotDamage or r.dotTotalDmg or 0) .. COLOR_RESET
local tickCount = r.numTicks or 0
local duration = r.duration or 0
AddLine(format(" %s%s/tick | %s total (%ds, %d ticks)",
dotLabel, tickStr, dotTotalStr, duration, tickCount))
end
-------------------------------------------------------------------------------
-- Spell-type-specific line builders
-------------------------------------------------------------------------------
local function AddDirectLines(r)
AddIdentityLine(r)
AddValueLine(r)
AddCoeffLine(r)
AddCastLine(r)
AddTalentLine(r)
if r.dodgeChance ~= nil then
AddMeleeStatsLines(r)
else
AddStatsLine(r)
end
end
local function AddDotLines(r)
AddIdentityLine(r)
AddValueLine(r)
AddCoeffLine(r)
AddCastLine(r)
AddTalentLine(r)
AddBreakdownLine(r)
AddStatsLine(r)
end
local function AddHybridLines(r)
AddIdentityLine(r)
AddValueLine(r)
AddCoeffLine(r)
AddCastLine(r)
AddTalentLine(r)
AddHybridBreakdownLines(r)
AddStatsLine(r)
end
local function AddChannelLines(r)
AddIdentityLine(r)
AddValueLine(r)
AddCoeffLine(r)
AddCastLine(r)
AddTalentLine(r)
AddBreakdownLine(r)
AddStatsLine(r)
end
local function AddUtilityLines(r)
AddIdentityLine(r)
local sc = ns.Format.GetSchoolColor(r.school)
if r.healthCost then
AddLine(format(" %s HP %s %s mana (%s+%s SP%s)",
sc .. FN(r.healthCost) .. COLOR_RESET,
ARROW,
sc .. FN(r.manaGain) .. COLOR_RESET,
COLOR_GREEN, FN(r.spellPowerBonus or 0), COLOR_RESET))
else
AddLine(format(" %s mana (%s+%s SP%s)",
sc .. FN(r.manaGain) .. COLOR_RESET,
COLOR_GREEN, FN(r.spellPowerBonus or 0), COLOR_RESET))
end
end
-------------------------------------------------------------------------------
-- Main dispatcher
-------------------------------------------------------------------------------
local function AddTooltipLines(r)
ResetLines()
if r.spellType == "utility" then
AddUtilityLines(r)
elseif r.spellType == "hybrid" then
AddHybridLines(r)
elseif r.spellType == "dot" then
AddDotLines(r)
elseif r.spellType == "channel" then
AddChannelLines(r)
else
AddDirectLines(r)
end
FinalizeFrame()
end
-------------------------------------------------------------------------------
-- Tooltip hook handler
-------------------------------------------------------------------------------
local function OnTooltipSetSpell(tooltip)
local _, a, b = tooltip:GetSpell()
local spellID = (type(b) == "number" and b) or (type(a) == "number" and a)
if not spellID then return end
-- Guard against re-entry (OnTooltipSetSpell can fire more than once)
if spellID == lastTooltipSpellID then return end
-- Resolve the rank-specific spellID to (baseKey, rankIndex)
local baseKey, rankIndex = ns.SpellResolver.Resolve(spellID)
if not baseKey then return end
-- Get current player state (uses cached snapshot, refreshed on relevant events)
local playerState = ns.StateCollector.GetCachedState()
if not playerState then return end
-- Run the full computation pipeline for the specific rank being hovered
local result = ns.Engine.Pipeline.Calculate(baseKey, playerState, rankIndex)
if not result then return end
-- Mark this spellID as processed only after all guards pass
lastTooltipSpellID = spellID
-- Append formatted lines to the tooltip
AddTooltipLines(result)
end
-------------------------------------------------------------------------------
-- ApplySettings -- reads config and updates positioning variables
-------------------------------------------------------------------------------
local POSITION_MAP = {
BOTTOM = { anchor = "TOPLEFT", relative = "BOTTOMLEFT", baseX = 0, baseY = -4 },
TOP = { anchor = "BOTTOMLEFT", relative = "TOPLEFT", baseX = 0, baseY = 4 },
LEFT = { anchor = "TOPRIGHT", relative = "TOPLEFT", baseX = -4, baseY = 0 },
RIGHT = { anchor = "TOPLEFT", relative = "TOPRIGHT", baseX = 4, baseY = 0 },
}
function ns.Tooltip.ApplySettings()
local config = ns.Addon and ns.Addon.db and ns.Addon.db.profile and ns.Addon.db.profile.tooltip
if not config then
config = { position = "BOTTOM", offsetX = 0, offsetY = 0 }
end
local pos = POSITION_MAP[config.position] or POSITION_MAP.BOTTOM
tooltipAnchorPoint = pos.anchor
tooltipRelativePoint = pos.relative
tooltipOffsetX = pos.baseX + (config.offsetX or 0)
tooltipOffsetY = pos.baseY + (config.offsetY or 0)
-- Re-position if the companion frame exists and is visible
if companionFrame and companionFrame:IsVisible() then
companionFrame:ClearAllPoints()
companionFrame:SetPoint(tooltipAnchorPoint, GameTooltip, tooltipRelativePoint, tooltipOffsetX, tooltipOffsetY)
end
end
-------------------------------------------------------------------------------
-- HookTooltip -- attaches the tooltip hooks (called once during init)
-------------------------------------------------------------------------------
local function HookTooltip()
GameTooltip:HookScript("OnTooltipSetSpell", OnTooltipSetSpell)
-- Clear the re-entry guard when the tooltip is cleared
GameTooltip:HookScript("OnTooltipCleared", function()
lastTooltipSpellID = nil
if companionFrame then companionFrame:Hide() end
end)
end
-------------------------------------------------------------------------------
-- Initialization -- deferred to PLAYER_LOGIN to ensure all data is ready
-------------------------------------------------------------------------------
local initFrame = CreateFrame("Frame")
initFrame:RegisterEvent("PLAYER_LOGIN")
initFrame:SetScript("OnEvent", function(self, event)
if event == "PLAYER_LOGIN" then
self:UnregisterEvent("PLAYER_LOGIN")
-- Resolve shared formatting references
Format = ns.Format
FN = Format.FormatNumber
FD = Format.FormatDPS
COLOR_GOLD = Format.COLOR_GOLD
COLOR_GREEN = Format.COLOR_GREEN
COLOR_WHITE = Format.COLOR_WHITE
COLOR_LABEL = Format.COLOR_LABEL
COLOR_RESET = Format.COLOR_RESET
MULTIPLY = Format.MULTIPLY
ARROW = Format.ARROW
-- Create companion tooltip frame
companionFrame = CreateFrame("Frame", "PhDamageTooltip", UIParent, "BackdropTemplate")
companionFrame:SetFrameStrata("TOOLTIP")
companionFrame:SetClampedToScreen(true)
companionFrame:Hide()
companionFrame:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true, tileSize = 16, edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 },
})
companionFrame:SetBackdropColor(0, 0, 0, 0.8)
-- Apply ElvUI skin if available (SetTemplate is a mixin method)
if ElvUI then
local E = unpack(ElvUI)
if E and E.Skins then
pcall(function() companionFrame:SetTemplate("Transparent") end)
end
end
HookTooltip()
-- Apply tooltip position settings
ns.Tooltip.ApplySettings()
end
end)