From 0ae83fcbed8ff492c02483d91869ab47582869ca Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Sun, 12 Mar 2023 16:11:16 +0000 Subject: [PATCH] Ensure tooltips stay on screen When the tooltip is wider than half the screen width, it often ends up partially off-screen. Let's ensure the tooltip always wraps to less than `screen.width / 2`. Additionally, lets render the tooltip above the cursor, instead of in-line with it. This allows the user to more easily read what they're hovering over without the tooltip getting in the way. --- .../clothconfig2/gui/entries/TooltipListEntry.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/me/shedaniel/clothconfig2/gui/entries/TooltipListEntry.java b/common/src/main/java/me/shedaniel/clothconfig2/gui/entries/TooltipListEntry.java index fbb73955..118ef627 100644 --- a/common/src/main/java/me/shedaniel/clothconfig2/gui/entries/TooltipListEntry.java +++ b/common/src/main/java/me/shedaniel/clothconfig2/gui/entries/TooltipListEntry.java @@ -56,14 +56,20 @@ public TooltipListEntry(Component fieldName, @Nullable Supplier tooltip = getTooltip(mouseX, mouseY); - if (tooltip.isPresent() && tooltip.get().length > 0) - addTooltip(Tooltip.of(new Point(mouseX, mouseY), postProcessTooltip(tooltip.get()))); + getTooltip(mouseX, mouseY).ifPresent(tooltip -> { + if (tooltip.length > 0) { + FormattedCharSequence[] processedTooltip = postProcessTooltip(tooltip); + int height = processedTooltip.length * Minecraft.getInstance().font.lineHeight; + addTooltip(Tooltip.of(new Point(mouseX, mouseY - height), processedTooltip)); + } + }); } } private FormattedCharSequence[] postProcessTooltip(Component[] tooltip) { - return Arrays.stream(tooltip).flatMap(component -> Minecraft.getInstance().font.split(component, getConfigScreen().width).stream()) + int maxWidth = getConfigScreen().width / 2 - 4; + return Arrays.stream(tooltip) + .flatMap(component -> Minecraft.getInstance().font.split(component, maxWidth).stream()) .toArray(FormattedCharSequence[]::new); }