Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure tooltips stay on screen & doesn't get in the way #197

Open
wants to merge 1 commit into
base: v8
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,20 @@ public TooltipListEntry(Component fieldName, @Nullable Supplier<Optional<Compone
public void render(PoseStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isHovered, float delta) {
super.render(matrices, index, y, x, entryWidth, entryHeight, mouseX, mouseY, isHovered, delta);
if (isMouseInside(mouseX, mouseY, x, y, entryWidth, entryHeight)) {
Optional<Component[]> 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);
}

Expand Down