Skip to content

Commit 7505914

Browse files
committed
Make extra tooltip columns shrink to min width
Tooltip column size previously blindly copied the width of the first column, which could lead to massive waste of space for uniques with very long mods.
1 parent 60eb67a commit 7505914

1 file changed

Lines changed: 65 additions & 10 deletions

File tree

src/Classes/Tooltip.lua

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,11 @@ end
186186

187187
function TooltipClass:GetDynamicSize(viewPort)
188188
local staticttW, staticttH = self:GetSize()
189-
local columns, ttH = self:CalculateColumns(0, 0, staticttH, staticttW, viewPort)
190-
local ttW = columns * staticttW
189+
local columns, ttH, _, extraColumnWidth = self:CalculateColumns(0, 0, staticttH, staticttW, viewPort)
190+
191+
-- ensure extra column width has sensible value
192+
extraColumnWidth = (columns > 1 and extraColumnWidth > 0) and extraColumnWidth or staticttW
193+
local ttW = staticttW + (m_max(columns - 1, 0) * extraColumnWidth)
191194

192195
return ttW + H_PAD, ttH + V_PAD
193196
end
@@ -200,6 +203,7 @@ function TooltipClass:CalculateColumns(ttY, ttX, ttH, ttW, viewPort)
200203
local x = ttX
201204
local columns = 1 -- reset to count columns by block heights
202205
local currentBlock = 1
206+
local extraColumnWidth = 0
203207
local maxColumnHeight = 0
204208
local drawStack = {}
205209
local font
@@ -279,6 +283,11 @@ function TooltipClass:CalculateColumns(ttY, ttX, ttH, ttW, viewPort)
279283
t_insert(drawStack, {lineX, y, lineAlign, data.size, font, data.text})
280284
y = y + data.size + 2
281285

286+
-- track max width for extra columns
287+
if columns > 1 then
288+
extraColumnWidth = m_max(extraColumnWidth, DrawStringWidth(data.size, font, data.text) + H_PAD)
289+
end
290+
282291
elseif data.separatorImage and main.showFlavourText then
283292
local sepSize = data.size or 10
284293
if currentBlock ~= data.block and y + sepSize > ttY + math.min(ttH, viewPort.height) then
@@ -298,7 +307,44 @@ function TooltipClass:CalculateColumns(ttY, ttX, ttH, ttW, viewPort)
298307
maxColumnHeight = m_max(y - ttY + 2 * BORDER_WIDTH, maxColumnHeight)
299308
end
300309

301-
return columns, maxColumnHeight, drawStack
310+
-- Resizing/Shrinking drawStack elements in extra columns
311+
-- NOTE: this logic depends on the current structure of `drawStack` --> needs adjustment if lengths or coordinates logic changes
312+
if columns > 1 and extraColumnWidth > 0 then
313+
for _, line in ipairs(drawStack) do
314+
local isText = #line >= 6 -- Text elements have 6 props, images/separators have 5
315+
local xIdx = isText and 1 or 2 -- `x` value at index 1 for text, 2 otherwise
316+
local origX = line[xIdx]
317+
318+
-- calculate column index (origX is at least x * original widths from start)
319+
local colIndex = m_floor((origX - ttX) / ttW) + 1
320+
321+
if colIndex > 1 then
322+
local oldBaseX = ttX + ttW * (colIndex - 1)
323+
local newBaseX = ttX + ttW + extraColumnWidth * (colIndex - 2) -- `- 2` because first column is unchanged
324+
325+
-- Update x coordinates
326+
if isText and line[3] == "CENTER_X" then
327+
-- centered texts
328+
line[xIdx] = newBaseX + extraColumnWidth / 2
329+
else
330+
-- "LEFT" aligned text and images (NOTE: "RIGHT" aligned does not seem to exist)
331+
line[xIdx] = origX - oldBaseX + newBaseX
332+
end
333+
334+
-- Resize separators/dividers (technically unlikely to appear in extra columns, but just in case)
335+
if not isText then
336+
-- separator images have `width` value at index 4
337+
if line[1] and type(line[1]) == "table" and line[1].isSeparator then
338+
line[4] = extraColumnWidth - H_PAD -- "fancy" separators get extra padding
339+
else
340+
line[4] = extraColumnWidth - BORDER_WIDTH
341+
end
342+
end
343+
end
344+
end
345+
end
346+
347+
return columns, maxColumnHeight, drawStack, extraColumnWidth
302348
end
303349
--- Draws tooltip to screen
304350
---@param x number x-coordinate to draw the tooltip at
@@ -374,11 +420,15 @@ function TooltipClass:Draw(x, y, w, h, viewPort)
374420
end
375421
end
376422
-- Initial column calculation
377-
local columns, maxColumnHeight, drawStack = self:CalculateColumns(ttY, ttX, ttH, ttW, viewPort)
423+
local columns, maxColumnHeight, drawStack, extraColumnWidth = self:CalculateColumns(ttY, ttX, ttH, ttW, viewPort)
424+
425+
-- ensure extraColumnWidth has sensible value and calculate new total width (original width + extraColumns)
426+
extraColumnWidth = (columns > 1 and extraColumnWidth > 0) and extraColumnWidth or ttW
427+
local totalDrawWidth = ttW + (m_max(columns - 1, 0) * extraColumnWidth)
378428

379429
-- If hover tooltip and extra columns don't fit, shift to left and adjust drawStack (because hover tooltips can't scroll)
380-
if columns > 1 and isHoverToolTip and ttW * columns + ttX >= viewPort.x + viewPort.width then
381-
local newX = m_max(viewPort.x, viewPort.x + viewPort.width - ttW * columns)
430+
if columns > 1 and isHoverToolTip and totalDrawWidth + ttX >= viewPort.x + viewPort.width then
431+
local newX = m_max(viewPort.x, viewPort.x + viewPort.width - totalDrawWidth)
382432
local offsetX = newX - ttX
383433
ttX = newX
384434

@@ -396,7 +446,7 @@ function TooltipClass:Draw(x, y, w, h, viewPort)
396446
-- background shading currently must be drawn before text lines. API change will allow something like the commented lines below
397447
SetDrawColor(0, 0, 0, .85)
398448
--SetDrawLayer(nil, GetDrawLayer() - 5)
399-
DrawImage(nil, ttX, ttY + BORDER_WIDTH, ttW * columns - BORDER_WIDTH, maxColumnHeight - 2 * BORDER_WIDTH)
449+
DrawImage(nil, ttX, ttY + BORDER_WIDTH, totalDrawWidth - BORDER_WIDTH, maxColumnHeight - 2 * BORDER_WIDTH)
400450
--SetDrawLayer(nil, GetDrawLayer())
401451
SetDrawColor(1, 1, 1)
402452

@@ -540,11 +590,16 @@ function TooltipClass:Draw(x, y, w, h, viewPort)
540590
else
541591
SetDrawColor(unpack(self.color))
542592
end
593+
594+
-- draw vertical borders, accounting for separate extra column width
543595
for i = 0, columns do
544-
DrawImage(nil, ttX + ttW * i - BORDER_WIDTH * math.ceil(i^2 / (i^2 + 1)), ttY, BORDER_WIDTH, maxColumnHeight)
596+
local extraColXOffset = i > 0 and ttW + ((i - 1) * extraColumnWidth) or 0
597+
local currentX = ttX + extraColXOffset
598+
DrawImage(nil, currentX - BORDER_WIDTH * math.ceil(i^2 / (i^2 + 1)), ttY, BORDER_WIDTH, maxColumnHeight)
545599
end
546-
DrawImage(nil, ttX, ttY, ttW * columns, BORDER_WIDTH) -- top border
547-
DrawImage(nil, ttX, ttY + maxColumnHeight - BORDER_WIDTH, ttW * columns, BORDER_WIDTH) -- bottom border
600+
-- draw horizontal borders
601+
DrawImage(nil, ttX, ttY, totalDrawWidth, BORDER_WIDTH) -- top
602+
DrawImage(nil, ttX, ttY + maxColumnHeight - BORDER_WIDTH, totalDrawWidth, BORDER_WIDTH) -- bottom
548603

549604
return ttW, ttH
550605
end

0 commit comments

Comments
 (0)